Merge "canScheduleExactAlarms returns true for older apps" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-06-22 03:32:23 +00:00
committed by Android (Google) Code Review
3 changed files with 18 additions and 9 deletions

View File

@@ -1285,14 +1285,20 @@ public class AlarmManager {
} }
/** /**
* Called to check if the caller has the permission * Called to check if the caller can schedule exact alarms.
* {@link Manifest.permission#SCHEDULE_EXACT_ALARM}. * <p>
* * Apps targeting {@link Build.VERSION_CODES#S} or higher can schedule exact alarms if they
* Apps can start {@link android.provider.Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM} to * have the {@link Manifest.permission#SCHEDULE_EXACT_ALARM} permission. These apps can also
* start {@link android.provider.Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM} to
* request this from the user. * request this from the user.
* <p>
* Apps targeting lower sdk versions, can always schedule exact alarms.
* *
* @return {@code true} if the caller has the permission, {@code false} otherwise. * @return {@code true} if the caller can schedule exact alarms.
* @see android.provider.Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM * @see android.provider.Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM
* @see #setExact(int, long, PendingIntent)
* @see #setExactAndAllowWhileIdle(int, long, PendingIntent)
* @see #setAlarmClock(AlarmClockInfo, PendingIntent)
*/ */
public boolean canScheduleExactAlarms() { public boolean canScheduleExactAlarms() {
return hasScheduleExactAlarm(mContext.getOpPackageName(), mContext.getUserId()); return hasScheduleExactAlarm(mContext.getOpPackageName(), mContext.getUserId());

View File

@@ -2572,6 +2572,9 @@ public class AlarmManagerService extends SystemService {
throw new SecurityException("Uid " + callingUid throw new SecurityException("Uid " + callingUid
+ " cannot query hasScheduleExactAlarm for uid " + uid); + " cannot query hasScheduleExactAlarm for uid " + uid);
} }
if (!isExactAlarmChangeEnabled(packageName, userId)) {
return true;
}
return (uid > 0) ? hasScheduleExactAlarmInternal(packageName, uid) : false; return (uid > 0) ? hasScheduleExactAlarmInternal(packageName, uid) : false;
} }

View File

@@ -1914,11 +1914,11 @@ public class AlarmManagerServiceTest {
public void hasScheduleExactAlarmBinderCallChangeDisabled() throws RemoteException { public void hasScheduleExactAlarmBinderCallChangeDisabled() throws RemoteException {
mockChangeEnabled(AlarmManager.REQUIRE_EXACT_ALARM_PERMISSION, false); mockChangeEnabled(AlarmManager.REQUIRE_EXACT_ALARM_PERMISSION, false);
mockExactAlarmPermissionGrant(true, false, MODE_DEFAULT); mockExactAlarmPermissionGrant(false, true, MODE_DEFAULT);
assertFalse(mBinder.hasScheduleExactAlarm(TEST_CALLING_PACKAGE, TEST_CALLING_USER)); assertTrue(mBinder.hasScheduleExactAlarm(TEST_CALLING_PACKAGE, TEST_CALLING_USER));
mockExactAlarmPermissionGrant(true, true, MODE_ALLOWED); mockExactAlarmPermissionGrant(true, false, MODE_ERRORED);
assertFalse(mBinder.hasScheduleExactAlarm(TEST_CALLING_PACKAGE, TEST_CALLING_USER)); assertTrue(mBinder.hasScheduleExactAlarm(TEST_CALLING_PACKAGE, TEST_CALLING_USER));
} }
private void mockChangeEnabled(long changeId, boolean enabled) { private void mockChangeEnabled(long changeId, boolean enabled) {