1 /* 2 * Copyright (C) 2021 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.systemui.telephony; 18 19 import android.telephony.ServiceState; 20 import android.telephony.TelephonyCallback.ActiveDataSubscriptionIdListener; 21 import android.telephony.TelephonyCallback.CallStateListener; 22 import android.telephony.TelephonyCallback.ServiceStateListener; 23 24 import androidx.annotation.NonNull; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 import javax.inject.Inject; 30 31 /** 32 * Class for use by {@link TelephonyListenerManager} to centralize TelephonyManager Callbacks. 33 * 34 * There are more callback interfaces defined in {@link android.telephony.TelephonyCallback} that 35 * are not currently covered. Add them here if they ever become necessary. 36 */ 37 class TelephonyCallback extends android.telephony.TelephonyCallback 38 implements ActiveDataSubscriptionIdListener, CallStateListener, ServiceStateListener { 39 40 private final List<ActiveDataSubscriptionIdListener> mActiveDataSubscriptionIdListeners = 41 new ArrayList<>(); 42 private final List<CallStateListener> mCallStateListeners = new ArrayList<>(); 43 private final List<ServiceStateListener> mServiceStateListeners = new ArrayList<>(); 44 45 @Inject TelephonyCallback()46 TelephonyCallback() { 47 } 48 hasAnyListeners()49 boolean hasAnyListeners() { 50 return !mActiveDataSubscriptionIdListeners.isEmpty() 51 || !mCallStateListeners.isEmpty() 52 || !mServiceStateListeners.isEmpty(); 53 } 54 55 @Override onActiveDataSubscriptionIdChanged(int subId)56 public void onActiveDataSubscriptionIdChanged(int subId) { 57 List<ActiveDataSubscriptionIdListener> listeners; 58 synchronized (mActiveDataSubscriptionIdListeners) { 59 listeners = new ArrayList<>(mActiveDataSubscriptionIdListeners); 60 } 61 listeners.forEach(listener -> listener.onActiveDataSubscriptionIdChanged(subId)); 62 } 63 addActiveDataSubscriptionIdListener(ActiveDataSubscriptionIdListener listener)64 void addActiveDataSubscriptionIdListener(ActiveDataSubscriptionIdListener listener) { 65 mActiveDataSubscriptionIdListeners.add(listener); 66 } 67 removeActiveDataSubscriptionIdListener(ActiveDataSubscriptionIdListener listener)68 void removeActiveDataSubscriptionIdListener(ActiveDataSubscriptionIdListener listener) { 69 mActiveDataSubscriptionIdListeners.remove(listener); 70 } 71 72 @Override onCallStateChanged(int state)73 public void onCallStateChanged(int state) { 74 List<CallStateListener> listeners; 75 synchronized (mCallStateListeners) { 76 listeners = new ArrayList<>(mCallStateListeners); 77 } 78 listeners.forEach(listener -> listener.onCallStateChanged(state)); 79 } 80 addCallStateListener(CallStateListener listener)81 void addCallStateListener(CallStateListener listener) { 82 mCallStateListeners.add(listener); 83 } 84 removeCallStateListener(CallStateListener listener)85 void removeCallStateListener(CallStateListener listener) { 86 mCallStateListeners.remove(listener); 87 } 88 89 @Override onServiceStateChanged(@onNull ServiceState serviceState)90 public void onServiceStateChanged(@NonNull ServiceState serviceState) { 91 List<ServiceStateListener> listeners; 92 synchronized (mServiceStateListeners) { 93 listeners = new ArrayList<>(mServiceStateListeners); 94 } 95 listeners.forEach(listener -> listener.onServiceStateChanged(serviceState)); 96 } 97 addServiceStateListener(ServiceStateListener listener)98 void addServiceStateListener(ServiceStateListener listener) { 99 mServiceStateListeners.add(listener); 100 } 101 removeServiceStateListener(ServiceStateListener listener)102 void removeServiceStateListener(ServiceStateListener listener) { 103 mServiceStateListeners.remove(listener); 104 } 105 } 106