[Settings] Apply the SettingsDataService to the SIM page, MobileDataPreferenceController
Bug: 257950125 Test: atest MobileDataPreferenceControllerTest Change-Id: Ia64a8baf931e9aa7f54cad2b2f5bb264420be9b0
This commit is contained in:
@@ -88,6 +88,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
|
||||
private Uri mAirplaneModeSettingUri;
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
private IntentFilter mFilter = new IntentFilter();
|
||||
private MobileDataContentObserver mDataContentObserver;
|
||||
|
||||
private int mPhysicalSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
|
||||
private int mLogicalSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX;
|
||||
@@ -125,6 +126,13 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
|
||||
mMobileNetworkInfoDao = mMobileNetworkDatabase.mMobileNetworkInfoDao();
|
||||
mAirplaneModeObserver = new AirplaneModeObserver(new Handler(Looper.getMainLooper()));
|
||||
mAirplaneModeSettingUri = Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON);
|
||||
mDataContentObserver = new MobileDataContentObserver(
|
||||
new Handler(Looper.getMainLooper()));
|
||||
mDataContentObserver.setOnMobileDataChangedListener(() -> {
|
||||
mExecutor.execute(() -> {
|
||||
insertMobileNetworkInfo(context);
|
||||
});
|
||||
});
|
||||
mFilter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
|
||||
mFilter.addAction(SubscriptionManager.ACTION_DEFAULT_SUBSCRIPTION_CHANGED);
|
||||
mFilter.addAction(ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED);
|
||||
@@ -163,6 +171,9 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
|
||||
public void addRegister(LifecycleOwner lifecycleOwner) {
|
||||
mSubscriptionManager.addOnSubscriptionsChangedListener(mContext.getMainExecutor(), this);
|
||||
mAirplaneModeObserver.register(mContext);
|
||||
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
mDataContentObserver.register(mContext, mSubId);
|
||||
}
|
||||
mContext.registerReceiver(mDataSubscriptionChangedReceiver, mFilter);
|
||||
observeAllSubInfo(lifecycleOwner);
|
||||
observeAllUiccInfo(lifecycleOwner);
|
||||
@@ -170,6 +181,9 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
|
||||
}
|
||||
|
||||
public void removeRegister() {
|
||||
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
mDataContentObserver.unRegister(mContext);
|
||||
}
|
||||
mAirplaneModeObserver.unRegister(mContext);
|
||||
if (mDataSubscriptionChangedReceiver != null) {
|
||||
mContext.unregisterReceiver(mDataSubscriptionChangedReceiver);
|
||||
@@ -404,7 +418,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions
|
||||
return new MobileNetworkInfoEntity(String.valueOf(mSubId),
|
||||
MobileNetworkUtils.isContactDiscoveryEnabled(context, mSubId),
|
||||
MobileNetworkUtils.isContactDiscoveryVisible(context, mSubId),
|
||||
MobileNetworkUtils.isMobileDataEnabled(context),
|
||||
mTelephonyManager.isDataEnabled(),
|
||||
MobileNetworkUtils.isCdmaOptions(context, mSubId),
|
||||
MobileNetworkUtils.isGsmOptions(context, mSubId),
|
||||
MobileNetworkUtils.isWorldMode(context, mSubId),
|
||||
|
@@ -41,6 +41,7 @@ import com.android.settings.network.telephony.MobileNetworkUtils;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.mobile.dataservice.DataServiceUtils;
|
||||
import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.UiccInfoEntity;
|
||||
@@ -181,10 +182,7 @@ public class NetworkProviderDownloadedSimListController extends
|
||||
|
||||
@Override
|
||||
public void onAvailableSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList) {
|
||||
if ((mSubInfoEntityList != null &&
|
||||
(subInfoEntityList.isEmpty() || !subInfoEntityList.equals(mSubInfoEntityList)))
|
||||
|| (!subInfoEntityList.isEmpty() && mSubInfoEntityList == null)) {
|
||||
Log.d(TAG, "subInfo list from framework is changed, update the subInfo entity list.");
|
||||
if (DataServiceUtils.shouldUpdateEntityList(mSubInfoEntityList, subInfoEntityList)) {
|
||||
mSubInfoEntityList = subInfoEntityList;
|
||||
mPreferenceCategory.setVisible(isAvailable());
|
||||
update();
|
||||
|
@@ -16,14 +16,19 @@
|
||||
|
||||
package com.android.settings.network.telephony;
|
||||
|
||||
import static androidx.lifecycle.Lifecycle.Event.ON_START;
|
||||
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.preference.Preference;
|
||||
@@ -32,16 +37,22 @@ import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.network.MobileDataContentObserver;
|
||||
import com.android.settings.network.MobileNetworkRepository;
|
||||
import com.android.settings.wifi.WifiPickerTrackerHelper;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.mobile.dataservice.DataServiceUtils;
|
||||
import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.UiccInfoEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Preference controller for "Mobile data"
|
||||
*/
|
||||
public class MobileDataPreferenceController extends TelephonyTogglePreferenceController
|
||||
implements LifecycleObserver, OnStart, OnStop {
|
||||
implements LifecycleObserver, MobileNetworkRepository.MobileNetworkCallback {
|
||||
|
||||
private static final String DIALOG_TAG = "MobileDataDialog";
|
||||
|
||||
@@ -56,12 +67,26 @@ public class MobileDataPreferenceController extends TelephonyTogglePreferenceCon
|
||||
boolean mNeedDialog;
|
||||
|
||||
private WifiPickerTrackerHelper mWifiPickerTrackerHelper;
|
||||
protected MobileNetworkRepository mMobileNetworkRepository;
|
||||
protected LifecycleOwner mLifecycleOwner;
|
||||
private List<SubscriptionInfoEntity> mSubscriptionInfoEntityList = new ArrayList<>();
|
||||
private List<MobileNetworkInfoEntity> mMobileNetworkInfoEntityList = new ArrayList<>();
|
||||
private int mDefaultSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
|
||||
SubscriptionInfoEntity mSubscriptionInfoEntity;
|
||||
MobileNetworkInfoEntity mMobileNetworkInfoEntity;
|
||||
|
||||
public MobileDataPreferenceController(Context context, String key) {
|
||||
public MobileDataPreferenceController(Context context, String key, Lifecycle lifecycle,
|
||||
LifecycleOwner lifecycleOwner, int subId) {
|
||||
super(context, key);
|
||||
mSubId = subId;
|
||||
mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
|
||||
mDataContentObserver = new MobileDataContentObserver(new Handler(Looper.getMainLooper()));
|
||||
mDataContentObserver.setOnMobileDataChangedListener(() -> updateState(mPreference));
|
||||
mMobileNetworkRepository = MobileNetworkRepository.createBySubId(context, this, mSubId);
|
||||
mLifecycleOwner = lifecycleOwner;
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,18 +102,14 @@ public class MobileDataPreferenceController extends TelephonyTogglePreferenceCon
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnLifecycleEvent(ON_START)
|
||||
public void onStart() {
|
||||
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
mDataContentObserver.register(mContext, mSubId);
|
||||
}
|
||||
mMobileNetworkRepository.addRegister(mLifecycleOwner);
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnLifecycleEvent(ON_STOP)
|
||||
public void onStop() {
|
||||
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
mDataContentObserver.unRegister(mContext);
|
||||
}
|
||||
mMobileNetworkRepository.removeRegister();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -122,39 +143,47 @@ public class MobileDataPreferenceController extends TelephonyTogglePreferenceCon
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
mTelephonyManager = getTelephonyManager();
|
||||
return mTelephonyManager.isDataEnabled();
|
||||
return mMobileNetworkInfoEntity == null ? false
|
||||
: mMobileNetworkInfoEntity.isMobileDataEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (isOpportunistic()) {
|
||||
preference.setEnabled(false);
|
||||
preference.setSummary(R.string.mobile_data_settings_summary_auto_switch);
|
||||
} else {
|
||||
preference.setEnabled(true);
|
||||
preference.setSummary(R.string.mobile_data_settings_summary);
|
||||
mPreference = (SwitchPreference) preference;
|
||||
update();
|
||||
}
|
||||
|
||||
private void update() {
|
||||
|
||||
if (mSubscriptionInfoEntity == null || mPreference == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
preference.setSelectable(false);
|
||||
preference.setSummary(R.string.mobile_data_settings_summary_unavailable);
|
||||
mPreference.setChecked(isChecked());
|
||||
if (mSubscriptionInfoEntity.isOpportunistic) {
|
||||
mPreference.setEnabled(false);
|
||||
mPreference.setSummary(R.string.mobile_data_settings_summary_auto_switch);
|
||||
} else {
|
||||
preference.setSelectable(true);
|
||||
mPreference.setEnabled(true);
|
||||
mPreference.setSummary(R.string.mobile_data_settings_summary);
|
||||
}
|
||||
if (!mSubscriptionInfoEntity.isValidSubscription) {
|
||||
mPreference.setSelectable(false);
|
||||
mPreference.setSummary(R.string.mobile_data_settings_summary_unavailable);
|
||||
} else {
|
||||
mPreference.setSelectable(true);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isOpportunistic() {
|
||||
SubscriptionInfo info = mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
|
||||
return info != null && info.isOpportunistic();
|
||||
}
|
||||
|
||||
public void init(FragmentManager fragmentManager, int subId) {
|
||||
public void init(FragmentManager fragmentManager, int subId,
|
||||
SubscriptionInfoEntity subInfoEntity, MobileNetworkInfoEntity networkInfoEntity) {
|
||||
mFragmentManager = fragmentManager;
|
||||
mSubId = subId;
|
||||
mTelephonyManager = null;
|
||||
mTelephonyManager = getTelephonyManager();
|
||||
mSubscriptionInfoEntity = subInfoEntity;
|
||||
mMobileNetworkInfoEntity = networkInfoEntity;
|
||||
}
|
||||
|
||||
private TelephonyManager getTelephonyManager() {
|
||||
@@ -179,9 +208,8 @@ public class MobileDataPreferenceController extends TelephonyTogglePreferenceCon
|
||||
final boolean enableData = !isChecked();
|
||||
mTelephonyManager = getTelephonyManager();
|
||||
final boolean isMultiSim = (mTelephonyManager.getActiveModemCount() > 1);
|
||||
final int defaultSubId = mSubscriptionManager.getDefaultDataSubscriptionId();
|
||||
final boolean needToDisableOthers = mSubscriptionManager
|
||||
.isActiveSubscriptionId(defaultSubId) && defaultSubId != mSubId;
|
||||
final boolean needToDisableOthers = mDefaultSubId != mSubId;
|
||||
|
||||
if (enableData && isMultiSim && needToDisableOthers) {
|
||||
mDialogType = MobileDataDialogFragment.TYPE_MULTI_SIM_DIALOG;
|
||||
return true;
|
||||
@@ -194,4 +222,62 @@ public class MobileDataPreferenceController extends TelephonyTogglePreferenceCon
|
||||
mSubId);
|
||||
dialogFragment.show(mFragmentManager, DIALOG_TAG);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setSubscriptionInfoEntity(SubscriptionInfoEntity subscriptionInfoEntity) {
|
||||
mSubscriptionInfoEntity = subscriptionInfoEntity;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setMobileNetworkInfoEntity(MobileNetworkInfoEntity mobileNetworkInfoEntity) {
|
||||
mMobileNetworkInfoEntity = mobileNetworkInfoEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAirplaneModeChanged(boolean airplaneModeEnabled) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAvailableSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActiveSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList) {
|
||||
if (DataServiceUtils.shouldUpdateEntityList(mSubscriptionInfoEntityList,
|
||||
subInfoEntityList)) {
|
||||
mSubscriptionInfoEntityList = subInfoEntityList;
|
||||
mSubscriptionInfoEntityList.forEach(entity -> {
|
||||
if (Integer.parseInt(entity.subId) == mSubId) {
|
||||
mSubscriptionInfoEntity = entity;
|
||||
}
|
||||
});
|
||||
if (mSubscriptionInfoEntity != null
|
||||
&& mSubscriptionInfoEntity.isDefaultDataSubscription) {
|
||||
mDefaultSubId = Integer.parseInt(mSubscriptionInfoEntity.subId);
|
||||
}
|
||||
update();
|
||||
refreshSummary(mPreference);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllUiccInfoChanged(List<UiccInfoEntity> uiccInfoEntityList) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAllMobileNetworkInfoChanged(
|
||||
List<MobileNetworkInfoEntity> mobileNetworkInfoEntityList) {
|
||||
if (DataServiceUtils.shouldUpdateEntityList(mMobileNetworkInfoEntityList,
|
||||
mobileNetworkInfoEntityList)) {
|
||||
mMobileNetworkInfoEntityList = mobileNetworkInfoEntityList;
|
||||
mMobileNetworkInfoEntityList.forEach(entity -> {
|
||||
if (Integer.parseInt(entity.subId) == mSubId) {
|
||||
mMobileNetworkInfoEntity = entity;
|
||||
update();
|
||||
refreshSummary(mPreference);
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -79,6 +79,7 @@ public class MobileNetworkSettings extends AbstractMobileNetworkSettings impleme
|
||||
private static final String KEY_ROAMING_PREF = "button_roaming_key";
|
||||
private static final String KEY_CALLS_PREF = "calls_preference";
|
||||
private static final String KEY_SMS_PREF = "sms_preference";
|
||||
private static final String KEY_MOBILE_DATA_PREF = "mobile_data_enable";
|
||||
|
||||
//String keys for preference lookup
|
||||
private static final String BUTTON_CDMA_SYSTEM_SELECT_KEY = "cdma_system_select_key";
|
||||
@@ -175,7 +176,9 @@ public class MobileNetworkSettings extends AbstractMobileNetworkSettings impleme
|
||||
new CallsDefaultSubscriptionController(context, KEY_CALLS_PREF,
|
||||
getSettingsLifecycle(), this),
|
||||
new SmsDefaultSubscriptionController(context, KEY_SMS_PREF, getSettingsLifecycle(),
|
||||
this));
|
||||
this),
|
||||
new MobileDataPreferenceController(context, KEY_MOBILE_DATA_PREF,
|
||||
getSettingsLifecycle(), this, mSubId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -231,10 +234,16 @@ public class MobileNetworkSettings extends AbstractMobileNetworkSettings impleme
|
||||
REQUEST_CODE_DELETE_SUBSCRIPTION);
|
||||
use(DisableSimFooterPreferenceController.class).init(mSubId);
|
||||
use(NrDisabledInDsdsFooterPreferenceController.class).init(mSubId);
|
||||
use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
|
||||
use(MobileDataPreferenceController.class).setWifiPickerTrackerHelper(
|
||||
new WifiPickerTrackerHelper(getSettingsLifecycle(), context,
|
||||
null /* WifiPickerTrackerCallback */));
|
||||
|
||||
final MobileDataPreferenceController mobileDataPreferenceController =
|
||||
use(MobileDataPreferenceController.class);
|
||||
if (mobileDataPreferenceController != null) {
|
||||
mobileDataPreferenceController.init(getFragmentManager(), mSubId,
|
||||
mSubscriptionInfoEntity, mMobileNetworkInfoEntity);
|
||||
mobileDataPreferenceController.setWifiPickerTrackerHelper(
|
||||
new WifiPickerTrackerHelper(getSettingsLifecycle(), context,
|
||||
null /* WifiPickerTrackerCallback */));
|
||||
}
|
||||
|
||||
final RoamingPreferenceController roamingPreferenceController =
|
||||
use(RoamingPreferenceController.class);
|
||||
|
@@ -26,6 +26,7 @@ android_test {
|
||||
"androidx.test.runner",
|
||||
"androidx.test.rules",
|
||||
"androidx.test.ext.junit",
|
||||
"mockito-target",
|
||||
],
|
||||
|
||||
test_suites: ["device-tests"],
|
||||
|
@@ -20,6 +20,8 @@ import static com.android.settings.testutils.CommonUtils.set_wifi_enabled;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import android.app.Instrumentation;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -31,6 +33,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.test.core.app.ActivityScenario;
|
||||
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
@@ -40,6 +43,9 @@ import androidx.test.platform.app.InstrumentationRegistry;
|
||||
import com.android.settings.Settings;
|
||||
import com.android.settings.testutils.CommonUtils;
|
||||
import com.android.settings.testutils.UiUtils;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assume;
|
||||
@@ -47,6 +53,8 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
@@ -54,6 +62,12 @@ import java.net.URL;
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@SmallTest
|
||||
public class MobileDataPreferenceControllerComponentTest {
|
||||
|
||||
@Mock
|
||||
private Lifecycle mLifecycle;
|
||||
@Mock
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
|
||||
public static final int TIMEOUT = 2000;
|
||||
private static int sSubscriptionId = 2;
|
||||
public final String TAG = this.getClass().getName();
|
||||
@@ -116,9 +130,11 @@ public class MobileDataPreferenceControllerComponentTest {
|
||||
try {
|
||||
URL url = new URL("https://www.google.net");
|
||||
MobileDataPreferenceController controller = new MobileDataPreferenceController(
|
||||
mInstrumentation.getTargetContext(), "mobile_data");
|
||||
mInstrumentation.getTargetContext(), "mobile_data", mLifecycle,
|
||||
mLifecycleOwner, sSubscriptionId);
|
||||
FragmentManager manager = ((FragmentActivity) activity).getSupportFragmentManager();
|
||||
controller.init(manager, sSubscriptionId);
|
||||
controller.init(manager, sSubscriptionId, mock(SubscriptionInfoEntity.class), mock(
|
||||
MobileNetworkInfoEntity.class));
|
||||
|
||||
// Make sure mobile network can connect at first.
|
||||
assertThat(UiUtils.waitUntilCondition(1000,
|
||||
|
@@ -28,18 +28,24 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Instrumentation;
|
||||
import android.content.Context;
|
||||
import android.os.Looper;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LifecycleRegistry;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
|
||||
import com.android.settings.testutils.ResourcesUtils;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
|
||||
import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -49,6 +55,10 @@ import org.mockito.MockitoAnnotations;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class MobileDataPreferenceControllerTest {
|
||||
private static final String SUB_ID_1 = "1";
|
||||
private static final String SUB_ID_2 = "2";
|
||||
private static final String DISPLAY_NAME_1 = "Sub 1";
|
||||
private static final String DISPLAY_NAME_2 = "Sub 2";
|
||||
private static final int SUB_ID = 2;
|
||||
private static final int SUB_ID_OTHER = 3;
|
||||
|
||||
@@ -64,7 +74,15 @@ public class MobileDataPreferenceControllerTest {
|
||||
private SubscriptionInfo mSubscriptionInfo;
|
||||
@Mock
|
||||
private FragmentTransaction mFragmentTransaction;
|
||||
|
||||
@Mock
|
||||
private Lifecycle mLifecycle;
|
||||
@Mock
|
||||
private LifecycleOwner mLifecycleOwner;
|
||||
private SubscriptionInfoEntity mSubInfo1;
|
||||
private SubscriptionInfoEntity mSubInfo2;
|
||||
private MobileNetworkInfoEntity mNetworkInfo1;
|
||||
private MobileNetworkInfoEntity mNetworkInfo2;
|
||||
private LifecycleRegistry mLifecycleRegistry;
|
||||
private MobileDataPreferenceController mController;
|
||||
private SwitchPreference mPreference;
|
||||
private Context mContext;
|
||||
@@ -73,6 +91,10 @@ public class MobileDataPreferenceControllerTest {
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare();
|
||||
}
|
||||
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
|
||||
|
||||
@@ -83,22 +105,45 @@ public class MobileDataPreferenceControllerTest {
|
||||
doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
|
||||
|
||||
mPreference = new SwitchPreference(mContext);
|
||||
mController = new MobileDataPreferenceController(mContext, "mobile_data");
|
||||
mController.init(mFragmentManager, SUB_ID);
|
||||
mController = new MobileDataPreferenceController(mContext, "mobile_data", mLifecycle,
|
||||
mLifecycleOwner, SUB_ID);
|
||||
mController.init(mFragmentManager, SUB_ID, mSubInfo1, mNetworkInfo1);
|
||||
mPreference.setKey(mController.getPreferenceKey());
|
||||
mLifecycleRegistry = new LifecycleRegistry(mLifecycleOwner);
|
||||
when(mLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry);
|
||||
}
|
||||
|
||||
private SubscriptionInfoEntity setupSubscriptionInfoEntity(String subId, String displayName,
|
||||
boolean isOpportunistic, boolean isValid, boolean isActive, boolean isAvailable,
|
||||
boolean isDefaultData) {
|
||||
int id = Integer.parseInt(subId);
|
||||
return new SubscriptionInfoEntity(subId, id, id,
|
||||
displayName, displayName, 0, "mcc", "mnc", "countryIso", false, id,
|
||||
TelephonyManager.DEFAULT_PORT_INDEX, isOpportunistic, null,
|
||||
SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, displayName, false,
|
||||
"1234567890", true, "default", false, isValid, true, isActive, isAvailable, false,
|
||||
false, isDefaultData, false, false);
|
||||
}
|
||||
|
||||
private MobileNetworkInfoEntity setupMobileNetworkInfoEntity(String subId,
|
||||
boolean isDatEnabled) {
|
||||
return new MobileNetworkInfoEntity(subId, false, false, isDatEnabled, false, false, false,
|
||||
false, false, false, false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_invalidSubscription_returnAvailableUnsearchable() {
|
||||
mController.init(mFragmentManager, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
|
||||
mController.init(mFragmentManager, SubscriptionManager.INVALID_SUBSCRIPTION_ID, mSubInfo1,
|
||||
mNetworkInfo1);
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDialogNeeded_disableSingleSim_returnFalse() {
|
||||
doReturn(true).when(mTelephonyManager).isDataEnabled();
|
||||
doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
|
||||
mSubInfo1 = setupSubscriptionInfoEntity(SUB_ID_1, DISPLAY_NAME_1, false, true, true, true,
|
||||
true);
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), true);
|
||||
doReturn(1).when(mTelephonyManager).getActiveModemCount();
|
||||
|
||||
assertThat(mController.isDialogNeeded()).isFalse();
|
||||
@@ -106,12 +151,15 @@ public class MobileDataPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void isDialogNeeded_enableNonDefaultSimInMultiSimMode_returnTrue() {
|
||||
doReturn(false).when(mTelephonyManager).isDataEnabled();
|
||||
doReturn(mSubscriptionInfo).when(mSubscriptionManager)
|
||||
.getActiveSubscriptionInfo(SUB_ID);
|
||||
mSubInfo1 = setupSubscriptionInfoEntity(SUB_ID_1, DISPLAY_NAME_1, false, true, true, true,
|
||||
false);
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), false);
|
||||
doReturn(1).when(mTelephonyManager).getActiveModemCount();
|
||||
// Ideally, it would be better if we could set the default data subscription to
|
||||
// SUB_ID_OTHER, and set that as an active subscription id.
|
||||
when(mSubscriptionManager.isActiveSubscriptionId(anyInt())).thenReturn(true);
|
||||
mSubInfo2 = setupSubscriptionInfoEntity(SUB_ID_2, DISPLAY_NAME_2, false, true, true, true,
|
||||
true);
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), true);
|
||||
doReturn(2).when(mTelephonyManager).getActiveModemCount();
|
||||
|
||||
assertThat(mController.isDialogNeeded()).isTrue();
|
||||
@@ -132,8 +180,11 @@ public class MobileDataPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_singleSim_On_shouldEnableData() {
|
||||
doReturn(true).when(mTelephonyManager).isDataEnabled();
|
||||
doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
|
||||
mSubInfo1 = setupSubscriptionInfoEntity(SUB_ID_1, DISPLAY_NAME_1, true, true, true, true,
|
||||
true);
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), true);
|
||||
mController.setSubscriptionInfoEntity(mSubInfo1);
|
||||
mController.setMobileNetworkInfoEntity(mNetworkInfo1);
|
||||
doReturn(1).when(mTelephonyManager).getActiveModemCount();
|
||||
|
||||
mController.onPreferenceChange(mPreference, true);
|
||||
@@ -143,8 +194,11 @@ public class MobileDataPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_multiSim_On_shouldEnableData() {
|
||||
doReturn(true).when(mTelephonyManager).isDataEnabled();
|
||||
doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
|
||||
mSubInfo1 = setupSubscriptionInfoEntity(SUB_ID_1, DISPLAY_NAME_1, true, true, true, true,
|
||||
true);
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), true);
|
||||
mController.setSubscriptionInfoEntity(mSubInfo1);
|
||||
mController.setMobileNetworkInfoEntity(mNetworkInfo1);
|
||||
doReturn(2).when(mTelephonyManager).getActiveModemCount();
|
||||
|
||||
mController.onPreferenceChange(mPreference, true);
|
||||
@@ -154,18 +208,20 @@ public class MobileDataPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void isChecked_returnUserDataEnabled() {
|
||||
mController.init(mFragmentManager, SUB_ID);
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), false);
|
||||
mController.init(mFragmentManager, SUB_ID, mSubInfo1, mNetworkInfo1);
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
|
||||
doReturn(true).when(mTelephonyManager).isDataEnabled();
|
||||
mNetworkInfo1 = setupMobileNetworkInfoEntity(String.valueOf(SUB_ID), true);
|
||||
mController.setMobileNetworkInfoEntity(mNetworkInfo1);
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_opportunistic_disabled() {
|
||||
doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
|
||||
mController.init(mFragmentManager, SUB_ID);
|
||||
doReturn(true).when(mSubscriptionInfo).isOpportunistic();
|
||||
mSubInfo1 = setupSubscriptionInfoEntity(SUB_ID_1, DISPLAY_NAME_1, true, true, true, true,
|
||||
true);
|
||||
mController.init(mFragmentManager, SUB_ID, mSubInfo1, mNetworkInfo1);
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isFalse();
|
||||
@@ -176,9 +232,9 @@ public class MobileDataPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updateState_notOpportunistic_enabled() {
|
||||
doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
|
||||
mController.init(mFragmentManager, SUB_ID);
|
||||
doReturn(false).when(mSubscriptionInfo).isOpportunistic();
|
||||
mSubInfo1 = setupSubscriptionInfoEntity(SUB_ID_1, DISPLAY_NAME_1, false, true, true, true,
|
||||
true);
|
||||
mController.init(mFragmentManager, SUB_ID, mSubInfo1, mNetworkInfo1);
|
||||
mController.updateState(mPreference);
|
||||
|
||||
assertThat(mPreference.isEnabled()).isTrue();
|
||||
|
Reference in New Issue
Block a user