Merge pull request #884 from lbryio/dark-theme-fix

Fix dark theme reset error due to WebView. Apply dark theme to web views.
This commit is contained in:
Akinwale Ariwodola 2020-05-23 09:41:59 +01:00 committed by GitHub
commit 08582238c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 12 deletions

View file

@ -62,6 +62,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'androidx.preference:preference:1.1.1' implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.webkit:webkit:1.3.0-alpha02'
implementation 'androidx.camera:camera-camera2:1.0.0-beta03' implementation 'androidx.camera:camera-camera2:1.0.0-beta03'
implementation 'androidx.camera:camera-lifecycle:1.0.0-beta03' implementation 'androidx.camera:camera-lifecycle:1.0.0-beta03'
implementation 'androidx.camera:camera-view:1.0.0-alpha10' implementation 'androidx.camera:camera-view:1.0.0-alpha10'

View file

@ -35,6 +35,7 @@ import android.view.Menu;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -337,6 +338,8 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
// workaround to fix dark theme because https://issuetracker.google.com/issues/37124582
new WebView(this);
AppCompatDelegate.setDefaultNightMode(isDarkMode() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO); AppCompatDelegate.setDefaultNightMode(isDarkMode() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
initKeyStore(); initKeyStore();

View file

@ -37,6 +37,8 @@ import androidx.media.session.MediaButtonReceiver;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView; import com.github.chrisbanes.photoview.PhotoView;
@ -174,6 +176,9 @@ public class FileViewFragment extends BaseFragment implements
private View layoutDisplayArea; private View layoutDisplayArea;
private View layoutResolving; private View layoutResolving;
private WebView webView;
private boolean webViewAdded;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_file_view, container, false); View root = inflater.inflate(R.layout.fragment_file_view, container, false);
@ -360,13 +365,32 @@ public class FileViewFragment extends BaseFragment implements
} }
private void initWebView(View root) { private void initWebView(View root) {
WebView webView = root.findViewById(R.id.file_view_webview); Context ctx = getContext();
webView.setWebViewClient(new LbryWebViewClient(getContext())); if (ctx != null) {
if (webView == null) {
webView = new WebView(ctx);
webView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
webView.setWebViewClient(new LbryWebViewClient(ctx));
WebSettings webSettings = webView.getSettings(); WebSettings webSettings = webView.getSettings();
webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccess(true);
webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptEnabled(true);
} }
if (!webViewAdded && root != null) {
((RelativeLayout) root.findViewById(R.id.file_view_webview_container)).addView(webView);
webViewAdded = true;
}
}
}
private void applyThemeToWebView() {
Context context = getContext();
if (context instanceof MainActivity && webView != null && WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
MainActivity activity = (MainActivity) context;
WebSettingsCompat.setForceDark(webView.getSettings(), activity.isDarkMode() ? WebSettingsCompat.FORCE_DARK_ON : WebSettingsCompat.FORCE_DARK_OFF);
}
}
private void logUrlEvent(String url) { private void logUrlEvent(String url) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString("uri", url); bundle.putString("uri", url);
@ -545,6 +569,13 @@ public class FileViewFragment extends BaseFragment implements
activity.removeStoragePermissionListener(this); activity.removeStoragePermissionListener(this);
//activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); //activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} }
if (webView != null) {
webView.removeAllViews();
webView.loadUrl("about:blank");
webView.destroy();
webView = null;
}
} }
private void setPlayerForPlayerView() { private void setPlayerForPlayerView() {
@ -1585,7 +1616,9 @@ public class FileViewFragment extends BaseFragment implements
} else if (mediaType.startsWith("text")) { } else if (mediaType.startsWith("text")) {
// show web view (and parse markdown too) // show web view (and parse markdown too)
View container = root.findViewById(R.id.file_view_webview_container); View container = root.findViewById(R.id.file_view_webview_container);
WebView webView = root.findViewById(R.id.file_view_webview); initWebView(root);
applyThemeToWebView();
if (Arrays.asList("text/markdown", "text/md").contains(mediaType.toLowerCase())) { if (Arrays.asList("text/markdown", "text/md").contains(mediaType.toLowerCase())) {
loadMarkdownFromFile(claimFile.getDownloadPath()); loadMarkdownFromFile(claimFile.getDownloadPath());
} else { } else {
@ -1610,9 +1643,10 @@ public class FileViewFragment extends BaseFragment implements
@Override @Override
public void onSuccess(String text) { public void onSuccess(String text) {
String html = buildMarkdownHtml(text); String html = buildMarkdownHtml(text);
WebView webView = getView().findViewById(R.id.file_view_webview); if (webView != null) {
webView.loadData(html, "text/html", "utf-8"); webView.loadData(html, "text/html", "utf-8");
} }
}
@Override @Override
public void onError(Exception error) { public void onError(Exception error) {

View file

@ -633,9 +633,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:visibility="gone"> android:visibility="gone">
<WebView
android:id="@+id/file_view_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout> </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>