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.localbroadcastmanager:localbroadcastmanager:1.0.0'
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-lifecycle:1.0.0-beta03'
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.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
@ -337,6 +338,8 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
@Override
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);
initKeyStore();

View file

@ -37,6 +37,8 @@ import androidx.media.session.MediaButtonReceiver;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewFeature;
import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
@ -174,6 +176,9 @@ public class FileViewFragment extends BaseFragment implements
private View layoutDisplayArea;
private View layoutResolving;
private WebView webView;
private boolean webViewAdded;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_file_view, container, false);
@ -360,11 +365,30 @@ public class FileViewFragment extends BaseFragment implements
}
private void initWebView(View root) {
WebView webView = root.findViewById(R.id.file_view_webview);
webView.setWebViewClient(new LbryWebViewClient(getContext()));
WebSettings webSettings = webView.getSettings();
webSettings.setAllowFileAccess(true);
webSettings.setJavaScriptEnabled(true);
Context ctx = 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.setAllowFileAccess(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) {
@ -545,6 +569,13 @@ public class FileViewFragment extends BaseFragment implements
activity.removeStoragePermissionListener(this);
//activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
if (webView != null) {
webView.removeAllViews();
webView.loadUrl("about:blank");
webView.destroy();
webView = null;
}
}
private void setPlayerForPlayerView() {
@ -1585,7 +1616,9 @@ public class FileViewFragment extends BaseFragment implements
} else if (mediaType.startsWith("text")) {
// show web view (and parse markdown too)
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())) {
loadMarkdownFromFile(claimFile.getDownloadPath());
} else {
@ -1610,8 +1643,9 @@ public class FileViewFragment extends BaseFragment implements
@Override
public void onSuccess(String text) {
String html = buildMarkdownHtml(text);
WebView webView = getView().findViewById(R.id.file_view_webview);
webView.loadData(html, "text/html", "utf-8");
if (webView != null) {
webView.loadData(html, "text/html", "utf-8");
}
}
@Override

View file

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