update CdmaApnPreferenceController

Rename it to ApnPreferenceController since it support both Gsm and
Cdma. Also add check for Gsm sim card.

Bug: 118402844
Test: RunSettingsRoboTests
Change-Id: Iafe111bfdc8f8e7d91d104df652ae9b27c9b166f
This commit is contained in:
jackqdyulei
2018-10-24 18:22:04 -07:00
parent 762f75f150
commit 4c0b300e21
6 changed files with 168 additions and 266 deletions

View File

@@ -117,16 +117,6 @@
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>
<PreferenceCategory
android:key="network_operators_category_key"
android:title="@string/network_operator_category"
@@ -144,6 +134,14 @@
settings:controller="com.android.settings.network.telephony.gsm.OpenNetworkSelectPagePreferenceController"/>
</PreferenceCategory>
<!--We want separate APN setting from reset of settings because we want user to change it with caution-->
<com.android.settingslib.RestrictedPreference
android:key="telephony_apn_key"
android:persistent="false"
android:title="@string/apn_settings"
settings:allowDividerAbove="true"
settings:controller="com.android.settings.network.telephony.ApnPreferenceController"/>
<Preference
android:key="carrier_settings_key"
android:title="@string/carrier_settings_title"

View File

@@ -0,0 +1,143 @@
/*
* 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;
import static android.provider.Telephony.Carriers.ENFORCE_MANAGED_URI;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.Looper;
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.core.BasePreferenceController;
import com.android.settings.network.ApnSettings;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* Preference controller for "Apn settings"
*/
public class ApnPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop {
@VisibleForTesting
CarrierConfigManager mCarrierConfigManager;
private int mSubId;
private Preference mPreference;
private DpcApnEnforcedObserver mDpcApnEnforcedObserver;
public ApnPreferenceController(Context context, String key) {
super(context, key);
mCarrierConfigManager = new CarrierConfigManager(context);
mDpcApnEnforcedObserver = new DpcApnEnforcedObserver(new Handler(Looper.getMainLooper()));
}
@Override
public int getAvailabilityStatus() {
final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
final boolean isCdmaApn = MobileNetworkUtils.isCdmaOptions(mContext, mSubId)
&& carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL);
final boolean isGsmApn = MobileNetworkUtils.isGsmOptions(mContext, mSubId)
&& carrierConfig.getBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL);
return carrierConfig != null
&& (isCdmaApn || isGsmApn)
? AVAILABLE
: CONDITIONALLY_UNAVAILABLE;
}
@Override
public void onStart() {
mDpcApnEnforcedObserver.register(mContext);
}
@Override
public void onStop() {
mDpcApnEnforcedObserver.unRegister(mContext);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
((RestrictedPreference) mPreference).setDisabledByAdmin(
MobileNetworkUtils.isDpcApnEnforced(mContext)
? RestrictedLockUtilsInternal.getDeviceOwner(mContext)
: null);
}
@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;
}
public void init(int subId) {
mSubId = subId;
}
@VisibleForTesting
void setPreference(Preference preference) {
mPreference = preference;
}
private class DpcApnEnforcedObserver extends ContentObserver {
DpcApnEnforcedObserver(Handler handler) {
super(handler);
}
public void register(Context context) {
context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false, this);
}
public void unRegister(Context context) {
context.getContentResolver().unregisterContentObserver(this);
}
@Override
public void onChange(boolean selfChange) {
updateState(mPreference);
}
}
}

View File

