Catch exception when stopping OTP session twice

An uncaught IllegalArgumentException thrown in
ActivityManager.removeOnUidImportanceListener would result in a one-time
permission session for a background group sometimes not getting cleaned
up properly, leading to background permissions not geting revoked.

Test: run cts -m CtsPermissionTestCases -t
android.permission.cts.RevokeSelfPermissionTest
Bug: 225386829

Change-Id: Iad7f181866b407d556672457ade1f3312d10a95d
This commit is contained in:
Thomas Vannet
2022-03-21 07:57:30 -07:00
parent 017ebacdaf
commit fb818c519d

View File

@@ -317,9 +317,21 @@ public class OneTimePermissionUserManager {
synchronized (mInnerLock) {
mIsFinished = true;
cancelAlarmLocked();
mActivityManager.removeOnUidImportanceListener(mStartTimerListener);
mActivityManager.removeOnUidImportanceListener(mSessionKillableListener);
mActivityManager.removeOnUidImportanceListener(mGoneListener);
try {
mActivityManager.removeOnUidImportanceListener(mStartTimerListener);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "Could not remove start timer listener", e);
}
try {
mActivityManager.removeOnUidImportanceListener(mSessionKillableListener);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "Could not remove session killable listener", e);
}
try {
mActivityManager.removeOnUidImportanceListener(mGoneListener);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "Could not remove gone listener", e);
}
}
}