fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/activities/WebviewActivity.java

357 lines
15 KiB
Java
Raw Normal View History

2017-05-26 17:20:36 +02:00
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
2017-05-26 17:20:36 +02:00
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
2017-07-10 10:33:24 +02:00
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-26 17:20:36 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2017-08-04 11:11:27 +02:00
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
2017-05-26 17:20:36 +02:00
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.activities;
import android.Manifest;
2018-09-29 14:11:42 +02:00
import android.content.Context;
2019-02-15 18:19:32 +01:00
import android.content.DialogInterface;
import android.content.Intent;
2017-06-30 17:09:07 +02:00
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
2019-02-14 18:14:20 +01:00
import android.database.sqlite.SQLiteDatabase;
2019-02-15 18:19:32 +01:00
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
2018-09-29 14:11:42 +02:00
import android.os.AsyncTask;
import android.os.Build;
2017-05-26 17:20:36 +02:00
import android.os.Bundle;
2019-02-14 18:14:20 +01:00
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
2019-02-15 18:19:32 +01:00
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.DownloadListener;
2017-05-26 17:20:36 +02:00
import android.webkit.WebView;
2019-02-15 18:19:32 +01:00
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
2018-09-15 14:14:30 +02:00
import android.widget.Toast;
2017-05-26 17:20:36 +02:00
2018-09-29 14:11:42 +02:00
import java.lang.ref.WeakReference;
2019-02-14 18:14:20 +01:00
import java.util.ArrayList;
2018-09-29 14:11:42 +02:00
import java.util.List;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
import fr.gouv.etalab.mastodon.R;
2018-09-29 14:11:42 +02:00
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Results;
2019-02-15 18:19:32 +01:00
import fr.gouv.etalab.mastodon.helper.CountDrawable;
2017-05-26 17:20:36 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
2019-02-14 18:14:20 +01:00
import fr.gouv.etalab.mastodon.sqlite.DomainBlockDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.webview.MastalabWebChromeClient;
import fr.gouv.etalab.mastodon.webview.MastalabWebViewClient;
2017-05-26 17:20:36 +02:00
import static fr.gouv.etalab.mastodon.helper.Helper.EXTERNAL_STORAGE_REQUEST_CODE;
2018-11-03 12:06:44 +01:00
import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT;
import static fr.gouv.etalab.mastodon.helper.Helper.manageDownloads;
2017-05-26 17:20:36 +02:00
/**
* Created by Thomas on 24/06/2017.
* Webview activity
2017-05-26 17:20:36 +02:00
*/
2017-12-12 18:17:01 +01:00
public class WebviewActivity extends BaseActivity {
2017-05-26 17:20:36 +02:00
private String url;
2018-09-29 14:11:42 +02:00
private String peertubeLinkToFetch;
private boolean peertubeLink;
private WebView webView;
2019-02-14 18:14:20 +01:00
public static List<String> trackingDomains;
2019-02-15 18:19:32 +01:00
private Menu defaultMenu;
private MastalabWebViewClient mastalabWebViewClient;
2017-06-24 12:02:29 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
2017-05-26 17:20:36 +02:00
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2018-05-11 11:28:05 +02:00
switch (theme){
case Helper.THEME_LIGHT:
setTheme(R.style.AppTheme);
break;
case Helper.THEME_DARK:
setTheme(R.style.AppThemeDark);
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
2017-06-30 17:09:07 +02:00
}
2018-05-11 11:28:05 +02:00
2019-02-14 18:14:20 +01:00
2017-05-26 17:20:36 +02:00
setContentView(R.layout.activity_webview);
Bundle b = getIntent().getExtras();
2018-09-29 14:11:42 +02:00
if(b != null) {
url = b.getString("url", null);
2018-09-29 14:11:42 +02:00
peertubeLinkToFetch = b.getString("peertubeLinkToFetch", null);
peertubeLink = b.getBoolean("peertubeLink", false);
}
if( url == null)
finish();
if( getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2017-05-26 17:20:36 +02:00
2019-01-06 16:06:18 +01:00
webView = Helper.initializeWebview(WebviewActivity.this, R.id.webview);
setTitle("");
2017-10-27 15:34:53 +02:00
FrameLayout webview_container = findViewById(R.id.webview_container);
final ViewGroup videoLayout = findViewById(R.id.videoLayout); // Your own view, read class comments
webView.getSettings().setJavaScriptEnabled(true);
MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(WebviewActivity.this, webView, webview_container, videoLayout);
mastalabWebChromeClient.setOnToggledFullscreen(new MastalabWebChromeClient.ToggledFullscreenCallback() {
@Override
public void toggledFullscreen(boolean fullscreen) {
2017-05-26 17:20:36 +02:00
if (fullscreen) {
videoLayout.setVisibility(View.VISIBLE);
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
} else {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
getWindow().setAttributes(attrs);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
videoLayout.setVisibility(View.GONE);
2017-05-26 17:20:36 +02:00
}
}
});
webView.setWebChromeClient(mastalabWebChromeClient);
2019-02-15 18:19:32 +01:00
mastalabWebViewClient = new MastalabWebViewClient(WebviewActivity.this);
webView.setWebViewClient(mastalabWebViewClient);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
if(Build.VERSION.SDK_INT >= 23 ){
if (ContextCompat.checkSelfPermission(WebviewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(WebviewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(WebviewActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, EXTERNAL_STORAGE_REQUEST_CODE);
} else {
manageDownloads(WebviewActivity.this, url);
}
}else{
manageDownloads(WebviewActivity.this, url);
}
}
});
if( !url.startsWith("http://") && !url.startsWith("https://"))
url = "http://" + url;
2019-02-14 18:14:20 +01:00
if( trackingDomains == null){
AsyncTask.execute(new Runnable() {
@Override
public void run() {
SQLiteDatabase db = Sqlite.getInstance(WebviewActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
trackingDomains = new DomainBlockDAO(WebviewActivity.this, db).getAll();
if( trackingDomains == null)
trackingDomains = new ArrayList<>();
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
webView.loadUrl(url);
}
};
mainHandler.post(myRunnable);
}
});
}else
webView.loadUrl(url);
2017-05-26 17:20:36 +02:00
}
2019-02-15 18:19:32 +01:00
public void setCount(Context context, String count) {
2019-02-15 18:43:48 +01:00
if( defaultMenu != null && !peertubeLink) {
2019-02-15 18:19:32 +01:00
MenuItem menuItem = defaultMenu.findItem(R.id.action_block);
LayerDrawable icon = (LayerDrawable) menuItem.getIcon();
CountDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_block_count);
if (reuse instanceof CountDrawable) {
badge = (CountDrawable) reuse;
} else {
badge = new CountDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_block_count, badge);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
2019-02-15 18:43:48 +01:00
if(!peertubeLink)
setCount(this, "0");
2019-02-15 18:19:32 +01:00
defaultMenu = menu;
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_webview, menu);
2019-02-15 18:19:32 +01:00
defaultMenu = menu;
2018-09-29 14:11:42 +02:00
if( peertubeLink ){
menu.findItem(R.id.action_go).setVisible(false);
2019-02-15 18:19:32 +01:00
menu.findItem(R.id.action_block).setVisible(false);
2018-09-29 14:11:42 +02:00
menu.findItem(R.id.action_comment).setVisible(true);
}
2018-11-03 12:06:44 +01:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
if( theme == THEME_LIGHT)
Helper.colorizeIconMenu(menu, R.color.black);
return true;
}
2017-05-26 17:20:36 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
2019-02-15 18:19:32 +01:00
return true;
case R.id.action_block:
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
List<String> domains = mastalabWebViewClient.getDomains();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(WebviewActivity.this, R.layout.domains_blocked);
for (String domain: domains)
arrayAdapter.add(domain);
int style;
if (theme == Helper.THEME_DARK) {
style = R.style.DialogDark;
} else if (theme == Helper.THEME_BLACK){
style = R.style.DialogBlack;
}else {
style = R.style.Dialog;
}
AlertDialog.Builder builder = new AlertDialog.Builder(WebviewActivity.this, style);
builder.setTitle(R.string.list_of_blocked_domains);
builder.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String strName = arrayAdapter.getItem(which);
assert strName != null;
Toasty.info(WebviewActivity.this, strName, Toast.LENGTH_LONG).show();
}
});
builder.show();
return true;
case R.id.action_go:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
2018-09-15 14:14:30 +02:00
try {
startActivity(browserIntent);
}catch (Exception e){
2018-11-25 10:45:16 +01:00
Toasty.error(WebviewActivity.this,getString(R.string.toast_error),Toast.LENGTH_LONG).show();
2018-09-15 14:14:30 +02:00
}
return true;
2018-09-29 14:11:42 +02:00
case R.id.action_comment:
2018-11-25 10:45:16 +01:00
Toasty.info(WebviewActivity.this, getString(R.string.retrieve_remote_status), Toast.LENGTH_LONG).show();
2018-09-29 14:11:42 +02:00
new AsyncTask<Void, Void, Void>() {
private List<fr.gouv.etalab.mastodon.client.Entities.Status> remoteStatuses;
private WeakReference<Context> contextReference = new WeakReference<>(WebviewActivity.this);
@Override
protected Void doInBackground(Void... voids) {
if(url != null) {
Results search = new API(contextReference.get()).search(peertubeLinkToFetch);
if (search != null) {
remoteStatuses = search.getStatuses();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Intent intent = new Intent(contextReference.get(), TootActivity.class);
Bundle b = new Bundle();
if( remoteStatuses == null || remoteStatuses.size() == 0){
2018-11-25 10:45:16 +01:00
Toasty.error(contextReference.get(),getString(R.string.toast_error),Toast.LENGTH_LONG).show();
2018-09-29 14:11:42 +02:00
return;
}
if( remoteStatuses.get(0).getReblog() != null ) {
b.putParcelable("tootReply", remoteStatuses.get(0).getReblog());
}else {
b.putParcelable("tootReply", remoteStatuses.get(0));
}
intent.putExtras(b); //Put your id to your next Intent
contextReference.get().startActivity(intent);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );
return true;
default:
return super.onOptionsItemSelected(item);
2017-05-26 17:20:36 +02:00
}
}
public void setUrl(String newUrl){
this.url = newUrl;
}
2017-09-02 16:52:16 +02:00
@Override
public void onPause(){
super.onPause();
if( webView != null)
webView.onPause();
}
@Override
public void onResume(){
super.onResume();
if( webView != null)
webView.onResume();
}
@Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
public void onDestroy(){
super.onDestroy();
2017-09-02 16:52:16 +02:00
if( webView != null)
webView.destroy();
}
}