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.settings.network.telephony;
18 
19 import android.content.Context;
20 import android.telephony.PhoneStateListener;
21 import android.telephony.TelephonyDisplayInfo;
22 import android.telephony.TelephonyManager;
23 import android.util.ArraySet;
24 
25 import com.google.common.collect.Sets;
26 
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Set;
30 
31 /**
32  * Help to listen telephony display info change to subscriptions.
33  * TODO(b/177647571): unit test is needed.
34  */
35 public class TelephonyDisplayInfoListener {
36 
37     private TelephonyManager mBaseTelephonyManager;
38     private Callback mCallback;
39     private Map<Integer, PhoneStateListener> mListeners;
40     private Map<Integer, TelephonyDisplayInfo> mDisplayInfos;
41 
42     private static final TelephonyDisplayInfo mDefaultTelephonyDisplayInfo =
43             new TelephonyDisplayInfo(TelephonyManager.NETWORK_TYPE_UNKNOWN,
44                     TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE);
45     /**
46      * Interface of callback and to use notify TelephonyDisplayInfo change.
47      */
48     public interface Callback {
49         /**
50          * Used to notify TelephonyDisplayInfo change.
51          */
onTelephonyDisplayInfoChanged(int subId, TelephonyDisplayInfo telephonyDisplayInfo)52         void onTelephonyDisplayInfoChanged(int subId, TelephonyDisplayInfo telephonyDisplayInfo);
53     }
54 
TelephonyDisplayInfoListener(Context context, Callback callback)55     public TelephonyDisplayInfoListener(Context context, Callback callback) {
56         mBaseTelephonyManager = context.getSystemService(TelephonyManager.class);
57         mCallback = callback;
58         mListeners = new HashMap<>();
59         mDisplayInfos = new HashMap<>();
60     }
61     /**
62      * Get TelephonyDisplayInfo.
63      */
getTelephonyDisplayInfo(int subId)64     public TelephonyDisplayInfo getTelephonyDisplayInfo(int subId) {
65         return mDisplayInfos.get(subId);
66     }
67 
68     /** Resumes listening telephony display info changes to the set of ids from the last call to
69      * {@link #updateSubscriptionIds(Set)}  */
resume()70     public void resume() {
71         for (int subId : mListeners.keySet()) {
72             startListening(subId);
73         }
74     }
75 
76     /** Pauses listening for telephony display info changes */
pause()77     public void pause() {
78         for (int subId : mListeners.keySet()) {
79             stopListening(subId);
80         }
81     }
82 
83     /** Updates the set of ids we want to be listening for, beginning to listen for any new ids and
84      * stopping listening for any ids not contained in the new set */
updateSubscriptionIds(Set<Integer> ids)85     public void updateSubscriptionIds(Set<Integer> ids) {
86         Set<Integer> currentIds = new ArraySet<>(mListeners.keySet());
87         for (int idToRemove : Sets.difference(currentIds, ids)) {
88             stopListening(idToRemove);
89             mListeners.remove(idToRemove);
90             mDisplayInfos.remove(idToRemove);
91         }
92         for (int idToAdd : Sets.difference(ids, currentIds)) {
93             PhoneStateListener listener = new PhoneStateListener() {
94                 @Override
95                 public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) {
96                     mDisplayInfos.put(idToAdd, telephonyDisplayInfo);
97                     mCallback.onTelephonyDisplayInfoChanged(idToAdd, telephonyDisplayInfo);
98                 }
99             };
100             mDisplayInfos.put(idToAdd, mDefaultTelephonyDisplayInfo);
101             mListeners.put(idToAdd, listener);
102             startListening(idToAdd);
103         }
104     }
105 
startListening(int subId)106     private void startListening(int subId) {
107         TelephonyManager mgr = mBaseTelephonyManager.createForSubscriptionId(subId);
108         mgr.listen(mListeners.get(subId), PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED);
109     }
110 
stopListening(int subId)111     private void stopListening(int subId) {
112         TelephonyManager mgr = mBaseTelephonyManager.createForSubscriptionId(subId);
113         mgr.listen(mListeners.get(subId), PhoneStateListener.LISTEN_NONE);
114     }
115 }
116