Refactor power menu bug reporting flow to work for multiple admins

Existing implementation of power menu bugreporting flow works only
for the primary user. In HSUM(Headless system user mode) there is
going to be more than one admin users who are allowed to trigger bug
report from the power menu, below refactorings has been done do handle
this case.

Configurations for custom_bugreport_handler_app and custom_bugreport_handler_user are moved from Global to
Secure settings so that each user can save them.

The current context user will be the fallback default user in the
absence of a correctly configured handlerApp or handlerUser.

Retrieval of the current bug report handler apps are refactored to add
shell as user's default preferences for any user(current user).

Test: manually tested the bugreport is working for multiple admin with
different combination of handlerApp and handlerUser

Bug: 261184202
Change-Id: I3110c1f9619ede2edeb744daf37521318f0ff6e7
This commit is contained in:
Nikhil Kumar
2022-12-08 18:39:46 +00:00
parent 642a0a74a4
commit 109084137a
3 changed files with 25 additions and 23 deletions

View File

@@ -78,7 +78,7 @@ public class BugReportHandlerUtil {
boolean needToResetOutdatedSettings = false; boolean needToResetOutdatedSettings = false;
if (!isBugreportAllowlistedApp(handlerApp)) { if (!isBugreportAllowlistedApp(handlerApp)) {
handlerApp = getDefaultBugReportHandlerApp(context); handlerApp = getDefaultBugReportHandlerApp(context);
handlerUser = UserHandle.USER_SYSTEM; handlerUser = context.getUserId();
} else if (getBugReportHandlerAppReceivers(context, handlerApp, handlerUser).isEmpty()) { } else if (getBugReportHandlerAppReceivers(context, handlerApp, handlerUser).isEmpty()) {
// It looks like the settings are outdated, need to reset outdated settings. // It looks like the settings are outdated, need to reset outdated settings.
// //
@@ -89,7 +89,7 @@ public class BugReportHandlerUtil {
// The chosen bugreport handler app is outdated because the profile is removed, // The chosen bugreport handler app is outdated because the profile is removed,
// so need to reset outdated settings // so need to reset outdated settings
handlerApp = getDefaultBugReportHandlerApp(context); handlerApp = getDefaultBugReportHandlerApp(context);
handlerUser = UserHandle.USER_SYSTEM; handlerUser = context.getUserId();
needToResetOutdatedSettings = true; needToResetOutdatedSettings = true;
} }
@@ -99,7 +99,7 @@ public class BugReportHandlerUtil {
// bugreport, so change to let shell to handle a bugreport and need to reset // bugreport, so change to let shell to handle a bugreport and need to reset
// settings. // settings.
handlerApp = SHELL_APP_PACKAGE; handlerApp = SHELL_APP_PACKAGE;
handlerUser = UserHandle.USER_SYSTEM; handlerUser = context.getUserId();
needToResetOutdatedSettings = true; needToResetOutdatedSettings = true;
} }
@@ -112,13 +112,13 @@ public class BugReportHandlerUtil {
private String getCustomBugReportHandlerApp(Context context) { private String getCustomBugReportHandlerApp(Context context) {
// Get the package of custom bugreport handler app // Get the package of custom bugreport handler app
return Settings.Global.getString(context.getContentResolver(), return Settings.Secure.getString(context.getContentResolver(),
Settings.Global.CUSTOM_BUGREPORT_HANDLER_APP); Settings.Secure.CUSTOM_BUGREPORT_HANDLER_APP);
} }
private int getCustomBugReportHandlerUser(Context context) { private int getCustomBugReportHandlerUser(Context context) {
return Settings.Global.getInt(context.getContentResolver(), return Settings.Secure.getInt(context.getContentResolver(),
Settings.Global.CUSTOM_BUGREPORT_HANDLER_USER, UserHandle.USER_NULL); Settings.Secure.CUSTOM_BUGREPORT_HANDLER_USER, UserHandle.USER_NULL);
} }
private String getDefaultBugReportHandlerApp(Context context) { private String getDefaultBugReportHandlerApp(Context context) {
@@ -164,22 +164,24 @@ public class BugReportHandlerUtil {
return validBugReportHandlerApplicationInfos; return validBugReportHandlerApplicationInfos;
} }
// Add "Shell with system user" as System default preference on top of screen int currentUser = UserHandle.getCallingUserId();
// Add "Shell" as user's default preference on top of screen
if (bugreportAllowlistedPackages.contains(SHELL_APP_PACKAGE) if (bugreportAllowlistedPackages.contains(SHELL_APP_PACKAGE)
&& !getBugReportHandlerAppReceivers(context, SHELL_APP_PACKAGE, && !getBugReportHandlerAppReceivers(context, SHELL_APP_PACKAGE,
UserHandle.USER_SYSTEM).isEmpty()) { currentUser).isEmpty()) {
try { try {
validBugReportHandlerApplicationInfos.add( validBugReportHandlerApplicationInfos.add(
Pair.create( Pair.create(
context.getPackageManager().getApplicationInfo(SHELL_APP_PACKAGE, context.getPackageManager().getApplicationInfo(SHELL_APP_PACKAGE,
PackageManager.MATCH_ANY_USER), UserHandle.USER_SYSTEM) PackageManager.MATCH_ANY_USER), currentUser)
); );
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
} }
} }
final UserManager userManager = context.getSystemService(UserManager.class); final UserManager userManager = context.getSystemService(UserManager.class);
final List<UserInfo> profileList = userManager.getProfiles(UserHandle.getCallingUserId()); final List<UserInfo> profileList = userManager.getProfiles(currentUser);
// Only add non-Shell app as normal preference // Only add non-Shell app as normal preference
final List<String> nonShellPackageList = bugreportAllowlistedPackages.stream() final List<String> nonShellPackageList = bugreportAllowlistedPackages.stream()
.filter(pkg -> !SHELL_APP_PACKAGE.equals(pkg)).collect(Collectors.toList()); .filter(pkg -> !SHELL_APP_PACKAGE.equals(pkg)).collect(Collectors.toList());
@@ -228,11 +230,11 @@ public class BugReportHandlerUtil {
private void setBugreportHandlerAppAndUser(Context context, String handlerApp, private void setBugreportHandlerAppAndUser(Context context, String handlerApp,
int handlerUser) { int handlerUser) {
Settings.Global.putString(context.getContentResolver(), Settings.Secure.putString(context.getContentResolver(),
Settings.Global.CUSTOM_BUGREPORT_HANDLER_APP, Settings.Secure.CUSTOM_BUGREPORT_HANDLER_APP,
handlerApp); handlerApp);
Settings.Global.putInt(context.getContentResolver(), Settings.Secure.putInt(context.getContentResolver(),
Settings.Global.CUSTOM_BUGREPORT_HANDLER_USER, handlerUser); Settings.Secure.CUSTOM_BUGREPORT_HANDLER_USER, handlerUser);
} }
/** /**

View File

@@ -59,7 +59,7 @@ public class BugReportInPowerPreferenceController extends
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean isEnabled = (Boolean) newValue; final boolean isEnabled = (Boolean) newValue;
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, Settings.Secure.BUGREPORT_IN_POWER_MENU,
isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF);
return true; return true;
} }
@@ -67,7 +67,7 @@ public class BugReportInPowerPreferenceController extends
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
final int mode = Settings.Secure.getInt(mContext.getContentResolver(), final int mode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF); Settings.Secure.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF);
((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF); ((SwitchPreference) mPreference).setChecked(mode != SETTING_VALUE_OFF);
} }
@@ -75,7 +75,7 @@ public class BugReportInPowerPreferenceController extends
protected void onDeveloperOptionsSwitchDisabled() { protected void onDeveloperOptionsSwitchDisabled() {
super.onDeveloperOptionsSwitchDisabled(); super.onDeveloperOptionsSwitchDisabled();
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF); Settings.Secure.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF);
((SwitchPreference) mPreference).setChecked(false); ((SwitchPreference) mPreference).setChecked(false);
} }
} }

View File

@@ -99,7 +99,7 @@ public class BugReportInPowerPreferenceControllerTest {
mController.onPreferenceChange(mPreference, false /* new value */); mController.onPreferenceChange(mPreference, false /* new value */);
int mode = Settings.Secure.getInt(mContext.getContentResolver(), int mode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, -1 /* default */); Settings.Secure.BUGREPORT_IN_POWER_MENU, -1 /* default */);
assertThat(mode).isEqualTo(SETTING_VALUE_OFF); assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
} }
@@ -111,7 +111,7 @@ public class BugReportInPowerPreferenceControllerTest {
mController.onPreferenceChange(mPreference, true /* new value */); mController.onPreferenceChange(mPreference, true /* new value */);
int mode = Settings.Secure.getInt(mContext.getContentResolver(), int mode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, -1 /* default */); Settings.Secure.BUGREPORT_IN_POWER_MENU, -1 /* default */);
assertThat(mode).isEqualTo(SETTING_VALUE_ON); assertThat(mode).isEqualTo(SETTING_VALUE_ON);
} }
@@ -121,7 +121,7 @@ public class BugReportInPowerPreferenceControllerTest {
public void updateState_settingsOn_preferenceShouldBeChecked() { public void updateState_settingsOn_preferenceShouldBeChecked() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_ON); Settings.Secure.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_ON);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateState(mPreference); mController.updateState(mPreference);
@@ -133,7 +133,7 @@ public class BugReportInPowerPreferenceControllerTest {
public void updateState_settingsOff_preferenceShouldNotBeChecked() { public void updateState_settingsOff_preferenceShouldNotBeChecked() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF); Settings.Secure.BUGREPORT_IN_POWER_MENU, SETTING_VALUE_OFF);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.updateState(mPreference); mController.updateState(mPreference);
@@ -148,7 +148,7 @@ public class BugReportInPowerPreferenceControllerTest {
mController.onDeveloperOptionsSwitchDisabled(); mController.onDeveloperOptionsSwitchDisabled();
int mode = Settings.Secure.getInt(mContext.getContentResolver(), int mode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, -1 /* default */); Settings.Secure.BUGREPORT_IN_POWER_MENU, -1 /* default */);
assertThat(mode).isEqualTo(SETTING_VALUE_OFF); assertThat(mode).isEqualTo(SETTING_VALUE_OFF);
verify(mPreference).setChecked(false); verify(mPreference).setChecked(false);