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 androidx.annotation.VisibleForTesting; 26 27 import com.android.internal.telephony.TelephonyIntents; 28 29 /** Listens to changes in availability of telephony subscriptions. */ 30 public class SubscriptionsChangeListener { 31 32 /** Client defined action to trigger when there are subscription changes. */ 33 public interface SubscriptionsChangeAction { 34 /** Action taken when subscriptions changed. */ onSubscriptionsChanged()35 void onSubscriptionsChanged(); 36 } 37 38 @VisibleForTesting 39 static final IntentFilter RADIO_TECH_CHANGED_FILTER = new IntentFilter( 40 TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED); 41 42 private final Context mContext; 43 private final SubscriptionManager mSubscriptionManager; 44 private final SubscriptionsChangeAction mSubscriptionsChangeAction; 45 46 @VisibleForTesting 47 final SubscriptionManager.OnSubscriptionsChangedListener mSubscriptionsChangedListener = 48 new SubscriptionManager.OnSubscriptionsChangedListener() { 49 @Override 50 public void onSubscriptionsChanged() { 51 subscriptionsChangedCallback(); 52 } 53 }; 54 55 @VisibleForTesting 56 final BroadcastReceiver mRadioTechChangeReceiver = new BroadcastReceiver() { 57 @Override 58 public void onReceive(Context context, Intent intent) { 59 if (!isInitialStickyBroadcast()) { 60 subscriptionsChangedCallback(); 61 } 62 } 63 }; 64 SubscriptionsChangeListener(Context context, SubscriptionsChangeAction action)65 public SubscriptionsChangeListener(Context context, SubscriptionsChangeAction action) { 66 mContext = context; 67 mSubscriptionManager = context.getSystemService(SubscriptionManager.class); 68 mSubscriptionsChangeAction = action; 69 } 70 71 /** Starts the various listeners necessary to track changes in telephony subscriptions. */ start()72 public void start() { 73 mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionsChangedListener); 74 mContext.registerReceiver(mRadioTechChangeReceiver, RADIO_TECH_CHANGED_FILTER); 75 } 76 77 /** Stops the various listeners necessary to track changes in telephony subscriptions. */ stop()78 public void stop() { 79 mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionsChangedListener); 80 mContext.unregisterReceiver(mRadioTechChangeReceiver); 81 } 82 subscriptionsChangedCallback()83 private void subscriptionsChangedCallback() { 84 if (mSubscriptionsChangeAction != null) { 85 mSubscriptionsChangeAction.onSubscriptionsChanged(); 86 } 87 } 88 } 89