From d909a8733fa8ff65a90ed5138b49e214390f8cae Mon Sep 17 00:00:00 2001 From: Luca Stefani Date: Fri, 26 Mar 2021 14:20:33 +0100 Subject: Dialer: Helplines: Use a webview dialog * Instead of relying on one browser we can actually display a webview ourselves * Set various headers to clear cache and to not track * Displays a progressbar and loading text while the page loads Co-authored-by: Michael W Change-Id: Ibad2832c0cb6bcef79b5f348c40ca76c2516b295 --- .../android/dialer/helplines/AndroidManifest.xml | 2 + .../android/dialer/helplines/HelplineActivity.java | 71 ++++++++++++++-------- .../dialer/helplines/res/layout/dialog_webview.xml | 52 ++++++++++++++++ .../dialer/helplines/res/values/strings.xml | 2 - .../dialer/helplines/utils/HelplineUtils.java | 23 ------- 5 files changed, 101 insertions(+), 49 deletions(-) create mode 100644 java/com/android/dialer/helplines/res/layout/dialog_webview.xml diff --git a/java/com/android/dialer/helplines/AndroidManifest.xml b/java/com/android/dialer/helplines/AndroidManifest.xml index 45706cdad..a16c36c0d 100644 --- a/java/com/android/dialer/helplines/AndroidManifest.xml +++ b/java/com/android/dialer/helplines/AndroidManifest.xml @@ -17,6 +17,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.dialer.helplines"> + + { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(content)); - boolean isIncognito = HelplineUtils.makeIncognito(HelplineActivity.this, - intent); - if (isIncognito) { - mDialog.dismiss(); - finish(); // Finish activity to get rid of own traces - startActivity(intent); - } else { - new AlertDialog.Builder(HelplineActivity.this) - .setTitle(R.string.helpline_browser_history_title) - .setMessage(R.string.helpline_browser_history_message) - .setPositiveButton(android.R.string.ok, (dlg, which) -> { - mDialog.dismiss(); - dlg.dismiss(); - finish(); // Finish activity to get rid of own traces - startActivity(intent); - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); + AlertDialog.Builder dialogBuilder = + new AlertDialog.Builder(HelplineActivity.this); + View webviewDlgView = inflater.inflate(R.layout.dialog_webview, null); + dialogBuilder.setView(webviewDlgView); + LinearLayout loadingLayout = webviewDlgView.findViewById(R.id.webview_loading); + + // Disable cookies + CookieManager.getInstance().setAcceptCookie(false); + // Setup WebView + WebView webView = webviewDlgView.findViewById(R.id.webview); + webView.setWebViewClient(new WebViewClient() { + @Override + public boolean shouldOverrideUrlLoading(WebView view, + WebResourceRequest request) { + return false; } - } - ); + + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + loadingLayout.setVisibility(ProgressBar.INVISIBLE); + webView.setVisibility(View.VISIBLE); + } + }); + // Override headers to disable cache and add "Do not track" + Map headers = new HashMap<>(3); + headers.put("Pragma", "no-cache"); + headers.put("Cache-Control", "no-cache"); + headers.put("DNT", "1"); + // Start loading the URL + webView.loadUrl(content, headers); + // Clear any WebView history + dialogBuilder.setPositiveButton(android.R.string.ok, (dlg, which) -> { + webView.clearHistory(); + dlg.dismiss(); + }); + dialogBuilder.setOnDismissListener(dialog -> webView.clearHistory()); + dialogBuilder.show(); + // dismiss the dialog, we show a new one already + mDialog.dismiss(); + }); } } }; diff --git a/java/com/android/dialer/helplines/res/layout/dialog_webview.xml b/java/com/android/dialer/helplines/res/layout/dialog_webview.xml new file mode 100644 index 000000000..b21bff6cb --- /dev/null +++ b/java/com/android/dialer/helplines/res/layout/dialog_webview.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + diff --git a/java/com/android/dialer/helplines/res/values/strings.xml b/java/com/android/dialer/helplines/res/values/strings.xml index 232ac46a8..6e9baa74c 100644 --- a/java/com/android/dialer/helplines/res/values/strings.xml +++ b/java/com/android/dialer/helplines/res/values/strings.xml @@ -33,8 +33,6 @@ Categories Number Website - Attention - Don\'t forget to clear the browser history %1$s (%2$s) diff --git a/java/com/android/dialer/helplines/utils/HelplineUtils.java b/java/com/android/dialer/helplines/utils/HelplineUtils.java index 7303dddca..a31a9ec44 100644 --- a/java/com/android/dialer/helplines/utils/HelplineUtils.java +++ b/java/com/android/dialer/helplines/utils/HelplineUtils.java @@ -15,10 +15,6 @@ */ package com.android.dialer.helplines.utils; -import android.content.Context; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.text.TextUtils; @@ -35,8 +31,6 @@ public class HelplineUtils { private static final String CATEGORY_STR_FORMAT = "helpline_category_%s"; private static final String LANGUAGE_STR_FORMAT = "helpline_language_%s"; - private static final String PKG_NAME_JELLY = "org.lineageos.jelly"; - /* Get the name of the helpline, fall back to the number if not given */ public static String getName(Resources res, Item item, String countryIso) { if (item != null) { @@ -94,21 +88,4 @@ public class HelplineUtils { } return ""; } - - /* Check if the browser is known and make it launch in incognito mode - * Returns true if it succeeded - */ - public static boolean makeIncognito(Context context, Intent intent) { - final PackageManager pm = context.getPackageManager(); - ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); - if (info != null) { - String pkgName = info.activityInfo.applicationInfo.packageName; - if (PKG_NAME_JELLY.equals(pkgName)) { - intent.putExtra("extra_incognito", true); - return true; - } - } - - return false; - } } -- cgit v1.2.3