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: Ie901ac6d89f954749793e95923a4a1b1945b9999
This commit is contained in:
Jeremy Goldman
2021-04-15 14:30:54 +08:00
parent 1812aabcec
commit 9c69df3b82

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) {
mSubscriptionManager.removeOnSubscriptionsChangedListener(
mSubscriptionsChangedListener);
mContext.getContentResolver().unregisterContentObserver(this); mContext.getContentResolver().unregisterContentObserver(this);
mContext.unregisterReceiver(mBroadcastReceiver); mContext.unregisterReceiver(mBroadcastReceiver);
mRunning = false;
} else {
Log.d(TAG, "Stop has been called without associated Start.");
}
} }
public boolean isAirplaneModeOn() { public boolean isAirplaneModeOn() {