]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java
Completely remove Privacy Browser from RAM after exit. Updated German translations...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / MainWebViewActivity.java
index 7872677dcceaa4f9c83eb532584bd4dd0b63a191..9bcc6e4b0b528087aee3577e427fdd1656b4a2de 100644 (file)
@@ -49,6 +49,7 @@ import android.support.v4.widget.SwipeRefreshLayout;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
+import android.support.v7.app.AppCompatDialogFragment;
 import android.support.v7.widget.Toolbar;
 import android.util.Patterns;
 import android.view.KeyEvent;
@@ -354,8 +355,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             @Override
             public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                 // Show the `DownloadFile` `AlertDialog` and name this instance `@string/download`.
-                DialogFragment downloadFileDialogFragment = DownloadFile.fromUrl(url, contentDisposition, contentLength);
-                downloadFileDialogFragment.show(getFragmentManager(), getResources().getString(R.string.download));
+                AppCompatDialogFragment downloadFileDialogFragment = DownloadFile.fromUrl(url, contentDisposition, contentLength);
+                downloadFileDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.download));
             }
         });
 
@@ -439,8 +440,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Set mainMenu so it can be used by `onOptionsItemSelected()` and `updatePrivacyIcons`.
         mainMenu = menu;
 
-        // Set the initial status of the privacy icons.
-        updatePrivacyIcons();
+        // Set the initial status of the privacy icons.  `false` does not call `invalidateOptionsMenu` as the last step.
+        updatePrivacyIcons(false);
 
         // Get handles for the menu items.
         toggleJavaScript = menu.findItem(R.id.toggleJavaScript);
@@ -579,8 +580,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Apply the new JavaScript status.
                 mainWebView.getSettings().setJavaScriptEnabled(javaScriptEnabled);
 
-                // Update the privacy icon.
-                updatePrivacyIcons();
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
 
                 // Display a `Snackbar`.
                 if (javaScriptEnabled) {  // JavaScrip is enabled.
@@ -605,8 +606,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Apply the new cookie status.
                 cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
 
-                // Update the privacy icon.
-                updatePrivacyIcons();
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
 
                 // Display a `Snackbar`.
                 if (firstPartyCookiesEnabled) {  // First-party cookies are enabled.
@@ -654,6 +655,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Apply the new DOM Storage status.
                 mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
 
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
+
                 // Display a `Snackbar`.
                 if (domStorageEnabled) {
                     Snackbar.make(findViewById(R.id.mainWebView), R.string.dom_storage_enabled, Snackbar.LENGTH_SHORT).show();
@@ -682,6 +686,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                     Snackbar.make(findViewById(R.id.mainWebView), R.string.form_data_disabled, Snackbar.LENGTH_SHORT).show();
                 }
 
+                // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+                updatePrivacyIcons(true);
+
                 // Reload the WebView.
                 mainWebView.reload();
                 return true;
@@ -735,11 +742,13 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 mainWebView.getSettings().setTextZoom(200);
                 return true;
 
+            /*
             case R.id.find_on_page:
                 appBar.setCustomView(R.layout.find_on_page_app_bar);
                 toggleJavaScript.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
                 appBar.invalidateOptionsMenu();
                 return true;
+                */
 
             case R.id.share:
                 Intent shareIntent = new Intent();
@@ -751,8 +760,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
 
             case R.id.addToHomescreen:
                 // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `@string/create_shortcut`.
-                DialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut();
-                createHomeScreenShortcutDialogFragment.show(getFragmentManager(), getResources().getString(R.string.create_shortcut));
+                AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut();
+                createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.create_shortcut));
 
                 //Everything else will be handled by `CreateHomeScreenShortcut` and the associated listener below.
                 return true;
@@ -869,12 +878,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                 // Destroy the internal state of the webview.
                 mainWebView.destroy();
 
-                // Close Privacy Browser.  finishAndRemoveTask also removes Privacy Browser from the recent app list.
+                // Close Privacy Browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
                 if (Build.VERSION.SDK_INT >= 21) {
                     finishAndRemoveTask();
                 } else {
                     finish();
                 }
