Merge "Use connectivity resources in service-connectivity"

This commit is contained in:
Remi NGUYEN VAN
2021-04-23 05:42:13 +00:00
committed by Gerrit Code Review
7 changed files with 108 additions and 37 deletions

View File

@@ -78,6 +78,11 @@
<item>1,3</item> <item>1,3</item>
</string-array> </string-array>
<!-- Reserved privileged keepalive slots per transport. -->
<integer translatable="false" name="config_reservedPrivilegedKeepaliveSlots">2</integer>
<!-- Allowed unprivileged keepalive slots per uid. -->
<integer translatable="false" name="config_allowedUnprivilegedKeepalivePerUid">2</integer>
<!-- Default value for ConnectivityManager.getMultipathPreference() on metered networks. Actual <!-- Default value for ConnectivityManager.getMultipathPreference() on metered networks. Actual
device behaviour is controlled by the metered multipath preference in device behaviour is controlled by the metered multipath preference in
@@ -89,4 +94,33 @@
Settings.Global.NETWORK_AVOID_BAD_WIFI. This is the default value of that setting. --> Settings.Global.NETWORK_AVOID_BAD_WIFI. This is the default value of that setting. -->
<integer translatable="false" name="config_networkAvoidBadWifi">1</integer> <integer translatable="false" name="config_networkAvoidBadWifi">1</integer>
<!-- Array of ConnectivityManager.TYPE_xxxx constants for networks that may only
be controlled by systemOrSignature apps. -->
<integer-array translatable="false" name="config_protectedNetworks">
<item>10</item>
<item>11</item>
<item>12</item>
<item>14</item>
<item>15</item>
</integer-array>
<!-- Whether the internal vehicle network should remain active even when no
apps requested it. -->
<bool name="config_vehicleInternalNetworkAlwaysRequested">false</bool>
<!-- If the hardware supports specially marking packets that caused a wakeup of the
main CPU, set this value to the mark used. -->
<integer name="config_networkWakeupPacketMark">0</integer>
<!-- Mask to use when checking skb mark defined in config_networkWakeupPacketMark above. -->
<integer name="config_networkWakeupPacketMask">0</integer>
<!-- Whether/how to notify the user on network switches. See LingerMonitor.java. -->
<integer translatable="false" name="config_networkNotifySwitchType">0</integer>
<!-- What types of network switches to notify. See LingerMonitor.java. -->
<string-array translatable="false" name="config_networkNotifySwitches">
</string-array>
</resources> </resources>

View File

@@ -26,6 +26,12 @@
<item type="integer" name="config_networkMeteredMultipathPreference"/> <item type="integer" name="config_networkMeteredMultipathPreference"/>
<item type="array" name="config_networkSupportedKeepaliveCount"/> <item type="array" name="config_networkSupportedKeepaliveCount"/>
<item type="integer" name="config_networkAvoidBadWifi"/> <item type="integer" name="config_networkAvoidBadWifi"/>
<item type="array" name="config_protectedNetworks"/>
<item type="bool" name="config_vehicleInternalNetworkAlwaysRequested"/>
<item type="integer" name="config_networkWakeupPacketMark"/>
<item type="integer" name="config_networkWakeupPacketMask"/>
<item type="integer" name="config_networkNotifySwitchType"/>
<item type="array" name="config_networkNotifySwitches"/>
</policy> </policy>
</overlayable> </overlayable>

View File

