Merge "Added onRestrictBackgroundWhitelistChanged callback." into nyc-dev

This commit is contained in:
TreeHugger Robot
2016-04-20 17:44:54 +00:00
committed by Android (Google) Code Review
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 onUidRulesChanged(int uid, int uidRules);
void onMeteredIfacesChanged(in String[] meteredIfaces); void onMeteredIfacesChanged(in String[] meteredIfaces);
void onRestrictBackgroundChanged(boolean restrictBackground); 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 { public interface Listener {

View File

@@ -1419,6 +1419,15 @@ public class ConnectivityService extends IConnectivityManager.Stub
log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); 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. // Checks if app was added or removed to the blacklist.
if ((oldPolicy == POLICY_NONE && policy == POLICY_REJECT_METERED_BACKGROUND) if ((oldPolicy == POLICY_NONE && policy == POLICY_REJECT_METERED_BACKGROUND)
|| (oldPolicy == POLICY_REJECT_METERED_BACKGROUND && policy == POLICY_NONE)) { || (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(); .sendToTarget();
} }
} }
@@ -1970,10 +1970,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
// ...but always persists the whitelist request. // ...but always persists the whitelist request.
writePolicyLocked(); writePolicyLocked();
} }
if (mRestrictBackground && !oldStatus && needFirewallRules) { int changed = (mRestrictBackground && !oldStatus && needFirewallRules) ? 1 : 0;
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0) mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed,
.sendToTarget(); Boolean.TRUE).sendToTarget();
}
} }
@Override @Override
@@ -1983,10 +1982,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
synchronized (mRulesLock) { synchronized (mRulesLock) {
changed = removeRestrictBackgroundWhitelistedUidLocked(uid, false, true); changed = removeRestrictBackgroundWhitelistedUidLocked(uid, false, true);
} }
if (changed) { mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed ? 1 : 0,
mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, 0) Boolean.FALSE).sendToTarget();
.sendToTarget();
}
} }
/** /**
@@ -2924,10 +2921,40 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
return true; return true;
} }
case MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED: { 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 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 PackageManager pm = mContext.getPackageManager();
final String[] packages = pm.getPackagesForUid(uid); 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); final int userId = UserHandle.getUserId(uid);
for (String packageName : packages) { for (String packageName : packages) {
final Intent intent = new Intent( final Intent intent = new Intent(
@@ -2936,8 +2963,6 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
mContext.sendBroadcastAsUser(intent, UserHandle.of(userId)); mContext.sendBroadcastAsUser(intent, UserHandle.of(userId));
} }
} else {
Slog.w(TAG, "no packages for uid " + uid);
} }
return true; return true;
} }