Protect failed setUidPolicy() and avoid storing invalid UIDs
Add protection for the setUidPolicy() failed case to avoid invalid UIDs to crash the caller process, and avoid storing the invalid UIDs to SharedPreferences if it is failed to setUidPolicy() Bug: 306329984 Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.fuelgauge" Change-Id: I5c14434708b82ba3c238c2338d47085c2ce244e0
This commit is contained in:
@@ -95,6 +95,9 @@ public final class DynamicDenylistManager {
|
|||||||
|
|
||||||
/** Suggest a list of package to set as POLICY_REJECT. */
|
/** Suggest a list of package to set as POLICY_REJECT. */
|
||||||
public void setDenylist(Set<Integer> denylistTargetUids) {
|
public void setDenylist(Set<Integer> denylistTargetUids) {
|
||||||
|
if (denylistTargetUids == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
final Set<Integer> manualDenylistUids = getDenylistAllUids(getManualDenylistPref());
|
final Set<Integer> manualDenylistUids = getDenylistAllUids(getManualDenylistPref());
|
||||||
denylistTargetUids.removeAll(manualDenylistUids);
|
denylistTargetUids.removeAll(manualDenylistUids);
|
||||||
|
|
||||||
@@ -105,27 +108,40 @@ public final class DynamicDenylistManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store target denied uids into DynamicDenylistPref.
|
final ArraySet<Integer> failedUids = new ArraySet<>();
|
||||||
final SharedPreferences.Editor editor = getDynamicDenylistPref().edit();
|
|
||||||
editor.clear();
|
|
||||||
denylistTargetUids.forEach(
|
|
||||||
uid -> editor.putInt(String.valueOf(uid), POLICY_REJECT_METERED_BACKGROUND));
|
|
||||||
editor.apply();
|
|
||||||
|
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
// Set new added UIDs into REJECT policy.
|
// Set new added UIDs into REJECT policy.
|
||||||
for (int uid : denylistTargetUids) {
|
for (int uid : denylistTargetUids) {
|
||||||
if (!lastDynamicDenylistUids.contains(uid)) {
|
if (!lastDynamicDenylistUids.contains(uid)) {
|
||||||
mNetworkPolicyManager.setUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND);
|
try {
|
||||||
|
mNetworkPolicyManager.setUidPolicy(uid, POLICY_REJECT_METERED_BACKGROUND);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "failed to setUidPolicy(REJECT) for " + uid, e);
|
||||||
|
failedUids.add(uid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unset removed UIDs back to NONE policy.
|
// Unset removed UIDs back to NONE policy.
|
||||||
for (int uid : lastDynamicDenylistUids) {
|
for (int uid : lastDynamicDenylistUids) {
|
||||||
if (!denylistTargetUids.contains(uid)) {
|
if (!denylistTargetUids.contains(uid)) {
|
||||||
mNetworkPolicyManager.setUidPolicy(uid, POLICY_NONE);
|
try {
|
||||||
|
mNetworkPolicyManager.setUidPolicy(uid, POLICY_NONE);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "failed to setUidPolicy(NONE) for " + uid, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store target denied uids into DynamicDenylistPref.
|
||||||
|
final SharedPreferences.Editor editor = getDynamicDenylistPref().edit();
|
||||||
|
editor.clear();
|
||||||
|
denylistTargetUids.forEach(uid -> {
|
||||||
|
if (!failedUids.contains(uid)) {
|
||||||
|
editor.putInt(String.valueOf(uid), POLICY_REJECT_METERED_BACKGROUND);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
editor.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Return true if the target uid is in {@link #getManualDenylistPref()}. */
|
/** Return true if the target uid is in {@link #getManualDenylistPref()}. */
|
||||||
|
|||||||
Reference in New Issue
Block a user