Refactor DataServicePreference

Bug: 114749736
Test: RunSettingsRoboTests
Change-Id: I773aeabdc1964436cc1e67bbbe9ad7fa6494e504
This commit is contained in:
jackqdyulei
2018-10-18 14:48:48 -07:00
parent 502f3c6c91
commit fd44f76bf7
4 changed files with 235 additions and 36 deletions

View File

@@ -20,10 +20,11 @@
android:title="@string/network_settings_title"
settings:initialExpandedChildrenCount="4">
<PreferenceScreen
<Preference
android:key="cdma_lte_data_service_key"
android:title="@string/cdma_lte_data_service">
</PreferenceScreen>
android:title="@string/cdma_lte_data_service"
settings:controller="com.android.settings.network.telephony.DataServiceSetupPreferenceController">
</Preference>
<SwitchPreference
android:key="mobile_data_enable"

View File

@@ -0,0 +1,91 @@
/*
* 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.net.Uri;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.core.BasePreferenceController;
/**
* Preference controller for "Data service setup"
*/
public class DataServiceSetupPreferenceController extends BasePreferenceController {
private CarrierConfigManager mCarrierConfigManager;
private TelephonyManager mTelephonyManager;
private PersistableBundle mCarrierConfig;
private String mSetupUrl;
private int mSubId;
public DataServiceSetupPreferenceController(Context context, String key) {
super(context, key);
mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
mTelephonyManager = context.getSystemService(TelephonyManager.class);
mSetupUrl = Settings.Global.getString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL);
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
}
@Override
public int getAvailabilityStatus() {
final boolean isLteOnCdma = mTelephonyManager.getLteOnCdmaMode()
== PhoneConstants.LTE_ON_CDMA_TRUE;
return mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID
&& mCarrierConfig != null
&& !mCarrierConfig.getBoolean(
CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)
&& isLteOnCdma && !TextUtils.isEmpty(mSetupUrl)
? AVAILABLE
: CONDITIONALLY_UNAVAILABLE;
}
public void init(int subId) {
mSubId = subId;
mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId);
mCarrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (getPreferenceKey().equals(preference.getKey())) {
if (!TextUtils.isEmpty(mSetupUrl)) {
String imsi = mTelephonyManager.getSubscriberId();
if (imsi == null) {
imsi = "";
}
final String url = TextUtils.expandTemplate(mSetupUrl, imsi).toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
mContext.startActivity(intent);
}
return true;
}
return false;
}
}

View File

