Files
app_Settings/src/com/android/settings/applications/AppStateAlarmsAndRemindersBridge.java
Suprabh Shukla 202bd8581a 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
2021-04-07 16:06:09 -07:00

131 lines
4.6 KiB
Java

/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.applications;
import android.Manifest;
import android.app.AlarmManager;
import android.app.AppGlobals;
import android.content.Context;
import android.content.pm.IPackageManager;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.ArrayUtils;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import com.android.settingslib.applications.ApplicationsState.AppFilter;
import libcore.util.EmptyArray;
import java.util.List;
/**
* Connects app op info to the ApplicationsState. Extends {@link AppStateAppOpsBridge} to tailor
* to the semantics of {@link Manifest.permission#SCHEDULE_EXACT_ALARM}.
* Also provides app filters that can use the info.
*/
public class AppStateAlarmsAndRemindersBridge extends AppStateBaseBridge {
private static final String PERMISSION = Manifest.permission.SCHEDULE_EXACT_ALARM;
private static final String TAG = "AlarmsAndRemindersBridge";
@VisibleForTesting
AlarmManager mAlarmManager;
@VisibleForTesting
String[] mRequesterPackages;
public AppStateAlarmsAndRemindersBridge(Context context, ApplicationsState appState,
Callback callback) {
super(appState, callback);
mAlarmManager = context.getSystemService(AlarmManager.class);
final IPackageManager iPm = AppGlobals.getPackageManager();
try {
mRequesterPackages = iPm.getAppOpPermissionPackages(PERMISSION);
} catch (RemoteException re) {
Log.e(TAG, "Cannot reach package manager", re);
mRequesterPackages = EmptyArray.STRING;
}
}
/**
* Returns information regarding {@link Manifest.permission#SCHEDULE_EXACT_ALARM} for the given
* package and uid.
*/
public AlarmsAndRemindersState createPermissionState(String packageName, int uid) {
final boolean permissionRequested = ArrayUtils.contains(mRequesterPackages, packageName);
final boolean permissionGranted = mAlarmManager.hasScheduleExactAlarm(packageName,
UserHandle.getUserId(uid));
return new AlarmsAndRemindersState(permissionRequested, permissionGranted);
}
@Override
protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
app.extraInfo = createPermissionState(pkg, uid);
}
@Override
protected void loadAllExtraInfo() {
final List<AppEntry> allApps = mAppSession.getAllApps();
for (int i = 0; i < allApps.size(); i++) {
final AppEntry currentEntry = allApps.get(i);
updateExtraInfo(currentEntry, currentEntry.info.packageName, currentEntry.info.uid);
}
}
public static final AppFilter FILTER_CLOCK_APPS = new AppFilter() {
@Override
public void init() {
}
@Override
public boolean filterApp(AppEntry info) {
if (info.extraInfo instanceof AlarmsAndRemindersState) {
final AlarmsAndRemindersState state = (AlarmsAndRemindersState) info.extraInfo;
return state.shouldBeVisible();
}
return false;
}
};
/**
* Class to denote the state of an app regarding
* {@link Manifest.permission#SCHEDULE_EXACT_ALARM}.
*/
public static class AlarmsAndRemindersState {
private boolean mPermissionRequested;
private boolean mPermissionGranted;
AlarmsAndRemindersState(boolean permissionRequested, boolean permissionGranted) {
mPermissionRequested = permissionRequested;
mPermissionGranted = permissionGranted;
}
/** Should the app associated with this state appear on the Settings screen */
public boolean shouldBeVisible() {
return mPermissionRequested;
}
/** Is the permission granted to the app associated with this state */
public boolean isAllowed() {
return mPermissionGranted;
}
}
}