fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/WebviewActivity.java

343 lines
14 KiB
Java
Raw Normal View History

2017-05-26 17:20:36 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
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.
*
2019-05-18 11:10:30 +02:00
* Fedilab 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.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-05-26 17:20:36 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-05-26 17:20:36 +02:00
import android.Manifest;
2020-03-08 10:29:06 +01:00
import android.annotation.SuppressLint;
2018-09-29 14:11:42 +02:00
import android.content.Context;
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;
2021-01-19 17:43:51 +01:00
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
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
2019-11-15 16:32:25 +01:00
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
2020-03-08 10:29:06 +01:00
import org.jetbrains.annotations.NotNull;
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;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.helper.CountDrawable;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.DomainBlockDAO;
import app.fedilab.android.sqlite.Sqlite;
2020-04-25 11:18:42 +02:00
import app.fedilab.android.webview.CustomWebview;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.webview.MastalabWebChromeClient;
import app.fedilab.android.webview.MastalabWebViewClient;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
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
2019-11-15 16:32:25 +01:00
public static List<String> trackingDomains;
private String url;
2018-09-29 14:11:42 +02:00
private String peertubeLinkToFetch;
private boolean peertubeLink;
2020-04-25 11:18:42 +02:00
private CustomWebview webView;
2019-02-15 18:19:32 +01:00
private Menu defaultMenu;
private MastalabWebViewClient mastalabWebViewClient;
2017-06-24 12:02:29 +02:00
2020-03-08 10:29:06 +01:00
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
2017-05-26 17:20:36 +02:00
super.onCreate(savedInstanceState);
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2018-05-11 11:28:05 +02:00
case Helper.THEME_LIGHT:
2019-11-09 15:47:38 +01:00
setTheme(R.style.AppTheme_Fedilab);
2018-05-11 11:28:05 +02:00
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();
2019-09-06 17:55:14 +02:00
if (b != null) {
url = b.getString("url", null);
2018-09-29 14:11:42 +02:00
peertubeLinkToFetch = b.getString("peertubeLinkToFetch", null);
2019-09-06 17:55:14 +02:00
peertubeLink = b.getBoolean("peertubeLink", false);
2018-09-29 14:11:42 +02:00
}
2019-09-06 17:55:14 +02:00
if (url == null)
finish();
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2017-05-26 17:20:36 +02:00
2020-02-02 12:08:11 +01:00
webView = Helper.initializeWebview(WebviewActivity.this, R.id.webview, null);
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);
2019-05-24 15:13:28 +02:00
2019-09-06 17:55:14 +02:00
MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(WebviewActivity.this, webView, webview_container, videoLayout);
2020-03-08 10:29:06 +01:00
mastalabWebChromeClient.setOnToggledFullscreen(fullscreen -> {
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);
2020-03-08 10:29:06 +01:00
webView.setDownloadListener((url, userAgent, contentDisposition, mimetype, 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}, Helper.EXTERNAL_STORAGE_REQUEST_CODE);
2019-09-06 17:55:14 +02:00
} else {
2019-05-18 11:10:30 +02:00
Helper.manageDownloads(WebviewActivity.this, url);
}
2020-03-08 10:29:06 +01:00
} else {
Helper.manageDownloads(WebviewActivity.this, url);
}
});
2019-10-05 08:50:24 +02:00
if (!url.toLowerCase().startsWith("http://") && !url.toLowerCase().startsWith("https://"))
url = "http://" + url;
2019-09-06 17:55:14 +02:00
if (trackingDomains == null) {
2020-03-08 10:29:06 +01:00
AsyncTask.execute(() -> {
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-03-08 10:29:06 +01:00
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 = () -> webView.loadUrl(url);
mainHandler.post(myRunnable);
2019-02-14 18:14:20 +01:00
});
2019-09-06 17:55:14 +02:00
} else
2019-02-14 18:14:20 +01:00
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-09-06 17:55:14 +02: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-09-06 17:55:14 +02:00
if (!peertubeLink)
2020-06-01 17:56:30 +02:00
setCount(WebviewActivity.this, "0");
2019-02-15 18:19:32 +01:00
defaultMenu = menu;
return super.onPrepareOptionsMenu(menu);
}
@Override
2020-03-08 10:29:06 +01:00
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_webview, menu);
2019-02-15 18:19:32 +01:00
defaultMenu = menu;
2019-09-06 17:55:14 +02:00
if (peertubeLink) {
2018-09-29 14:11:42 +02:00
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);
}
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
2018-11-03 12:06:44 +01:00
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
if (theme == Helper.THEME_LIGHT)
2018-11-03 12:06:44 +01:00
Helper.colorizeIconMenu(menu, R.color.black);
return true;
}
2019-09-06 17:55:14 +02:00
2017-05-26 17:20:36 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2021-01-19 17:43:51 +01:00
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
finish();
return true;
} else if (itemId == R.id.action_block) {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, 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);
arrayAdapter.addAll(domains);
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, (dialog, which) -> dialog.dismiss());
builder.setAdapter(arrayAdapter, (dialog, which) -> {
String strName = arrayAdapter.getItem(which);
assert strName != null;
Toasty.info(WebviewActivity.this, strName, Toast.LENGTH_LONG).show();
});
builder.show();
return true;
} else if (itemId == R.id.action_go) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
try {
startActivity(browserIntent);
} catch (Exception e) {
Toasty.error(WebviewActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
}
return true;
} else if (itemId == R.id.action_comment) {
Toasty.info(WebviewActivity.this, getString(R.string.retrieve_remote_status), Toast.LENGTH_LONG).show();
new connnectAsync(new WeakReference<>(WebviewActivity.this), url, peertubeLinkToFetch);
return true;
2017-05-26 17:20:36 +02:00
}
2021-01-19 17:43:51 +01:00
return super.onOptionsItemSelected(item);
2017-05-26 17:20:36 +02:00
}
2019-09-06 17:55:14 +02:00
public void setUrl(String newUrl) {
this.url = newUrl;
}
2017-09-02 16:52:16 +02:00
@Override
2019-09-06 17:55:14 +02:00
public void onPause() {
2017-09-02 16:52:16 +02:00
super.onPause();
2019-09-06 17:55:14 +02:00
if (webView != null)
2017-09-02 16:52:16 +02:00
webView.onPause();
}
@Override
2019-09-06 17:55:14 +02:00
public void onResume() {
2017-09-02 16:52:16 +02:00
super.onResume();
2019-09-06 17:55:14 +02:00
if (webView != null)
2017-09-02 16:52:16 +02:00
webView.onResume();
}
@Override
public void onBackPressed() {
2019-09-06 17:55:14 +02:00
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
@Override
2019-09-06 17:55:14 +02:00
public void onDestroy() {
super.onDestroy();
2019-09-06 17:55:14 +02:00
if (webView != null)
2017-09-02 16:52:16 +02:00
webView.destroy();
}
2020-03-08 10:29:06 +01:00
2021-01-19 17:43:51 +01:00
static class connnectAsync {
private final WeakReference<Context> contextReference;
private final String url;
private final String peertubeLinkToFetch;
2020-03-08 10:29:06 +01:00
connnectAsync(WeakReference<Context> contextReference, String url, String peertubeLinkToFetch) {
this.contextReference = contextReference;
this.url = url;
this.peertubeLinkToFetch = peertubeLinkToFetch;
2021-01-19 17:43:51 +01:00
doInBackground();
2020-03-08 10:29:06 +01:00
}
2021-01-19 17:43:51 +01:00
protected void doInBackground() {
new Thread(() -> {
List<app.fedilab.android.client.Entities.Status> remoteStatuses = null;
if (url != null) {
APIResponse search = new API(contextReference.get()).search(peertubeLinkToFetch);
if (search != null && search.getResults() != null) {
remoteStatuses = search.getResults().getStatuses();
}
2020-03-08 10:29:06 +01:00
}
2021-01-19 17:43:51 +01:00
Handler mainHandler = new Handler(Looper.getMainLooper());
List<app.fedilab.android.client.Entities.Status> finalRemoteStatuses = remoteStatuses;
Runnable myRunnable = () -> {
Intent intent = new Intent(contextReference.get(), TootActivity.class);
Bundle b = new Bundle();
if (finalRemoteStatuses == null || finalRemoteStatuses.size() == 0) {
Toasty.error(contextReference.get(), contextReference.get().getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return;
}
if (finalRemoteStatuses.get(0).getReblog() != null) {
b.putParcelable("tootReply", finalRemoteStatuses.get(0).getReblog());
} else {
b.putParcelable("tootReply", finalRemoteStatuses.get(0));
}
intent.putExtras(b); //Put your id to your next Intent
contextReference.get().startActivity(intent);
};
mainHandler.post(myRunnable);
}).start();
2020-03-08 10:29:06 +01:00
}
}
}