From eec2c8779795b95bbe2a08d1841fe8076cd6694d Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 10 Mar 2022 12:40:32 +0000 Subject: [PATCH] Catch exception in Huawei power management setup. --- .../android/account/HuaweiAppLaunchView.java | 51 +++++++++++++++---- briar-android/src/main/res/values/strings.xml | 1 + 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/account/HuaweiAppLaunchView.java b/briar-android/src/main/java/org/briarproject/briar/android/account/HuaweiAppLaunchView.java index bd5dac903..f193892cd 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/account/HuaweiAppLaunchView.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/account/HuaweiAppLaunchView.java @@ -4,13 +4,14 @@ package org.briarproject.briar.android.account; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.util.AttributeSet; +import android.widget.Toast; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.briar.R; import java.util.List; +import java.util.logging.Logger; import javax.annotation.Nullable; @@ -19,13 +20,25 @@ import androidx.annotation.UiThread; import static android.content.pm.PackageManager.MATCH_DEFAULT_ONLY; import static android.os.Build.VERSION.SDK_INT; +import static android.widget.Toast.LENGTH_LONG; +import static java.util.Arrays.asList; +import static java.util.logging.Level.WARNING; +import static java.util.logging.Logger.getLogger; +import static org.briarproject.bramble.util.LogUtils.logException; @UiThread @NotNullByDefault class HuaweiAppLaunchView extends PowerView { + private static final Logger LOG = + getLogger(HuaweiAppLaunchView.class.getName()); + private final static String PACKAGE_NAME = "com.huawei.systemmanager"; - private final static String CLASS_NAME = + // First try to open StartupNormalAppListActivity + private final static String CLASS_NAME_1 = + PACKAGE_NAME + ".startupmgr.ui.StartupNormalAppListActivity"; + // Fall back to HwPowerManagerActivity + private final static String CLASS_NAME_2 = PACKAGE_NAME + ".power.ui.HwPowerManagerActivity"; public HuaweiAppLaunchView(Context context) { @@ -52,9 +65,12 @@ class HuaweiAppLaunchView extends PowerView { // "App launch" was introduced in EMUI 8 (Android 8.0) if (SDK_INT < 26) return false; PackageManager pm = context.getPackageManager(); - List resolveInfos = pm.queryIntentActivities(getIntent(), - MATCH_DEFAULT_ONLY); - return !resolveInfos.isEmpty(); + for (Intent i : getIntents()) { + if (!pm.queryIntentActivities(i, MATCH_DEFAULT_ONLY).isEmpty()) { + return true; + } + } + return false; } @Override @@ -65,14 +81,27 @@ class HuaweiAppLaunchView extends PowerView { @Override protected void onButtonClick() { - getContext().startActivity(getIntent()); + Context context = getContext(); + for (Intent i : getIntents()) { + try { + context.startActivity(i); + setChecked(true); + return; + } catch (Exception e) { + logException(LOG, WARNING, e); + } + } + Toast.makeText(context, R.string.setup_huawei_app_launch_error_toast, + LENGTH_LONG).show(); + // Let the user continue with setup setChecked(true); } - private static Intent getIntent() { - Intent intent = new Intent(); - intent.setClassName(PACKAGE_NAME, CLASS_NAME); - return intent; + private static List getIntents() { + Intent intent1 = new Intent(); + intent1.setClassName(PACKAGE_NAME, CLASS_NAME_1); + Intent intent2 = new Intent(); + intent2.setClassName(PACKAGE_NAME, CLASS_NAME_2); + return asList(intent1, intent2); } - } diff --git a/briar-android/src/main/res/values/strings.xml b/briar-android/src/main/res/values/strings.xml index 318425d01..f42478056 100644 --- a/briar-android/src/main/res/values/strings.xml +++ b/briar-android/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ Please tap the button below, open the \"App launch\" screen and make sure Briar is set to \"Manage manually\". Open Battery Settings If Briar is not set to \"Manage manually\" in the \"App launch\" screen, it will not be able to run in the background. + Could not open battery settings To run in the background, Briar needs to be locked to the recent apps list. Protect Briar If Briar is not locked to the recent apps list, it will be unable to run in the background.