Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 49: Add Dark mode support for OSS Licenses Webview. #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.annotation:annotation:1.5.0"
implementation 'androidx.preference:preference:1.2.0'
implementation 'androidx.webkit:webkit:1.5.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
30 changes: 27 additions & 3 deletions app/src/main/java/com/czlucius/scan/ui/PreferencesFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

package com.czlucius.scan.ui;

import static androidx.webkit.WebViewFeature.ALGORITHMIC_DARKENING;
import static androidx.webkit.WebViewFeature.isFeatureSupported;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -36,13 +40,16 @@
import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.webkit.WebSettingsCompat;

import com.czlucius.scan.R;
import com.czlucius.scan.Utils;
import com.czlucius.scan.callbacks.ManualResetPreferenceClickListener;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class PreferencesFragment extends PreferenceFragmentCompat {
private static final String TAG = PreferencesFragment.class.getSimpleName();

boolean nightModeActive = false;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand All @@ -55,6 +62,16 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbra
// Instantiate ads if on play flavour.
View v = super.onCreateView(inflater, container, savedInstanceState);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Configuration configuration = v.getResources().getConfiguration();
if (configuration != null) {
nightModeActive = v.getResources().getConfiguration().isNightModeActive();
Log.d(TAG, "Is night mode active: " + nightModeActive);
} else {
Log.e(TAG, "Configuration is null");
}
}

Preference oss_link = findPreference("open_source");
if (oss_link != null) {
oss_link.setOnPreferenceClickListener(preference -> {
Expand Down Expand Up @@ -147,6 +164,13 @@ private boolean shouldOverrideUrlLoading(Uri url) {
});
webView.zoomBy(0.5F);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && isFeatureSupported(ALGORITHMIC_DARKENING)) {
WebSettingsCompat.setAlgorithmicDarkeningAllowed(webView.getSettings(), nightModeActive);
} else if (nightModeActive) {
//TODO: Remove this else block once app sets minSdkVersion to 29
WebSettingsCompat.setForceDark(webView.getSettings(), WebSettingsCompat.FORCE_DARK_ON);
}

MaterialAlertDialogBuilder ossDialog = new MaterialAlertDialogBuilder(requireContext());
ossDialog.setView(webView);
AlertDialog finalDialog = ossDialog.create();
Expand Down