Merge "Revert "Move the auto data switch preference into DDS sim""
This commit is contained in:
committed by
Android (Google) Code Review
commit
2eb121ce3f
@@ -22,10 +22,8 @@ import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
|
||||
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.util.Log;
|
||||
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
@@ -38,8 +36,6 @@ import com.android.settings.datausage.DataUsageUtils;
|
||||
import com.android.settings.network.MobileDataContentObserver;
|
||||
import com.android.settings.network.SubscriptionsChangeListener;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Controls whether switch mobile data to the non-default SIM if the non-default SIM has better
|
||||
* availability.
|
||||
@@ -49,88 +45,39 @@ import java.util.List;
|
||||
* signal/connectivity.
|
||||
* If this feature is enabled, data will be temporarily enabled on the non-default data SIM,
|
||||
* including during any voice calls.
|
||||
*
|
||||
* Showing this preference in the default data sim UI.
|
||||
*/
|
||||
public class AutoDataSwitchPreferenceController extends TelephonyTogglePreferenceController
|
||||
implements LifecycleObserver,
|
||||
SubscriptionsChangeListener.SubscriptionsChangeListenerClient {
|
||||
private static final String TAG = "AutoDataSwitchPreferenceController";
|
||||
|
||||
private SwitchPreference mPreference;
|
||||
private SubscriptionsChangeListener mChangeListener;
|
||||
private TelephonyManager mManager;
|
||||
private MobileDataContentObserver mMobileDataContentObserver;
|
||||
private PreferenceScreen mScreen;
|
||||
private SubscriptionManager mSubscriptionManager;
|
||||
private List<SubscriptionInfo> mSubInfoList;
|
||||
|
||||
public AutoDataSwitchPreferenceController(Context context,
|
||||
String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
|
||||
}
|
||||
|
||||
void init(int subId) {
|
||||
this.mSubId = subId;
|
||||
if (renewSubscriptionInfoList()) {
|
||||
// If the subscriptionInfos are changed, then
|
||||
mManager = mContext.getSystemService(TelephonyManager.class)
|
||||
.createForSubscriptionId(getNonDdsSubId());
|
||||
}
|
||||
if (mMobileDataContentObserver == null) {
|
||||
mMobileDataContentObserver = new MobileDataContentObserver(
|
||||
new Handler(Looper.getMainLooper()));
|
||||
mMobileDataContentObserver.setOnMobileDataChangedListener(() -> {
|
||||
mManager = mContext.getSystemService(TelephonyManager.class)
|
||||
.createForSubscriptionId(getNonDdsSubId());
|
||||
refreshPreference();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void renewTelephonyComponent() {
|
||||
if (renewSubscriptionInfoList()) {
|
||||
// If the subscriptionInfos are changed, then
|
||||
if (mMobileDataContentObserver != null) {
|
||||
mMobileDataContentObserver.unRegister(mContext);
|
||||
}
|
||||
}
|
||||
if (mSubInfoList == null) {
|
||||
Log.d(TAG, "mSubInfoList is null. Stop to register the listener");
|
||||
return;
|
||||
}
|
||||
if (mMobileDataContentObserver != null) {
|
||||
for (SubscriptionInfo subInfo : mSubInfoList) {
|
||||
mMobileDataContentObserver.register(mContext, subInfo.getSubscriptionId());
|
||||
}
|
||||
}
|
||||
mManager = mContext.getSystemService(TelephonyManager.class)
|
||||
.createForSubscriptionId(getNonDdsSubId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Renew the subscriptionInfoList if the subscriptionInfos are changed.
|
||||
* @return true if the subscriptionInfos are changed. Otherwise, return false.
|
||||
*/
|
||||
private boolean renewSubscriptionInfoList() {
|
||||
final List<SubscriptionInfo> newSubInfoList =
|
||||
mSubscriptionManager.getActiveSubscriptionInfoList();
|
||||
if ((newSubInfoList == null && mSubInfoList == null)
|
||||
|| (mSubInfoList != null && mSubInfoList.equals(newSubInfoList))) {
|
||||
return false;
|
||||
}
|
||||
mSubInfoList = newSubInfoList;
|
||||
return true;
|
||||
mManager = mContext.getSystemService(TelephonyManager.class).createForSubscriptionId(subId);
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(ON_RESUME)
|
||||
public void onResume() {
|
||||
renewTelephonyComponent();
|
||||
if (mChangeListener == null) {
|
||||
mChangeListener = new SubscriptionsChangeListener(mContext, this);
|
||||
}
|
||||
mChangeListener.start();
|
||||
if (mMobileDataContentObserver == null) {
|
||||
mMobileDataContentObserver = new MobileDataContentObserver(
|
||||
new Handler(Looper.getMainLooper()));
|
||||
mMobileDataContentObserver.setOnMobileDataChangedListener(() -> refreshPreference());
|
||||
}
|
||||
mMobileDataContentObserver.register(mContext, mSubId);
|
||||
}
|
||||
|
||||
@OnLifecycleEvent(ON_PAUSE)
|
||||
@@ -158,10 +105,6 @@ public class AutoDataSwitchPreferenceController extends TelephonyTogglePreferenc
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
if (mManager == null) {
|
||||
Log.d(TAG, "mManager is null.");
|
||||
return false;
|
||||
}
|
||||
mManager.setMobileDataPolicyEnabled(
|
||||
TelephonyManager.MOBILE_DATA_POLICY_AUTO_DATA_SWITCH,
|
||||
isChecked);
|
||||
@@ -176,8 +119,7 @@ public class AutoDataSwitchPreferenceController extends TelephonyTogglePreferenc
|
||||
@Override
|
||||
public int getAvailabilityStatus(int subId) {
|
||||
if (!SubscriptionManager.isValidSubscriptionId(subId)
|
||||
|| SubscriptionManager.getDefaultDataSubscriptionId() != subId
|
||||
|| !SubscriptionManager.isValidSubscriptionId(getNonDdsSubId())
|
||||
|| SubscriptionManager.getDefaultDataSubscriptionId() == subId
|
||||
|| (!hasMobileData())) {
|
||||
return CONDITIONALLY_UNAVAILABLE;
|
||||
}
|
||||
@@ -194,12 +136,10 @@ public class AutoDataSwitchPreferenceController extends TelephonyTogglePreferenc
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAirplaneModeChanged(boolean airplaneModeEnabled) {
|
||||
}
|
||||
public void onAirplaneModeChanged(boolean airplaneModeEnabled) {}
|
||||
|
||||
@Override
|
||||
public void onSubscriptionsChanged() {
|
||||
renewTelephonyComponent();
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@@ -212,23 +152,4 @@ public class AutoDataSwitchPreferenceController extends TelephonyTogglePreferenc
|
||||
super.displayPreference(mScreen);
|
||||
}
|
||||
}
|
||||
|
||||
private int getNonDdsSubId() {
|
||||
int ddsSubId = SubscriptionManager.getDefaultDataSubscriptionId();
|
||||
Log.d(TAG, "DDS SubId: " + ddsSubId);
|
||||
|
||||
if (ddsSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
|
||||
}
|
||||
|
||||
if (mSubInfoList == null) {
|
||||
return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
|
||||
}
|
||||
|
||||
return mSubInfoList.stream()
|
||||
.mapToInt(subInfo -> subInfo.getSubscriptionId())
|
||||
.filter(subId -> subId != ddsSubId)
|
||||
.findFirst()
|
||||
.orElse(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user