Merge changes from topic "backport-connectivityresources" am: a31a7b1bf4

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1645297

Change-Id: Id6e8f89870b70332212e9052168e6f7c2faa3ee3
This commit is contained in:
Lorenzo Colitti
2021-03-21 16:51:02 +00:00
committed by Automerger Merge Worker
4 changed files with 40 additions and 15 deletions

View File

@@ -68,8 +68,6 @@
<item>VPN</item> <item>VPN</item>
</string-array> </string-array>
<!-- Network type names used in the network_switch_metered and network_switch_metered_detail strings. These must be kept in the sync with the values NetworkCapabilities.TRANSPORT_xxx values, and in the same order. -->
<!-- Network type name displayed if one of the types is not found in network_switch_type_name. --> <!-- Network type name displayed if one of the types is not found in network_switch_type_name. -->
<string name="network_switch_type_name_unknown">an unknown network type</string> <string name="network_switch_type_name_unknown">an unknown network type</string>

View File

@@ -28,6 +28,8 @@ import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.drawable.Icon;
import android.net.ConnectivityResources;
import android.net.NetworkSpecifier; import android.net.NetworkSpecifier;
import android.net.TelephonyNetworkSpecifier; import android.net.TelephonyNetworkSpecifier;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
@@ -40,7 +42,7 @@ import android.util.SparseArray;
import android.util.SparseIntArray; import android.util.SparseIntArray;
import android.widget.Toast; import android.widget.Toast;
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.messages.nano.SystemMessageProto.SystemMessage; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
@@ -82,6 +84,7 @@ public class NetworkNotificationManager {
// The context is for the current user (system server) // The context is for the current user (system server)
private final Context mContext; private final Context mContext;
private final Resources mResources;
private final TelephonyManager mTelephonyManager; private final TelephonyManager mTelephonyManager;
// The notification manager is created from a context for User.ALL, so notifications // The notification manager is created from a context for User.ALL, so notifications
// will be sent to all users. // will be sent to all users.
@@ -96,6 +99,7 @@ public class NetworkNotificationManager {
(NotificationManager) c.createContextAsUser(UserHandle.ALL, 0 /* flags */) (NotificationManager) c.createContextAsUser(UserHandle.ALL, 0 /* flags */)
.getSystemService(Context.NOTIFICATION_SERVICE); .getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationTypeMap = new SparseIntArray(); mNotificationTypeMap = new SparseIntArray();
mResources = new ConnectivityResources(mContext).get();
} }
@VisibleForTesting @VisibleForTesting
@@ -113,20 +117,19 @@ public class NetworkNotificationManager {
return -1; return -1;
} }
private static String getTransportName(final int transportType) { private String getTransportName(final int transportType) {
Resources r = Resources.getSystem(); String[] networkTypes = mResources.getStringArray(R.array.network_switch_type_name);
String[] networkTypes = r.getStringArray(R.array.network_switch_type_name);
try { try {
return networkTypes[transportType]; return networkTypes[transportType];
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
return r.getString(R.string.network_switch_type_name_unknown); return mResources.getString(R.string.network_switch_type_name_unknown);
} }
} }
private static int getIcon(int transportType) { private static int getIcon(int transportType) {
return (transportType == TRANSPORT_WIFI) return (transportType == TRANSPORT_WIFI)
? R.drawable.stat_notify_wifi_in_range : // TODO: Distinguish ! from ?. ? R.drawable.stat_notify_wifi_in_range // TODO: Distinguish ! from ?.
R.drawable.stat_notify_rssi_in_range; : R.drawable.stat_notify_rssi_in_range;
} }
/** /**
@@ -194,10 +197,10 @@ public class NetworkNotificationManager {
tag, nameOf(eventId), getTransportName(transportType), name, highPriority)); tag, nameOf(eventId), getTransportName(transportType), name, highPriority));
} }
Resources r = mContext.getResources(); final Resources r = mResources;
final CharSequence title; final CharSequence title;
final CharSequence details; final CharSequence details;
int icon = getIcon(transportType); Icon icon = Icon.createWithResource(r, getIcon(transportType));
if (notifyType == NotificationType.NO_INTERNET && transportType == TRANSPORT_WIFI) { if (notifyType == NotificationType.NO_INTERNET && transportType == TRANSPORT_WIFI) {
title = r.getString(R.string.wifi_no_internet, name); title = r.getString(R.string.wifi_no_internet, name);
details = r.getString(R.string.wifi_no_internet_detailed); details = r.getString(R.string.wifi_no_internet_detailed);
@@ -272,8 +275,7 @@ public class NetworkNotificationManager {
.setSmallIcon(icon) .setSmallIcon(icon)
.setAutoCancel(true) .setAutoCancel(true)
.setTicker(title) .setTicker(title)
.setColor(mContext.getColor( .setColor(mContext.getColor(android.R.color.system_notification_accent_color))
com.android.internal.R.color.system_notification_accent_color))
.setContentTitle(title) .setContentTitle(title)
.setContentIntent(intent) .setContentIntent(intent)
.setLocalOnly(true) .setLocalOnly(true)
@@ -353,7 +355,7 @@ public class NetworkNotificationManager {
public void showToast(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) { public void showToast(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
String fromTransport = getTransportName(approximateTransportType(fromNai)); String fromTransport = getTransportName(approximateTransportType(fromNai));
String toTransport = getTransportName(approximateTransportType(toNai)); String toTransport = getTransportName(approximateTransportType(toNai));
String text = mContext.getResources().getString( String text = mResources.getString(
R.string.network_switch_metered_toast, fromTransport, toTransport); R.string.network_switch_metered_toast, fromTransport, toTransport);
Toast.makeText(mContext, text, Toast.LENGTH_LONG).show(); Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
} }

View File

@@ -274,6 +274,7 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import com.android.connectivity.resources.R;
import com.android.internal.net.VpnConfig; import com.android.internal.net.VpnConfig;
import com.android.internal.net.VpnProfile; import com.android.internal.net.VpnProfile;
import com.android.internal.util.ArrayUtils; import com.android.internal.util.ArrayUtils;
@@ -1571,6 +1572,14 @@ public class ConnectivityServiceTest {
doReturn(com.android.connectivity.resources.R.array.config_networkSupportedKeepaliveCount) doReturn(com.android.connectivity.resources.R.array.config_networkSupportedKeepaliveCount)
.when(mResources).getIdentifier(eq("config_networkSupportedKeepaliveCount"), .when(mResources).getIdentifier(eq("config_networkSupportedKeepaliveCount"),
eq("array"), any()); eq("array"), any());
doReturn(com.android.connectivity.resources.R.array.network_switch_type_name)
.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.
// 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);
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

@@ -36,6 +36,7 @@ import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityResources;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.os.UserHandle; import android.os.UserHandle;
@@ -44,9 +45,10 @@ import android.telephony.TelephonyManager;
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.connectivity.NetworkNotificationManager.NotificationType; import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@@ -120,11 +122,25 @@ public class NetworkNotificationManagerTest {
when(mCtx.getSystemService(eq(Context.NOTIFICATION_SERVICE))) when(mCtx.getSystemService(eq(Context.NOTIFICATION_SERVICE)))
.thenReturn(mNotificationManager); .thenReturn(mNotificationManager);
when(mNetworkInfo.getExtraInfo()).thenReturn(TEST_EXTRA_INFO); when(mNetworkInfo.getExtraInfo()).thenReturn(TEST_EXTRA_INFO);
ConnectivityResources.setResourcesContextForTest(mCtx);
when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B); when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B);
// Come up with some credible-looking transport names. The actual values do not matter.
String[] transportNames = new String[NetworkCapabilities.MAX_TRANSPORT + 1];
for (int transport = 0; transport <= NetworkCapabilities.MAX_TRANSPORT; transport++) {
transportNames[transport] = NetworkCapabilities.transportNameOf(transport);
}
when(mResources.getStringArray(R.array.network_switch_type_name))
.thenReturn(transportNames);
mManager = new NetworkNotificationManager(mCtx, mTelephonyManager); mManager = new NetworkNotificationManager(mCtx, mTelephonyManager);
} }
@After
public void tearDown() {
ConnectivityResources.setResourcesContextForTest(null);
}
private void verifyTitleByNetwork(final int id, final NetworkAgentInfo nai, final int title) { private void verifyTitleByNetwork(final int id, final NetworkAgentInfo nai, final int title) {
final String tag = NetworkNotificationManager.tagFor(id); final String tag = NetworkNotificationManager.tagFor(id);
mManager.showNotification(id, PRIVATE_DNS_BROKEN, nai, null, null, true); mManager.showNotification(id, PRIVATE_DNS_BROKEN, nai, null, null, true);