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

Commit f5f9b78a authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Reduce the calling times of getCustomizedContextOrDefault()" am:...

Merge "Reduce the calling times of getCustomizedContextOrDefault()" am: f729bd21 am: b3808db0 am: 955651c4 am: feb08079 am: f83ba154

Original change: https://android-review.googlesource.com/c/platform/packages/modules/NetworkStack/+/1335695

Change-Id: I579163dba0a4d438474a9e230ef8133c2d12d7ab
parents bb1ae6c2 f83ba154
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -411,6 +411,7 @@ public class NetworkMonitor extends StateMachine {
    private String mPrivateDnsProviderHostname = "";

    private final Context mContext;
    private final Context mCustomizedContext;
    private final INetworkMonitorCallbacks mCallback;
    private final int mCallbackVersion;
    private final Network mCleartextDnsNetwork;
@@ -548,6 +549,7 @@ public class NetworkMonitor extends StateMachine {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        mNotifier = serviceManager.getNotifier();
        mCustomizedContext = getCustomizedContextOrDefault();

        // CHECKSTYLE:OFF IndentationCheck
        addState(mDefaultState);
@@ -1824,10 +1826,10 @@ public class NetworkMonitor extends StateMachine {
    private String getCaptivePortalServerHttpsUrl() {
        final String testUrl = getTestUrl(TEST_CAPTIVE_PORTAL_HTTPS_URL);
        if (isValidTestUrl(testUrl)) return testUrl;
        final Context targetContext = getCustomizedContextOrDefault();
        return getSettingFromResource(targetContext,
        return getSettingFromResource(mCustomizedContext,
                R.string.config_captive_portal_https_url, CAPTIVE_PORTAL_HTTPS_URL,
                targetContext.getResources().getString(R.string.default_captive_portal_https_url));
                mCustomizedContext.getResources().getString(
                R.string.default_captive_portal_https_url));
    }

    private static boolean isValidTestUrl(@Nullable String url) {
@@ -1917,10 +1919,10 @@ public class NetworkMonitor extends StateMachine {
    public String getCaptivePortalServerHttpUrl() {
        final String testUrl = getTestUrl(TEST_CAPTIVE_PORTAL_HTTP_URL);
        if (isValidTestUrl(testUrl)) return testUrl;
        final Context targetContext = getCustomizedContextOrDefault();
        return getSettingFromResource(targetContext,
        return getSettingFromResource(mCustomizedContext,
                R.string.config_captive_portal_http_url, CAPTIVE_PORTAL_HTTP_URL,
                targetContext.getResources().getString(R.string.default_captive_portal_http_url));
                mCustomizedContext.getResources().getString(
                R.string.default_captive_portal_http_url));
    }

    private int getConsecutiveDnsTimeoutThreshold() {
@@ -2078,7 +2080,7 @@ public class NetworkMonitor extends StateMachine {
     */
    private <T> T[] getProbeUrlArrayConfig(@NonNull T[] providerValue, @ArrayRes int configResId,
            @ArrayRes int defaultResId, @NonNull Function<String, T> resourceConverter) {
        final Resources res = getCustomizedContextOrDefault().getResources();
        final Resources res = mCustomizedContext.getResources();
        return getProbeUrlArrayConfig(providerValue, configResId, res.getStringArray(defaultResId),
                resourceConverter);
    }
@@ -2096,7 +2098,7 @@ public class NetworkMonitor extends StateMachine {
     */
    private <T> T[] getProbeUrlArrayConfig(@NonNull T[] providerValue, @ArrayRes int configResId,
            String[] defaultConfig, @NonNull Function<String, T> resourceConverter) {
        final Resources res = getCustomizedContextOrDefault().getResources();
        final Resources res = mCustomizedContext.getResources();
        String[] configValue = res.getStringArray(configResId);

        if (configValue.length == 0) {
+30 −11
Original line number Diff line number Diff line
@@ -927,6 +927,18 @@ public class NetworkMonitorTest {
        return info;
    }

    private void setupNoSimCardNeighborMcc() throws Exception {
        // Enable using neighbor resource by camping mcc feature.
        doReturn(true).when(mResources).getBoolean(R.bool.config_no_sim_card_uses_neighbor_mcc);
        final List<CellInfo> cellList = new ArrayList<CellInfo>();
        final int testMcc = 460;
        cellList.add(makeTestCellInfoGsm(Integer.toString(testMcc)));
        doReturn(cellList).when(mTelephony).getAllCellInfo();
        final Configuration config = mResources.getConfiguration();
        config.mcc = testMcc;
        doReturn(mMccContext).when(mContext).createConfigurationContext(eq(config));
    }

    @Test
    public void testMakeFallbackUrls() throws Exception {
        final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor();
@@ -950,21 +962,28 @@ public class NetworkMonitorTest {
        assertEquals("http://testUrl1.com", urls[0].toString());
        assertEquals("http://testUrl2.com", urls[1].toString());

        // Value is expected to be replaced by location resource.
        doReturn(true).when(mResources).getBoolean(R.bool.config_no_sim_card_uses_neighbor_mcc);

        final List<CellInfo> cellList = new ArrayList<CellInfo>();
        final int testMcc = 460;
        cellList.add(makeTestCellInfoGsm(Integer.toString(testMcc)));
        doReturn(cellList).when(mTelephony).getAllCellInfo();
        final Configuration config = mResources.getConfiguration();
        config.mcc = testMcc;
        doReturn(mMccContext).when(mContext).createConfigurationContext(eq(config));
        // Even though the using neighbor resource by camping mcc feature is enabled, the
        // customized context has been assigned and won't change. So calling
        // makeCaptivePortalFallbackUrls() still gets the original value.
        setupNoSimCardNeighborMcc();
        doReturn(new String[] {"http://testUrl3.com"}).when(mMccResource)
                .getStringArray(R.array.config_captive_portal_fallback_urls);
        urls = wnm.makeCaptivePortalFallbackUrls();
        assertEquals(urls.length, 2);
        assertEquals("http://testUrl1.com", urls[0].toString());
        assertEquals("http://testUrl2.com", urls[1].toString());
    }

    @Test
    public void testMakeFallbackUrlsWithCustomizedContext() throws Exception {
        // Value is expected to be replaced by location resource.
        setupNoSimCardNeighborMcc();
        doReturn(new String[] {"http://testUrl.com"}).when(mMccResource)
                .getStringArray(R.array.config_captive_portal_fallback_urls);
        final WrappedNetworkMonitor wnm = makeCellNotMeteredNetworkMonitor();
        final URL[] urls = wnm.makeCaptivePortalFallbackUrls();
        assertEquals(urls.length, 1);
        assertEquals("http://testUrl3.com", urls[0].toString());
        assertEquals("http://testUrl.com", urls[0].toString());
    }

    private static CellIdentityGsm makeCellIdentityGsm(int lac, int cid, int arfcn, int bsic,