+
+                // Remove the terminated program from RAM.  The status code is `0`.
+                System.exit(0);
                 break;
 
             default:
@@ -905,11 +917,11 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         adView = findViewById(R.id.adView);
 
         // `invalidateOptionsMenu` should recalculate the number of action buttons from the menu to display on the app bar, but it doesn't because of the this bug:  https://code.google.com/p/android/issues/detail?id=20493#c8
-        // invalidateOptionsMenu();
+        // ActivityCompat.invalidateOptionsMenu(this);
     }
 
     @Override
-    public void onCreateHomeScreenShortcut(DialogFragment dialogFragment) {
+    public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) {
         // Get shortcutNameEditText from the alert dialog.
         EditText shortcutNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext);
 
@@ -928,7 +940,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
     }
 
     @Override
-    public void onDownloadFile(DialogFragment dialogFragment, String downloadUrl) {
+    public void onDownloadFile(AppCompatDialogFragment dialogFragment, String downloadUrl) {
         DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
         DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(downloadUrl));
 
@@ -936,9 +948,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         EditText downloadFileNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.download_file_name);
         String fileName = downloadFileNameEditText.getText().toString();
 
-        // Set the download save in the the `DIRECTORY_DOWNLOADS`using `fileName`.
         // Once we have `WRITE_EXTERNAL_STORAGE` permissions we can use `setDestinationInExternalPublicDir`.
-        downloadRequest.setDestinationInExternalFilesDir(this, "/", fileName);
+        if (Build.VERSION.SDK_INT >= 23) { // If API >= 23, set the download save in the the `DIRECTORY_DOWNLOADS` using `fileName`.
+            downloadRequest.setDestinationInExternalFilesDir(this, "/", fileName);
+        } else { // Only set the title using `fileName`.
+            downloadRequest.setTitle(fileName);
+        }
 
         // Allow `MediaScanner` to index the download if it is a media file.
         downloadRequest.allowScanningByMediaScanner();
@@ -1011,8 +1026,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Apply the settings from shared preferences, which might have been changed in `SettingsActivity`.
         applySettings();
 
-        // Update the privacy icons.
-        updatePrivacyIcons();
+        // Update the privacy icon.  `true` runs `invalidateOptionsMenu` as the last step.
+        updatePrivacyIcons(true);
 
     }
 
@@ -1156,7 +1171,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         }
     }
 
-    private void updatePrivacyIcons() {
+    private void updatePrivacyIcons(boolean runInvalidateOptionsMenu) {
         // Get handles for the icons.
         MenuItem privacyIcon = mainMenu.findItem(R.id.toggleJavaScript);
         MenuItem firstPartyCookiesIcon = mainMenu.findItem(R.id.toggleFirstPartyCookies);
@@ -1182,7 +1197,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
         // Update `domStorageIcon`.
         if (javaScriptEnabled && domStorageEnabled) {  // Both JavaScript and DOM storage are enabled.
             domStorageIcon.setIcon(R.drawable.dom_storage_enabled);
-        } else if (javaScriptEnabled){  // JavaScript is enabled but DOM storage is disabled.
+        } else if (javaScriptEnabled) {  // JavaScript is enabled but DOM storage is disabled.
             domStorageIcon.setIcon(R.drawable.dom_storage_disabled);
         } else {  // JavaScript is disabled, so DOM storage is ghosted.
             domStorageIcon.setIcon(R.drawable.dom_storage_ghosted);
@@ -1195,8 +1210,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
             formDataIcon.setIcon(R.drawable.form_data_disabled);
         }
 
-        // `invalidateOptionsMenu` calls `onPrepareOptionsMenu()` and redraws the icons in the `AppBar`.
-        // `this` references the current activity.
-        ActivityCompat.invalidateOptionsMenu(this);
+        // `invalidateOptionsMenu` calls `onPrepareOptionsMenu()` and redraws the icons in the `AppBar`.  `this` references the current activity.
+        if (runInvalidateOptionsMenu) {
+            ActivityCompat.invalidateOptionsMenu(this);
+        }
     }
 }