From b931d0ce7dbdc34b4b58c61b641739e9c8b6bf2a Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sat, 23 May 2020 09:37:58 +0100 Subject: [PATCH 1/2] Fix dark theme reset error due to WebView. Apply dark theme to web views. --- app/build.gradle | 1 + .../java/io/lbry/browser/MainActivity.java | 3 ++ .../ui/findcontent/FileViewFragment.java | 50 ++++++++++++++++--- .../main/res/layout/fragment_file_view.xml | 5 +- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9464b20e..7a6f27ac 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index 9e2e4281..854fd6cb 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -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(); diff --git a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java index f464af14..a36ce9fc 100644 --- a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java @@ -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 diff --git a/app/src/main/res/layout/fragment_file_view.xml b/app/src/main/res/layout/fragment_file_view.xml index 9a61879a..c414b88d 100644 --- a/app/src/main/res/layout/fragment_file_view.xml +++ b/app/src/main/res/layout/fragment_file_view.xml @@ -633,9 +633,6 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> - + \ No newline at end of file -- 2.45.2 From 6c60d299af6930c63cb2701558fe7ff1ba0fdfe2 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Sat, 23 May 2020 15:16:11 +0100 Subject: [PATCH 2/2] Use thin divider in transaction history page. Set font for action bar title. --- .../main/java/io/lbry/browser/MainActivity.java | 14 ++++++++++++++ .../browser/ui/channel/ChannelFormFragment.java | 11 +++-------- .../io/lbry/browser/ui/other/AboutFragment.java | 5 +---- .../io/lbry/browser/ui/other/SettingsFragment.java | 5 +---- .../browser/ui/publish/PublishFormFragment.java | 10 ++-------- .../ui/wallet/TransactionHistoryFragment.java | 10 +++++----- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index 9e2e4281..c03e8727 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -17,6 +17,7 @@ import android.content.res.Configuration; import android.content.res.TypedArray; import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; +import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.AsyncTask; @@ -26,7 +27,10 @@ import android.os.Handler; import android.provider.MediaStore; import android.support.v4.media.session.MediaSessionCompat; import android.text.Editable; +import android.text.Spannable; +import android.text.SpannableString; import android.text.TextWatcher; +import android.text.style.TypefaceSpan; import android.util.Base64; import android.util.Log; import android.view.KeyEvent; @@ -62,6 +66,7 @@ import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; import androidx.core.content.res.ResourcesCompat; +import androidx.core.graphics.TypefaceCompat; import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.view.GravityCompat; import androidx.core.view.OnApplyWindowInsetsListener; @@ -539,6 +544,15 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener } } + public void setActionBarTitle(int stringResourceId) { + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + SpannableString spannable = new SpannableString(getString(stringResourceId)); + spannable.setSpan(new TypefaceSpan("inter"), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + actionBar.setTitle(spannable); + } + } + public void addScreenOrientationListener(ScreenOrientationListener listener) { if (!screenOrientationListeners.contains(listener)) { screenOrientationListeners.add(listener); diff --git a/app/src/main/java/io/lbry/browser/ui/channel/ChannelFormFragment.java b/app/src/main/java/io/lbry/browser/ui/channel/ChannelFormFragment.java index 441ba9b1..e8ae6052 100644 --- a/app/src/main/java/io/lbry/browser/ui/channel/ChannelFormFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/channel/ChannelFormFragment.java @@ -503,10 +503,8 @@ public class ChannelFormFragment extends BaseFragment implements activity.addFilePickerListener(this); activity.addWalletBalanceListener(this); - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(editMode ? R.string.edit_channel : R.string.create_a_channel); - } + activity.setActionBarTitle(editMode ? R.string.edit_channel : R.string.create_a_channel); + } } @@ -544,10 +542,7 @@ public class ChannelFormFragment extends BaseFragment implements LbryAnalytics.setCurrentScreen(activity, "Channel Form", "ChannelForm"); activity.addStoragePermissionListener(this); if (editMode) { - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.edit_channel); - } + activity.setActionBarTitle(R.string.edit_channel); } } String filterText = Helper.getValue(inputTagFilter.getText()); diff --git a/app/src/main/java/io/lbry/browser/ui/other/AboutFragment.java b/app/src/main/java/io/lbry/browser/ui/other/AboutFragment.java index 0923ad1b..8091014f 100644 --- a/app/src/main/java/io/lbry/browser/ui/other/AboutFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/other/AboutFragment.java @@ -180,10 +180,7 @@ public class AboutFragment extends BaseFragment implements SdkStatusListener { activity.lockDrawer(); activity.hideFloatingWalletBalance(); - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.about_lbry); - } + activity.setActionBarTitle(R.string.about_lbry); } } diff --git a/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java b/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java index 6cf4deaf..7e93b6ca 100644 --- a/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java @@ -35,10 +35,7 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared activity.lockDrawer(); activity.hideFloatingWalletBalance(); - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.settings); - } + activity.setActionBarTitle(R.string.settings); } } diff --git a/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java b/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java index 7e5614ac..368d7481 100644 --- a/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java @@ -499,10 +499,7 @@ public class PublishFormFragment extends BaseFragment implements activity.addFilePickerListener(this); activity.addWalletBalanceListener(this); - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(editMode ? R.string.edit_content : R.string.new_publish); - } + activity.setActionBarTitle(editMode ? R.string.edit_content : R.string.new_publish); } } @@ -889,10 +886,7 @@ public class PublishFormFragment extends BaseFragment implements LbryAnalytics.setCurrentScreen(activity, "Channel Form", "ChannelForm"); activity.addStoragePermissionListener(this); if (editMode) { - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.edit_content); - } + activity.setActionBarTitle(R.string.edit_content); } } diff --git a/app/src/main/java/io/lbry/browser/ui/wallet/TransactionHistoryFragment.java b/app/src/main/java/io/lbry/browser/ui/wallet/TransactionHistoryFragment.java index d9b70c7c..32df97ca 100644 --- a/app/src/main/java/io/lbry/browser/ui/wallet/TransactionHistoryFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/wallet/TransactionHistoryFragment.java @@ -10,6 +10,7 @@ import android.widget.ProgressBar; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; +import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -48,7 +49,9 @@ public class TransactionHistoryFragment extends BaseFragment implements Transact Context context = getContext(); LinearLayoutManager llm = new LinearLayoutManager(context); transactionList.setLayoutManager(llm); - transactionList.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL)); + DividerItemDecoration itemDecoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL); + itemDecoration.setDrawable(ContextCompat.getDrawable(context, R.drawable.thin_divider)); + transactionList.addItemDecoration(itemDecoration); transactionList.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override @@ -133,10 +136,7 @@ public class TransactionHistoryFragment extends BaseFragment implements Transact activity.showNavigationBackIcon(); activity.lockDrawer(); - ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - actionBar.setTitle(R.string.transaction_history); - } + activity.setActionBarTitle(R.string.transaction_history); } } -- 2.45.2