Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8b77bf79 authored by Lorenzo Colitti's avatar Lorenzo Colitti
Browse files

Use module resources in NetworkNotificationManager.

Also make getTransportName non-static so it can access the module
resources.

Also fix a duplicate comment in a resource file.

Bug: 183097033
Test: atest FrameworksNetTests
Test: connected to Wi-Fi with no Internet, observed notification
Change-Id: Ic0d24d36af0b87153d527083f8964ddc6cd78482
Merged-In: Ic0d24d36af0b87153d527083f8964ddc6cd78482
parent 250710c6
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -68,8 +68,6 @@
        <item>VPN</item>
    </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. -->
    <string name="network_switch_type_name_unknown">an unknown network type</string>

+14 −12
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Icon;
import android.net.ConnectivityResources;
import android.net.NetworkSpecifier;
import android.net.TelephonyNetworkSpecifier;
import android.net.wifi.WifiInfo;
@@ -40,7 +42,7 @@ import android.util.SparseArray;
import android.util.SparseIntArray;
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.messages.nano.SystemMessageProto.SystemMessage;

@@ -82,6 +84,7 @@ public class NetworkNotificationManager {

    // The context is for the current user (system server)
    private final Context mContext;
    private final Resources mResources;
    private final TelephonyManager mTelephonyManager;
    // The notification manager is created from a context for User.ALL, so notifications
    // will be sent to all users.
@@ -96,6 +99,7 @@ public class NetworkNotificationManager {
                (NotificationManager) c.createContextAsUser(UserHandle.ALL, 0 /* flags */)
                        .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationTypeMap = new SparseIntArray();
        mResources = new ConnectivityResources(mContext).get();
    }

    @VisibleForTesting
@@ -113,20 +117,19 @@ public class NetworkNotificationManager {
        return -1;
    }

    private static String getTransportName(final int transportType) {
        Resources r = Resources.getSystem();
        String[] networkTypes = r.getStringArray(R.array.network_switch_type_name);
    private String getTransportName(final int transportType) {
        String[] networkTypes = mResources.getStringArray(R.array.network_switch_type_name);
        try {
            return networkTypes[transportType];
        } 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) {
        return (transportType == TRANSPORT_WIFI)
                ? R.drawable.stat_notify_wifi_in_range :  // TODO: Distinguish ! from ?.
                R.drawable.stat_notify_rssi_in_range;
                ? R.drawable.stat_notify_wifi_in_range  // TODO: Distinguish ! from ?.
                : R.drawable.stat_notify_rssi_in_range;
    }

    /**
@@ -194,10 +197,10 @@ public class NetworkNotificationManager {
                    tag, nameOf(eventId), getTransportName(transportType), name, highPriority));
        }

        Resources r = mContext.getResources();
        final Resources r = mResources;
        final CharSequence title;
        final CharSequence details;
        int icon = getIcon(transportType);
        Icon icon = Icon.createWithResource(r, getIcon(transportType));
        if (notifyType == NotificationType.NO_INTERNET && transportType == TRANSPORT_WIFI) {
            title = r.getString(R.string.wifi_no_internet, name);
            details = r.getString(R.string.wifi_no_internet_detailed);
@@ -272,8 +275,7 @@ public class NetworkNotificationManager {
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .setTicker(title)
                .setColor(mContext.getColor(
                        com.android.internal.R.color.system_notification_accent_color))
                .setColor(mContext.getColor(android.R.color.system_notification_accent_color))
                .setContentTitle(title)
                .setContentIntent(intent)
                .setLocalOnly(true)
@@ -353,7 +355,7 @@ public class NetworkNotificationManager {
    public void showToast(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
        String fromTransport = getTransportName(approximateTransportType(fromNai));
        String toTransport = getTransportName(approximateTransportType(toNai));
        String text = mContext.getResources().getString(
        String text = mResources.getString(
                R.string.network_switch_metered_toast, fromTransport, toTransport);
        Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
    }
+9 −0
Original line number Diff line number Diff line
@@ -274,6 +274,7 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.connectivity.resources.R;
import com.android.internal.net.VpnConfig;
import com.android.internal.net.VpnProfile;
import com.android.internal.util.ArrayUtils;
@@ -1571,6 +1572,14 @@ public class ConnectivityServiceTest {
        doReturn(com.android.connectivity.resources.R.array.config_networkSupportedKeepaliveCount)
                .when(mResources).getIdentifier(eq("config_networkSupportedKeepaliveCount"),
                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);
        doReturn(mResources).when(connRes).get();
        doReturn(connRes).when(deps).getResources(any());
+17 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityResources;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.UserHandle;
@@ -44,9 +45,10 @@ import android.telephony.TelephonyManager;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.internal.R;
import com.android.connectivity.resources.R;
import com.android.server.connectivity.NetworkNotificationManager.NotificationType;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -120,11 +122,25 @@ public class NetworkNotificationManagerTest {
        when(mCtx.getSystemService(eq(Context.NOTIFICATION_SERVICE)))
                .thenReturn(mNotificationManager);
        when(mNetworkInfo.getExtraInfo()).thenReturn(TEST_EXTRA_INFO);
        ConnectivityResources.setResourcesContextForTest(mCtx);
        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);
    }

    @After
    public void tearDown() {
        ConnectivityResources.setResourcesContextForTest(null);
    }

    private void verifyTitleByNetwork(final int id, final NetworkAgentInfo nai, final int title) {
        final String tag = NetworkNotificationManager.tagFor(id);
        mManager.showNotification(id, PRIVATE_DNS_BROKEN, nai, null, null, true);