import android.app.Activity;
import android.app.DialogFragment;
import android.app.DownloadManager;
+import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
+import android.print.PrintDocumentAdapter;
+import android.print.PrintManager;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
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;
SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener {
// `appBar` is public static so it can be accessed from `OrbotProxyHelper`.
- // It is also used in `onCreate()`.
+ // It is also used in `onCreate()` and `onOptionsItemSelected()`.
public static ActionBar appBar;
// `favoriteIcon` is public static so it can be accessed from `CreateHomeScreenShortcut`, `BookmarksActivity`, `CreateBookmark`, `CreateBookmarkFolder`, and `EditBookmark`.
// It is also used in `onCreate()`, `onOptionsItemSelected()`, `onCreateHomeScreenShortcutCreate()`, and `loadUrlFromTextBox()`.
public static String formattedUrlString;
- // `customHeader` is public static so it can be accessed from `BookmarksActivity`. It is also used in `onCreate()`, `onOptionsItemSelected()`, and `loadUrlFromTextBox()`.
- public static Map<String, String> customHeaders = new HashMap<>();
-
// `sslCertificate` is public static so it can be accessed from `ViewSslCertificate`. It is also used in `onCreate()`.
public static SslCertificate sslCertificate;
// `cookieManager` is used in `onCreate()`, `onOptionsItemSelected()`, and `onNavigationItemSelected()`, and `onRestart()`.
private CookieManager cookieManager;
+ // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, and `loadUrlFromTextBox()`.
+ private final Map<String, String> customHeaders = new HashMap<>();
+
// `javaScriptEnabled` is also used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, `loadUrlFromTextBox()`, and `applySettings()`.
// It is `Boolean` instead of `boolean` because `applySettings()` needs to know if it is `null`.
private Boolean javaScriptEnabled;
// `sslErrorHandler` is used in `onCreate()`, `onSslErrorCancel()`, and `onSslErrorProceed`.
private SslErrorHandler sslErrorHandler;
+ private MenuItem toggleJavaScript;
+
@Override
// Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. The whole premise of Privacy Browser is built around an understanding of these dangers.
@SuppressLint("SetJavaScriptEnabled")
// This is needed to get rid of the Android Studio warning that appBar might be null.
assert appBar != null;
- // Add the custom url_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
- appBar.setCustomView(R.layout.url_bar);
+ // Add the custom url_app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+ appBar.setCustomView(R.layout.url_app_bar);
appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// Set the "go" button on the keyboard to load the URL in urlTextBox.
@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));
}
});
updatePrivacyIcons();
// Get handles for the menu items.
+ toggleJavaScript = menu.findItem(R.id.toggleJavaScript);
MenuItem toggleFirstPartyCookies = menu.findItem(R.id.toggleFirstPartyCookies);
MenuItem toggleThirdPartyCookies = menu.findItem(R.id.toggleThirdPartyCookies);
MenuItem toggleDomStorage = menu.findItem(R.id.toggleDomStorage);
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();
shareIntent.setAction(Intent.ACTION_SEND);
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;
+ case R.id.print:
+ // Get a `PrintManager` instance.
+ PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
+
+ // Convert `mainWebView` to `printDocumentAdapter`.
+ PrintDocumentAdapter printDocumentAdapter = mainWebView.createPrintDocumentAdapter();
+
+ // Print the document. The print attributes are `null`.
+ printManager.print(getResources().getString(R.string.privacy_browser_web_page), printDocumentAdapter, null);
+ return true;
+
case R.id.refresh:
mainWebView.reload();
return true;
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();
+ // invalidateOptionsMenu();
}
@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);
}
@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));
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();
cookieManager.setAcceptCookie(firstPartyCookiesEnabled);
mainWebView.getSettings().setDomStorageEnabled(domStorageEnabled);
mainWebView.getSettings().setSaveFormData(saveFormDataEnabled);
+ mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
// Set third-party cookies status if API >= 21.
if (Build.VERSION.SDK_INT >= 21) {
}
}
- // Apply the settings from `sharedPreferences`.
+ // Apply the other settings from `sharedPreferences`.
homepage = homepageString;
- mainWebView.getSettings().setTextZoom(Integer.valueOf(defaultFontSizeString));
swipeRefreshLayout.setEnabled(swipeToRefreshEnabled);
// Set the user agent initial status.