@@ -1,151 +0,0 @@
/*
* 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;
import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.TelephonyManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.R;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference;
/**
* List of Network-specific settings screens.
*/
public class GsmUmtsOptions {
private static final String LOG_TAG = "GsmUmtsOptions";
private CarrierConfigManager mCarrierConfigManager;
private RestrictedPreference mButtonAPNExpand;
private Preference mCategoryAPNExpand;
Preference mCarrierSettingPref;
private static final String BUTTON_APN_EXPAND_KEY = "button_gsm_apn_key";
private static final String CATEGORY_APN_EXPAND_KEY = "category_gsm_apn_key";
private static final String BUTTON_CARRIER_SETTINGS_KEY = "carrier_settings_key";
public static final String EXTRA_SUB_ID = "sub_id";
private PreferenceFragmentCompat mPrefFragment;
private PreferenceScreen mPrefScreen;
public GsmUmtsOptions(PreferenceFragmentCompat prefFragment, PreferenceScreen prefScreen,
final int subId) {
final Context context = prefFragment.getContext();
mPrefFragment = prefFragment;
mPrefScreen = prefScreen;
mCarrierConfigManager = new CarrierConfigManager(context);
mPrefFragment.addPreferencesFromResource(R.xml.gsm_umts_options);
mButtonAPNExpand = (RestrictedPreference) mPrefScreen.findPreference(BUTTON_APN_EXPAND_KEY);
mCategoryAPNExpand = mPrefScreen.findPreference(CATEGORY_APN_EXPAND_KEY);
mCarrierSettingPref = mPrefScreen.findPreference(BUTTON_CARRIER_SETTINGS_KEY);
update(subId);
}
// Unlike mPrefFragment or mPrefScreen, subId may change during lifecycle of GsmUmtsOptions.
// When that happens, we update GsmUmtsOptions with new parameters.
protected void update(final int subId) {
boolean addAPNExpand = true;
boolean addCarrierSettings = true;
final TelephonyManager telephonyManager = TelephonyManager.from(mPrefFragment.getContext())
.createForSubscriptionId(subId);
//TODO(b/115429509): Get phone from subId
Phone phone = null;
if (phone == null) return;
if (telephonyManager.getPhoneType() != PhoneConstants.PHONE_TYPE_GSM) {
log("Not a GSM phone");
addAPNExpand = false;
} else {
log("Not a CDMA phone");
PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId);
// Determine which options to display. For GSM these are defaulted to true in
// CarrierConfigManager, but they maybe overriden by DefaultCarrierConfigService or a
// carrier app.
// Note: these settings used to be controlled with overlays in
// Telephony/res/values/config.xml
if (!carrierConfig.getBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL)
&& mCategoryAPNExpand != null) {
addAPNExpand = false;
}
// Read platform settings for carrier settings
addCarrierSettings = carrierConfig.getBoolean(
CarrierConfigManager.KEY_CARRIER_SETTINGS_ENABLE_BOOL);
}
// 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(EXTRA_SUB_ID, subId);
mPrefFragment.startActivity(intent);
return true;
}
});
mPrefScreen.addPreference(mCategoryAPNExpand);
} else {
mPrefScreen.removePreference(mCategoryAPNExpand);
}
if (addCarrierSettings) {
mPrefScreen.addPreference(mCarrierSettingPref);
} else {
mPrefScreen.removePreference(mCarrierSettingPref);
}
}
protected boolean preferenceTreeClick(Preference preference) {
return false;
}
protected void log(String s) {
android.util.Log.d(LOG_TAG, s);
}
}

View File

@@ -56,7 +56,6 @@ 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.network.telephony.gsm.AutoSelectPreferenceController;
@@ -128,9 +127,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
private ImsManager mImsMgr;
private boolean mOkClicked;
//GsmUmts options and Cdma options
GsmUmtsOptions mGsmUmtsOptions;
private String mClickedPrefKey;
private boolean mShow4GForLTE;
private boolean mIsGlobalCdma;
@@ -159,9 +155,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
/** TODO: Refactor and get rid of the if's using subclasses */
if (preference.getKey().equals(BUTTON_4G_LTE_KEY)) {
return true;
} else if (mGsmUmtsOptions != null &&
mGsmUmtsOptions.preferenceTreeClick(preference) == true) {
return true;
} else if (TextUtils.equals(key, BUTTON_CDMA_SYSTEM_SELECT_KEY)
|| TextUtils.equals(key, BUTTON_CDMA_SUBSCRIPTION_KEY)) {
if (mTelephonyManager.getEmergencyCallbackMode()) {
@@ -213,7 +206,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
use(RoamingPreferenceController.class).init(getFragmentManager(), mSubId);
use(CdmaApnPreferenceController.class).init(mSubId);
use(ApnPreferenceController.class).init(mSubId);
use(CarrierPreferenceController.class).init(mSubId);
use(DataUsagePreferenceController.class).init(mSubId);
use(PreferredNetworkModePreferenceController.class).init(mSubId);
@@ -693,11 +686,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
// We don't want to re-create GsmUmtsOptions if already exists. Otherwise, the
// preferences inside it will also be re-created which causes unexpected behavior.
// For example, the open dialog gets dismissed or detached after pause / resume.
if (mGsmUmtsOptions == null) {
mGsmUmtsOptions = new GsmUmtsOptions(prefFragment, prefScreen, subId);
} else {
mGsmUmtsOptions.update(subId);
}
}
private static Intent buildPhoneAccountConfigureIntent(

View File

@@ -1,87 +0,0 @@
/*
* 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

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.android.settings.network.telephony.cdma;
package com.android.settings.network.telephony;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
@@ -47,7 +47,7 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class CdmaApnPreferenceControllerTest {
public class ApnPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
@@ -59,7 +59,7 @@ public class CdmaApnPreferenceControllerTest {
@Mock
private CarrierConfigManager mCarrierConfigManager;
private CdmaApnPreferenceController mController;
private ApnPreferenceController mController;
private RestrictedPreference mPreference;
private Context mContext;
@@ -73,11 +73,12 @@ public class CdmaApnPreferenceControllerTest {
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
mPreference = new RestrictedPreference(mContext);
mController = new CdmaApnPreferenceController(mContext, "mobile_data");
mController = new ApnPreferenceController(mContext, "mobile_data");
mController.init(SUB_ID);
mController.mPreference = mPreference;
mController.setPreference(mPreference);
mController.mCarrierConfigManager = mCarrierConfigManager;
mPreference.setKey(mController.getPreferenceKey());
}
@@ -102,6 +103,16 @@ public class CdmaApnPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_apnSettingsSupportedWithGsm_returnAvailable() {
doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_APN_EXPAND_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);