View source: add dark mode

This commit is contained in:
M66B 2023-11-05 07:59:19 +01:00
parent 5a8fcf5590
commit 77b64984e0
2 changed files with 36 additions and 4 deletions

View File

@ -42,6 +42,8 @@ import androidx.activity.OnBackPressedCallback;
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 org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -60,9 +62,10 @@ public class ActivityCode extends ActivityBase {
private ContentLoadingProgressBar pbWait; private ContentLoadingProgressBar pbWait;
private Group grpReady; private Group grpReady;
private boolean force_light = false;
private boolean sanitize = false;
private boolean lines = false; private boolean lines = false;
private boolean links = false; private boolean links = false;
private boolean sanitize = false;
private static final int REQUEST_SAVE = 1; private static final int REQUEST_SAVE = 1;
@ -71,9 +74,10 @@ public class ActivityCode extends ActivityBase {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState != null) { if (savedInstanceState != null) {
force_light = savedInstanceState.getBoolean("fair:force_light");
sanitize = savedInstanceState.getBoolean("fair:sanitize");
lines = savedInstanceState.getBoolean("fair:lines"); lines = savedInstanceState.getBoolean("fair:lines");
links = savedInstanceState.getBoolean("fair:links"); links = savedInstanceState.getBoolean("fair:links");
sanitize = savedInstanceState.getBoolean("fair:sanitize");
} }
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -106,6 +110,8 @@ public class ActivityCode extends ActivityBase {
settings.setBlockNetworkImage(true); settings.setBlockNetworkImage(true);
settings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
setDarkMode();
wvCode.setWebViewClient(new WebViewClient() { wvCode.setWebViewClient(new WebViewClient() {
@Override @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { public boolean shouldOverrideUrlLoading(WebView view, String url) {
@ -141,9 +147,10 @@ public class ActivityCode extends ActivityBase {
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("fair:force_light", force_light);
outState.putBoolean("fair:sanitize", sanitize);
outState.putBoolean("fair:lines", lines); outState.putBoolean("fair:lines", lines);
outState.putBoolean("fair:links", links); outState.putBoolean("fair:links", links);
outState.putBoolean("fair:sanitize", sanitize);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@ -159,6 +166,12 @@ public class ActivityCode extends ActivityBase {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean debug = prefs.getBoolean("debug", false); boolean debug = prefs.getBoolean("debug", false);
boolean dark = Helper.isDarkTheme(this);
boolean canDarken = WebViewEx.isFeatureSupported(this, WebViewFeature.ALGORITHMIC_DARKENING);
menu.findItem(R.id.menu_force_light)
.setVisible(dark && canDarken)
.getIcon().setLevel(force_light ? 1 : 0);
menu.findItem(R.id.menu_sanitize) menu.findItem(R.id.menu_sanitize)
.setVisible(BuildConfig.DEBUG || debug) .setVisible(BuildConfig.DEBUG || debug)
.setChecked(sanitize) .setChecked(sanitize)
@ -178,7 +191,12 @@ public class ActivityCode extends ActivityBase {
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId(); int itemId = item.getItemId();
if (itemId == R.id.menu_sanitize) { if (itemId == R.id.menu_force_light) {
force_light = !force_light;
invalidateOptionsMenu();
setDarkMode();
return true;
} else if (itemId == R.id.menu_sanitize) {
sanitize = !sanitize; sanitize = !sanitize;
invalidateOptionsMenu(); invalidateOptionsMenu();
load(); load();
@ -219,6 +237,14 @@ public class ActivityCode extends ActivityBase {
} }
} }
private void setDarkMode() {
WebSettings settings = wvCode.getSettings();
boolean dark = (Helper.isDarkTheme(this) && !force_light);
boolean canDarken = WebViewEx.isFeatureSupported(this, WebViewFeature.ALGORITHMIC_DARKENING);
if (canDarken)
WebSettingsCompat.setAlgorithmicDarkeningAllowed(settings, dark);
}
private void load() { private void load() {
Intent intent = getIntent(); Intent intent = getIntent();
long id = intent.getLongExtra("id", -1L); long id = intent.getLongExtra("id", -1L);

View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> 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" />
<item <item
android:id="@+id/menu_sanitize" android:id="@+id/menu_sanitize"
android:checkable="true" android:checkable="true"