@@ -216,6 +216,7 @@ import android.util.Pair;
import android.util.SparseArray; import android.util.SparseArray;
import android.util.SparseIntArray; import android.util.SparseIntArray;
import com.android.connectivity.resources.R;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
@@ -830,8 +831,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private ArrayMap<Integer, Integer> loadRestoreTimers() { private ArrayMap<Integer, Integer> loadRestoreTimers() {
final String[] configs = mService.mResources.get().getStringArray( final String[] configs = mService.mResources.get().getStringArray(
com.android.connectivity.resources.R.array R.array.config_legacy_networktype_restore_timers);
.config_legacy_networktype_restore_timers);
final ArrayMap<Integer, Integer> ret = new ArrayMap<>(configs.length); final ArrayMap<Integer, Integer> ret = new ArrayMap<>(configs.length);
for (final String config : configs) { for (final String config : configs) {
final String[] splits = TextUtils.split(config, ","); final String[] splits = TextUtils.split(config, ",");
@@ -1310,8 +1310,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
mLegacyTypeTracker.loadSupportedTypes(mContext, mTelephonyManager); mLegacyTypeTracker.loadSupportedTypes(mContext, mTelephonyManager);
mProtectedNetworks = new ArrayList<>(); mProtectedNetworks = new ArrayList<>();
int[] protectedNetworks = context.getResources().getIntArray( int[] protectedNetworks = mResources.get().getIntArray(R.array.config_protectedNetworks);
com.android.internal.R.array.config_protectedNetworks);
for (int p : protectedNetworks) { for (int p : protectedNetworks) {
if (mLegacyTypeTracker.isTypeSupported(p) && !mProtectedNetworks.contains(p)) { if (mLegacyTypeTracker.isTypeSupported(p) && !mProtectedNetworks.contains(p)) {
mProtectedNetworks.add(p); mProtectedNetworks.add(p);
@@ -1483,8 +1482,14 @@ public class ConnectivityService extends IConnectivityManager.Stub
ConnectivitySettingsManager.MOBILE_DATA_ALWAYS_ON, true /* defaultValue */); ConnectivitySettingsManager.MOBILE_DATA_ALWAYS_ON, true /* defaultValue */);
handleAlwaysOnNetworkRequest(mDefaultWifiRequest, handleAlwaysOnNetworkRequest(mDefaultWifiRequest,
ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED, false /* defaultValue */); ConnectivitySettingsManager.WIFI_ALWAYS_REQUESTED, false /* defaultValue */);
final boolean vehicleAlwaysRequested = mResources.get().getBoolean(
R.bool.config_vehicleInternalNetworkAlwaysRequested);
// TODO (b/183076074): remove legacy fallback after migrating overlays
final boolean legacyAlwaysRequested = mContext.getResources().getBoolean(
mContext.getResources().getIdentifier(
"config_vehicleInternalNetworkAlwaysRequested", "bool", "android"));
handleAlwaysOnNetworkRequest(mDefaultVehicleRequest, handleAlwaysOnNetworkRequest(mDefaultVehicleRequest,
com.android.internal.R.bool.config_vehicleInternalNetworkAlwaysRequested); vehicleAlwaysRequested || legacyAlwaysRequested);
} }
private void registerSettingsCallbacks() { private void registerSettingsCallbacks() {
@@ -4839,7 +4844,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
mWakelockLogs.log("ACQUIRE for " + forWhom); mWakelockLogs.log("ACQUIRE for " + forWhom);
Message msg = mHandler.obtainMessage(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); Message msg = mHandler.obtainMessage(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK);
final int lockTimeout = mResources.get().getInteger( final int lockTimeout = mResources.get().getInteger(
com.android.connectivity.resources.R.integer.config_networkTransitionTimeout); R.integer.config_networkTransitionTimeout);
mHandler.sendMessageDelayed(msg, lockTimeout); mHandler.sendMessageDelayed(msg, lockTimeout);
} }
@@ -6700,10 +6705,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
return; return;
} }
int mark = mContext.getResources().getInteger( int mark = mResources.get().getInteger(R.integer.config_networkWakeupPacketMark);
com.android.internal.R.integer.config_networkWakeupPacketMark); int mask = mResources.get().getInteger(R.integer.config_networkWakeupPacketMask);
int mask = mContext.getResources().getInteger(
com.android.internal.R.integer.config_networkWakeupPacketMask); // TODO (b/183076074): remove legacy fallback after migrating overlays
final int legacyMark = mContext.getResources().getInteger(mContext.getResources()
.getIdentifier("config_networkWakeupPacketMark", "integer", "android"));
final int legacyMask = mContext.getResources().getInteger(mContext.getResources()
.getIdentifier("config_networkWakeupPacketMask", "integer", "android"));
mark = mark == 0 ? legacyMark : mark;
mask = mask == 0 ? legacyMask : mask;
// Mask/mark of zero will not detect anything interesting. // Mask/mark of zero will not detect anything interesting.
// Don't install rules unless both values are nonzero. // Don't install rules unless both values are nonzero.
@@ -6896,8 +6907,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
private void updateWakeOnLan(@NonNull LinkProperties lp) { private void updateWakeOnLan(@NonNull LinkProperties lp) {
if (mWolSupportedInterfaces == null) { if (mWolSupportedInterfaces == null) {
mWolSupportedInterfaces = new ArraySet<>(mResources.get().getStringArray( mWolSupportedInterfaces = new ArraySet<>(mResources.get().getStringArray(
com.android.connectivity.resources.R.array R.array.config_wakeonlan_supported_interfaces));
.config_wakeonlan_supported_interfaces));
} }
lp.setWakeOnLanSupported(mWolSupportedInterfaces.contains(lp.getInterfaceName())); lp.setWakeOnLanSupported(mWolSupportedInterfaces.contains(lp.getInterfaceName()));
} }
@@ -8473,7 +8483,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
public String getCaptivePortalServerUrl() { public String getCaptivePortalServerUrl() {
enforceNetworkStackOrSettingsPermission(); enforceNetworkStackOrSettingsPermission();
String settingUrl = mResources.get().getString( String settingUrl = mResources.get().getString(
com.android.connectivity.resources.R.string.config_networkCaptivePortalServerUrl); R.string.config_networkCaptivePortalServerUrl);
if (!TextUtils.isEmpty(settingUrl)) { if (!TextUtils.isEmpty(settingUrl)) {
return settingUrl; return settingUrl;

View File

@@ -37,6 +37,7 @@ import static android.net.SocketKeepalive.SUCCESS;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityResources;
import android.net.ISocketKeepaliveCallback; import android.net.ISocketKeepaliveCallback;
import android.net.InetAddresses; import android.net.InetAddresses;
import android.net.InvalidPacketException; import android.net.InvalidPacketException;
@@ -57,7 +58,7 @@ import android.system.Os;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import com.android.internal.R; import com.android.connectivity.resources.R;
import com.android.internal.util.IndentingPrintWriter; import com.android.internal.util.IndentingPrintWriter;
import com.android.net.module.util.HexDump; import com.android.net.module.util.HexDump;
import com.android.net.module.util.IpUtils; import com.android.net.module.util.IpUtils;
@@ -112,10 +113,19 @@ public class KeepaliveTracker {
mTcpController = new TcpKeepaliveController(handler); mTcpController = new TcpKeepaliveController(handler);
mContext = context; mContext = context;
mSupportedKeepalives = KeepaliveUtils.getSupportedKeepalives(mContext); mSupportedKeepalives = KeepaliveUtils.getSupportedKeepalives(mContext);
mReservedPrivilegedSlots = mContext.getResources().getInteger(
R.integer.config_reservedPrivilegedKeepaliveSlots); // TODO (b/183076074): stop reading legacy resources after migrating overlays
mAllowedUnprivilegedSlotsForUid = mContext.getResources().getInteger( final int legacyReservedSlots = mContext.getResources().getInteger(
R.integer.config_allowedUnprivilegedKeepalivePerUid); mContext.getResources().getIdentifier(
"config_reservedPrivilegedKeepaliveSlots", "integer", "android"));
final int legacyAllowedSlots = mContext.getResources().getInteger(
mContext.getResources().getIdentifier(
"config_allowedUnprivilegedKeepalivePerUid", "integer", "android"));
final ConnectivityResources res = new ConnectivityResources(mContext);
mReservedPrivilegedSlots = Math.min(legacyReservedSlots, res.get().getInteger(
R.integer.config_reservedPrivilegedKeepaliveSlots));
mAllowedUnprivilegedSlotsForUid = Math.min(legacyAllowedSlots, res.get().getInteger(
R.integer.config_allowedUnprivilegedKeepalivePerUid));
} }
/** /**

View File

@@ -24,6 +24,8 @@ import android.app.PendingIntent;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources;
import android.net.ConnectivityResources;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.UserHandle; import android.os.UserHandle;
@@ -34,7 +36,7 @@ import android.util.SparseArray;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import android.util.SparseIntArray; import android.util.SparseIntArray;
import com.android.internal.R; import com.android.connectivity.resources.R;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.MessageUtils; import com.android.internal.util.MessageUtils;
import com.android.server.connectivity.NetworkNotificationManager.NotificationType; import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
@@ -72,6 +74,7 @@ public class LingerMonitor {
new Class[] { LingerMonitor.class }, new String[]{ "NOTIFY_TYPE_" }); new Class[] { LingerMonitor.class }, new String[]{ "NOTIFY_TYPE_" });
private final Context mContext; private final Context mContext;
final Resources mResources;
private final NetworkNotificationManager mNotifier; private final NetworkNotificationManager mNotifier;
private final int mDailyLimit; private final int mDailyLimit;
private final long mRateLimitMillis; private final long mRateLimitMillis;
@@ -89,6 +92,7 @@ public class LingerMonitor {
public LingerMonitor(Context context, NetworkNotificationManager notifier, public LingerMonitor(Context context, NetworkNotificationManager notifier,
int dailyLimit, long rateLimitMillis) { int dailyLimit, long rateLimitMillis) {
mContext = context; mContext = context;
mResources = new ConnectivityResources(mContext).get();
mNotifier = notifier; mNotifier = notifier;
mDailyLimit = dailyLimit; mDailyLimit = dailyLimit;
mRateLimitMillis = rateLimitMillis; mRateLimitMillis = rateLimitMillis;
@@ -128,8 +132,7 @@ public class LingerMonitor {
@VisibleForTesting @VisibleForTesting
public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) { public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
// TODO: Evaluate moving to CarrierConfigManager. // TODO: Evaluate moving to CarrierConfigManager.
String[] notifySwitches = String[] notifySwitches = mResources.getStringArray(R.array.config_networkNotifySwitches);
mContext.getResources().getStringArray(R.array.config_networkNotifySwitches);
if (VDBG) { if (VDBG) {
Log.d(TAG, "Notify on network switches: " + Arrays.toString(notifySwitches)); Log.d(TAG, "Notify on network switches: " + Arrays.toString(notifySwitches));
@@ -178,8 +181,7 @@ public class LingerMonitor {
// Notify the user of a network switch using a notification or a toast. // Notify the user of a network switch using a notification or a toast.
private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) { private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) {
int notifyType = int notifyType = mResources.getInteger(R.integer.config_networkNotifySwitchType);
mContext.getResources().getInteger(R.integer.config_networkNotifySwitchType);
if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) { if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) {
notifyType = NOTIFY_TYPE_TOAST; notifyType = NOTIFY_TYPE_TOAST;
} }

View File

@@ -1637,25 +1637,26 @@ public class ConnectivityServiceTest {
}).when(deps).makeMultinetworkPolicyTracker(any(), any(), any()); }).when(deps).makeMultinetworkPolicyTracker(any(), any(), any());
doReturn(true).when(deps).getCellular464XlatEnabled(); doReturn(true).when(deps).getCellular464XlatEnabled();
doReturn(60000).when(mResources).getInteger( doReturn(60000).when(mResources).getInteger(R.integer.config_networkTransitionTimeout);
com.android.connectivity.resources.R.integer.config_networkTransitionTimeout); doReturn("").when(mResources).getString(R.string.config_networkCaptivePortalServerUrl);
doReturn("").when(mResources).getString(
com.android.connectivity.resources.R.string.config_networkCaptivePortalServerUrl);
doReturn(new String[]{ WIFI_WOL_IFNAME }).when(mResources).getStringArray( doReturn(new String[]{ WIFI_WOL_IFNAME }).when(mResources).getStringArray(
com.android.connectivity.resources.R.array.config_wakeonlan_supported_interfaces); R.array.config_wakeonlan_supported_interfaces);
doReturn(new String[] { "0,1", "1,3" }).when(mResources).getStringArray( doReturn(new String[] { "0,1", "1,3" }).when(mResources).getStringArray(
com.android.connectivity.resources.R.array.config_networkSupportedKeepaliveCount); R.array.config_networkSupportedKeepaliveCount);
doReturn(com.android.connectivity.resources.R.array.config_networkSupportedKeepaliveCount) doReturn(new String[0]).when(mResources).getStringArray(
.when(mResources).getIdentifier(eq("config_networkSupportedKeepaliveCount"), R.array.config_networkNotifySwitches);
eq("array"), any()); doReturn(new int[]{10, 11, 12, 14, 15}).when(mResources).getIntArray(
doReturn(com.android.connectivity.resources.R.array.network_switch_type_name) R.array.config_protectedNetworks);
.when(mResources).getIdentifier(eq("network_switch_type_name"),
eq("array"), any());
// We don't test the actual notification value strings, so just return an empty array. // We don't test the actual notification value strings, so just return an empty array.
// It doesn't matter what the values are as long as it's not null. // It doesn't matter what the values are as long as it's not null.
doReturn(new String[0]).when(mResources).getStringArray(R.array.network_switch_type_name); doReturn(new String[0]).when(mResources).getStringArray(R.array.network_switch_type_name);
doReturn(R.array.config_networkSupportedKeepaliveCount).when(mResources)
.getIdentifier(eq("config_networkSupportedKeepaliveCount"), eq("array"), any());
doReturn(R.array.network_switch_type_name).when(mResources)
.getIdentifier(eq("network_switch_type_name"), eq("array"), any());
final ConnectivityResources connRes = mock(ConnectivityResources.class); final ConnectivityResources connRes = mock(ConnectivityResources.class);
doReturn(mResources).when(connRes).get(); doReturn(mResources).when(connRes).get();
doReturn(connRes).when(deps).getResources(any()); doReturn(connRes).when(deps).getResources(any());

View File

@@ -32,6 +32,7 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.ConnectivityResources;
import android.net.IDnsResolver; import android.net.IDnsResolver;
import android.net.INetd; import android.net.INetd;
import android.net.LinkProperties; import android.net.LinkProperties;
@@ -47,10 +48,11 @@ import android.text.format.DateUtils;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.internal.R; import com.android.connectivity.resources.R;
import com.android.server.ConnectivityService; import com.android.server.ConnectivityService;
import com.android.server.connectivity.NetworkNotificationManager.NotificationType; import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -84,10 +86,16 @@ public class LingerMonitorTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(mCtx.getResources()).thenReturn(mResources); when(mCtx.getResources()).thenReturn(mResources);
when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity"); when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
ConnectivityResources.setResourcesContextForTest(mCtx);
mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT); mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
} }
@After
public void tearDown() {
ConnectivityResources.setResourcesContextForTest(null);
}
@Test @Test
public void testTransitions() { public void testTransitions() {
setNotificationSwitch(transition(WIFI, CELLULAR)); setNotificationSwitch(transition(WIFI, CELLULAR));