X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.java;h=33687f9a48512202c96d3a5a5086fc49cee92d0b;hp=b0cd103a68203752c29cafd546a9341d2ddd3725;hb=8a72caf321663f9549997695af01d89db45fe7d1;hpb=0c842fcba25551727cc26a7edc55bd5d5bf262ee 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 b0cd103a..33687f9a 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -115,6 +115,7 @@ import com.stoutner.privacybrowser.BuildConfig; import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.adapters.WebViewPagerAdapter; import com.stoutner.privacybrowser.asynctasks.GetHostIpAddresses; +import com.stoutner.privacybrowser.asynctasks.PopulateBlocklists; import com.stoutner.privacybrowser.dialogs.AdConsentDialog; import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog; import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog; @@ -157,7 +158,7 @@ import java.util.Set; // 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, - EditBookmarkFolderDialog.EditBookmarkFolderListener, NavigationView.OnNavigationItemSelectedListener, WebViewTabFragment.NewTabListener { + EditBookmarkFolderDialog.EditBookmarkFolderListener, NavigationView.OnNavigationItemSelectedListener, PopulateBlocklists.PopulateBlocklistsListener, WebViewTabFragment.NewTabListener { // `orbotStatus` is public static so it can be accessed from `OrbotProxyHelper`. It is also used in `onCreate()`, `onResume()`, and `applyProxyThroughOrbot()`. public static String orbotStatus; @@ -199,7 +200,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // The options menu is set in `onCreateOptionsMenu()` and used in `onOptionsItemSelected()`, `updatePrivacyIcons()`, and `initializeWebView()`. private Menu optionsMenu; - // The blocklists are populated in `onCreate()` and accessed from `initializeWebView()`. + // The blocklists are populated in `finishedPopulatingBlocklists()` and accessed from `initializeWebView()`. private ArrayList> easyList; private ArrayList> easyPrivacy; private ArrayList> fanboysAnnoyanceList; @@ -443,16 +444,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Register `orbotStatusBroadcastReceiver` on `this` context. this.registerReceiver(orbotStatusBroadcastReceiver, new IntentFilter("org.torproject.android.intent.action.STATUS")); - // Instantiate the blocklist helper. - BlockListHelper blockListHelper = new BlockListHelper(); - - // Parse the block lists. - easyList = blockListHelper.parseBlockList(getAssets(), "blocklists/easylist.txt"); - easyPrivacy = blockListHelper.parseBlockList(getAssets(), "blocklists/easyprivacy.txt"); - fanboysAnnoyanceList = blockListHelper.parseBlockList(getAssets(), "blocklists/fanboy-annoyance.txt"); - fanboysSocialList = blockListHelper.parseBlockList(getAssets(), "blocklists/fanboy-social.txt"); - ultraPrivacy = blockListHelper.parseBlockList(getAssets(), "blocklists/ultraprivacy.txt"); - // Get handles for views that need to be modified. DrawerLayout drawerLayout = findViewById(R.id.drawerlayout); NavigationView navigationView = findViewById(R.id.navigationview); @@ -504,7 +495,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Create a handler to select the tab. Handler selectTabHandler = new Handler(); - // Create a runnable select the new tab. + // Create a runnable to select the tab. Runnable selectTabRunnable = () -> { // Get a handle for the tab. TabLayout.Tab tab = tabLayout.getTabAt(position); @@ -516,8 +507,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook tab.select(); }; - // Select the tab layout after 100 milliseconds, which leaves enough time for a new tab to be created. - selectTabHandler.postDelayed(selectTabRunnable, 100); + // Select the tab layout after 150 milliseconds, which leaves enough time for a new tab to be inflated. + selectTabHandler.postDelayed(selectTabRunnable, 150); } } @@ -550,9 +541,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } }); - // Add the first tab. - addTab(null); - // Set the bookmarks drawer resources according to the theme. This can't be done in the layout due to compatibility issues with the `DrawerLayout` support widget. // The deprecated `getResources().getDrawable()` must be used until the minimum API >= 21 and and `getResources().getColor()` must be used until the minimum API >= 23. if (darkTheme) { @@ -805,6 +793,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Destroy the bare WebView. bareWebView.destroy(); + + // Populate the blocklists. + new PopulateBlocklists(this, this).execute(); } @Override @@ -1900,13 +1891,8 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Update the swipe refresh layout. if (currentWebView.getSwipeToRefresh()) { // Swipe to refresh is enabled. - if (Build.VERSION.SDK_INT >= 23) { // For API >= 23, the status of the scroll refresh listener is continuously updated by the on scroll change listener. - // Only enable the swipe refresh layout if the WebView is scrolled to the top. - swipeRefreshLayout.setEnabled(currentWebView.getY() == 0); - } else { // For API < 23, the swipe refresh layout is always enabled. - // Enable the swipe refresh layout. - swipeRefreshLayout.setEnabled(true); - } + // Only enable the swipe refresh layout if the WebView is scrolled to the top. It is updated every time the scroll changes. + swipeRefreshLayout.setEnabled(currentWebView.getY() == 0); } else { // Swipe to refresh is disabled. // Disable the swipe refresh layout. swipeRefreshLayout.setEnabled(false); @@ -1986,6 +1972,29 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook }, 200); return true; + case R.id.print: + // Get a print manager instance. + PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); + + // Remove the lint error below that print manager might be null. + assert printManager != null; + + // Create a print document adapter from the current WebView. + PrintDocumentAdapter printDocumentAdapter = currentWebView.createPrintDocumentAdapter(); + + // Print the document. + printManager.print(getString(R.string.privacy_browser_web_page), printDocumentAdapter, null); + return true; + + case R.id.add_to_homescreen: + // Instantiate the create home screen shortcut dialog. + DialogFragment createHomeScreenShortcutDialogFragment = CreateHomeScreenShortcutDialog.createDialog(currentWebView.getTitle(), currentWebView.getUrl(), + currentWebView.getFavoriteOrDefaultIcon()); + + // Show the create home screen shortcut dialog. + createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut)); + return true; + case R.id.view_source: // Create an intent to launch the view source activity. Intent viewSourceIntent = new Intent(this, ViewSourceActivity.class); @@ -2011,20 +2020,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook startActivity(Intent.createChooser(shareIntent, getString(R.string.share_url))); return true; - case R.id.print: - // Get a print manager instance. - PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); - - // Remove the lint error below that print manager might be null. - assert printManager != null; - - // Create a print document adapter from the current WebView. - PrintDocumentAdapter printDocumentAdapter = currentWebView.createPrintDocumentAdapter(); - - // Print the document. - printManager.print(getString(R.string.privacy_browser_web_page), printDocumentAdapter, null); - return true; - case R.id.open_with_app: openWithApp(currentWebView.getUrl()); return true; @@ -2033,15 +2028,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook openWithBrowser(currentWebView.getUrl()); return true; - case R.id.add_to_homescreen: - // Instantiate the create home screen shortcut dialog. - DialogFragment createHomeScreenShortcutDialogFragment = CreateHomeScreenShortcutDialog.createDialog(currentWebView.getTitle(), currentWebView.getUrl(), - currentWebView.getFavoriteOrDefaultIcon()); - - // Show the create home screen shortcut dialog. - createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getString(R.string.create_shortcut)); - return true; - case R.id.proxy_through_orbot: // Toggle the proxy through Orbot variable. proxyThroughOrbot = !proxyThroughOrbot; @@ -3058,7 +3044,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook assert inputMethodManager != null; // Hide the keyboard. - inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0); + inputMethodManager.hideSoftInputFromWindow(toolbar.getWindowToken(), 0); } private void applyAppSettings() { @@ -4028,6 +4014,18 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook return url; } + public void finishedPopulatingBlocklists(ArrayList>> combinedBlocklists) { + // Store the blocklists. + easyList = combinedBlocklists.get(0); + easyPrivacy = combinedBlocklists.get(1); + fanboysAnnoyanceList = combinedBlocklists.get(2); + fanboysSocialList = combinedBlocklists.get(3); + ultraPrivacy = combinedBlocklists.get(4); + + // Add the first tab. + addNewTab(""); + } + public void addTab(View view) { // Add a new tab with a blank URL. addNewTab(""); @@ -4283,7 +4281,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook EditText urlEditText = findViewById(R.id.url_edittext); SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swiperefreshlayout); - //Stop the swipe to refresh indicator if it is running + // Stop the swipe to refresh indicator if it is running swipeRefreshLayout.setRefreshing(false); // Get the WebView tab fragment. @@ -4293,19 +4291,14 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook View fragmentView = webViewTabFragment.getView(); // Set the current WebView if the fragment view is not null. - if (fragmentView != null) { + if (fragmentView != null) { // The fragment has been populated. // Store the current WebView. currentWebView = fragmentView.findViewById(R.id.nestedscroll_webview); // Update the status of swipe to refresh. if (currentWebView.getSwipeToRefresh()) { // Swipe to refresh is enabled. - if (Build.VERSION.SDK_INT >= 23) { // For API >= 23, swipe refresh layout is continuously updated with an on scroll change listener and only enabled if the WebView is scrolled to the top. - // Enable the swipe refresh layout if the WebView is scrolled all the way to the top. - swipeRefreshLayout.setEnabled(currentWebView.getY() == 0); - } else { - // Enable the swipe refresh layout. - swipeRefreshLayout.setEnabled(true); - } + // Enable the swipe refresh layout if the WebView is scrolled all the way to the top. It is updated every time the scroll changes. + swipeRefreshLayout.setEnabled(currentWebView.getY() == 0); } else { // Swipe to refresh is disabled. // Disable the swipe refresh layout. swipeRefreshLayout.setEnabled(false); @@ -4369,6 +4362,18 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook } else { urlRelativeLayout.setBackground(getResources().getDrawable(R.color.transparent)); } + } else { // The fragment has not been populated. Try again in 100 milliseconds. + // Create a handler to set the current WebView. + Handler setCurrentWebViewHandler = new Handler(); + + // Create a runnable to set the current WebView. + Runnable setCurrentWebWebRunnable = () -> { + // Set the current WebView. + setCurrentWebView(pageNumber); + }; + + // Try setting the current WebView again after 100 milliseconds. + setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 100); } } @@ -4402,9 +4407,6 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Get a handle for the shared preferences. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); - // Get the relevant preferences. - boolean downloadWithExternalApp = sharedPreferences.getBoolean("download_with_external_app", false); - // Initialize the favorite icon. nestedScrollWebView.initializeFavoriteIcon(); @@ -4525,7 +4527,7 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook // Allow the downloading of files. nestedScrollWebView.setDownloadListener((String downloadUrl, String userAgent, String contentDisposition, String mimetype, long contentLength) -> { // Check if the download should be processed by an external app. - if (downloadWithExternalApp) { // Download with an external app. + if (sharedPreferences.getBoolean("download_with_external_app", false)) { // Download with an external app. // Create a download intent. Not specifying the action type will display the maximum number of options. Intent downloadIntent = new Intent();