import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
+// `ShortcutInfoCompat`, `ShortcutManagerCompat`, and `IconCompat` can be switched to the non-compat version once API >= 26.
+import android.support.v4.content.pm.ShortcutInfoCompat;
+import android.support.v4.content.pm.ShortcutManagerCompat;
+import android.support.v4.graphics.drawable.IconCompat;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import com.stoutner.privacybrowser.BannerAd;
import com.stoutner.privacybrowser.BuildConfig;
import com.stoutner.privacybrowser.R;
+import com.stoutner.privacybrowser.dialogs.AddDomainDialog;
import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
import com.stoutner.privacybrowser.dialogs.CreateHomeScreenShortcutDialog;
import java.util.Set;
// We need to use AppCompatActivity from android.support.v7.app.AppCompatActivity to have access to the SupportActionBar until the minimum API is >= 21.
-public class MainWebViewActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, CreateHomeScreenShortcutDialog.CreateHomeScreenSchortcutListener,
+public class MainWebViewActivity extends AppCompatActivity implements AddDomainDialog.AddDomainListener, CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, CreateHomeScreenShortcutDialog.CreateHomeScreenSchortcutListener,
DownloadFileDialog.DownloadFileListener, DownloadImageDialog.DownloadImageListener, EditBookmarkDialog.EditBookmarkListener, EditBookmarkFolderDialog.EditBookmarkFolderListener, HttpAuthenticationDialog.HttpAuthenticationListener,
NavigationView.OnNavigationItemSelectedListener, PinnedSslCertificateMismatchDialog.PinnedSslCertificateMismatchListener, SslCertificateErrorDialog.SslCertificateErrorListener, UrlHistoryDialog.UrlHistoryListener {
// `loadBookmarksFolder()`.
public static String currentBookmarksFolder;
- // The pinned domain SSL Certificate variables are public static so they can be accessed from `PinnedSslCertificateMismatchDialog`. They are also used in `onCreate()` and `applyDomainSettings()`.
+ // `domainSettingsDatabaseId` is public static so it can be accessed from `PinnedSslCertificateMismatchDialog`. It is also used in `onCreate()`, `onOptionsItemSelected()`, and `applyDomainSettings()`.
public static int domainSettingsDatabaseId;
+
+ // The pinned domain SSL Certificate variables are public static so they can be accessed from `PinnedSslCertificateMismatchDialog`. They are also used in `onCreate()` and `applyDomainSettings()`.
public static String pinnedDomainSslIssuedToCNameString;
public static String pinnedDomainSslIssuedToONameString;
public static String pinnedDomainSslIssuedToUNameString;
// `translucentNavigationBarOnFullscreen` is used in `onCreate()` and `applyAppSettings()`.
private boolean translucentNavigationBarOnFullscreen;
- // `currentDomainName` is used in `onCreate()`, `onNavigationItemSelected()`, `onSslMismatchProceed()`, and `applyDomainSettings()`.
+ // `reapplyDomainSettingsOnRestart` is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, and `onAddDomain()`, .
+ private boolean reapplyDomainSettingsOnRestart;
+
+ // `currentDomainName` is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onAddDomain()`, and `applyDomainSettings()`.
private String currentDomainName;
// `ignorePinnedSslCertificateForDomain` is used in `onCreate()`, `onSslMismatchProceed()`, and `applyDomainSettings()`.
// `waitingForOrbot` is used in `onCreate()` and `applyAppSettings()`.
private boolean waitingForOrbot;
- // `domainSettingsApplied` is used in `applyDomainSettings()` and `setDisplayWebpageImages()`.
+ // `domainSettingsApplied` is used in `prepareOptionsMenu()`, `applyDomainSettings()`, and `setDisplayWebpageImages()`.
private boolean domainSettingsApplied;
// `displayWebpageImagesInt` is used in `applyDomainSettings()` and `setDisplayWebpageImages()`.
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
- if (url.startsWith("mailto:")) { // Load the URL in an external email program because it begins with `mailto:`.
- // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
+ if (url.startsWith("mailto:")) { // Load the email address in an external email program.
+ // Use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
// Parse the url and set it as the data for the `Intent`.
// Make it so.
startActivity(emailIntent);
+ // Returning `true` indicates the application is handling the URL.
+ return true;
+ } else if (url.startsWith("tel:")) { // Load the phone number in the dialer.
+ // `ACTION_DIAL` open the dialer and loads the phone number, but waits for the user to place the call.
+ Intent dialIntent = new Intent(Intent.ACTION_DIAL);
+
+ // Add the phone number to the intent.
+ dialIntent.setData(Uri.parse(url));
+
+ // `FLAG_ACTIVITY_NEW_TASK` opens the dialer in a new task instead as part of Privacy Browser.
+ dialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ // Make it so.
+ startActivity(dialIntent);
+
// Returning `true` indicates the application is handling the URL.
return true;
} else { // Load the URL in Privacy Browser.
@Override
public void onRestart() {
+ // Run the default commands.
super.onRestart();
// Apply the app settings, which may have been changed in `SettingsActivity`.
applyAppSettings();
+ // Apply the domain settings if returning from the Domains Activity.
+ if (reapplyDomainSettingsOnRestart) {
+ // Reset `reapplyDomainSettingsOnRestart`.
+ reapplyDomainSettingsOnRestart = false;
+
+ // Reapply the domain settings.
+ applyDomainSettings(formattedUrlString);
+ }
+
// Update the privacy icon. `true` runs `invalidateOptionsMenu` as the last step.
updatePrivacyIcons(true);
// `onResume()` runs after `onStart()`, which runs after `onCreate()` and `onRestart()`.
@Override
public void onResume() {
+ // Run the default commands.
super.onResume();
// Resume JavaScript (if enabled).
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Get handles for the menu items.
+ MenuItem addOrEditDomain = menu.findItem(R.id.add_or_edit_domain);
MenuItem toggleFirstPartyCookiesMenuItem = menu.findItem(R.id.toggle_first_party_cookies);
MenuItem toggleThirdPartyCookiesMenuItem = menu.findItem(R.id.toggle_third_party_cookies);
MenuItem toggleDomStorageMenuItem = menu.findItem(R.id.toggle_dom_storage);
MenuItem toggleSaveFormDataMenuItem = menu.findItem(R.id.toggle_save_form_data);
+ MenuItem clearDataMenuItem = menu.findItem(R.id.clear_data);
MenuItem clearCookiesMenuItem = menu.findItem(R.id.clear_cookies);
MenuItem clearDOMStorageMenuItem = menu.findItem(R.id.clear_dom_storage);
MenuItem clearFormDataMenuItem = menu.findItem(R.id.clear_form_data);
MenuItem displayImagesMenuItem = menu.findItem(R.id.display_images);
MenuItem refreshMenuItem = menu.findItem(R.id.refresh);
+ // Set the text for the domain menu item.
+ if (domainSettingsApplied) {
+ addOrEditDomain.setTitle(R.string.edit_domain_settings);
+ } else {
+ addOrEditDomain.setTitle(R.string.add_domain_settings);
+ }
+
// Set the status of the menu item checkboxes.
toggleFirstPartyCookiesMenuItem.setChecked(firstPartyCookiesEnabled);
toggleThirdPartyCookiesMenuItem.setChecked(thirdPartyCookiesEnabled);
WebViewDatabase mainWebViewDatabase = WebViewDatabase.getInstance(this);
clearFormDataMenuItem.setEnabled(mainWebViewDatabase.hasFormData());
+ // Enable `Clear Data` if any of the submenu items are enabled.
+ clearDataMenuItem.setEnabled(clearCookiesMenuItem.isEnabled() || clearDOMStorageMenuItem.isEnabled() || clearFormDataMenuItem.isEnabled());
+
// Initialize font size variables.
int fontSize = mainWebView.getSettings().getTextZoom();
String fontSizeTitle;
// Set the commands that relate to the menu entries.
switch (menuItemId) {
+ case R.id.add_or_edit_domain:
+ if (domainSettingsApplied) { // Edit the current domain settings.
+ // Reapply the domain settings on returning to `MainWebViewActivity`.
+ reapplyDomainSettingsOnRestart = true;
+ currentDomainName = "";
+
+ // Create an intent to launch the domains activity.
+ Intent domainsIntent = new Intent(this, DomainsActivity.class);
+
+ // Put extra information instructing the domains activity to directly load the current domain.
+ domainsIntent.putExtra("LoadDomain", domainSettingsDatabaseId);
+
+ // Make it so.
+ startActivity(domainsIntent);
+ } else { // Add a new domain.
+ // Show the add domain `AlertDialog`.
+ AppCompatDialogFragment addDomainDialog = new AddDomainDialog();
+ addDomainDialog.show(getSupportFragmentManager(), getResources().getString(R.string.add_domain));
+ }
+ return true;
+
case R.id.toggle_javascript:
// Switch the status of javaScriptEnabled.
javaScriptEnabled = !javaScriptEnabled;
WebStorage webStorage = WebStorage.getInstance();
webStorage.deleteAllData();
- // Manually remove `IndexedDB` if it exists.
+ // Manually delete the DOM storage files and directories.
try {
+ // A `String[]` must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
+ privacyBrowserRuntime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
+
+ // Multiple commands must be used because `Runtime.exec()` does not like `*`.
privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
+ privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
+ privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
+ privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/databases");
} catch (IOException e) {
// Do nothing if an error is thrown.
}
break;
case R.id.domains:
- // Reset `currentDomainName` so that domain settings are reapplied after returning to `MainWebViewActivity`.
+ // Reapply the domain settings on returning to `MainWebViewActivity`.
+ reapplyDomainSettingsOnRestart = true;
currentDomainName = "";
// Launch `DomainsActivity`.
break;
case R.id.settings:
- // Reset `currentDomainName` so that domain settings are reapplied after returning to `MainWebViewActivity`.
+ // Reapply the domain settings on returning to `MainWebViewActivity`.
+ reapplyDomainSettingsOnRestart = true;
currentDomainName = "";
// Launch `SettingsActivity`.
// Manually delete the DOM storage files and directories, as `WebStorage` sometimes will not flush its changes to disk before `System.exit(0)` is run.
try {
- // We have to use a `String[]` because the directory contains a space and `Runtime.exec` will not escape the string correctly otherwise.
+ // A `String[]` must be used because the directory contains a space and `Runtime.exec` will otherwise not escape the string correctly.
privacyBrowserRuntime.exec(new String[] {"rm", "-rf", privateDataDirectoryString + "/app_webview/Local Storage/"});
- // We have to use multiple commands because `Runtime.exec()` does not like `*`.
+ // Multiple commands must be used because `Runtime.exec()` does not like `*`.
privacyBrowserRuntime.exec("rm -rf " + privateDataDirectoryString + "/app_webview/IndexedDB");
privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager");
privacyBrowserRuntime.exec("rm -f " + privateDataDirectoryString + "/app_webview/QuotaManager-journal");
}
}
+ @Override
+ public void onAddDomain(AppCompatDialogFragment dialogFragment) {
+ // Reapply the domain settings on returning to `MainWebViewActivity`.
+ reapplyDomainSettingsOnRestart = true;
+ currentDomainName = "";
+
+ // Get the new domain name `String` from `dialogFragment`.
+ EditText domainNameEditText = dialogFragment.getDialog().findViewById(R.id.domain_name_edittext);
+ String domainNameString = domainNameEditText.getText().toString();
+
+ // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
+ // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
+ DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(this, null, null, 0);
+
+ // Create the domain and store the database ID in `currentDomainDatabaseId`.
+ int newDomainDatabaseId = domainsDatabaseHelper.addDomain(domainNameString);
+
+ // Create an intent to launch the domains activity.
+ Intent domainsIntent = new Intent(this, DomainsActivity.class);
+
+ // Put extra information instructing the domains activity to directly load the current domain.
+ domainsIntent.putExtra("LoadDomain", newDomainDatabaseId);
+
+ // Make it so.
+ startActivity(domainsIntent);
+ }
+
@Override
public void onCreateBookmark(AppCompatDialogFragment dialogFragment) {
// Get the `EditTexts` from the `dialogFragment`.
@Override
public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) {
- // Get shortcutNameEditText from the alert dialog.
+ // Get the shortcut name.
EditText shortcutNameEditText = dialogFragment.getDialog().findViewById(R.id.shortcut_name_edittext);
+ String shortcutNameString = shortcutNameEditText.getText().toString();
+
+ // Convert the favorite icon bitmap to an `Icon`. `IconCompat` is required until API >= 26.
+ IconCompat favoriteIcon = IconCompat.createWithBitmap(favoriteIconBitmap);
+
+ // Setup the shortcut intent.
+ Intent shortcutIntent = new Intent();
+ shortcutIntent.setAction(Intent.ACTION_VIEW);
+ shortcutIntent.setData(Uri.parse(formattedUrlString));
+
+ // Create a shortcut info builder. The shortcut name becomes the shortcut ID.
+ ShortcutInfoCompat.Builder shortcutInfoBuilder = new ShortcutInfoCompat.Builder(this, shortcutNameString);
+
+ // Add the required fields to the shortcut info builder.
+ shortcutInfoBuilder.setIcon(favoriteIcon);
+ shortcutInfoBuilder.setIntent(shortcutIntent);
+ shortcutInfoBuilder.setShortLabel(shortcutNameString);
- // Create the bookmark shortcut based on formattedUrlString.
- Intent bookmarkShortcut = new Intent();
- bookmarkShortcut.setAction(Intent.ACTION_VIEW);
- bookmarkShortcut.setData(Uri.parse(formattedUrlString));
-
- // Place the bookmark shortcut on the home screen.
- Intent placeBookmarkShortcut = new Intent();
- placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.INTENT", bookmarkShortcut);
- placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.NAME", shortcutNameEditText.getText().toString());
- placeBookmarkShortcut.putExtra("android.intent.extra.shortcut.ICON", favoriteIconBitmap);
- placeBookmarkShortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
- sendBroadcast(placeBookmarkShortcut);
+ // Request the pin. `ShortcutManagerCompat` can be switched to `ShortcutManager` once API >= 26.
+ ShortcutManagerCompat.requestPinShortcut(this, shortcutInfoBuilder.build(), null);
}
@Override