[Settings] Apply the SettingsDataService to the SIM page, MobileDataPreferenceController
Bug: 257950125 Test: atest MobileDataPreferenceControllerTest Change-Id: Ia64a8baf931e9aa7f54cad2b2f5bb264420be9b0
This commit is contained in:
@@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user