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

Commit 6da43028 authored by Pengquan Meng's avatar Pengquan Meng
Browse files

Fix CarrierSettings preference

This used the CarrierConfigManager to get the CarrierSettings activity
component and open that activity when the preference is clicked.

Bug: 117651939
Test: make -j40 ROBOTEST_FILTER=telephony RunSettingsRoboTests
Change-Id: Ic5267bba18ab32fbcc2e04aa61e7361151130275
parent 408dc883
Loading
Loading
Loading
Loading
+26 −1
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@
package com.android.settings.network.telephony;

import android.content.Context;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
@@ -62,10 +66,31 @@ public class CarrierPreferenceController extends BasePreferenceController {
    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (getPreferenceKey().equals(preference.getKey())) {
            //TODO(b/117651939): start carrier settings activity
            final Intent carrierSettingsIntent = getCarrierSettingsActivityIntent(mSubId);
            if (carrierSettingsIntent != null) {
                mContext.startActivity(carrierSettingsIntent);
            }
            return true;
        }

        return false;
    }

    private Intent getCarrierSettingsActivityIntent(int subId) {
        final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId);
        final ComponentName cn = ComponentName.unflattenFromString(
                config == null ? "" : config.getString(
                        CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING,
                        "" /* default value */));

        if (cn == null) return null;

        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        final PackageManager pm = mContext.getPackageManager();
        final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0 /* flags */);
        return resolveInfo != null ? intent : null;
    }
}
+62 −0
Original line number Diff line number Diff line
@@ -21,10 +21,18 @@ import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_U

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
@@ -38,12 +46,15 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.ArgumentCaptor;
import org.robolectric.RuntimeEnvironment;

@RunWith(SettingsRobolectricTestRunner.class)
public class CarrierPreferenceControllerTest {
    private static final int SUB_ID = 2;
    private static final String CARRIER_SETTINGS_COMPONENT = "packageName/className";

    @Mock
    private TelephonyManager mTelephonyManager;
@@ -106,4 +117,55 @@ public class CarrierPreferenceControllerTest {

        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void handlePreferenceClick_activityFound_openCarrierSettingActivity() {
        final PersistableBundle bundle = new PersistableBundle();
        bundle.putString(
                CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING,
                CARRIER_SETTINGS_COMPONENT);
        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
        PackageManager pm = Mockito.mock(PackageManager.class);
        doReturn(pm).when(mContext).getPackageManager();
        doReturn(new ResolveInfo()).when(pm).resolveActivity(any(Intent.class), anyInt());

        mController.handlePreferenceTreeClick(mPreference);

        final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
        verify(mContext).startActivity(captor.capture());
        final Intent intent = captor.getValue();
        assertThat(intent.getComponent()).isEqualTo(
                ComponentName.unflattenFromString(CARRIER_SETTINGS_COMPONENT));
    }

    @Test
    public void handlePreferenceClick_activityNotFound_DoNothing() {
        final PersistableBundle bundle = new PersistableBundle();
        bundle.putString(
                CarrierConfigManager.KEY_CARRIER_SETTINGS_ACTIVITY_COMPONENT_NAME_STRING,
                CARRIER_SETTINGS_COMPONENT);
        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
        PackageManager pm = Mockito.mock(PackageManager.class);
        doReturn(pm).when(mContext).getPackageManager();
        doReturn(null).when(pm).resolveActivity(any(Intent.class), anyInt());

        mController.handlePreferenceTreeClick(mPreference);

        final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
        verify(mContext, never()).startActivity(captor.capture());
    }

    @Test
    public void handlePreferenceClick_activityNotConfigured_DoNothing() {
        final PersistableBundle bundle = new PersistableBundle();
        doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
        PackageManager pm = Mockito.mock(PackageManager.class);
        doReturn(pm).when(mContext).getPackageManager();
        doReturn(new ResolveInfo()).when(pm).resolveActivity(any(Intent.class), anyInt());

        mController.handlePreferenceTreeClick(mPreference);

        final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
        verify(mContext, never()).startActivity(captor.capture());
    }
}