+ @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;
+ }
+ }
+