X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2FMainWebViewActivity.java;h=779d2d69b91a0e1c6d299c4dab2315439a063a75;hb=eb1e349d876e09e2b82e4eb9d6dc199147e1cde5;hp=7928c944a52df874a6aa95f9aad48b39e687b41d;hpb=16f63187a36d1e8f63fdfdf27884bbed16a56473;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index 7928c944..779d2d69 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -53,6 +53,7 @@ import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; import android.util.Patterns; +import android.view.ContextMenu; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -61,6 +62,7 @@ import android.view.inputmethod.InputMethodManager; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.webkit.SslErrorHandler; +import android.webkit.WebBackForwardList; import android.webkit.WebChromeClient; import android.webkit.WebStorage; import android.webkit.WebView; @@ -83,7 +85,7 @@ import java.util.Map; // 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 NavigationView.OnNavigationItemSelectedListener, CreateHomeScreenShortcut.CreateHomeScreenSchortcutListener, - SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener { + SslCertificateError.SslCertificateErrorListener, DownloadFile.DownloadFileListener, DownloadImage.DownloadImageListener, UrlHistory.UrlHistoryListener { // `appBar` is public static so it can be accessed from `OrbotProxyHelper`. // It is also used in `onCreate()`, `onOptionsItemSelected()`, and `closeFindOnPage()`. @@ -101,7 +103,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation public static SslCertificate sslCertificate; - // 'mainWebView' is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `findPreviousOnPage()`, `findNextOnPage()`, `closeFindOnPage`, and `loadUrlFromTextBox()`. + // 'mainWebView' is used in `onCreate()`, `onOptionsItemSelected()`, `onNavigationItemSelected()`, `onRestart()`, `onCreateContextMenu()`, `findPreviousOnPage()`, `findNextOnPage()`, `closeFindOnPage()`, and `loadUrlFromTextBox()`. private WebView mainWebView; // `swipeRefreshLayout` is used in `onCreate()`, `onPrepareOptionsMenu`, and `onRestart()`. @@ -110,7 +112,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // `cookieManager` is used in `onCreate()`, `onOptionsItemSelected()`, and `onNavigationItemSelected()`, and `onRestart()`. private CookieManager cookieManager; - // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, and `loadUrlFromTextBox()`. + // `customHeader` is used in `onCreate()`, `onOptionsItemSelected()`, `onCreateContextMenu()`, and `loadUrlFromTextBox()`. private final Map customHeaders = new HashMap<>(); // `javaScriptEnabled` is also used in `onCreate()`, `onCreateOptionsMenu()`, `onOptionsItemSelected()`, `loadUrlFromTextBox()`, and `applySettings()`. @@ -282,13 +284,45 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Create the navigation drawer. drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); - // The DrawerTitle identifies the drawer in accessibility mode. + // `DrawerTitle` identifies the drawer in accessibility mode. drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer)); // Listen for touches on the navigation menu. final NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView); navigationView.setNavigationItemSelectedListener(this); + // Get handles for `navigationMenu` and the back and forward menu items. The menu is zero-based, so item 1 and 2 and the second and third items in the menu. + final Menu navigationMenu = navigationView.getMenu(); + final MenuItem navigationBackMenuItem = navigationMenu.getItem(1); + final MenuItem navigationForwardMenuItem = navigationMenu.getItem(2); + final MenuItem navigationHistoryMenuItem = navigationMenu.getItem(3); + + // The `DrawerListener` allows us to update the Navigation Menu. + drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() { + @Override + public void onDrawerSlide(View drawerView, float slideOffset) { + } + + @Override + public void onDrawerOpened(View drawerView) { + } + + @Override + public void onDrawerClosed(View drawerView) { + } + + @Override + public void onDrawerStateChanged(int newState) { + // Update the `Back`, `Forward`, and `History` menu items every time the drawer opens. + navigationBackMenuItem.setEnabled(mainWebView.canGoBack()); + navigationForwardMenuItem.setEnabled(mainWebView.canGoForward()); + navigationHistoryMenuItem.setEnabled((mainWebView.canGoBack() || mainWebView.canGoForward())); + + // Hide the keyboard so we can see the navigation menu. `0` indicates no additional flags. + inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); + } + }); + // drawerToggle creates the hamburger icon at the start of the AppBar. drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, supportAppBar, R.string.open_navigation, R.string.close_navigation); @@ -419,6 +453,9 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation } }); + // Register `mainWebView` for a context menu. This is used to see link targets and download images. + registerForContextMenu(mainWebView); + // Allow the downloading of files. mainWebView.setDownloadListener(new DownloadListener() { @Override @@ -844,7 +881,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation return true; case R.id.addToHomescreen: - // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `@string/create_shortcut`. + // Show the `CreateHomeScreenShortcut` `AlertDialog` and name this instance `R.string.create_shortcut`. AppCompatDialogFragment createHomeScreenShortcutDialogFragment = new CreateHomeScreenShortcut(); createHomeScreenShortcutDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.create_shortcut)); @@ -895,6 +932,15 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation } break; + case R.id.history: + // Gte the `WebBackForwardList`. + WebBackForwardList webBackForwardList = mainWebView.copyBackForwardList(); + + // Show the `UrlHistory` `AlertDialog` and name this instance `R.string.history`. `this` is the `Context`. + AppCompatDialogFragment urlHistoryDialogFragment = UrlHistory.loadBackForwardList(this, webBackForwardList); + urlHistoryDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.history)); + break; + case R.id.bookmarks: // Launch BookmarksActivity. Intent bookmarksIntent = new Intent(this, BookmarksActivity.class); @@ -1009,6 +1055,134 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // ActivityCompat.invalidateOptionsMenu(this); } + @Override + public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { + // Store the `HitTestResult`. + final WebView.HitTestResult hitTestResult = mainWebView.getHitTestResult(); + + // Create strings. + final String imageUrl; + final String linkUrl; + + switch (hitTestResult.getType()) { + // `SRC_ANCHOR_TYPE` is a link. + case WebView.HitTestResult.SRC_ANCHOR_TYPE: + // Get the target URL. + linkUrl = hitTestResult.getExtra(); + + // Set the target URL as the title of the `ContextMenu`. + menu.setHeaderTitle(linkUrl); + + // Add a `Load URL` button. + menu.add(R.string.load_url).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + mainWebView.loadUrl(linkUrl, customHeaders); + return false; + } + }); + + // Add a `Cancel` button, which by default closes the `ContextMenu`. + menu.add(R.string.cancel); + break; + + case WebView.HitTestResult.EMAIL_TYPE: + // Get the target URL. + linkUrl = hitTestResult.getExtra(); + + // Set the target URL as the title of the `ContextMenu`. + menu.setHeaderTitle(linkUrl); + + // Add a `Write Email` button. + menu.add(R.string.write_email).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + // We 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`. + emailIntent.setData(Uri.parse("mailto:" + linkUrl)); + + // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of Privacy Browser. + emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + // Make it so. + startActivity(emailIntent); + return false; + } + }); + + // Add a `Cancel` button, which by default closes the `ContextMenu`. + menu.add(R.string.cancel); + break; + + // `IMAGE_TYPE` is an image. + case WebView.HitTestResult.IMAGE_TYPE: + // Get the image URL. + imageUrl = hitTestResult.getExtra(); + + // Set the image URL as the title of the `ContextMenu`. + menu.setHeaderTitle(imageUrl); + + // Add a `View Image` button. + menu.add(R.string.view_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + mainWebView.loadUrl(imageUrl, customHeaders); + return false; + } + }); + + // Add a `Download Image` button. + menu.add(R.string.download_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + // Show the `DownloadImage` `AlertDialog` and name this instance `@string/download`. + AppCompatDialogFragment downloadImageDialogFragment = DownloadImage.imageUrl(imageUrl); + downloadImageDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.download)); + return false; + } + }); + + // Add a `Cancel` button, which by default closes the `ContextMenu`. + menu.add(R.string.cancel); + break; + + + // `SRC_IMAGE_ANCHOR_TYPE` is an image that is also a link. + case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE: + // Get the image URL. + imageUrl = hitTestResult.getExtra(); + + // Set the image URL as the title of the `ContextMenu`. + menu.setHeaderTitle(imageUrl); + + // Add a `View Image` button. + menu.add(R.string.view_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + mainWebView.loadUrl(imageUrl, customHeaders); + return false; + } + }); + + // Add a `Download Image` button. + menu.add(R.string.download_image).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + // Show the `DownloadImage` `AlertDialog` and name this instance `@string/download`. + AppCompatDialogFragment downloadImageDialogFragment = DownloadImage.imageUrl(imageUrl); + downloadImageDialogFragment.show(getSupportFragmentManager(), getResources().getString(R.string.download)); + return false; + } + }); + + // Add a `Cancel` button, which by default closes the `ContextMenu`. + menu.add(R.string.cancel); + break; + } + } + @Override public void onCreateHomeScreenShortcut(AppCompatDialogFragment dialogFragment) { // Get shortcutNameEditText from the alert dialog. @@ -1028,9 +1202,44 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation sendBroadcast(placeBookmarkShortcut); } + @Override + public void onDownloadImage(AppCompatDialogFragment dialogFragment, String imageUrl) { + // Get a handle for the system `DOWNLOAD_SERVICE`. + DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); + + // Parse `imageUrl`. + DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(imageUrl)); + + // Get the file name from `dialogFragment`. + EditText downloadImageNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.download_image_name); + String imageName = downloadImageNameEditText.getText().toString(); + + // Once we have `WRITE_EXTERNAL_STORAGE` permissions we can use `setDestinationInExternalPublicDir`. + if (Build.VERSION.SDK_INT >= 23) { // If API >= 23, set the download save in the the `DIRECTORY_DOWNLOADS` using `imageName`. + downloadRequest.setDestinationInExternalFilesDir(this, "/", imageName); + } else { // Only set the title using `imageName`. + downloadRequest.setTitle(imageName); + } + + // Allow `MediaScanner` to index the download if it is a media file. + downloadRequest.allowScanningByMediaScanner(); + + // Add the URL as the description for the download. + downloadRequest.setDescription(imageUrl); + + // Show the download notification after the download is completed. + downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); + + // Initiate the download. + downloadManager.enqueue(downloadRequest); + } + @Override public void onDownloadFile(AppCompatDialogFragment dialogFragment, String downloadUrl) { + // Get a handle for the system `DOWNLOAD_SERVICE`. DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); + + // Parse `downloadUrl`. DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(downloadUrl)); // Get the file name from `dialogFragment`. @@ -1053,7 +1262,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Show the download notification after the download is completed. downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - // Initiate the download and display a Snackbar. + // Initiate the download. downloadManager.enqueue(downloadRequest); } @@ -1073,6 +1282,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation sslErrorHandler.proceed(); } + @Override + public void onUrlHistoryEntrySelected(int moveBackOrForwardSteps) { + // Load the history entry. + mainWebView.goBackOrForward(moveBackOrForwardSteps); + } + // Override onBackPressed to handle the navigation drawer and mainWebView. @Override public void onBackPressed() { @@ -1164,7 +1379,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation mainWebView.loadUrl(formattedUrlString, customHeaders); - // Hides the keyboard so we can see the webpage. `0` indicates no additional flags. + // Hide the keyboard so we can see the webpage. `0` indicates no additional flags. inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); } @@ -1193,7 +1408,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation Toolbar appBarToolbar = (Toolbar) findViewById(R.id.appBar); appBarToolbar.setVisibility(View.VISIBLE); - // Hides the keyboard so we can see the webpage. `0` indicates no additional flags. + // Hide the keyboard so we can see the webpage. `0` indicates no additional flags. inputMethodManager.hideSoftInputFromWindow(mainWebView.getWindowToken(), 0); }