From 459e44fd15c407f223190ba7cab74f64c7255c9e Mon Sep 17 00:00:00 2001 From: tornaco Date: Sat, 25 Sep 2021 21:34:12 +0800 Subject: [PATCH] Introduce Android X browser --- .../android/thanos/main/NavActivity.java | 9 ++---- .../settings/SettingsDashboardActivity.java | 10 ++----- .../thanos/start/StartRuleActivity.java | 10 ++----- .../project/android/thanox/Dependencies.kt | 1 + .../modules/module_common/build.gradle.kts | 3 ++ .../android/thanos/util/BrowserUtils.java | 28 +++++++++++++++++++ .../module/profile/RuleListActivity.java | 8 ++---- 7 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/BrowserUtils.java diff --git a/android/app/src/main/java/github/tornaco/android/thanos/main/NavActivity.java b/android/app/src/main/java/github/tornaco/android/thanos/main/NavActivity.java index 4a41d1463..91dafe766 100644 --- a/android/app/src/main/java/github/tornaco/android/thanos/main/NavActivity.java +++ b/android/app/src/main/java/github/tornaco/android/thanos/main/NavActivity.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -31,6 +30,7 @@ import github.tornaco.android.thanos.settings.ExportPatchUi; import github.tornaco.android.thanos.settings.PowerSettingsActivity; import github.tornaco.android.thanos.settings.SettingsDashboardActivity; +import github.tornaco.android.thanos.util.BrowserUtils; import github.tornaco.android.thanos.widget.ModernAlertDialog; import github.tornaco.permission.requester.RequiresPermission; import github.tornaco.permission.requester.RuntimePermissions; @@ -81,12 +81,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { SettingsDashboardActivity.start(this); return true; case R.id.guide: - final Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse(BuildProp.THANOX_URL_DOCS_HOME)); - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(Intent.createChooser(intent, "")); - } + BrowserUtils.launch(thisActivity(), BuildProp.THANOX_URL_DOCS_HOME); return true; } return false; diff --git a/android/app/src/main/java/github/tornaco/android/thanos/settings/SettingsDashboardActivity.java b/android/app/src/main/java/github/tornaco/android/thanos/settings/SettingsDashboardActivity.java index ab63f4834..5f5bdd53a 100644 --- a/android/app/src/main/java/github/tornaco/android/thanos/settings/SettingsDashboardActivity.java +++ b/android/app/src/main/java/github/tornaco/android/thanos/settings/SettingsDashboardActivity.java @@ -1,8 +1,6 @@ package github.tornaco.android.thanos.settings; import android.content.Context; -import android.content.Intent; -import android.net.Uri; import android.os.Bundle; import android.view.MenuItem; import android.view.View; @@ -17,6 +15,7 @@ import github.tornaco.android.thanos.R; import github.tornaco.android.thanos.theme.ThemeActivity; import github.tornaco.android.thanos.util.ActivityUtils; +import github.tornaco.android.thanos.util.BrowserUtils; public class SettingsDashboardActivity extends ThemeActivity { @@ -43,12 +42,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { findViewById(R.id.guide).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - final Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse(BuildProp.THANOX_URL_DOCS_HOME)); - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(Intent.createChooser(intent, "")); - } + BrowserUtils.launch(thisActivity(), BuildProp.THANOX_URL_DOCS_HOME); } }); } diff --git a/android/app/src/main/java/github/tornaco/android/thanos/start/StartRuleActivity.java b/android/app/src/main/java/github/tornaco/android/thanos/start/StartRuleActivity.java index 546a79b7e..43d68de9a 100644 --- a/android/app/src/main/java/github/tornaco/android/thanos/start/StartRuleActivity.java +++ b/android/app/src/main/java/github/tornaco/android/thanos/start/StartRuleActivity.java @@ -2,8 +2,6 @@ import android.content.Context; import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; import android.view.LayoutInflater; @@ -29,6 +27,7 @@ import github.tornaco.android.thanos.databinding.ActivityStartRulesBinding; import github.tornaco.android.thanos.theme.ThemeActivity; import github.tornaco.android.thanos.util.ActivityUtils; +import github.tornaco.android.thanos.util.BrowserUtils; import github.tornaco.android.thanos.widget.SwitchBar; public class StartRuleActivity extends ThemeActivity implements StartRuleItemClickListener { @@ -180,12 +179,7 @@ private void showInfoDialog() { new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - final Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse(BuildProp.THANOX_URL_DOCS_START_RULES)); - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(Intent.createChooser(intent, "")); - } + BrowserUtils.launch(thisActivity(), BuildProp.THANOX_URL_DOCS_START_RULES); } }) .setCancelable(false) diff --git a/android/buildSrc/src/main/kotlin/tornaco/project/android/thanox/Dependencies.kt b/android/buildSrc/src/main/kotlin/tornaco/project/android/thanox/Dependencies.kt index d59b46c99..bb2a2500d 100644 --- a/android/buildSrc/src/main/kotlin/tornaco/project/android/thanox/Dependencies.kt +++ b/android/buildSrc/src/main/kotlin/tornaco/project/android/thanox/Dependencies.kt @@ -88,6 +88,7 @@ object Libs { const val lifeCycleCompiler = "androidx.lifecycle:lifecycle-compiler:2.3.1" const val biometric = "androidx.biometric:biometric:1.2.0-alpha03" + const val browser = "androidx.browser:browser:1.3.0" } object Room { diff --git a/android/modules/module_common/build.gradle.kts b/android/modules/module_common/build.gradle.kts index b559b9aad..4c594f360 100644 --- a/android/modules/module_common/build.gradle.kts +++ b/android/modules/module_common/build.gradle.kts @@ -43,6 +43,9 @@ dependencies { implementation(Libs.AndroidX.constraint) implementation(Libs.AndroidX.interpolator) implementation(Libs.AndroidX.swipeRefreshLayout) + implementation(Libs.AndroidX.browser) { + exclude(group = "com.google.guava", module = "listenablefuture") + } implementation(Libs.Others.glide) annotationProcessor(Libs.Others.glideCompiler) diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/BrowserUtils.java b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/BrowserUtils.java new file mode 100644 index 000000000..e2e883abe --- /dev/null +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/BrowserUtils.java @@ -0,0 +1,28 @@ +package github.tornaco.android.thanos.util; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; + +import androidx.browser.customtabs.CustomTabsIntent; + +public class BrowserUtils { + + @SuppressLint("QueryPermissionsNeeded") + public static void launch(Context context, String url) { + try { + CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); + CustomTabsIntent customTabsIntent = builder.build(); + customTabsIntent.launchUrl(context, Uri.parse(url)); + } catch (Throwable e) { + final Intent intent = new Intent(); + intent.setAction(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (intent.resolveActivity(context.getPackageManager()) != null) { + context.startActivity(Intent.createChooser(intent, "")); + } + } + } +} diff --git a/android/modules/module_profile/src/main/java/github/tornaco/thanos/android/module/profile/RuleListActivity.java b/android/modules/module_profile/src/main/java/github/tornaco/thanos/android/module/profile/RuleListActivity.java index 8ccac6877..56121820d 100644 --- a/android/modules/module_profile/src/main/java/github/tornaco/thanos/android/module/profile/RuleListActivity.java +++ b/android/modules/module_profile/src/main/java/github/tornaco/thanos/android/module/profile/RuleListActivity.java @@ -31,6 +31,7 @@ import github.tornaco.android.thanos.core.profile.RuleInfo; import github.tornaco.android.thanos.theme.ThemeActivity; import github.tornaco.android.thanos.util.ActivityUtils; +import github.tornaco.android.thanos.util.BrowserUtils; import github.tornaco.android.thanos.util.IntentUtils; import github.tornaco.android.thanos.widget.SwitchBar; import github.tornaco.permission.requester.RequiresPermission; @@ -135,12 +136,7 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (R.id.action_view_wiki == item.getItemId()) { - final Intent intent = new Intent(); - intent.setAction(Intent.ACTION_VIEW); - intent.setData(Uri.parse(BuildProp.THANOX_URL_DOCS_PROFILE)); - if (intent.resolveActivity(getPackageManager()) != null) { - startActivity(Intent.createChooser(intent, "")); - } + BrowserUtils.launch(thisActivity(), BuildProp.THANOX_URL_DOCS_PROFILE); return true; }