Check if the SubscriptionsChangeListener has been started before stop

calling mContext.unregisterReceiver on an unregistered broadcast
receiver will result in an exception. To make the
SubscriptionsChangeListener safe from this kind of error, add a check to
see whether start has been called before implementing stop's
functionality.

Bug: 184662284
Bug: 184521296
Test: atest -c SettingsUnitTests
Change-Id: Ic13911ee7573666f5d42ab4612c025968984ba47
This commit is contained in:
Jeremy Goldman
2021-04-19 16:53:07 +08:00
parent 6de2953232
commit 660e7164c7

View File

@@ -27,12 +27,15 @@ import android.os.Looper;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener;
import android.util.Log;
import com.android.internal.telephony.TelephonyIntents; import com.android.internal.telephony.TelephonyIntents;
/** Helper class for listening to changes in availability of telephony subscriptions */ /** Helper class for listening to changes in availability of telephony subscriptions */
public class SubscriptionsChangeListener extends ContentObserver { public class SubscriptionsChangeListener extends ContentObserver {
private static final String TAG = "SubscriptionsChangeListener";
public interface SubscriptionsChangeListenerClient { public interface SubscriptionsChangeListenerClient {
void onAirplaneModeChanged(boolean airplaneModeEnabled); void onAirplaneModeChanged(boolean airplaneModeEnabled);
void onSubscriptionsChanged(); void onSubscriptionsChanged();
@@ -44,6 +47,7 @@ public class SubscriptionsChangeListener extends ContentObserver {
private OnSubscriptionsChangedListener mSubscriptionsChangedListener; private OnSubscriptionsChangedListener mSubscriptionsChangedListener;
private Uri mAirplaneModeSettingUri; private Uri mAirplaneModeSettingUri;
private BroadcastReceiver mBroadcastReceiver; private BroadcastReceiver mBroadcastReceiver;
private boolean mRunning = false;
public SubscriptionsChangeListener(Context context, SubscriptionsChangeListenerClient client) { public SubscriptionsChangeListener(Context context, SubscriptionsChangeListenerClient client) {
super(new Handler(Looper.getMainLooper())); super(new Handler(Looper.getMainLooper()));
@@ -75,12 +79,19 @@ public class SubscriptionsChangeListener extends ContentObserver {
final IntentFilter radioTechnologyChangedFilter = new IntentFilter( final IntentFilter radioTechnologyChangedFilter = new IntentFilter(
TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED); TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
mContext.registerReceiver(mBroadcastReceiver, radioTechnologyChangedFilter); mContext.registerReceiver(mBroadcastReceiver, radioTechnologyChangedFilter);
mRunning = true;
} }
public void stop() { public void stop() {
mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionsChangedListener); if (mRunning) {
mContext.getContentResolver().unregisterContentObserver(this); mSubscriptionManager.removeOnSubscriptionsChangedListener(
mContext.unregisterReceiver(mBroadcastReceiver); mSubscriptionsChangedListener);
mContext.getContentResolver().unregisterContentObserver(this);
mContext.unregisterReceiver(mBroadcastReceiver);
mRunning = false;
} else {
Log.d(TAG, "Stop has been called without associated Start.");
}
} }
public boolean isAirplaneModeOn() { public boolean isAirplaneModeOn() {