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