Added force light to AMP

pull/194/merge
M66B 3 years ago
parent 55713ebfaa
commit db553478f4

@ -19,6 +19,9 @@ package eu.faircode.email;
Copyright 2018-2022 by Marcel Bokhorst (M66B) Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/ */
import static androidx.webkit.WebSettingsCompat.FORCE_DARK_OFF;
import static androidx.webkit.WebSettingsCompat.FORCE_DARK_ON;
import android.Manifest; import android.Manifest;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
@ -28,6 +31,9 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
@ -35,6 +41,8 @@ import android.webkit.WebView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
@ -54,6 +62,8 @@ public class ActivityAMP extends ActivityBase {
private ContentLoadingProgressBar pbWait; private ContentLoadingProgressBar pbWait;
private Group grpReady; private Group grpReady;
private boolean force_light = false;
private static final List<String> ALLOWED_SCRIPT_HOSTS = Collections.unmodifiableList(Arrays.asList( private static final List<String> ALLOWED_SCRIPT_HOSTS = Collections.unmodifiableList(Arrays.asList(
"cdn.ampproject.org" "cdn.ampproject.org"
)); ));
@ -62,6 +72,9 @@ public class ActivityAMP extends ActivityBase {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState != null)
force_light = savedInstanceState.getBoolean("fair:force_light");
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setSubtitle("AMP"); getSupportActionBar().setSubtitle("AMP");
@ -92,6 +105,8 @@ public class ActivityAMP extends ActivityBase {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
settings.setSafeBrowsingEnabled(safe_browsing); settings.setSafeBrowsingEnabled(safe_browsing);
setDarkMode();
settings.setLoadsImagesAutomatically(true); settings.setLoadsImagesAutomatically(true);
settings.setBlockNetworkLoads(false); settings.setBlockNetworkLoads(false);
settings.setBlockNetworkImage(false); settings.setBlockNetworkImage(false);
@ -103,6 +118,46 @@ public class ActivityAMP extends ActivityBase {
load(); load();
} }
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("fair:force_light", force_light);
super.onSaveInstanceState(outState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_amp, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean available =
(WebViewEx.isFeatureSupported(WebViewFeature.FORCE_DARK) &&
Helper.isDarkTheme(this));
menu.findItem(R.id.menu_force_light)
.setVisible(available)
.getIcon().setLevel(force_light ? 1 : 0);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.menu_force_light) {
onMenuForceLight();
return true;
}
return super.onOptionsItemSelected(item);
}
private void onMenuForceLight() {
force_light = !force_light;
invalidateOptionsMenu();
setDarkMode();
}
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
super.onNewIntent(intent); super.onNewIntent(intent);
@ -110,6 +165,13 @@ public class ActivityAMP extends ActivityBase {
load(); load();
} }
private void setDarkMode() {
WebSettings settings = wvAmp.getSettings();
boolean dark = (Helper.isDarkTheme(this) && !force_light);
if (WebViewEx.isFeatureSupported(WebViewFeature.FORCE_DARK))
WebSettingsCompat.setForceDark(settings, dark ? FORCE_DARK_ON : FORCE_DARK_OFF);
}
private void load() { private void load() {
Uri uri = getIntent().getData(); Uri uri = getIntent().getData();
Log.i("AMP uri=" + uri); Log.i("AMP uri=" + uri);

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_force_light"
android:icon="@drawable/lightdark"
android:title="@string/title_force_light"
app:showAsAction="always" />
</menu>
Loading…
Cancel
Save