Merge "Propagate the temp allowlist reason to NPMS." into sc-dev

This commit is contained in:
Sudheer Shanka
2021-06-14 17:50:08 +00:00
committed by Android (Google) Code Review
4 changed files with 44 additions and 21 deletions

View File

@@ -1536,13 +1536,14 @@ public class DeviceIdleController extends SystemService
@VisibleForTesting @VisibleForTesting
static final int MSG_REPORT_STATIONARY_STATUS = 7; static final int MSG_REPORT_STATIONARY_STATUS = 7;
private static final int MSG_FINISH_IDLE_OP = 8; private static final int MSG_FINISH_IDLE_OP = 8;
private static final int MSG_REPORT_TEMP_APP_WHITELIST_CHANGED_TO_NPMS = 9;
private static final int MSG_SEND_CONSTRAINT_MONITORING = 10; private static final int MSG_SEND_CONSTRAINT_MONITORING = 10;
@VisibleForTesting @VisibleForTesting
static final int MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR = 11; static final int MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR = 11;
@VisibleForTesting @VisibleForTesting
static final int MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR = 12; static final int MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR = 12;
private static final int MSG_REPORT_TEMP_APP_WHITELIST_CHANGED = 13; private static final int MSG_REPORT_TEMP_APP_WHITELIST_CHANGED = 13;
private static final int MSG_REPORT_TEMP_APP_WHITELIST_ADDED_TO_NPMS = 14;
private static final int MSG_REPORT_TEMP_APP_WHITELIST_REMOVED_TO_NPMS = 15;
final class MyHandler extends Handler { final class MyHandler extends Handler {
MyHandler(Looper looper) { MyHandler(Looper looper) {
@@ -1659,10 +1660,17 @@ public class DeviceIdleController extends SystemService
} }
} }
} break; } break;
case MSG_REPORT_TEMP_APP_WHITELIST_CHANGED_TO_NPMS: { case MSG_REPORT_TEMP_APP_WHITELIST_ADDED_TO_NPMS: {
final int appId = msg.arg1; final int appId = msg.arg1;
final boolean added = (msg.arg2 == 1); final int reasonCode = msg.arg2;
mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, added); final String reason = (String) msg.obj;
mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, true,
reasonCode, reason);
} break;
case MSG_REPORT_TEMP_APP_WHITELIST_REMOVED_TO_NPMS: {
final int appId = msg.arg1;
mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, false,
REASON_UNKNOWN, /* reason= */ null);
} break; } break;
case MSG_SEND_CONSTRAINT_MONITORING: { case MSG_SEND_CONSTRAINT_MONITORING: {
final IDeviceIdleConstraint constraint = (IDeviceIdleConstraint) msg.obj; final IDeviceIdleConstraint constraint = (IDeviceIdleConstraint) msg.obj;
@@ -2811,14 +2819,15 @@ public class DeviceIdleController extends SystemService
// NPMS needs to update its state synchronously in certain situations so we // NPMS needs to update its state synchronously in certain situations so we
// can't have it use the TempAllowlistChangeListener path right now. // can't have it use the TempAllowlistChangeListener path right now.
// TODO: see if there's a way to simplify/consolidate // TODO: see if there's a way to simplify/consolidate
mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_CHANGED_TO_NPMS, appId, 1) mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_ADDED_TO_NPMS, appId,
.sendToTarget(); reasonCode, reason).sendToTarget();
} }
reportTempWhitelistChangedLocked(uid, true); reportTempWhitelistChangedLocked(uid, true);
} }
} }
if (informWhitelistChanged) { if (informWhitelistChanged) {
mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, true); mNetworkPolicyManagerInternal.onTempPowerSaveWhitelistChange(appId, true,
reasonCode, reason);
} }
} }
@@ -2890,8 +2899,8 @@ public class DeviceIdleController extends SystemService
final int appId = UserHandle.getAppId(uid); final int appId = UserHandle.getAppId(uid);
updateTempWhitelistAppIdsLocked(uid, false, 0, 0, REASON_UNKNOWN, updateTempWhitelistAppIdsLocked(uid, false, 0, 0, REASON_UNKNOWN,
reason, INVALID_UID); reason, INVALID_UID);
mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_CHANGED_TO_NPMS, appId, 0) mHandler.obtainMessage(MSG_REPORT_TEMP_APP_WHITELIST_REMOVED_TO_NPMS, appId,
.sendToTarget(); /* unused= */ 0).sendToTarget();
reportTempWhitelistChangedLocked(uid, false); reportTempWhitelistChangedLocked(uid, false);
try { try {
mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_FINISH, mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_TEMP_WHITELIST_FINISH,

View File

@@ -26,6 +26,7 @@ import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_POWERSAVE;
import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_RESTRICTED; import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_RESTRICTED;
import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_STANDBY; import static android.net.NetworkPolicyManager.FIREWALL_CHAIN_NAME_STANDBY;
import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT; import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT;
import static android.os.PowerExemptionManager.reasonCodeToString;
import static android.os.Process.INVALID_UID; import static android.os.Process.INVALID_UID;
import android.app.ActivityManager; import android.app.ActivityManager;
@@ -192,13 +193,12 @@ public class NetworkPolicyLogger {
} }
} }
void tempPowerSaveWlChanged(int appId, boolean added) { void tempPowerSaveWlChanged(int appId, boolean added, int reasonCode, String reason) {
synchronized (mLock) { synchronized (mLock) {
if (LOGV || appId == UserHandle.getAppId(mDebugUid)) { if (LOGV || appId == UserHandle.getAppId(mDebugUid)) {
Slog.v(TAG, Slog.v(TAG, getTempPowerSaveWlChangedLog(appId, added, reasonCode, reason));
getTempPowerSaveWlChangedLog(appId, added));
} }
mEventsBuffer.tempPowerSaveWlChanged(appId, added); mEventsBuffer.tempPowerSaveWlChanged(appId, added, reasonCode, reason);
} }
} }
@@ -326,8 +326,10 @@ public class NetworkPolicyLogger {
return "Parole state: " + paroleOn; return "Parole state: " + paroleOn;
} }
private static String getTempPowerSaveWlChangedLog(int appId, boolean added) { private static String getTempPowerSaveWlChangedLog(int appId, boolean added,
return "temp-power-save whitelist for " + appId + " changed to: " + added; int reasonCode, String reason) {
return "temp-power-save whitelist for " + appId + " changed to: " + added
+ "; reason=" + reasonCodeToString(reasonCode) + " <" + reason + ">";
} }
private static String getUidFirewallRuleChangedLog(int chain, int uid, int rule) { private static String getUidFirewallRuleChangedLog(int chain, int uid, int rule) {
@@ -497,14 +499,17 @@ public class NetworkPolicyLogger {
data.timeStamp = System.currentTimeMillis(); data.timeStamp = System.currentTimeMillis();
} }
public void tempPowerSaveWlChanged(int appId, boolean added) { public void tempPowerSaveWlChanged(int appId, boolean added,
int reasonCode, String reason) {
final Data data = getNextSlot(); final Data data = getNextSlot();
if (data == null) return; if (data == null) return;
data.reset(); data.reset();
data.type = EVENT_TEMP_POWER_SAVE_WL_CHANGED; data.type = EVENT_TEMP_POWER_SAVE_WL_CHANGED;
data.ifield1 = appId; data.ifield1 = appId;
data.ifield2 = reasonCode;
data.bfield1 = added; data.bfield1 = added;
data.sfield1 = reason;
data.timeStamp = System.currentTimeMillis(); data.timeStamp = System.currentTimeMillis();
} }
@@ -571,7 +576,8 @@ public class NetworkPolicyLogger {
case EVENT_PAROLE_STATE_CHANGED: case EVENT_PAROLE_STATE_CHANGED:
return getParoleStateChanged(data.bfield1); return getParoleStateChanged(data.bfield1);
case EVENT_TEMP_POWER_SAVE_WL_CHANGED: case EVENT_TEMP_POWER_SAVE_WL_CHANGED:
return getTempPowerSaveWlChangedLog(data.ifield1, data.bfield1); return getTempPowerSaveWlChangedLog(data.ifield1, data.bfield1,
data.ifield2, data.sfield1);
case EVENT_UID_FIREWALL_RULE_CHANGED: case EVENT_UID_FIREWALL_RULE_CHANGED:
return getUidFirewallRuleChangedLog(data.ifield1, data.ifield2, data.ifield3); return getUidFirewallRuleChangedLog(data.ifield1, data.ifield2, data.ifield3);
case EVENT_FIREWALL_CHAIN_ENABLED: case EVENT_FIREWALL_CHAIN_ENABLED:

View File

@@ -17,9 +17,11 @@
package com.android.server.net; package com.android.server.net;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.Network; import android.net.Network;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.net.netstats.provider.NetworkStatsProvider; import android.net.netstats.provider.NetworkStatsProvider;
import android.os.PowerExemptionManager.ReasonCode;
import android.telephony.SubscriptionPlan; import android.telephony.SubscriptionPlan;
import java.util.Set; import java.util.Set;
@@ -41,9 +43,13 @@ public abstract class NetworkPolicyManagerInternal {
* that network rules for that appId can be updated. * that network rules for that appId can be updated.
* *
* @param appId The appId which has been updated in the allowlist. * @param appId The appId which has been updated in the allowlist.
* @param added Denotes whether the {@param appId} has been added or removed from the allowlist. * @param added Denotes whether the {@code appId} has been added or removed from the allowlist.
* @param reasonCode one of {@link ReasonCode} indicating the reason for the change.
* Only valid when {@code added} is {@code true}.
* @param reason an optional human-readable reason explaining why the app is temp allow-listed.
*/ */
public abstract void onTempPowerSaveWhitelistChange(int appId, boolean added); public abstract void onTempPowerSaveWhitelistChange(int appId, boolean added,
@ReasonCode int reasonCode, @Nullable String reason);
/** /**
* Return the active {@link SubscriptionPlan} for the given network. * Return the active {@link SubscriptionPlan} for the given network.

View File

@@ -211,6 +211,7 @@ import android.os.Message;
import android.os.MessageQueue.IdleHandler; import android.os.MessageQueue.IdleHandler;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.os.PowerExemptionManager.ReasonCode;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.PowerManager.ServiceType; import android.os.PowerManager.ServiceType;
import android.os.PowerManagerInternal; import android.os.PowerManagerInternal;
@@ -5743,12 +5744,13 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
} }
@Override @Override
public void onTempPowerSaveWhitelistChange(int appId, boolean added) { public void onTempPowerSaveWhitelistChange(int appId, boolean added,
@ReasonCode int reasonCode, @Nullable String reason) {
synchronized (mUidRulesFirstLock) { synchronized (mUidRulesFirstLock) {
if (!mSystemReady) { if (!mSystemReady) {
return; return;
} }
mLogger.tempPowerSaveWlChanged(appId, added); mLogger.tempPowerSaveWlChanged(appId, added, reasonCode, reason);
if (added) { if (added) {
mPowerSaveTempWhitelistAppIds.put(appId, true); mPowerSaveTempWhitelistAppIds.put(appId, true);
} else { } else {