Merge "Flag to mark foreground jobs, fix data saver." into nyc-dev am: 9a977b7d45

am: 4b6d6eb09f

* commit '4b6d6eb09fe922be19fac9fa1b289584e84d1bb0':
  Flag to mark foreground jobs, fix data saver.

Change-Id: I30b87193a1d5d45f4ffeb919276a70ae709a1cdc
This commit is contained in:
Jeff Sharkey
2016-04-29 23:01:47 +00:00
committed by android-build-merger
3 changed files with 94 additions and 42 deletions

View File

@@ -774,8 +774,13 @@ public class ConnectivityManager {
* @hide * @hide
*/ */
public Network getActiveNetworkForUid(int uid) { public Network getActiveNetworkForUid(int uid) {
return getActiveNetworkForUid(uid, false);
}
/** {@hide} */
public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
try { try {
return mService.getActiveNetworkForUid(uid); return mService.getActiveNetworkForUid(uid, ignoreBlocked);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }
@@ -836,8 +841,13 @@ public class ConnectivityManager {
* {@hide} * {@hide}
*/ */
public NetworkInfo getActiveNetworkInfoForUid(int uid) { public NetworkInfo getActiveNetworkInfoForUid(int uid) {
return getActiveNetworkInfoForUid(uid, false);
}
/** {@hide} */
public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
try { try {
return mService.getActiveNetworkInfoForUid(uid); return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }
@@ -880,8 +890,13 @@ public class ConnectivityManager {
* is not valid. * is not valid.
*/ */
public NetworkInfo getNetworkInfo(Network network) { public NetworkInfo getNetworkInfo(Network network) {
return getNetworkInfoForUid(network, Process.myUid(), false);
}
/** {@hide} */
public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
try { try {
return mService.getNetworkInfoForNetwork(network); return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -44,11 +44,11 @@ import com.android.internal.net.VpnProfile;
interface IConnectivityManager interface IConnectivityManager
{ {
Network getActiveNetwork(); Network getActiveNetwork();
Network getActiveNetworkForUid(int uid); Network getActiveNetworkForUid(int uid, boolean ignoreBlocked);
NetworkInfo getActiveNetworkInfo(); NetworkInfo getActiveNetworkInfo();
NetworkInfo getActiveNetworkInfoForUid(int uid); NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked);
NetworkInfo getNetworkInfo(int networkType); NetworkInfo getNetworkInfo(int networkType);
NetworkInfo getNetworkInfoForNetwork(in Network network); NetworkInfo getNetworkInfoForUid(in Network network, int uid, boolean ignoreBlocked);
NetworkInfo[] getAllNetworkInfo(); NetworkInfo[] getAllNetworkInfo();
Network getNetworkForType(int networkType); Network getNetworkForType(int networkType);
Network[] getAllNetworks(); Network[] getAllNetworks();

View File

@@ -29,8 +29,12 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; 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_REJECT_ALL; 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_UNKNOWN;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.BroadcastOptions; import android.app.BroadcastOptions;
import android.app.Notification; import android.app.Notification;
@@ -96,8 +100,10 @@ import android.security.Credentials;
import android.security.KeyStore; import android.security.KeyStore;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.ArraySet;
import android.util.LocalLog; import android.util.LocalLog;
import android.util.LocalLog.ReadOnlyLocalLog; import android.util.LocalLog.ReadOnlyLocalLog;
import android.util.Log;
import android.util.Pair; import android.util.Pair;
import android.util.Slog; import android.util.Slog;
import android.util.SparseArray; import android.util.SparseArray;
@@ -121,9 +127,9 @@ import com.android.internal.util.XmlUtils;
import com.android.server.am.BatteryStatsService; import com.android.server.am.BatteryStatsService;
import com.android.server.connectivity.DataConnectionStats; import com.android.server.connectivity.DataConnectionStats;
import com.android.server.connectivity.KeepaliveTracker; import com.android.server.connectivity.KeepaliveTracker;
import com.android.server.connectivity.NetworkDiagnostics;
import com.android.server.connectivity.Nat464Xlat; import com.android.server.connectivity.Nat464Xlat;
import com.android.server.connectivity.NetworkAgentInfo; import com.android.server.connectivity.NetworkAgentInfo;
import com.android.server.connectivity.NetworkDiagnostics;
import com.android.server.connectivity.NetworkMonitor; import com.android.server.connectivity.NetworkMonitor;
import com.android.server.connectivity.PacManager; import com.android.server.connectivity.PacManager;
import com.android.server.connectivity.PermissionMonitor; import com.android.server.connectivity.PermissionMonitor;
@@ -131,8 +137,8 @@ import com.android.server.connectivity.Tethering;
import com.android.server.connectivity.Vpn; import com.android.server.connectivity.Vpn;
import com.android.server.net.BaseNetworkObserver; import com.android.server.net.BaseNetworkObserver;
import com.android.server.net.LockdownVpnTracker; import com.android.server.net.LockdownVpnTracker;
import com.google.android.collect.Lists; import com.google.android.collect.Lists;
import com.google.android.collect.Sets;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@@ -152,11 +158,11 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
/** /**
* @hide * @hide
@@ -202,9 +208,14 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */
private Object mRulesLock = new Object(); private Object mRulesLock = new Object();
/** Currently active network rules by UID. */ /** Currently active network rules by UID. */
@GuardedBy("mRulesLock")
private SparseIntArray mUidRules = new SparseIntArray(); private SparseIntArray mUidRules = new SparseIntArray();
/** Set of ifaces that are costly. */ /** Set of ifaces that are costly. */
private HashSet<String> mMeteredIfaces = Sets.newHashSet(); @GuardedBy("mRulesLock")
private ArraySet<String> mMeteredIfaces = new ArraySet<>();
/** Flag indicating if background data is restricted. */
@GuardedBy("mRulesLock")
private boolean mRestrictBackground;
final private Context mContext; final private Context mContext;
private int mNetworkPreference; private int mNetworkPreference;
@@ -651,7 +662,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
try { try {
mPolicyManager.registerListener(mPolicyListener); mPolicyManager.setConnectivityListener(mPolicyListener);
mRestrictBackground = mPolicyManager.getRestrictBackground();
} 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.toString());
@@ -819,7 +831,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
throw new IllegalStateException("No free netIds"); throw new IllegalStateException("No free netIds");
} }
private NetworkState getFilteredNetworkState(int networkType, int uid) { private NetworkState getFilteredNetworkState(int networkType, int uid, boolean ignoreBlocked) {
if (mLegacyTypeTracker.isTypeSupported(networkType)) { if (mLegacyTypeTracker.isTypeSupported(networkType)) {
final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
final NetworkState state; final NetworkState state;
@@ -834,7 +846,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
state = new NetworkState(info, new LinkProperties(), new NetworkCapabilities(), state = new NetworkState(info, new LinkProperties(), new NetworkCapabilities(),
null, null, null); null, null, null);
} }
filterNetworkStateForUid(state, uid); filterNetworkStateForUid(state, uid, ignoreBlocked);
return state; return state;
} else { } else {
return NetworkState.EMPTY; return NetworkState.EMPTY;
@@ -890,22 +902,36 @@ public class ConnectivityService extends IConnectivityManager.Stub
/** /**
* Check if UID should be blocked from using the network with the given LinkProperties. * Check if UID should be blocked from using the network with the given LinkProperties.
*/ */
private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid) { private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid,
final boolean networkCostly; boolean ignoreBlocked) {
// Networks aren't blocked when ignoring blocked status
if (ignoreBlocked) return false;
// Networks are never blocked for system services
if (uid < Process.FIRST_APPLICATION_UID) return false;
final boolean networkMetered;
final int uidRules; final int uidRules;
final String iface = (lp == null ? "" : lp.getInterfaceName()); final String iface = (lp == null ? "" : lp.getInterfaceName());
synchronized (mRulesLock) { synchronized (mRulesLock) {
networkCostly = mMeteredIfaces.contains(iface); networkMetered = mMeteredIfaces.contains(iface);
uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); uidRules = mUidRules.get(uid, RULE_UNKNOWN);
} }
if (uidRules == RULE_REJECT_ALL) { switch (uidRules) {
return true; case RULE_ALLOW_ALL:
} else if ((uidRules == RULE_REJECT_METERED) && networkCostly) { case RULE_ALLOW_METERED:
return true; case RULE_TEMPORARY_ALLOW_METERED:
} else {
return false; return false;
case RULE_REJECT_METERED:
return networkMetered;
case RULE_REJECT_ALL:
return true;
case RULE_UNKNOWN:
default:
// When background data is restricted device-wide, the default
// behavior for apps should be like RULE_REJECT_METERED
return mRestrictBackground ? networkMetered : false;
} }
} }
@@ -930,10 +956,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
* on {@link #isNetworkWithLinkPropertiesBlocked}, or * on {@link #isNetworkWithLinkPropertiesBlocked}, or
* {@link NetworkInfo#isMetered()} based on network policies. * {@link NetworkInfo#isMetered()} based on network policies.
*/ */
private void filterNetworkStateForUid(NetworkState state, int uid) { private void filterNetworkStateForUid(NetworkState state, int uid, boolean ignoreBlocked) {
if (state == null || state.networkInfo == null || state.linkProperties == null) return; if (state == null || state.networkInfo == null || state.linkProperties == null) return;
if (isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid)) { if (isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, ignoreBlocked)) {
state.networkInfo.setDetailedState(DetailedState.BLOCKED, null, null); state.networkInfo.setDetailedState(DetailedState.BLOCKED, null, null);
} }
if (mLockdownTracker != null) { if (mLockdownTracker != null) {
@@ -962,7 +988,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
enforceAccessPermission(); enforceAccessPermission();
final int uid = Binder.getCallingUid(); final int uid = Binder.getCallingUid();
final NetworkState state = getUnfilteredActiveNetworkState(uid); final NetworkState state = getUnfilteredActiveNetworkState(uid);
filterNetworkStateForUid(state, uid); filterNetworkStateForUid(state, uid, false);
maybeLogBlockedNetworkInfo(state.networkInfo, uid); maybeLogBlockedNetworkInfo(state.networkInfo, uid);
return state.networkInfo; return state.networkInfo;
} }
@@ -970,16 +996,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
@Override @Override
public Network getActiveNetwork() { public Network getActiveNetwork() {
enforceAccessPermission(); enforceAccessPermission();
return getActiveNetworkForUidInternal(Binder.getCallingUid()); return getActiveNetworkForUidInternal(Binder.getCallingUid(), false);
} }
@Override @Override
public Network getActiveNetworkForUid(int uid) { public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
enforceConnectivityInternalPermission(); enforceConnectivityInternalPermission();
return getActiveNetworkForUidInternal(uid); return getActiveNetworkForUidInternal(uid, ignoreBlocked);
} }
private Network getActiveNetworkForUidInternal(final int uid) { private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) {
final int user = UserHandle.getUserId(uid); final int user = UserHandle.getUserId(uid);
int vpnNetId = NETID_UNSET; int vpnNetId = NETID_UNSET;
synchronized (mVpns) { synchronized (mVpns) {
@@ -994,7 +1020,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
if (nai != null) return nai.network; if (nai != null) return nai.network;
} }
nai = getDefaultNetwork(); nai = getDefaultNetwork();
if (nai != null && isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid)) nai = null; if (nai != null
&& isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, ignoreBlocked)) {
nai = null;
}
return nai != null ? nai.network : null; return nai != null ? nai.network : null;
} }
@@ -1006,10 +1035,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
} }
@Override @Override
public NetworkInfo getActiveNetworkInfoForUid(int uid) { public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
enforceConnectivityInternalPermission(); enforceConnectivityInternalPermission();
final NetworkState state = getUnfilteredActiveNetworkState(uid); final NetworkState state = getUnfilteredActiveNetworkState(uid);
filterNetworkStateForUid(state, uid); filterNetworkStateForUid(state, uid, ignoreBlocked);
return state.networkInfo; return state.networkInfo;
} }
@@ -1023,22 +1052,21 @@ public class ConnectivityService extends IConnectivityManager.Stub
// getUnfilteredActiveNetworkState. // getUnfilteredActiveNetworkState.
final NetworkState state = getUnfilteredActiveNetworkState(uid); final NetworkState state = getUnfilteredActiveNetworkState(uid);
if (state.networkInfo != null && state.networkInfo.getType() == networkType) { if (state.networkInfo != null && state.networkInfo.getType() == networkType) {
filterNetworkStateForUid(state, uid); filterNetworkStateForUid(state, uid, false);
return state.networkInfo; return state.networkInfo;
} }
} }
final NetworkState state = getFilteredNetworkState(networkType, uid); final NetworkState state = getFilteredNetworkState(networkType, uid, false);
return state.networkInfo; return state.networkInfo;
} }
@Override @Override
public NetworkInfo getNetworkInfoForNetwork(Network network) { public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
enforceAccessPermission(); enforceAccessPermission();
final int uid = Binder.getCallingUid();
final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
if (nai != null) { if (nai != null) {
final NetworkState state = nai.getNetworkState(); final NetworkState state = nai.getNetworkState();
filterNetworkStateForUid(state, uid); filterNetworkStateForUid(state, uid, ignoreBlocked);
return state.networkInfo; return state.networkInfo;
} else { } else {
return null; return null;
@@ -1063,8 +1091,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
public Network getNetworkForType(int networkType) { public Network getNetworkForType(int networkType) {
enforceAccessPermission(); enforceAccessPermission();
final int uid = Binder.getCallingUid(); final int uid = Binder.getCallingUid();
NetworkState state = getFilteredNetworkState(networkType, uid); NetworkState state = getFilteredNetworkState(networkType, uid, false);
if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid)) { if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, false)) {
return state.network; return state.network;
} }
return null; return null;
@@ -1381,6 +1409,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
if (LOGD_RULES) { if (LOGD_RULES) {
log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")");
} }
synchronized (mRulesLock) {
mRestrictBackground = restrictBackground;
}
if (restrictBackground) { if (restrictBackground) {
log("onRestrictBackgroundChanged(true): disabling tethering"); log("onRestrictBackgroundChanged(true): disabling tethering");
mTethering.untetherAll(); mTethering.untetherAll();
@@ -1824,6 +1857,10 @@ public class ConnectivityService extends IConnectivityManager.Stub
pw.decreaseIndent(); pw.decreaseIndent();
pw.println(); pw.println();
pw.print("Restrict background: ");
pw.println(mRestrictBackground);
pw.println();
pw.println("Network Requests:"); pw.println("Network Requests:");
pw.increaseIndent(); pw.increaseIndent();
for (NetworkRequestInfo nri : mNetworkRequests.values()) { for (NetworkRequestInfo nri : mNetworkRequests.values()) {
@@ -2765,7 +2802,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
// which isn't meant to work on uncreated networks. // which isn't meant to work on uncreated networks.
if (!nai.created) return; if (!nai.created) return;
if (isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid)) return; if (isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, false)) return;
nai.networkMonitor.sendMessage(NetworkMonitor.CMD_FORCE_REEVALUATION, uid); nai.networkMonitor.sendMessage(NetworkMonitor.CMD_FORCE_REEVALUATION, uid);
} }