import android.support.v7.app.ActionBar;
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;
import android.view.Menu;
import java.net.URL;
import java.net.URLEncoder;
+// 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 MainWebView extends AppCompatActivity implements CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener {
// favoriteIcon is public static so it can be accessed from CreateHomeScreenShortcut.
public static Bitmap favoriteIcon;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
+ Toolbar toolbar = (Toolbar) findViewById(R.id.appBar);
+ setSupportActionBar(toolbar);
final FrameLayout fullScreenVideoFrameLayout = (FrameLayout) findViewById(R.id.fullScreenVideoFrameLayout);
final Activity mainWebViewActivity = this;
- final ActionBar actionBar = getSupportActionBar();
+ // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
+ final ActionBar appBar = getSupportActionBar();
mainWebView = (WebView) findViewById(R.id.mainWebView);
- if (actionBar != null) {
- // Remove the title from the action bar.
- actionBar.setDisplayShowTitleEnabled(false);
+ if (appBar != null) {
+ // Remove the title from the app bar.
+ appBar.setDisplayShowTitleEnabled(false);
- // Add the custom app_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
- actionBar.setCustomView(R.layout.app_bar);
- actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
+ // Add the custom url_bar layout, which shows the favoriteIcon, urlTextBar, and progressBar.
+ appBar.setCustomView(R.layout.url_bar);
+ appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
// Set the "go" button on the keyboard to load the URL in urlTextBox.
- urlTextBox = (EditText) actionBar.getCustomView().findViewById(R.id.urlTextBox);
+ urlTextBox = (EditText) appBar.getCustomView().findViewById(R.id.urlTextBox);
urlTextBox.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button, load the URL.
// Update the progress bar when a page is loading.
@Override
public void onProgressChanged(WebView view, int progress) {
- // Make sure that actionBar is not null.
- if (actionBar != null) {
- ProgressBar progressBar = (ProgressBar) actionBar.getCustomView().findViewById(R.id.progressBar);
+ // Make sure that appBar is not null.
+ if (appBar != null) {
+ ProgressBar progressBar = (ProgressBar) appBar.getCustomView().findViewById(R.id.progressBar);
progressBar.setProgress(progress);
if (progress < 100) {
progressBar.setVisibility(View.VISIBLE);
// Save a copy of the favorite icon for use if a shortcut is added to the home screen.
favoriteIcon = icon;
- // Place the favorite icon in the actionBar if it is not null.
- if (actionBar != null) {
- ImageView imageViewFavoriteIcon = (ImageView) actionBar.getCustomView().findViewById(R.id.favoriteIcon);
+ // Place the favorite icon in the appBar if it is not null.
+ if (appBar != null) {
+ ImageView imageViewFavoriteIcon = (ImageView) appBar.getCustomView().findViewById(R.id.favoriteIcon);
imageViewFavoriteIcon.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true));
}
}
// Enter full screen video
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
- if (actionBar != null) {
- actionBar.hide();
+ if (appBar != null) {
+ appBar.hide();
}
fullScreenVideoFrameLayout.addView(view);
// Exit full screen video
public void onHideCustomView() {
- if (actionBar != null) {
- actionBar.show();
+ if (appBar != null) {
+ appBar.show();
}
mainWebView.setVisibility(View.VISIBLE);
public boolean onOptionsItemSelected(MenuItem menuItem) {
int menuItemId = menuItem.getItemId();
- // Some options need to access the clipboard.
- ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
-
// Some options need to update the drawable for toggleJavaScript.
MenuItem toggleJavaScript = mainMenu.findItem(R.id.toggleJavaScript);
- // Sets the commands that relate to the menu entries.
+ // Set the commands that relate to the menu entries.
switch (menuItemId) {
case R.id.toggleJavaScript:
if (javaScriptEnabled) {
Toast.makeText(getApplicationContext(), "Cookies deleted", Toast.LENGTH_SHORT).show();
return true;
+ case R.id.addToHomescreen:
+ // Show the CreateHomeScreenShortcut AlertDialog and name this instance createShortcut.
+ AppCompatDialogFragment shortcutDialog = new CreateHomeScreenShortcut();
+ shortcutDialog.show(getSupportFragmentManager(), "createShortcut");
+
+ //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
+ return true;
+
+ case R.id.downloads:
+ // Launch the system Download Manager.
+ Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
+
+ // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
+ downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+ startActivity(downloadManangerIntent);
+ return true;
+
case R.id.home:
mainWebView.loadUrl(homepage);
return true;
mainWebView.goForward();
return true;
- case R.id.copyURL:
- clipboard.setPrimaryClip(ClipData.newPlainText("URL", urlTextBox.getText()));
- return true;
-
- case R.id.pasteURL:
- ClipData.Item clipboardData = clipboard.getPrimaryClip().getItemAt(0);
- urlTextBox.setText(clipboardData.coerceToText(this));
- try {
- loadUrlFromTextBox();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return true;
-
- case R.id.shareURL:
+ case R.id.share:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, urlTextBox.getText().toString());
startActivity(Intent.createChooser(shareIntent, "Share URL"));
return true;
- case R.id.addToHomescreen:
- // Show the CreateHomeScreenShortcut AlertDialog and name this instance createShortcut.
- AppCompatDialogFragment shortcutDialog = new CreateHomeScreenShortcut();
- shortcutDialog.show(getSupportFragmentManager(), "createShortcut");
-
- //Everything else will be handled by CreateHomeScreenShortcut and the associated listeners below.
- return true;
-
- case R.id.downloads:
- // Launch the system Download Manager.
- Intent downloadManangerIntent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
-
- // Launch as a new task so that Download Manager and Privacy Browser show as separate windows in the recent tasks list.
- downloadManangerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-
- startActivity(downloadManangerIntent);
- return true;
-
case R.id.settings:
// Start the Settings activity.
Intent intent = new Intent(this, Settings.class);