From e7380180e2936dc8ebafaae1999a4b6789309c13 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Tue, 23 Apr 2019 11:32:41 -0700 Subject: [PATCH] Finish tabbed browsing. https://redmine.stoutner.com/issues/22 --- .../activities/MainWebViewActivity.java | 66 ++++++++++++------- .../asynctasks/GetHostIpAddresses.java | 1 - .../fragments/SettingsFragment.java | 37 +++++++++-- .../layout/appbar_spinner_dropdown_item.xml | 2 +- .../main/res/layout/appbar_spinner_item.xml | 2 +- .../bookmarks_activity_item_linearlayout.xml | 2 +- ...okmarks_databaseview_item_linearlayout.xml | 6 +- .../bookmarks_drawer_item_linearlayout.xml | 2 +- .../databaseview_spinner_dropdown_items.xml | 3 +- .../res/layout/databaseview_spinner_item.xml | 3 +- .../res/layout/domain_name_linearlayout.xml | 2 +- ...domain_settings_spinner_dropdown_items.xml | 2 +- .../move_to_folder_item_linearlayout.xml | 2 +- .../requests_appbar_spinner_dropdown_item.xml | 2 +- .../layout/requests_appbar_spinner_item.xml | 2 +- .../res/layout/requests_item_linearlayout.xml | 2 +- .../res/layout/spinner_dropdown_items.xml | 2 +- app/src/main/res/layout/spinner_item.xml | 2 +- .../layout/url_history_item_linearlayout.xml | 2 +- .../main/res/layout/webview_framelayout.xml | 7 +- app/src/main/res/values-v21/styles.xml | 14 ++-- app/src/main/res/values/attrs.xml | 1 + app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/styles.xml | 14 ++-- 24 files changed, 113 insertions(+), 66 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index a363ef1d..908979b9 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -152,8 +152,6 @@ import java.util.List; import java.util.Map; import java.util.Set; -// TODO. New tabs are white in dark mode. - // AppCompatActivity from android.support.v7.app.AppCompatActivity must be used to have access to the SupportActionBar until the minimum API is >= 21. public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, DownloadFileDialog.DownloadFileListener, DownloadImageDialog.DownloadImageListener, DownloadLocationPermissionDialog.DownloadLocationPermissionDialogListener, EditBookmarkDialog.EditBookmarkListener, @@ -611,8 +609,10 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook @Override public void afterTextChanged(Editable s) { - // Search for the text in `mainWebView`. - currentWebView.findAllAsync(findOnPageEditText.getText().toString()); + // Search for the text in the WebView if it is not null. Sometimes on resume after a period of non-use the WebView will be null. + if (currentWebView != null) { + currentWebView.findAllAsync(findOnPageEditText.getText().toString()); + } } }); @@ -894,8 +894,10 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Reset the current domain name so the domain settings will be reapplied. nestedScrollWebView.resetCurrentDomainName(); - // Reapply the domain settings. - applyDomainSettings(nestedScrollWebView, nestedScrollWebView.getUrl(), false, true); + // Reapply the domain settings if the URL is not null, which can happen if an empty tab is active when returning from settings. + if (nestedScrollWebView.getUrl() != null) { + applyDomainSettings(nestedScrollWebView, nestedScrollWebView.getUrl(), false, true); + } } } } @@ -4226,23 +4228,37 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Update the privacy icons. `true` redraws the icons in the app bar. updatePrivacyIcons(true); - // Clear the focus from the URL text box. - urlEditText.clearFocus(); - // Get a handle for the input method manager. InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); // Remove the lint warning below that the input method manager might be null. assert inputMethodManager != null; - // Hide the soft keyboard. - inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0); + // Get the current URL. + String url = currentWebView.getUrl(); + + if ((url == null) || url.equals("about:blank")) { // The WebView is blank. + // Display the hint in the URL edit text. + urlEditText.setText(""); + + // Request focus for the URL text box. + urlEditText.requestFocus(); - // Display the current URL in the URL text box. - urlEditText.setText(currentWebView.getUrl()); + // Display the keyboard. + inputMethodManager.showSoftInput(urlEditText, 0); + } else { // The WebView has a loaded URL. + // Clear the focus from the URL text box. + urlEditText.clearFocus(); - // Highlight the URL text. - highlightUrlText(); + // Hide the soft keyboard. + inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0); + + // Display the current URL in the URL text box. + urlEditText.setText(url); + + // Highlight the URL text. + highlightUrlText(); + } // Set the background to indicate the domain settings status. if (currentWebView.getDomainSettingsApplied()) { @@ -4481,7 +4497,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook @Override public void onProgressChanged(WebView view, int progress) { // Inject the night mode CSS if night mode is enabled. - if (nestedScrollWebView.getNightMode()) { + if (nestedScrollWebView.getNightMode()) { // Night mode is enabled. // `background-color: #212121` sets the background to be dark gray. `color: #BDBDBD` sets the text color to be light gray. `box-shadow: none` removes a lower underline on links // used by WordPress. `text-decoration: none` removes all text underlines. `text-shadow: none` removes text shadows, which usually have a hard coded color. // `border: none` removes all borders, which can also be used to underline text. `a {color: #1565C0}` sets links to be a dark blue. @@ -4503,6 +4519,11 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Display the WebView after 500 milliseconds. displayWebViewHandler.postDelayed(displayWebViewRunnable, 500); }); + } else { // Night mode is disabled. + // Display the nested scroll WebView if night mode is disabled. + // Because of a race condition between `applyDomainSettings` and `onPageStarted`, + // when night mode is set by domain settings the WebView may be hidden even if night mode is not currently enabled. + nestedScrollWebView.setVisibility(View.VISIBLE); } // Update the progress bar. @@ -4516,13 +4537,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Hide the progress bar. progressBar.setVisibility(View.GONE); - // Display the nested scroll WebView if night mode is disabled. - // Because of a race condition between `applyDomainSettings` and `onPageStarted`, - // when night mode is set by domain settings the WebView may be hidden even if night mode is not currently enabled. - if (!nestedScrollWebView.getNightMode()) { - nestedScrollWebView.setVisibility(View.VISIBLE); - } - //Stop the swipe to refresh indicator if it is running swipeRefreshLayout.setRefreshing(false); } @@ -5107,6 +5121,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // If night mode is enabled, hide `mainWebView` until after the night mode CSS is applied. if (nestedScrollWebView.getNightMode()) { nestedScrollWebView.setVisibility(View.INVISIBLE); + } else { + nestedScrollWebView.setVisibility(View.VISIBLE); } // Hide the keyboard. @@ -5250,8 +5266,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Display the keyboard. inputMethodManager.showSoftInput(urlEditText, 0); - // Hide the WebView, which causes the default background color to be displayed according to the theme. // TODO - nestedScrollWebView.setVisibility(View.GONE); + // Hide the WebView, which causes the default background color to be displayed according to the theme. + nestedScrollWebView.setVisibility(View.INVISIBLE); // Apply the domain settings. This clears any settings from the previous domain. applyDomainSettings(nestedScrollWebView, "", true, false); diff --git a/app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java b/app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java index 66111de7..050ae1dd 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java +++ b/app/src/main/java/com/stoutner/privacybrowser/asynctasks/GetHostIpAddresses.java @@ -24,7 +24,6 @@ import android.os.AsyncTask; import androidx.fragment.app.FragmentManager; -import com.stoutner.privacybrowser.activities.MainWebViewActivity; import com.stoutner.privacybrowser.helpers.CheckPinnedMismatchHelper; import com.stoutner.privacybrowser.views.NestedScrollWebView; diff --git a/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java index 3b28d663..4001345b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/fragments/SettingsFragment.java @@ -25,6 +25,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; +import android.os.Handler; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; @@ -942,11 +943,23 @@ public class SettingsFragment extends PreferenceFragment { // Assert that the intent is not null to remove the lint error below. assert allowScreenshotsRestartIntent != null; - // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. TODO. + // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. allowScreenshotsRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - // Make it so. - startActivity(allowScreenshotsRestartIntent); + // Create a handler to restart the activity. + Handler allowScreenshotsRestartHandler = new Handler(); + + // Create a runnable to restart the activity. + Runnable allowScreenshotsRestartRunnable = () -> { + // Restart the activity. + startActivity(allowScreenshotsRestartIntent); + + // Kill this instance of Privacy Browser. Otherwise, the app exhibits sporadic behavior after the restart. + System.exit(0); + }; + + // Restart the activity after 100 milliseconds, so that the app has enough time to save the change to the preference. + allowScreenshotsRestartHandler.postDelayed(allowScreenshotsRestartRunnable, 100); break; case "easylist": @@ -1482,11 +1495,23 @@ public class SettingsFragment extends PreferenceFragment { // Assert that the intent is not null to remove the lint error below. assert changeThemeRestartIntent != null; - // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. TODO. + // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. changeThemeRestartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - // Make it so. - startActivity(changeThemeRestartIntent); + // Create a handler to restart the activity. + Handler changeThemeRestartHandler = new Handler(); + + // Create a runnable to restart the activity. + Runnable changeThemeRestartRunnable = () -> { + // Restart the activity. + startActivity(changeThemeRestartIntent); + + // Kill this instance of Privacy Browser. Otherwise, the app exhibits sporadic behavior after the restart. + System.exit(0); + }; + + // Restart the activity after 100 milliseconds, so that the app has enought time to save the change to the preference. + changeThemeRestartHandler.postDelayed(changeThemeRestartRunnable, 100); break; case "night_mode": diff --git a/app/src/main/res/layout/appbar_spinner_dropdown_item.xml b/app/src/main/res/layout/appbar_spinner_dropdown_item.xml index a6586750..a04ab57c 100644 --- a/app/src/main/res/layout/appbar_spinner_dropdown_item.xml +++ b/app/src/main/res/layout/appbar_spinner_dropdown_item.xml @@ -39,7 +39,7 @@ android:id="@+id/spinner_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:paddingStart="20dp" android:paddingEnd="20dp" diff --git a/app/src/main/res/layout/appbar_spinner_item.xml b/app/src/main/res/layout/appbar_spinner_item.xml index 3b79130b..edc8d7c9 100644 --- a/app/src/main/res/layout/appbar_spinner_item.xml +++ b/app/src/main/res/layout/appbar_spinner_item.xml @@ -38,7 +38,7 @@ android:id="@+id/spinner_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:paddingStart="10dp" android:paddingEnd="10dp" diff --git a/app/src/main/res/layout/bookmarks_activity_item_linearlayout.xml b/app/src/main/res/layout/bookmarks_activity_item_linearlayout.xml index 6cd123d1..83501e81 100644 --- a/app/src/main/res/layout/bookmarks_activity_item_linearlayout.xml +++ b/app/src/main/res/layout/bookmarks_activity_item_linearlayout.xml @@ -44,6 +44,6 @@ android:textColor="?android:attr/textColorPrimary" android:textSize="22sp" android:layout_margin="10dp" - android:maxLines="1" + android:lines="1" android:ellipsize="end" /> \ No newline at end of file diff --git a/app/src/main/res/layout/bookmarks_databaseview_item_linearlayout.xml b/app/src/main/res/layout/bookmarks_databaseview_item_linearlayout.xml index 85031f9b..dda54ea8 100644 --- a/app/src/main/res/layout/bookmarks_databaseview_item_linearlayout.xml +++ b/app/src/main/res/layout/bookmarks_databaseview_item_linearlayout.xml @@ -60,7 +60,7 @@ android:textColor="?android:attr/textColorPrimary" android:textSize="22sp" android:ellipsize="end" - android:maxLines="1" /> + android:lines="1" /> @@ -73,7 +73,7 @@ android:textColor="?android:attr/textColorPrimary" android:textSize="22sp" android:ellipsize="end" - android:maxLines="1" /> + android:lines="1" /> + android:lines="1" /> \ No newline at end of file diff --git a/app/src/main/res/layout/bookmarks_drawer_item_linearlayout.xml b/app/src/main/res/layout/bookmarks_drawer_item_linearlayout.xml index 3009a352..203685dd 100644 --- a/app/src/main/res/layout/bookmarks_drawer_item_linearlayout.xml +++ b/app/src/main/res/layout/bookmarks_drawer_item_linearlayout.xml @@ -43,6 +43,6 @@ android:textColor="?android:attr/textColorPrimary" android:textSize="22sp" android:layout_margin="10dp" - android:maxLines="1" + android:lines="1" android:ellipsize="end" /> \ No newline at end of file diff --git a/app/src/main/res/layout/databaseview_spinner_dropdown_items.xml b/app/src/main/res/layout/databaseview_spinner_dropdown_items.xml index 5e3c1ff0..9efa9ef1 100644 --- a/app/src/main/res/layout/databaseview_spinner_dropdown_items.xml +++ b/app/src/main/res/layout/databaseview_spinner_dropdown_items.xml @@ -35,11 +35,10 @@ \ No newline at end of file diff --git a/app/src/main/res/layout/domain_settings_spinner_dropdown_items.xml b/app/src/main/res/layout/domain_settings_spinner_dropdown_items.xml index 6d746b6b..d0b6eb12 100644 --- a/app/src/main/res/layout/domain_settings_spinner_dropdown_items.xml +++ b/app/src/main/res/layout/domain_settings_spinner_dropdown_items.xml @@ -23,7 +23,7 @@ android:id="@+id/spinner_dropdown_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:gravity="center_vertical" android:paddingStart="16dp" diff --git a/app/src/main/res/layout/move_to_folder_item_linearlayout.xml b/app/src/main/res/layout/move_to_folder_item_linearlayout.xml index a4b3bd17..02bc3d77 100644 --- a/app/src/main/res/layout/move_to_folder_item_linearlayout.xml +++ b/app/src/main/res/layout/move_to_folder_item_linearlayout.xml @@ -41,7 +41,7 @@ android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_margin="10dp" - android:maxLines="1" + android:lines="1" android:textColor="?android:attr/textColorPrimary" android:textSize="22sp" /> \ No newline at end of file diff --git a/app/src/main/res/layout/requests_appbar_spinner_dropdown_item.xml b/app/src/main/res/layout/requests_appbar_spinner_dropdown_item.xml index 6c9fd9d0..5021ab0d 100644 --- a/app/src/main/res/layout/requests_appbar_spinner_dropdown_item.xml +++ b/app/src/main/res/layout/requests_appbar_spinner_dropdown_item.xml @@ -24,7 +24,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:paddingStart="20dp" android:paddingEnd="20dp" diff --git a/app/src/main/res/layout/requests_appbar_spinner_item.xml b/app/src/main/res/layout/requests_appbar_spinner_item.xml index 459f8547..4d707505 100644 --- a/app/src/main/res/layout/requests_appbar_spinner_item.xml +++ b/app/src/main/res/layout/requests_appbar_spinner_item.xml @@ -23,7 +23,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:paddingStart="10dp" android:paddingEnd="10dp" diff --git a/app/src/main/res/layout/requests_item_linearlayout.xml b/app/src/main/res/layout/requests_item_linearlayout.xml index b0489d74..fee27826 100644 --- a/app/src/main/res/layout/requests_item_linearlayout.xml +++ b/app/src/main/res/layout/requests_item_linearlayout.xml @@ -38,6 +38,6 @@ android:layout_width="wrap_content" android:layout_margin="10dp" android:textSize="16sp" - android:maxLines="1" + android:lines="1" android:ellipsize="end" /> \ No newline at end of file diff --git a/app/src/main/res/layout/spinner_dropdown_items.xml b/app/src/main/res/layout/spinner_dropdown_items.xml index 7105179b..8467f38c 100644 --- a/app/src/main/res/layout/spinner_dropdown_items.xml +++ b/app/src/main/res/layout/spinner_dropdown_items.xml @@ -24,7 +24,7 @@ android:id="@+id/spinner_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:paddingStart="20dp" android:paddingEnd="20dp" diff --git a/app/src/main/res/layout/spinner_item.xml b/app/src/main/res/layout/spinner_item.xml index 5bc6f820..62b737b6 100644 --- a/app/src/main/res/layout/spinner_item.xml +++ b/app/src/main/res/layout/spinner_item.xml @@ -23,7 +23,7 @@ android:id="@+id/spinner_item_textview" android:layout_height="wrap_content" android:layout_width="match_parent" - android:maxLines="1" + android:lines="1" android:ellipsize="end" android:paddingStart="10dp" android:paddingEnd="10dp" diff --git a/app/src/main/res/layout/url_history_item_linearlayout.xml b/app/src/main/res/layout/url_history_item_linearlayout.xml index b6e7338d..e099fc8b 100644 --- a/app/src/main/res/layout/url_history_item_linearlayout.xml +++ b/app/src/main/res/layout/url_history_item_linearlayout.xml @@ -41,6 +41,6 @@ android:textColor="?attr/urlHistoryText" android:textSize="16sp" android:layout_margin="10dp" - android:maxLines="1" + android:lines="1" android:ellipsize="end"/> \ No newline at end of file diff --git a/app/src/main/res/layout/webview_framelayout.xml b/app/src/main/res/layout/webview_framelayout.xml index 27e62773..0243a062 100644 --- a/app/src/main/res/layout/webview_framelayout.xml +++ b/app/src/main/res/layout/webview_framelayout.xml @@ -23,14 +23,17 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" - android:layout_height="match_parent" > + android:layout_height="match_parent" + android:background="?attr/webViewBackground" > + + android:focusableInTouchMode="true" + android:visibility="invisible" /> @@ -126,15 +127,15 @@ diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index e0e767e3..3632b7cc 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -39,6 +39,7 @@ + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 0bcee21d..bb10d238 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -53,6 +53,7 @@ #FF163317 + #FFFAFAFA #FFF5F5F5 #FFEEEEEE #FFE0E0E0 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index c8af48ff..694ed6b6 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -24,16 +24,16 @@ @@ -124,15 +125,15 @@ -- 2.45.2