1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.settings.network; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.telephony.SubscriptionManager; 24 25 import com.android.internal.telephony.TelephonyIntents; 26 27 /** Listens to changes in availability of telephony subscriptions. */ 28 public class SubscriptionsChangeListener { 29 30 /** Client defined action to trigger when there are subscription changes. */ 31 public interface SubscriptionsChangeAction { 32 /** Action taken when subscriptions changed. */ onSubscriptionsChanged()33 void onSubscriptionsChanged(); 34 } 35 36 private static final IntentFilter RADIO_TECH_CHANGED_FILTER = new IntentFilter( 37 TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED); 38 39 private final Context mContext; 40 private final SubscriptionManager mSubscriptionManager; 41 private final SubscriptionsChangeAction mSubscriptionsChangeAction; 42 43 private final SubscriptionManager.OnSubscriptionsChangedListener mSubscriptionsChangedListener = 44 new SubscriptionManager.OnSubscriptionsChangedListener() { 45 @Override 46 public void onSubscriptionsChanged() { 47 subscriptionsChangedCallback(); 48 } 49 }; 50 51 private final BroadcastReceiver mRadioTechChangeReceiver = new BroadcastReceiver() { 52 @Override 53 public void onReceive(Context context, Intent intent) { 54 if (!isInitialStickyBroadcast()) { 55 subscriptionsChangedCallback(); 56 } 57 } 58 }; 59 SubscriptionsChangeListener(Context context, SubscriptionsChangeAction action)60 public SubscriptionsChangeListener(Context context, SubscriptionsChangeAction action) { 61 mContext = context; 62 mSubscriptionManager = context.getSystemService(SubscriptionManager.class); 63 mSubscriptionsChangeAction = action; 64 } 65 66 /** Starts the various listeners necessary to track changes in telephony subscriptions. */ start()67 public void start() { 68 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionsChangedListener); 69 mContext.registerReceiver(mRadioTechChangeReceiver, RADIO_TECH_CHANGED_FILTER); 70 } 71 72 /** Stops the various listeners necessary to track changes in telephony subscriptions. */ stop()73 public void stop() { 74 mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionsChangedListener); 75 mContext.unregisterReceiver(mRadioTechChangeReceiver); 76 } 77 subscriptionsChangedCallback()78 private void subscriptionsChangedCallback() { 79 if (mSubscriptionsChangeAction != null) { 80 mSubscriptionsChangeAction.onSubscriptionsChanged(); 81 } 82 } 83 } 84