Refactor CdmaApnPreference

Bug: 114749736
Test: RunSettingsRoboTests
Change-Id: I3d0203b7b5fb732e2814fb741761af4371924e9a
This commit is contained in:
jackqdyulei
2018-10-11 16:54:14 -07:00
parent 37f2e60b8c
commit 2de3d28138
7 changed files with 221 additions and 70 deletions

View File

@@ -17,17 +17,6 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--We want separate APN setting from reset of settings because-->
<!--we want user to change it with caution.-->
<PreferenceCategory
android:key="category_cdma_apn_key">
<!-- The launching Intent will be defined thru code as we need to pass some Extra -->
<com.android.settingslib.RestrictedPreference
android:key="button_cdma_apn_key"
android:title="@string/apn_settings"
android:persistent="false"/>
</PreferenceCategory>
<Preference
android:key="carrier_settings_key"
android:title="@string/carrier_settings_title">

View File

@@ -109,4 +109,14 @@
android:dialogTitle="@string/cdma_subscription_dialogtitle"
settings:controller="com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController"/>
<!--We want separate APN setting from reset of settings because we want user to change it with caution-->
<PreferenceCategory
android:key="category_cdma_apn_key">
<com.android.settingslib.RestrictedPreference
android:key="button_cdma_apn_key"
android:title="@string/apn_settings"
android:persistent="false"
settings:controller="com.android.settings.network.telephony.cdma.CdmaApnPreferenceController"/>
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -16,25 +16,15 @@
package com.android.settings.network.telephony;
import android.content.Intent;
import android.os.PersistableBundle;
import android.os.SystemProperties;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;
/**
* List of Phone-specific settings screens.
@@ -43,21 +33,18 @@ public class CdmaOptions {
private static final String LOG_TAG = "CdmaOptions";
private CarrierConfigManager mCarrierConfigManager;
private RestrictedPreference mButtonAPNExpand;
private Preference mCategoryAPNExpand;
private Preference mButtonCarrierSettings;
private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
private static final String BUTTON_CDMA_SUBSCRIPTION_KEY = "cdma_subscription_key";
private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
private static final String BUTTON_APN_EXPAND_KEY = "button_cdma_apn_key";
private static final String CATEGORY_APN_EXPAND_KEY = "category_cdma_apn_key";
private PreferenceFragmentCompat mPrefFragment;
private PreferenceScreen mPrefScreen;
private int mSubId;
public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen, int subId) {
public CdmaOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen,
int subId) {
mPrefFragment = prefFragment;
mPrefScreen = prefScreen;
mPrefFragment.addPreferencesFromResource(R.xml.cdma_options);
@@ -65,20 +52,14 @@ public class CdmaOptions {
// Initialize preferences.
mButtonCarrierSettings = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY);
mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY);
updateSubscriptionId(subId);
}
protected void updateSubscriptionId(int subId) {
mSubId = subId;
int phoneType = TelephonyManager.from(mPrefFragment.getContext())
.createForSubscriptionId(mSubId).getPhoneType();
PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
// Some CDMA carriers want the APN settings.
boolean addAPNExpand = shouldAddApnExpandPreference(phoneType, carrierConfig);
// Read platform settings for carrier settings
boolean addCarrierSettings =
carrierConfig.getBoolean(CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
@@ -86,34 +67,6 @@ public class CdmaOptions {
// Making no assumptions of whether they are added or removed at this point.
// Calling add or remove explicitly to make sure they are updated.
if (addAPNExpand) {
log("update: addAPNExpand");
mButtonAPNExpand.setDisabledByAdmin(
MobileNetworkUtils.isDpcApnEnforced(mButtonAPNExpand.getContext())
? RestrictedLockUtilsInternal.getDeviceOwner(
mButtonAPNExpand.getContext())
: null);
mButtonAPNExpand.setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
MetricsLogger.action(mButtonAPNExpand.getContext(),
MetricsEvent.ACTION_MOBILE_NETWORK_APN_SETTINGS);
// We need to build the Intent by hand as the Preference Framework
// does not allow to add an Intent with some extras into a Preference
// XML file
final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
// This will setup the Home and Search affordance
intent.putExtra(":settings:show_fragment_as_subsetting", true);
intent.putExtra("sub_id", mSubId);
mPrefFragment.startActivity(intent);
return true;
}
});
mPrefScreen.addPreference(mCategoryAPNExpand);
} else {
mPrefScreen.removePreference(mCategoryAPNExpand);
}
if (addCarrierSettings) {
mPrefScreen.addPreference(mButtonCarrierSettings);
@@ -122,16 +75,6 @@ public class CdmaOptions {
}
}
/**
* Return whether we should add the APN expandable preference based on the phone type and
* carrier config
*/
@VisibleForTesting
public static boolean shouldAddApnExpandPreference(int phoneType, PersistableBundle config) {
return phoneType == PhoneConstants.PHONE_TYPE_CDMA
&& config.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
}
public boolean preferenceTreeClick(Preference preference) {
if (preference.getKey().equals(BUTTON_CDMA_SYSTEM_SELECT_KEY)) {
log("preferenceTreeClick: return BUTTON_CDMA_ROAMING_KEY true");

View File

@@ -66,6 +66,7 @@ import com.android.internal.telephony.PhoneConstants;
import com.android.internal.telephony.TelephonyIntents;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.network.telephony.cdma.CdmaApnPreferenceController;
import com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController;
import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -399,6 +400,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
use(CdmaApnPreferenceController.class).init(mSubId);
mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network.telephony.cdma;
import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.SettingsActivity;
import com.android.settings.network.ApnSettings;
import com.android.settings.network.telephony.MobileNetworkUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;
/**
* Preference controller for "CDMA Apn"
*/
public class CdmaApnPreferenceController extends CdmaBasePreferenceController {
private static final String CATEGORY_KEY = "category_cdma_apn_key";
@VisibleForTesting
CarrierConfigManager mCarrierConfigManager;
public CdmaApnPreferenceController(Context context, String key) {
super(context, key);
mCarrierConfigManager = new CarrierConfigManager(context);
}
@Override
public int getAvailabilityStatus() {
final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
return carrierConfig != null
&& carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL)
&& MobileNetworkUtils.isCdmaOptions(mContext, mSubId)
? AVAILABLE
: CONDITIONALLY_UNAVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (isAvailable()) {
((RestrictedPreference) mPreference).setDisabledByAdmin(
MobileNetworkUtils.isDpcApnEnforced(mContext)
? RestrictedLockUtilsInternal.getDeviceOwner(mContext)
: null);
} else {
screen.findPreference(CATEGORY_KEY).setVisible(false);
}
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (getPreferenceKey().equals(preference.getKey())) {
// This activity runs in phone process, we must use intent to start
final Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
// This will setup the Home and Search affordance
intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SUBSETTING, true);
intent.putExtra(ApnSettings.SUB_ID, mSubId);
mContext.startActivity(intent);
return true;
}
return false;
}
}

View File

@@ -74,6 +74,10 @@ public abstract class CdmaBasePreferenceController extends BasePreferenceControl
mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId);
}
public void init(int subId) {
init(null, subId);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);

View File

@@ -0,0 +1,116 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network.telephony.cdma;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.network.ApnSettings;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class CdmaApnPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private CdmaApnPreferenceController mController;
private RestrictedPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mPreference = new RestrictedPreference(mContext);
mController = new CdmaApnPreferenceController(mContext, "mobile_data");
mController.init(SUB_ID);
mController.mPreference = mPreference;
mController.mCarrierConfigManager = mCarrierConfigManager;
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_apnSettingsNotSupported_returnUnavailable() {
doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, false);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_apnSettingsSupportedWithCDMA_returnAvailable() {
doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, true);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void handPreferenceTreeClick_fireIntent() {
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext).startActivity(captor.capture());
final Intent intent = captor.getValue();
assertThat(intent.getAction()).isEqualTo(Settings.ACTION_APN_SETTINGS);
assertThat(intent.getIntExtra(ApnSettings.SUB_ID, 0)).isEqualTo(SUB_ID);
}
}