Added onRestrictBackgroundWhitelistChanged callback.

When users add or remove an app to the Data Saver Mode whitelist, the
app is notified through a
ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED. But besides this
broadcast, it's also necessary to notify internal apps such Settings,
hence a new method is being added to INetworkPolicyListener.

BUG: 27481520
Change-Id: I1537a77becf6b7da1535ed5faabdc894fd9f7084
This commit is contained in:
Felipe Leme
2016-04-19 10:24:39 -07:00
parent b1dea03f0a
commit 019fcd2bec
4 changed files with 51 additions and 12 deletions

View File

@@ -22,5 +22,6 @@ oneway interface INetworkPolicyListener {
void onUidRulesChanged(int uid, int uidRules);
void onMeteredIfacesChanged(in String[] meteredIfaces);
void onRestrictBackgroundChanged(boolean restrictBackground);
void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted);
}

View File

@@ -89,6 +89,10 @@ public class DataSaverController {
}
});
}
@Override
public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) {
}
};
public interface Listener {

View File

@@ -1419,6 +1419,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")");
}
}
@Override
public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) {
if (LOGD_RULES) {
// caller is NPMS, since we only register with them
log("onRestrictBackgroundWhitelistChanged(uid=" + uid + ", whitelisted="
+ whitelisted + ")");
}
}
};
/**

View File

@@ -1678,7 +1678,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
// Checks if app was added or removed to the blacklist.
if ((oldPolicy == POLICY_NONE && policy == POLICY_REJECT_METERED_BACKGROUND)
|| (oldPolicy == POLICY_REJECT_METERED_BACKGROUND && policy == POLICY_NONE)) {
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0)
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 1, null)
.sendToTarget();
}
}
@@ -1970,10 +1970,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
// ...but always persists the whitelist request.
writePolicyLocked();
}
if (mRestrictBackground && !oldStatus && needFirewallRules) {
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0)
.sendToTarget();
}
int changed = (mRestrictBackground && !oldStatus && needFirewallRules) ? 1 : 0;
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed,
Boolean.TRUE).sendToTarget();
}
@Override
@@ -1983,10 +1982,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
synchronized (mRulesLock) {
changed = removeRestrictBackgroundWhitelistedUidLocked(uid, false, true);
}
if (changed) {
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0)
.sendToTarget();
}
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed ? 1 : 0,
Boolean.FALSE).sendToTarget();
}
/**
@@ -2924,10 +2921,40 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
return true;
}
case MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED: {
// MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED can be called in 2 occasions:
// - when an app is whitelisted
// - when an app is blacklisted
//
// Whether the internal listeners (INetworkPolicyListener implementations) or
// app broadcast receivers are notified depend on the following rules:
//
// - App receivers are only notified when the app status changed (msg.arg2 = 1)
// - Listeners are only notified when app was whitelisted (msg.obj is not null),
// since blacklist notifications are handled through MSG_RULES_CHANGED).
final int uid = msg.arg1;
final boolean changed = msg.arg2 == 1;
final Boolean whitelisted = (Boolean) msg.obj;
if (whitelisted != null) {
final int length = mListeners.beginBroadcast();
for (int i = 0; i < length; i++) {
// First notify internal listeners...
final INetworkPolicyListener listener = mListeners.getBroadcastItem(i);
if (listener != null) {
try {
listener.onRestrictBackgroundWhitelistChanged(uid,
whitelisted.booleanValue());
} catch (RemoteException e) {
}
}
}
mListeners.finishBroadcast();
}
final PackageManager pm = mContext.getPackageManager();
final String[] packages = pm.getPackagesForUid(uid);
if (packages != null) {
if (changed && packages != null) {
// ...then notify apps listening to ACTION_RESTRICT_BACKGROUND_CHANGED
final int userId = UserHandle.getUserId(uid);
for (String packageName : packages) {
final Intent intent = new Intent(
@@ -2936,8 +2963,6 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mContext.sendBroadcastAsUser(intent, UserHandle.of(userId));
}
} else {
Slog.w(TAG, "no packages for uid " + uid);
}
return true;
}