Merge changes I99720a37,I99a8f255 into qt-qpr1-dev

* changes:
  Force update uid state when pending uid state is applied
  Notify all packages is uid-mode is changed
This commit is contained in:
TreeHugger Robot
2020-02-06 19:30:31 +00:00
committed by Android (Google) Code Review

View File

@@ -31,10 +31,13 @@ import static android.app.AppOpsManager.UID_STATE_FOREGROUND_SERVICE_LOCATION;
import static android.app.AppOpsManager.UID_STATE_MAX_LAST_NON_RESTRICTED; import static android.app.AppOpsManager.UID_STATE_MAX_LAST_NON_RESTRICTED;
import static android.app.AppOpsManager.UID_STATE_PERSISTENT; import static android.app.AppOpsManager.UID_STATE_PERSISTENT;
import static android.app.AppOpsManager.UID_STATE_TOP; import static android.app.AppOpsManager.UID_STATE_TOP;
import static android.app.AppOpsManager.WATCH_FOREGROUND_CHANGES;
import static android.app.AppOpsManager.modeToName; import static android.app.AppOpsManager.modeToName;
import static android.app.AppOpsManager.opToName; import static android.app.AppOpsManager.opToName;
import static android.app.AppOpsManager.resolveFirstUnrestrictedUidState; import static android.app.AppOpsManager.resolveFirstUnrestrictedUidState;
import static java.lang.Long.max;
import android.Manifest; import android.Manifest;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
@@ -933,6 +936,19 @@ public class AppOpsService extends IAppOpsService.Stub {
} }
} }
/**
* Update the pending state for the uid
*
* @param currentTime The current elapsed real time
* @param uid The uid that has a pending state
*/
private void updatePendingState(long currentTime, int uid) {
synchronized (this) {
mLastRealtime = max(currentTime, mLastRealtime);
updatePendingStateIfNeededLocked(mUidStates.get(uid));
}
}
public void updateUidProcState(int uid, int procState) { public void updateUidProcState(int uid, int procState) {
synchronized (this) { synchronized (this) {
final UidState uidState = getUidStateLocked(uid, true); final UidState uidState = getUidStateLocked(uid, true);
@@ -958,7 +974,12 @@ public class AppOpsService extends IAppOpsService.Stub {
} else { } else {
settleTime = mConstants.BG_STATE_SETTLE_TIME; settleTime = mConstants.BG_STATE_SETTLE_TIME;
} }
uidState.pendingStateCommitTime = SystemClock.elapsedRealtime() + settleTime; final long commitTime = SystemClock.elapsedRealtime() + settleTime;
uidState.pendingStateCommitTime = commitTime;
mHandler.sendMessageDelayed(
PooledLambda.obtainMessage(AppOpsService::updatePendingState, this,
commitTime + 1, uid), settleTime + 1);
} }
if (uidState.startNesting != 0) { if (uidState.startNesting != 0) {
// There is some actively running operation... need to find it // There is some actively running operation... need to find it
@@ -1288,6 +1309,18 @@ public class AppOpsService extends IAppOpsService.Stub {
uidState.evalForegroundOps(mOpModeWatchers); uidState.evalForegroundOps(mOpModeWatchers);
} }
notifyOpChangedForAllPkgsInUid(code, uid, false);
notifyOpChangedSync(code, uid, null, mode);
}
/**
* Notify that an op changed for all packages in an uid.
*
* @param code The op that changed
* @param uid The uid the op was changed for
* @param onlyForeground Only notify watchers that watch for foreground changes
*/
private void notifyOpChangedForAllPkgsInUid(int code, int uid, boolean onlyForeground) {
String[] uidPackageNames = getPackagesForUid(uid); String[] uidPackageNames = getPackagesForUid(uid);
ArrayMap<ModeCallback, ArraySet<String>> callbackSpecs = null; ArrayMap<ModeCallback, ArraySet<String>> callbackSpecs = null;
@@ -1297,6 +1330,10 @@ public class AppOpsService extends IAppOpsService.Stub {
final int callbackCount = callbacks.size(); final int callbackCount = callbacks.size();
for (int i = 0; i < callbackCount; i++) { for (int i = 0; i < callbackCount; i++) {
ModeCallback callback = callbacks.valueAt(i); ModeCallback callback = callbacks.valueAt(i);
if (onlyForeground && (callback.mFlags & WATCH_FOREGROUND_CHANGES) == 0) {
continue;
}
ArraySet<String> changedPackages = new ArraySet<>(); ArraySet<String> changedPackages = new ArraySet<>();
Collections.addAll(changedPackages, uidPackageNames); Collections.addAll(changedPackages, uidPackageNames);
if (callbackSpecs == null) { if (callbackSpecs == null) {
@@ -1315,6 +1352,10 @@ public class AppOpsService extends IAppOpsService.Stub {
final int callbackCount = callbacks.size(); final int callbackCount = callbacks.size();
for (int i = 0; i < callbackCount; i++) { for (int i = 0; i < callbackCount; i++) {
ModeCallback callback = callbacks.valueAt(i); ModeCallback callback = callbacks.valueAt(i);
if (onlyForeground && (callback.mFlags & WATCH_FOREGROUND_CHANGES) == 0) {
continue;
}
ArraySet<String> changedPackages = callbackSpecs.get(callback); ArraySet<String> changedPackages = callbackSpecs.get(callback);
if (changedPackages == null) { if (changedPackages == null) {
changedPackages = new ArraySet<>(); changedPackages = new ArraySet<>();
@@ -1327,7 +1368,6 @@ public class AppOpsService extends IAppOpsService.Stub {
} }
if (callbackSpecs == null) { if (callbackSpecs == null) {
notifyOpChangedSync(code, uid, null, mode);
return; return;
} }
@@ -1349,8 +1389,6 @@ public class AppOpsService extends IAppOpsService.Stub {
} }
} }
} }
notifyOpChangedSync(code, uid, null, mode);
} }
private void notifyOpChangedSync(int code, int uid, @NonNull String packageName, int mode) { private void notifyOpChangedSync(int code, int uid, @NonNull String packageName, int mode) {
@@ -2467,6 +2505,18 @@ public class AppOpsService extends IAppOpsService.Stub {
uidState = new UidState(uid); uidState = new UidState(uid);
mUidStates.put(uid, uidState); mUidStates.put(uid, uidState);
} else { } else {
updatePendingStateIfNeededLocked(uidState);
}
return uidState;
}
/**
* Check if the pending state should be updated and do so if needed
*
* @param uidState The uidState that might have a pending state
*/
private void updatePendingStateIfNeededLocked(@NonNull UidState uidState) {
if (uidState != null) {
if (uidState.pendingStateCommitTime != 0) { if (uidState.pendingStateCommitTime != 0) {
if (uidState.pendingStateCommitTime < mLastRealtime) { if (uidState.pendingStateCommitTime < mLastRealtime) {
commitUidPendingStateLocked(uidState); commitUidPendingStateLocked(uidState);
@@ -2478,7 +2528,6 @@ public class AppOpsService extends IAppOpsService.Stub {
} }
} }
} }
return uidState;
} }
private void commitUidPendingStateLocked(UidState uidState) { private void commitUidPendingStateLocked(UidState uidState) {
@@ -2496,24 +2545,28 @@ public class AppOpsService extends IAppOpsService.Stub {
if (resolvedLastFg == resolvedNowFg) { if (resolvedLastFg == resolvedNowFg) {
continue; continue;
} }
final ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code);
if (callbacks != null) { if (uidState.opModes != null
for (int cbi = callbacks.size() - 1; cbi >= 0; cbi--) { && uidState.opModes.indexOfKey(code) >= 0
final ModeCallback callback = callbacks.valueAt(cbi); && uidState.opModes.get(code) == AppOpsManager.MODE_FOREGROUND) {
if ((callback.mFlags & AppOpsManager.WATCH_FOREGROUND_CHANGES) == 0 mHandler.sendMessage(PooledLambda.obtainMessage(
|| !callback.isWatchingUid(uidState.uid)) { AppOpsService::notifyOpChangedForAllPkgsInUid,
continue; this, code, uidState.uid, true));
} } else {
boolean doAllPackages = uidState.opModes != null final ArraySet<ModeCallback> callbacks = mOpModeWatchers.get(code);
&& uidState.opModes.indexOfKey(code) >= 0 if (callbacks != null) {
&& uidState.opModes.get(code) == AppOpsManager.MODE_FOREGROUND; for (int cbi = callbacks.size() - 1; cbi >= 0; cbi--) {
if (uidState.pkgOps != null) { final ModeCallback callback = callbacks.valueAt(cbi);
if ((callback.mFlags & AppOpsManager.WATCH_FOREGROUND_CHANGES) == 0
|| !callback.isWatchingUid(uidState.uid)) {
continue;
}
for (int pkgi = uidState.pkgOps.size() - 1; pkgi >= 0; pkgi--) { for (int pkgi = uidState.pkgOps.size() - 1; pkgi >= 0; pkgi--) {
final Op op = uidState.pkgOps.valueAt(pkgi).get(code); final Op op = uidState.pkgOps.valueAt(pkgi).get(code);
if (op == null) { if (op == null) {
continue; continue;
} }
if (doAllPackages || op.mode == AppOpsManager.MODE_FOREGROUND) { if (op.mode == AppOpsManager.MODE_FOREGROUND) {
mHandler.sendMessage(PooledLambda.obtainMessage( mHandler.sendMessage(PooledLambda.obtainMessage(
AppOpsService::notifyOpChanged, AppOpsService::notifyOpChanged,
this, callback, code, uidState.uid, this, callback, code, uidState.uid,