@@ -122,7 +122,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
//UI objects
private SwitchPreference mButton4glte;
private Preference mLteDataServicePref;
private Preference mEuiccSettingsPref;
private PreferenceCategory mCallingCategory;
private Preference mWiFiCallingPref;
@@ -223,23 +222,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
REQUEST_CODE_EXIT_ECM);
}
return true;
} else if (preference == mLteDataServicePref) {
String tmpl = android.provider.Settings.Global.getString(
getContext().getContentResolver(),
android.provider.Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL);
if (!TextUtils.isEmpty(tmpl)) {
String imsi = mTelephonyManager.getSubscriberId();
if (imsi == null) {
imsi = "";
}
final String url = TextUtils.isEmpty(tmpl) ? null
: TextUtils.expandTemplate(tmpl, imsi).toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} else {
android.util.Log.e(LOG_TAG, "Missing SETUP_PREPAID_DATA_SERVICE_URL");
}
return true;
} else if (preference == mEuiccSettingsPref) {
Intent intent = new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
startActivity(intent);
@@ -296,6 +278,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
use(PreferredNetworkModePreferenceController.class).init(mSubId);
use(EnabledNetworkModePreferenceController.class).init(mSubId);
use(Enhanced4gLtePreferenceController.class).init(mSubId);
use(DataServiceSetupPreferenceController.class).init(mSubId);
mCdmaSystemSelectPreferenceController = use(CdmaSystemSelectPreferenceController.class);
mCdmaSystemSelectPreferenceController.init(getPreferenceManager(), mSubId);
@@ -339,8 +322,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
//get UI object references
PreferenceScreen prefSet = getPreferenceScreen();
mLteDataServicePref = prefSet.findPreference(BUTTON_CDMA_LTE_DATA_SERVICE_KEY);
mEuiccSettingsPref = prefSet.findPreference(BUTTON_CARRIER_SETTINGS_EUICC_KEY);
mEuiccSettingsPref.setOnPreferenceChangeListener(this);
@@ -507,7 +488,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
&& carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_CDMA_CHOICES_BOOL);
if (carrierConfig.getBoolean(
CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL)) {
prefSet.removePreference(mLteDataServicePref);
} else if (carrierConfig.getBoolean(CarrierConfigManager
.KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL)
&& !mTelephonyManager.getServiceState().getRoaming()
@@ -526,8 +506,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
// in case it is currently something else. That is possible if user
// changed the setting while roaming and is now back to home network.
settingsNetworkMode = preferredNetworkMode;
} else if (carrierConfig.getBoolean(
CarrierConfigManager.KEY_WORLD_PHONE_BOOL) == true) {
} else if (carrierConfig.getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL)) {
// set the listener for the mButtonPreferredNetworkMode list preference so we can issue
// change Preferred Network Mode.
@@ -538,11 +517,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
final boolean missingDataServiceUrl = TextUtils.isEmpty(
android.provider.Settings.Global.getString(activity.getContentResolver(),
android.provider.Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL));
if (!isLteOnCdma || missingDataServiceUrl) {
prefSet.removePreference(mLteDataServicePref);
} else {
android.util.Log.d(LOG_TAG, "keep ltePref");
}
updatePreferredNetworkType();
updateCallingCategory();
@@ -561,7 +535,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
mOnlyAutoSelectInHomeNW = carrierConfig.getBoolean(
CarrierConfigManager.KEY_ONLY_AUTO_SELECT_IN_HOME_NETWORK_BOOL);
mLteDataServicePref.setEnabled(hasActiveSubscriptions);
Preference ps;
ps = findPreference(BUTTON_CELL_BROADCAST_SETTINGS);
if (ps != null) {
@@ -907,8 +880,7 @@ public class MobileNetworkFragment extends DashboardFragment implements
// For ListPreferences, we log it here without a value, only indicating it's clicked to
// open the list dialog. When a value is chosen, another MetricsEvent is logged with
// new value in onPreferenceChange.
if (preference == mLteDataServicePref
|| preference == mEuiccSettingsPref
if (preference == mEuiccSettingsPref
|| preference == mWiFiCallingPref
|| preference == preferenceScreen.findPreference(BUTTON_CDMA_SYSTEM_SELECT_KEY)
|| preference == preferenceScreen.findPreference(BUTTON_CDMA_SUBSCRIPTION_KEY)
@@ -943,8 +915,6 @@ public class MobileNetworkFragment extends DashboardFragment implements
if (preference == null) {
return MetricsProto.MetricsEvent.VIEW_UNKNOWN;
} else if (preference == mLteDataServicePref) {
return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_SET_UP_DATA_SERVICE;
} else if (preference == mEuiccSettingsPref) {
return MetricsProto.MetricsEvent.ACTION_MOBILE_NETWORK_EUICC_SETTING;
} else if (preference == mWiFiCallingPref) {

View File

@@ -0,0 +1,137 @@
/*
* 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 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.net.Uri;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import androidx.preference.Preference;
import com.android.internal.telephony.PhoneConstants;
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 DataServiceSetupPreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String SETUP_URL = "url://tmp_url:^1";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private PersistableBundle mCarrierConfig;
private DataServiceSetupPreferenceController mController;
private Preference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, SETUP_URL);
mCarrierConfig = new PersistableBundle();
doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
mPreference = new RestrictedPreference(mContext);
mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_allConfigOn_returnAvailable() {
doReturn(PhoneConstants.LTE_ON_CDMA_TRUE).when(mTelephonyManager).getLteOnCdmaMode();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_missUrl_returnUnavailable() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, "");
doReturn(PhoneConstants.LTE_ON_CDMA_TRUE).when(mTelephonyManager).getLteOnCdmaMode();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
mController.init(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_notCdma_returnUnavailable() {
doReturn(PhoneConstants.LTE_ON_CDMA_FALSE).when(mTelephonyManager).getLteOnCdmaMode();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void handlePreferenceTreeClick_startActivity() {
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext).startActivity(captor.capture());
final Intent intent = captor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
assertThat(intent.getData()).isEqualTo(
Uri.parse(TextUtils.expandTemplate(SETUP_URL, "").toString()));
}
}