[Settings] Refactor AirplaneModeEnabler
1. Multiple instance of PhoneStateListener is not required. 2. It will be better to monitor radio power state changed instead of service state. Bug: 147029746 Test: Manual Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=AirplaneModePreferenceControllerTest Change-Id: I51465904ce2409b6c07c3ba80ea362d208989fbd
This commit is contained in:
@@ -22,9 +22,7 @@ import android.content.Intent;
|
|||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.telephony.PhoneStateListener;
|
import android.telephony.PhoneStateListener;
|
||||||
import android.telephony.ServiceState;
|
|
||||||
import android.telephony.SubscriptionInfo;
|
import android.telephony.SubscriptionInfo;
|
||||||
import android.telephony.SubscriptionManager;
|
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -34,7 +32,6 @@ import com.android.settings.overlay.FeatureFactory;
|
|||||||
import com.android.settingslib.WirelessUtils;
|
import com.android.settingslib.WirelessUtils;
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,12 +58,11 @@ public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
|
|||||||
|
|
||||||
private TelephonyManager mTelephonyManager;
|
private TelephonyManager mTelephonyManager;
|
||||||
private ProxySubscriptionManager mProxySubscriptionMgr;
|
private ProxySubscriptionManager mProxySubscriptionMgr;
|
||||||
private List<ServiceStateListener> mServiceStateListeners;
|
private PhoneStateListener mPhoneStateListener;
|
||||||
|
|
||||||
private GlobalSettingsChangeListener mAirplaneModeObserver;
|
private GlobalSettingsChangeListener mAirplaneModeObserver;
|
||||||
|
|
||||||
public AirplaneModeEnabler(Context context,
|
public AirplaneModeEnabler(Context context, OnAirplaneModeChangedListener listener) {
|
||||||
OnAirplaneModeChangedListener listener) {
|
|
||||||
super(context, Settings.Global.AIRPLANE_MODE_ON);
|
super(context, Settings.Global.AIRPLANE_MODE_ON);
|
||||||
|
|
||||||
mContext = context;
|
mContext = context;
|
||||||
@@ -75,6 +71,16 @@ public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
|
|||||||
|
|
||||||
mTelephonyManager = context.getSystemService(TelephonyManager.class);
|
mTelephonyManager = context.getSystemService(TelephonyManager.class);
|
||||||
mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context);
|
mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context);
|
||||||
|
|
||||||
|
mPhoneStateListener = new PhoneStateListener() {
|
||||||
|
@Override
|
||||||
|
public void onRadioPowerStateChanged(int state) {
|
||||||
|
if (DEBUG) {
|
||||||
|
Log.d(LOG_TAG, "RadioPower: " + state);
|
||||||
|
}
|
||||||
|
onAirplaneModeChanged();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,35 +94,13 @@ public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void resume() {
|
public void resume() {
|
||||||
final List<SubscriptionInfo> subInfoList =
|
mTelephonyManager.listen(mPhoneStateListener,
|
||||||
mProxySubscriptionMgr.getActiveSubscriptionsInfo();
|
PhoneStateListener.LISTEN_RADIO_POWER_STATE_CHANGED);
|
||||||
|
|
||||||
mServiceStateListeners = new ArrayList<ServiceStateListener>();
|
|
||||||
|
|
||||||
// add default listener
|
|
||||||
mServiceStateListeners.add(new ServiceStateListener(mTelephonyManager,
|
|
||||||
SubscriptionManager.INVALID_SUBSCRIPTION_ID, this));
|
|
||||||
|
|
||||||
if (subInfoList != null) {
|
|
||||||
for (SubscriptionInfo subInfo : subInfoList) {
|
|
||||||
mServiceStateListeners.add(new ServiceStateListener(mTelephonyManager,
|
|
||||||
subInfo.getSubscriptionId(), this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (ServiceStateListener listener : mServiceStateListeners) {
|
|
||||||
listener.start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
if (mServiceStateListeners == null) {
|
mTelephonyManager.listen(mPhoneStateListener,
|
||||||
return;
|
PhoneStateListener.LISTEN_NONE);
|
||||||
}
|
|
||||||
for (ServiceStateListener listener : mServiceStateListeners) {
|
|
||||||
listener.stop();
|
|
||||||
}
|
|
||||||
mServiceStateListeners = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setAirplaneModeOn(boolean enabling) {
|
private void setAirplaneModeOn(boolean enabling) {
|
||||||
@@ -200,51 +184,4 @@ public class AirplaneModeEnabler extends GlobalSettingsChangeListener {
|
|||||||
public boolean isAirplaneModeOn() {
|
public boolean isAirplaneModeOn() {
|
||||||
return WirelessUtils.isAirplaneModeOn(mContext);
|
return WirelessUtils.isAirplaneModeOn(mContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ServiceStateListener extends PhoneStateListener {
|
|
||||||
private ServiceStateListener(TelephonyManager telephonyManager, int subscriptionId,
|
|
||||||
AirplaneModeEnabler enabler) {
|
|
||||||
super();
|
|
||||||
mSubId = subscriptionId;
|
|
||||||
mTelephonyManager = getSubIdSpecificTelephonyManager(telephonyManager);
|
|
||||||
mEnabler = enabler;
|
|
||||||
}
|
|
||||||
|
|
||||||
private final int mSubId;
|
|
||||||
private final TelephonyManager mTelephonyManager;
|
|
||||||
private final AirplaneModeEnabler mEnabler;
|
|
||||||
|
|
||||||
int getSubscriptionId() {
|
|
||||||
return mSubId;
|
|
||||||
}
|
|
||||||
|
|
||||||
void start() {
|
|
||||||
if (mTelephonyManager != null) {
|
|
||||||
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_SERVICE_STATE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() {
|
|
||||||
if (mTelephonyManager != null) {
|
|
||||||
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_NONE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onServiceStateChanged(ServiceState serviceState) {
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(LOG_TAG, "ServiceState in sub" + mSubId + ": " + serviceState);
|
|
||||||
}
|
|
||||||
mEnabler.onAirplaneModeChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
private TelephonyManager getSubIdSpecificTelephonyManager(
|
|
||||||
TelephonyManager telephonyManager) {
|
|
||||||
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
|
|
||||||
return telephonyManager;
|
|
||||||
}
|
|
||||||
return telephonyManager.createForSubscriptionId(mSubId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user