Merge "Fixed connectivity state in some restricted network scenarios." into nyc-dev
am: 638a6e8140 * commit '638a6e8140a22a0ddb5d5dacb02173795073bd1a': Fixed connectivity state in some restricted network scenarios. Change-Id: I0fd66b16c83680940f294ea8e117443ffd8a50cd
This commit is contained in:
@@ -30,8 +30,9 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
|
|||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED;
|
||||||
import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
|
import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
|
||||||
import static android.net.NetworkPolicyManager.RULE_ALLOW_METERED;
|
import static android.net.NetworkPolicyManager.RULE_ALLOW_METERED;
|
||||||
|
import static android.net.NetworkPolicyManager.MASK_METERED_NETWORKS;
|
||||||
|
import static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS;
|
||||||
import static android.net.NetworkPolicyManager.RULE_NONE;
|
import static android.net.NetworkPolicyManager.RULE_NONE;
|
||||||
import static android.net.NetworkPolicyManager.RULE_REJECT_ALL;
|
|
||||||
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
|
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
|
||||||
import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED;
|
import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED;
|
||||||
import static android.net.NetworkPolicyManager.uidRulesToString;
|
import static android.net.NetworkPolicyManager.uidRulesToString;
|
||||||
@@ -217,6 +218,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
/** Flag indicating if background data is restricted. */
|
/** Flag indicating if background data is restricted. */
|
||||||
@GuardedBy("mRulesLock")
|
@GuardedBy("mRulesLock")
|
||||||
private boolean mRestrictBackground;
|
private boolean mRestrictBackground;
|
||||||
|
/** Flag indicating if background data is restricted due to battery savings. */
|
||||||
|
@GuardedBy("mRulesLock")
|
||||||
|
private boolean mRestrictPower;
|
||||||
|
|
||||||
final private Context mContext;
|
final private Context mContext;
|
||||||
private int mNetworkPreference;
|
private int mNetworkPreference;
|
||||||
@@ -665,9 +669,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
try {
|
try {
|
||||||
mPolicyManager.setConnectivityListener(mPolicyListener);
|
mPolicyManager.setConnectivityListener(mPolicyListener);
|
||||||
mRestrictBackground = mPolicyManager.getRestrictBackground();
|
mRestrictBackground = mPolicyManager.getRestrictBackground();
|
||||||
|
mRestrictPower = mPolicyManager.getRestrictPower();
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
// ouch, no rules updates means some processes may never get network
|
// ouch, no rules updates means some processes may never get network
|
||||||
loge("unable to register INetworkPolicyListener" + e.toString());
|
loge("unable to register INetworkPolicyListener" + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
final PowerManager powerManager = (PowerManager) context.getSystemService(
|
final PowerManager powerManager = (PowerManager) context.getSystemService(
|
||||||
@@ -919,22 +924,34 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
uidRules = mUidRules.get(uid, RULE_NONE);
|
uidRules = mUidRules.get(uid, RULE_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (uidRules) {
|
boolean allowed = true;
|
||||||
case RULE_ALLOW_ALL:
|
// Check Data Saver Mode first...
|
||||||
case RULE_ALLOW_METERED:
|
if (networkMetered) {
|
||||||
case RULE_TEMPORARY_ALLOW_METERED:
|
if ((uidRules & RULE_REJECT_METERED) != 0) {
|
||||||
return false;
|
if (LOGD_RULES) Log.d(TAG, "uid " + uid + " is blacklisted");
|
||||||
case RULE_REJECT_METERED:
|
// Explicitly blacklisted.
|
||||||
return networkMetered;
|
allowed = false;
|
||||||
case RULE_REJECT_ALL:
|
} else {
|
||||||
return true;
|
allowed = !mRestrictBackground
|
||||||
case RULE_NONE:
|
|| (uidRules & RULE_ALLOW_METERED) != 0
|
||||||
default:
|
|| (uidRules & RULE_TEMPORARY_ALLOW_METERED) != 0;
|
||||||
// When background data is restricted device-wide, the default
|
if (LOGD_RULES) Log.d(TAG, "allowed status for uid " + uid + " when"
|
||||||
// behavior for apps should be like RULE_REJECT_METERED
|
+ " mRestrictBackground=" + mRestrictBackground
|
||||||
return mRestrictBackground ? networkMetered : false;
|
+ ", whitelisted=" + ((uidRules & RULE_ALLOW_METERED) != 0)
|
||||||
|
+ ", tempWhitelist= + ((uidRules & RULE_TEMPORARY_ALLOW_METERED) != 0)"
|
||||||
|
+ ": " + allowed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// ...then Battery Saver Mode.
|
||||||
|
if (allowed && mRestrictPower) {
|
||||||
|
allowed = (uidRules & RULE_ALLOW_ALL) != 0;
|
||||||
|
if (LOGD_RULES) Log.d(TAG, "allowed status for uid " + uid + " when"
|
||||||
|
+ " mRestrictPower=" + mRestrictPower
|
||||||
|
+ ", whitelisted=" + ((uidRules & RULE_ALLOW_ALL) != 0)
|
||||||
|
+ ": " + allowed);
|
||||||
|
}
|
||||||
|
return !allowed;
|
||||||
|
}
|
||||||
|
|
||||||
private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) {
|
private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) {
|
||||||
if (ni == null || !LOGD_BLOCKED_NETWORKINFO) return;
|
if (ni == null || !LOGD_BLOCKED_NETWORKINFO) return;
|
||||||
@@ -1380,7 +1397,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
|
|
||||||
synchronized (mRulesLock) {
|
synchronized (mRulesLock) {
|
||||||
// skip update when we've already applied rules
|
// skip update when we've already applied rules
|
||||||
final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL);
|
final int oldRules = mUidRules.get(uid, RULE_NONE);
|
||||||
if (oldRules == uidRules) return;
|
if (oldRules == uidRules) return;
|
||||||
|
|
||||||
mUidRules.put(uid, uidRules);
|
mUidRules.put(uid, uidRules);
|
||||||
@@ -1421,6 +1438,18 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRestrictPowerChanged(boolean restrictPower) {
|
||||||
|
// caller is NPMS, since we only register with them
|
||||||
|
if (LOGD_RULES) {
|
||||||
|
log("onRestrictPowerChanged(restrictPower=" + restrictPower + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized (mRulesLock) {
|
||||||
|
mRestrictPower = restrictPower;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) {
|
public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) {
|
||||||
if (LOGD_RULES) {
|
if (LOGD_RULES) {
|
||||||
@@ -1862,6 +1891,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
pw.println(mRestrictBackground);
|
pw.println(mRestrictBackground);
|
||||||
pw.println();
|
pw.println();
|
||||||
|
|
||||||
|
pw.print("Restrict power: ");
|
||||||
|
pw.println(mRestrictPower);
|
||||||
|
pw.println();
|
||||||
|
|
||||||
pw.println("Status for known UIDs:");
|
pw.println("Status for known UIDs:");
|
||||||
pw.increaseIndent();
|
pw.increaseIndent();
|
||||||
final int size = mUidRules.size();
|
final int size = mUidRules.size();
|
||||||
@@ -3998,9 +4031,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
synchronized(mRulesLock) {
|
synchronized(mRulesLock) {
|
||||||
uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
|
uidRules = mUidRules.get(uid, RULE_ALLOW_ALL);
|
||||||
}
|
}
|
||||||
if (uidRules != RULE_ALLOW_ALL) {
|
if ((uidRules & RULE_ALLOW_ALL) == 0) {
|
||||||
// we could silently fail or we can filter the available nets to only give
|
// we could silently fail or we can filter the available nets to only give
|
||||||
// them those they have access to. Chose the more useful
|
// them those they have access to. Chose the more useful option.
|
||||||
networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED);
|
networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user