Changes to "Alarms and Reminders" permission
Calling AlarmManager to know the state of the permission, which is now the source of truth, as the logic deciding the state of the permission may change. Also killing the app when the permission changes to denied, this avoids the app maintaining stale state about this permission. Test: make -j RunSettingsRoboTests atest SettingsUnitTests:AppStateAlarmsAndRemindersBridgeTest Manually: Toggle the state of an app to "Not allowed" and check the logs for activity manager kill messages. Bug: 181152252 Bug: 183136253 Change-Id: I438782eaef4caae50fd76685c675a3e45ee28d9f
This commit is contained in:
@@ -18,6 +18,7 @@ package com.android.settings.applications.appinfo;
|
||||
import static android.app.Activity.RESULT_CANCELED;
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.AppOpsManager;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
@@ -31,7 +32,6 @@ import com.android.settings.R;
|
||||
import com.android.settings.Settings;
|
||||
import com.android.settings.applications.AppInfoWithHeader;
|
||||
import com.android.settings.applications.AppStateAlarmsAndRemindersBridge;
|
||||
import com.android.settings.applications.AppStateAppOpsBridge;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settingslib.applications.ApplicationsState.AppEntry;
|
||||
|
||||
@@ -47,23 +47,24 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
|
||||
private AppStateAlarmsAndRemindersBridge mAppBridge;
|
||||
private AppOpsManager mAppOpsManager;
|
||||
private RestrictedSwitchPreference mSwitchPref;
|
||||
private AppStateAppOpsBridge.PermissionState mPermissionState;
|
||||
private AppStateAlarmsAndRemindersBridge.AlarmsAndRemindersState mPermissionState;
|
||||
private ActivityManager mActivityManager;
|
||||
|
||||
/**
|
||||
* Returns the string that states whether the app has access to
|
||||
* {@link android.Manifest.permission#SCHEDULE_EXACT_ALARM}.
|
||||
*/
|
||||
public static int getSummary(Context context, AppEntry entry) {
|
||||
final AppStateAppOpsBridge.PermissionState state;
|
||||
if (entry.extraInfo instanceof AppStateAppOpsBridge.PermissionState) {
|
||||
state = (AppStateAppOpsBridge.PermissionState) entry.extraInfo;
|
||||
final AppStateAlarmsAndRemindersBridge.AlarmsAndRemindersState state;
|
||||
if (entry.extraInfo instanceof AppStateAlarmsAndRemindersBridge.AlarmsAndRemindersState) {
|
||||
state = (AppStateAlarmsAndRemindersBridge.AlarmsAndRemindersState) entry.extraInfo;
|
||||
} else {
|
||||
state = new AppStateAlarmsAndRemindersBridge(context, /*appState=*/null,
|
||||
/*callback=*/null).createPermissionState(entry.info.packageName,
|
||||
entry.info.uid);
|
||||
}
|
||||
|
||||
return state.isPermissible() ? R.string.app_permission_summary_allowed
|
||||
return state.isAllowed() ? R.string.app_permission_summary_allowed
|
||||
: R.string.app_permission_summary_not_allowed;
|
||||
}
|
||||
|
||||
@@ -74,6 +75,7 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
|
||||
final Context context = getActivity();
|
||||
mAppBridge = new AppStateAlarmsAndRemindersBridge(context, mState, /*callback=*/null);
|
||||
mAppOpsManager = context.getSystemService(AppOpsManager.class);
|
||||
mActivityManager = context.getSystemService(ActivityManager.class);
|
||||
|
||||
addPreferencesFromResource(R.xml.alarms_and_reminders);
|
||||
mSwitchPref = findPreference(KEY_SWITCH);
|
||||
@@ -84,7 +86,7 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
final boolean checked = (Boolean) newValue;
|
||||
if (preference == mSwitchPref) {
|
||||
if (mPermissionState != null && checked != mPermissionState.isPermissible()) {
|
||||
if (mPermissionState != null && checked != mPermissionState.isAllowed()) {
|
||||
if (Settings.AlarmsAndRemindersAppActivity.class.getName().equals(
|
||||
getIntent().getComponent().getClassName())) {
|
||||
setResult(checked ? RESULT_OK : RESULT_CANCELED);
|
||||
@@ -99,9 +101,13 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
|
||||
}
|
||||
|
||||
private void setCanScheduleAlarms(boolean newState) {
|
||||
mAppOpsManager.setUidMode(AppOpsManager.OPSTR_SCHEDULE_EXACT_ALARM,
|
||||
mPackageInfo.applicationInfo.uid,
|
||||
final int uid = mPackageInfo.applicationInfo.uid;
|
||||
mAppOpsManager.setUidMode(AppOpsManager.OPSTR_SCHEDULE_EXACT_ALARM, uid,
|
||||
newState ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED);
|
||||
if (!newState) {
|
||||
mActivityManager.killUid(uid,
|
||||
AppOpsManager.OPSTR_SCHEDULE_EXACT_ALARM + " no longer allowed.");
|
||||
}
|
||||
}
|
||||
|
||||
private void logPermissionChange(boolean newState, String packageName) {
|
||||
@@ -120,8 +126,8 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
|
||||
}
|
||||
mPermissionState = mAppBridge.createPermissionState(mPackageName,
|
||||
mPackageInfo.applicationInfo.uid);
|
||||
mSwitchPref.setEnabled(mPermissionState.permissionDeclared);
|
||||
mSwitchPref.setChecked(mPermissionState.isPermissible());
|
||||
mSwitchPref.setEnabled(mPermissionState.shouldBeVisible());
|
||||
mSwitchPref.setChecked(mPermissionState.isAllowed());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user