[Settings] Replace getSimCount() API
Change design of monitor change in Settings.Global.MOBILE_DATA and Settings.Global.DATA_ROAMING in order to avoid from accessing getSimCount(). Bug: 144251589 Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=RoamingPreferenceControllerTest make RunSettingsRoboTests -j ROBOTEST_FILTER=VideoCallingPreferenceControllerTest Change-Id: I965a0c07c7c9ef5897b33809bae6a7921977db18
This commit is contained in:
@@ -42,7 +42,7 @@ public class RoamingDialogFragment extends InstrumentedDialogFragment implements
|
||||
|
||||
public static RoamingDialogFragment newInstance(int subId) {
|
||||
final RoamingDialogFragment dialogFragment = new RoamingDialogFragment();
|
||||
Bundle args = new Bundle();
|
||||
final Bundle args = new Bundle();
|
||||
args.putInt(SUB_ID_KEY, subId);
|
||||
dialogFragment.setArguments(args);
|
||||
|
||||
@@ -52,17 +52,17 @@ public class RoamingDialogFragment extends InstrumentedDialogFragment implements
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
Bundle args = getArguments();
|
||||
final Bundle args = getArguments();
|
||||
mSubId = args.getInt(SUB_ID_KEY);
|
||||
mCarrierConfigManager = new CarrierConfigManager(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
int title = R.string.roaming_alert_title;
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
final int title = R.string.roaming_alert_title;
|
||||
int message = R.string.roaming_warning;
|
||||
PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
|
||||
final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
|
||||
if (carrierConfig != null && carrierConfig.getBoolean(
|
||||
CarrierConfigManager.KEY_CHECK_PRICING_WITH_CARRIER_FOR_DATA_ROAMING_BOOL)) {
|
||||
message = R.string.roaming_check_price_warning;
|
||||
@@ -84,8 +84,13 @@ public class RoamingDialogFragment extends InstrumentedDialogFragment implements
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
// let the host know that the positive button has been clicked
|
||||
if (which == dialog.BUTTON_POSITIVE) {
|
||||
TelephonyManager.from(getContext()).createForSubscriptionId(
|
||||
mSubId).setDataRoamingEnabled(true);
|
||||
final TelephonyManager telephonyManager =
|
||||
getContext().getSystemService(TelephonyManager.class)
|
||||
.createForSubscriptionId(mSubId);
|
||||
if (telephonyManager == null) {
|
||||
return;
|
||||
}
|
||||
telephonyManager.setDataRoamingEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -17,22 +17,20 @@
|
||||
package com.android.settings.network.telephony;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
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 android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.network.GlobalSettingsChangeListener;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
@@ -44,31 +42,59 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
public class RoamingPreferenceController extends TelephonyTogglePreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private static final String TAG = "RoamingController";
|
||||
private static final String DIALOG_TAG = "MobileDataDialog";
|
||||
|
||||
private RestrictedSwitchPreference mSwitchPreference;
|
||||
private TelephonyManager mTelephonyManager;
|
||||
private CarrierConfigManager mCarrierConfigManager;
|
||||
private DataContentObserver mDataContentObserver;
|
||||
@VisibleForTesting
|
||||
boolean mNeedDialog;
|
||||
|
||||
/**
|
||||
* There're 2 listeners both activated at the same time.
|
||||
* For project that access DATA_ROAMING, only first listener is functional.
|
||||
* For project that access "DATA_ROAMING + subId", first listener will be stopped when receiving
|
||||
* any onChange from second listener.
|
||||
*/
|
||||
private GlobalSettingsChangeListener mListener;
|
||||
private GlobalSettingsChangeListener mListenerForSubId;
|
||||
|
||||
@VisibleForTesting
|
||||
FragmentManager mFragmentManager;
|
||||
|
||||
public RoamingPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
|
||||
mDataContentObserver = new DataContentObserver(new Handler(Looper.getMainLooper()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mDataContentObserver.register(mContext, mSubId);
|
||||
if (mListener == null) {
|
||||
mListener = new GlobalSettingsChangeListener(mContext,
|
||||
Settings.Global.DATA_ROAMING) {
|
||||
public void onChanged(String field) {
|
||||
updateState(mSwitchPreference);
|
||||
}
|
||||
};
|
||||
}
|
||||
stopMonitorSubIdSpecific();
|
||||
|
||||
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
return;
|
||||
}
|
||||
|
||||
mListenerForSubId = new GlobalSettingsChangeListener(mContext,
|
||||
Settings.Global.DATA_ROAMING + mSubId) {
|
||||
public void onChanged(String field) {
|
||||
stopMonitor();
|
||||
updateState(mSwitchPreference);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mDataContentObserver.unRegister(mContext);
|
||||
stopMonitor();
|
||||
stopMonitorSubIdSpecific();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,7 +113,7 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
|
||||
if (mNeedDialog) {
|
||||
if (isDialogNeeded()) {
|
||||
showDialog();
|
||||
}
|
||||
return true;
|
||||
@@ -98,9 +124,7 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
mNeedDialog = isDialogNeeded();
|
||||
|
||||
if (!mNeedDialog) {
|
||||
if (!isDialogNeeded()) {
|
||||
// Update data directly if we don't need dialog
|
||||
mTelephonyManager.setDataRoamingEnabled(isChecked);
|
||||
return true;
|
||||
@@ -141,7 +165,18 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
|
||||
public void init(FragmentManager fragmentManager, int subId) {
|
||||
mFragmentManager = fragmentManager;
|
||||
mSubId = subId;
|
||||
mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId);
|
||||
mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
|
||||
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
return;
|
||||
}
|
||||
final TelephonyManager telephonyManager = mTelephonyManager
|
||||
.createForSubscriptionId(mSubId);
|
||||
if (telephonyManager == null) {
|
||||
Log.w(TAG, "fail to init in sub" + mSubId);
|
||||
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
|
||||
return;
|
||||
}
|
||||
mTelephonyManager = telephonyManager;
|
||||
}
|
||||
|
||||
private void showDialog() {
|
||||
@@ -150,32 +185,17 @@ public class RoamingPreferenceController extends TelephonyTogglePreferenceContro
|
||||
dialogFragment.show(mFragmentManager, DIALOG_TAG);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener that listens data roaming change
|
||||
*/
|
||||
public class DataContentObserver extends ContentObserver {
|
||||
|
||||
public DataContentObserver(Handler handler) {
|
||||
super(handler);
|
||||
private void stopMonitor() {
|
||||
if (mListener != null) {
|
||||
mListener.close();
|
||||
mListener = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
super.onChange(selfChange);
|
||||
updateState(mSwitchPreference);
|
||||
}
|
||||
|
||||
public void register(Context context, int subId) {
|
||||
Uri uri = Settings.Global.getUriFor(Settings.Global.DATA_ROAMING);
|
||||
if (TelephonyManager.getDefault().getSimCount() != 1) {
|
||||
uri = Settings.Global.getUriFor(Settings.Global.DATA_ROAMING + subId);
|
||||
}
|
||||
context.getContentResolver().registerContentObserver(uri, false, this);
|
||||
|
||||
}
|
||||
|
||||
public void unRegister(Context context) {
|
||||
context.getContentResolver().unregisterContentObserver(this);
|
||||
private void stopMonitorSubIdSpecific() {
|
||||
if (mListenerForSubId != null) {
|
||||
mListenerForSubId.close();
|
||||
mListenerForSubId = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -17,12 +17,8 @@
|
||||
package com.android.settings.network.telephony;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.PersistableBundle;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.CarrierConfigManager;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.SubscriptionManager;
|
||||
@@ -34,6 +30,7 @@ import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.ims.ImsManager;
|
||||
import com.android.settings.network.MobileDataEnabledListener;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
@@ -43,6 +40,7 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
*/
|
||||
public class VideoCallingPreferenceController extends TelephonyTogglePreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop,
|
||||
MobileDataEnabledListener.Client,
|
||||
Enhanced4gBasePreferenceController.On4gLteUpdateListener {
|
||||
|
||||
private Preference mPreference;
|
||||
@@ -51,12 +49,14 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
@VisibleForTesting
|
||||
ImsManager mImsManager;
|
||||
private PhoneCallStateListener mPhoneStateListener;
|
||||
private DataContentObserver mDataContentObserver;
|
||||
@VisibleForTesting
|
||||
Integer mCallState;
|
||||
private MobileDataEnabledListener mDataContentObserver;
|
||||
|
||||
public VideoCallingPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
|
||||
mDataContentObserver = new DataContentObserver(new Handler(Looper.getMainLooper()));
|
||||
mDataContentObserver = new MobileDataEnabledListener(context, this);
|
||||
mPhoneStateListener = new PhoneCallStateListener(Looper.getMainLooper());
|
||||
}
|
||||
|
||||
@@ -77,18 +77,21 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
@Override
|
||||
public void onStart() {
|
||||
mPhoneStateListener.register(mSubId);
|
||||
mDataContentObserver.register(mContext, mSubId);
|
||||
mDataContentObserver.start(mSubId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mPhoneStateListener.unregister();
|
||||
mDataContentObserver.unRegister(mContext);
|
||||
mDataContentObserver.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (mCallState == null) {
|
||||
return;
|
||||
}
|
||||
final SwitchPreference switchPreference = (SwitchPreference) preference;
|
||||
final boolean videoCallEnabled = isVideoCallEnabled(mSubId, mImsManager);
|
||||
switchPreference.setVisible(videoCallEnabled);
|
||||
@@ -96,7 +99,7 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
final boolean is4gLteEnabled = mImsManager.isEnhanced4gLteModeSettingEnabledByUser()
|
||||
&& mImsManager.isNonTtyOrTtyOnVolteEnabled();
|
||||
preference.setEnabled(is4gLteEnabled &&
|
||||
mTelephonyManager.getCallState(mSubId) == TelephonyManager.CALL_STATE_IDLE);
|
||||
mCallState == TelephonyManager.CALL_STATE_IDLE);
|
||||
switchPreference.setChecked(is4gLteEnabled && mImsManager.isVtEnabledByUser());
|
||||
}
|
||||
}
|
||||
@@ -114,8 +117,9 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
|
||||
public VideoCallingPreferenceController init(int subId) {
|
||||
mSubId = subId;
|
||||
mTelephonyManager = TelephonyManager.from(mContext).createForSubscriptionId(mSubId);
|
||||
mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
|
||||
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
mTelephonyManager = mTelephonyManager.createForSubscriptionId(mSubId);
|
||||
mImsManager = ImsManager.getInstance(mContext, SubscriptionManager.getPhoneId(mSubId));
|
||||
}
|
||||
|
||||
@@ -132,8 +136,10 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
@VisibleForTesting
|
||||
boolean isVideoCallEnabled(int subId, ImsManager imsManager) {
|
||||
final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(subId);
|
||||
final TelephonyManager telephonyManager = TelephonyManager
|
||||
.from(mContext).createForSubscriptionId(subId);
|
||||
TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class);
|
||||
if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
||||
telephonyManager = telephonyManager.createForSubscriptionId(subId);
|
||||
}
|
||||
return carrierConfig != null && imsManager != null
|
||||
&& imsManager.isVtEnabledByPlatform()
|
||||
&& imsManager.isVtProvisionedOnDevice()
|
||||
@@ -156,6 +162,7 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
|
||||
@Override
|
||||
public void onCallStateChanged(int state, String incomingNumber) {
|
||||
mCallState = state;
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@@ -165,36 +172,15 @@ public class VideoCallingPreferenceController extends TelephonyTogglePreferenceC
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
mCallState = null;
|
||||
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listener that listens mobile data state change.
|
||||
* Implementation of MobileDataEnabledListener.Client
|
||||
*/
|
||||
public class DataContentObserver extends ContentObserver {
|
||||
|
||||
public DataContentObserver(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
super.onChange(selfChange);
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
public void register(Context context, int subId) {
|
||||
Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA);
|
||||
if (TelephonyManager.getDefault().getSimCount() != 1) {
|
||||
uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + subId);
|
||||
}
|
||||
context.getContentResolver().registerContentObserver(uri,
|
||||
false /* notifyForDescendants */, this /* observer */);
|
||||
}
|
||||
|
||||
public void unRegister(Context context) {
|
||||
context.getContentResolver().unregisterContentObserver(this);
|
||||
}
|
||||
public void onMobileDataEnabledChange() {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user