Merge branch '2270-huawei-power-management-crash' into 'master'

Catch exception in Huawei power management setup

Closes #2270

See merge request briar/briar!1602
This commit is contained in:
Torsten Grote
2022-03-29 12:54:16 +00:00
2 changed files with 41 additions and 11 deletions

View File

@@ -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<ResolveInfo> 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<Intent> 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);
}
}

View File

@@ -30,6 +30,7 @@
<string name="setup_huawei_app_launch_text">Please tap the button below, open the \"App launch\" screen and make sure Briar is set to \"Manage manually\".</string>
<string name="setup_huawei_app_launch_button">Open Battery Settings</string>
<string name="setup_huawei_app_launch_help">If Briar is not set to \"Manage manually\" in the \"App launch\" screen, it will not be able to run in the background.</string>
<string name="setup_huawei_app_launch_error_toast">Could not open battery settings</string>
<string name="setup_xiaomi_text">To run in the background, Briar needs to be locked to the recent apps list.</string>
<string name="setup_xiaomi_button">Protect Briar</string>
<string name="setup_xiaomi_help">If Briar is not locked to the recent apps list, it will be unable to run in the background.</string>