1 /*
2  * Copyright (C) 2006 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.internal.telephony;
18 
19 import android.annotation.NonNull;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.content.Context;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.telephony.Annotation.DataFailureCause;
25 import android.telephony.Annotation.RadioPowerState;
26 import android.telephony.Annotation.SrvccState;
27 import android.telephony.BarringInfo;
28 import android.telephony.CallQuality;
29 import android.telephony.CellIdentity;
30 import android.telephony.CellInfo;
31 import android.telephony.PhoneCapability;
32 import android.telephony.PreciseCallState;
33 import android.telephony.PreciseDataConnectionState;
34 import android.telephony.ServiceState;
35 import android.telephony.TelephonyDisplayInfo;
36 import android.telephony.TelephonyManager;
37 import android.telephony.TelephonyRegistryManager;
38 import android.telephony.emergency.EmergencyNumber;
39 import android.telephony.ims.ImsReasonInfo;
40 
41 import com.android.internal.telephony.PhoneInternalInterface.DataActivityState;
42 import com.android.telephony.Rlog;
43 
44 import java.util.List;
45 
46 /**
47  * broadcast intents
48  */
49 public class DefaultPhoneNotifier implements PhoneNotifier {
50 
51     private static final String LOG_TAG = "DefaultPhoneNotifier";
52     private static final boolean DBG = false; // STOPSHIP if true
53 
54     @UnsupportedAppUsage
55     protected ITelephonyRegistry mRegistry;
56     private TelephonyRegistryManager mTelephonyRegistryMgr;
57 
58 
DefaultPhoneNotifier(Context context)59     public DefaultPhoneNotifier(Context context) {
60         mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
61             "telephony.registry"));
62         mTelephonyRegistryMgr = (TelephonyRegistryManager) context.getSystemService(
63             Context.TELEPHONY_REGISTRY_SERVICE);
64     }
65 
66     @Override
notifyPhoneState(Phone sender)67     public void notifyPhoneState(Phone sender) {
68         Call ringingCall = sender.getRingingCall();
69         int subId = sender.getSubId();
70         int phoneId = sender.getPhoneId();
71         String incomingNumber = "";
72         if (ringingCall != null && ringingCall.getEarliestConnection() != null) {
73             incomingNumber = ringingCall.getEarliestConnection().getAddress();
74         }
75         mTelephonyRegistryMgr.notifyCallStateChanged(subId, phoneId,
76             PhoneConstantConversions.convertCallState(sender.getState()), incomingNumber);
77     }
78 
79     @Override
notifyServiceState(Phone sender)80     public void notifyServiceState(Phone sender) {
81         ServiceState ss = sender.getServiceState();
82         int phoneId = sender.getPhoneId();
83         int subId = sender.getSubId();
84 
85         Rlog.d(LOG_TAG, "notifyServiceState: mRegistry=" + mRegistry + " ss=" + ss
86             + " sender=" + sender + " phondId=" + phoneId + " subId=" + subId);
87         if (ss == null) {
88             ss = new ServiceState();
89             ss.setStateOutOfService();
90         }
91         mTelephonyRegistryMgr.notifyServiceStateChanged(subId, phoneId, ss);
92     }
93 
94     @Override
notifySignalStrength(Phone sender)95     public void notifySignalStrength(Phone sender) {
96         int phoneId = sender.getPhoneId();
97         int subId = sender.getSubId();
98         if (DBG) {
99             // too chatty to log constantly
100             Rlog.d(LOG_TAG, "notifySignalStrength: mRegistry=" + mRegistry
101                 + " ss=" + sender.getSignalStrength() + " sender=" + sender);
102         }
103         mTelephonyRegistryMgr.notifySignalStrengthChanged(subId, phoneId,
104             sender.getSignalStrength());
105     }
106 
107     @Override
notifyMessageWaitingChanged(Phone sender)108     public void notifyMessageWaitingChanged(Phone sender) {
109         int phoneId = sender.getPhoneId();
110         int subId = sender.getSubId();
111         mTelephonyRegistryMgr.notifyMessageWaitingChanged(subId, phoneId,
112             sender.getMessageWaitingIndicator());
113     }
114 
115     @Override
notifyCallForwardingChanged(Phone sender)116     public void notifyCallForwardingChanged(Phone sender) {
117         int subId = sender.getSubId();
118         Rlog.d(LOG_TAG, "notifyCallForwardingChanged: subId=" + subId + ", isCFActive="
119             + sender.getCallForwardingIndicator());
120 
121         mTelephonyRegistryMgr.notifyCallForwardingChanged(subId,
122             sender.getCallForwardingIndicator());
123     }
124 
125     @Override
notifyDataActivity(Phone sender)126     public void notifyDataActivity(Phone sender) {
127         int subId = sender.getSubId();
128         if (mRegistry != null) {
129             mTelephonyRegistryMgr.notifyDataActivityChanged(subId,
130                 convertDataActivityState(sender.getDataActivityState()));
131         }
132     }
133 
134     @Override
notifyDataConnection( Phone sender, String apnType, PreciseDataConnectionState preciseState)135     public void notifyDataConnection(
136             Phone sender, String apnType, PreciseDataConnectionState preciseState) {
137 
138         int subId = sender.getSubId();
139         int phoneId = sender.getPhoneId();
140 
141         mTelephonyRegistryMgr.notifyDataConnectionForSubscriber(
142                 phoneId, subId, apnType, preciseState);
143     }
144 
145     @Override
notifyCellLocation(Phone sender, CellIdentity cellIdentity)146     public void notifyCellLocation(Phone sender, CellIdentity cellIdentity) {
147         int subId = sender.getSubId();
148         mTelephonyRegistryMgr.notifyCellLocation(subId, cellIdentity);
149     }
150 
151     @Override
notifyCellInfo(Phone sender, List<CellInfo> cellInfo)152     public void notifyCellInfo(Phone sender, List<CellInfo> cellInfo) {
153         int subId = sender.getSubId();
154         mTelephonyRegistryMgr.notifyCellInfoChanged(subId, cellInfo);
155     }
156 
notifyPreciseCallState(Phone sender)157     public void notifyPreciseCallState(Phone sender) {
158         Call ringingCall = sender.getRingingCall();
159         Call foregroundCall = sender.getForegroundCall();
160         Call backgroundCall = sender.getBackgroundCall();
161         if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
162             mTelephonyRegistryMgr.notifyPreciseCallState(sender.getSubId(), sender.getPhoneId(),
163                 convertPreciseCallState(ringingCall.getState()),
164                 convertPreciseCallState(foregroundCall.getState()),
165                 convertPreciseCallState(backgroundCall.getState()));
166         }
167     }
168 
notifyDisconnectCause(Phone sender, int cause, int preciseCause)169     public void notifyDisconnectCause(Phone sender, int cause, int preciseCause) {
170         mTelephonyRegistryMgr.notifyDisconnectCause(sender.getSubId(), sender.getPhoneId(), cause,
171             preciseCause);
172     }
173 
174     @Override
notifyImsDisconnectCause(@onNull Phone sender, ImsReasonInfo imsReasonInfo)175     public void notifyImsDisconnectCause(@NonNull Phone sender, ImsReasonInfo imsReasonInfo) {
176         mTelephonyRegistryMgr.notifyImsDisconnectCause(sender.getSubId(), imsReasonInfo);
177     }
178 
179     @Override
180     /** Notify the TelephonyRegistry that a data connection has failed with a specified cause */
notifyDataConnectionFailed(Phone sender, String apnType, String apn, @DataFailureCause int failCause)181     public void notifyDataConnectionFailed(Phone sender, String apnType,
182         String apn, @DataFailureCause int failCause) {
183         mTelephonyRegistryMgr.notifyPreciseDataConnectionFailed(sender.getSubId(),
184             sender.getPhoneId(), apnType, apn, failCause);
185     }
186 
187     @Override
notifySrvccStateChanged(Phone sender, @SrvccState int state)188     public void notifySrvccStateChanged(Phone sender, @SrvccState int state) {
189         mTelephonyRegistryMgr.notifySrvccStateChanged(sender.getSubId(), state);
190     }
191 
192     @Override
notifyDataActivationStateChanged(Phone sender, int activationState)193     public void notifyDataActivationStateChanged(Phone sender, int activationState) {
194         mTelephonyRegistryMgr.notifyDataActivationStateChanged(sender.getSubId(),
195             sender.getPhoneId(), activationState);
196     }
197 
198     @Override
notifyVoiceActivationStateChanged(Phone sender, int activationState)199     public void notifyVoiceActivationStateChanged(Phone sender, int activationState) {
200         mTelephonyRegistryMgr.notifyVoiceActivationStateChanged(sender.getSubId(),
201             sender.getPhoneId(),  activationState);
202     }
203 
204     @Override
notifyUserMobileDataStateChanged(Phone sender, boolean state)205     public void notifyUserMobileDataStateChanged(Phone sender, boolean state) {
206         mTelephonyRegistryMgr.notifyUserMobileDataStateChanged(sender.getPhoneId(),
207                 sender.getSubId(), state);
208     }
209 
210     @Override
notifyDisplayInfoChanged(Phone sender, TelephonyDisplayInfo telephonyDisplayInfo)211     public void notifyDisplayInfoChanged(Phone sender, TelephonyDisplayInfo telephonyDisplayInfo) {
212         mTelephonyRegistryMgr.notifyDisplayInfoChanged(sender.getPhoneId(), sender.getSubId(),
213                 telephonyDisplayInfo);
214     }
215 
216     @Override
notifyPhoneCapabilityChanged(PhoneCapability capability)217     public void notifyPhoneCapabilityChanged(PhoneCapability capability) {
218         mTelephonyRegistryMgr.notifyPhoneCapabilityChanged(capability);
219     }
220 
221     @Override
notifyRadioPowerStateChanged(Phone sender, @RadioPowerState int state)222     public void notifyRadioPowerStateChanged(Phone sender, @RadioPowerState int state) {
223         mTelephonyRegistryMgr.notifyRadioPowerStateChanged(sender.getSubId(), sender.getPhoneId(),
224             state);
225     }
226 
227     @Override
notifyEmergencyNumberList(Phone sender)228     public void notifyEmergencyNumberList(Phone sender) {
229         mTelephonyRegistryMgr.notifyEmergencyNumberList(sender.getSubId(), sender.getPhoneId());
230     }
231 
232     @Override
notifyOutgoingEmergencyCall(Phone sender, EmergencyNumber emergencyNumber)233     public void notifyOutgoingEmergencyCall(Phone sender, EmergencyNumber emergencyNumber) {
234         try {
235             if (mRegistry != null) {
236                 mRegistry.notifyOutgoingEmergencyCall(sender.getPhoneId(), sender.getSubId(),
237                         emergencyNumber);
238             }
239         } catch (RemoteException ex) {
240             // system process is dead
241         }
242     }
243 
244     @Override
notifyOutgoingEmergencySms(Phone sender, EmergencyNumber emergencyNumber)245     public void notifyOutgoingEmergencySms(Phone sender, EmergencyNumber emergencyNumber) {
246         try {
247             if (mRegistry != null) {
248                 mRegistry.notifyOutgoingEmergencySms(sender.getPhoneId(), sender.getSubId(),
249                         emergencyNumber);
250             }
251         } catch (RemoteException ex) {
252             // system process is dead
253         }
254     }
255 
256     @Override
notifyCallQualityChanged(Phone sender, CallQuality callQuality, int callNetworkType)257     public void notifyCallQualityChanged(Phone sender, CallQuality callQuality,
258         int callNetworkType) {
259         mTelephonyRegistryMgr.notifyCallQualityChanged(sender.getSubId(), sender.getPhoneId(),
260             callQuality, callNetworkType);
261     }
262 
263     @Override
notifyRegistrationFailed(Phone sender, @NonNull CellIdentity cellIdentity, @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode)264     public void notifyRegistrationFailed(Phone sender, @NonNull CellIdentity cellIdentity,
265             @NonNull String chosenPlmn, int domain, int causeCode, int additionalCauseCode) {
266         mTelephonyRegistryMgr.notifyRegistrationFailed(sender.getPhoneId(), sender.getSubId(),
267                 cellIdentity, chosenPlmn, domain, causeCode, additionalCauseCode);
268     }
269 
270     @Override
notifyBarringInfoChanged(Phone sender, BarringInfo barringInfo)271     public void notifyBarringInfoChanged(Phone sender, BarringInfo barringInfo) {
272         mTelephonyRegistryMgr.notifyBarringInfoChanged(sender.getPhoneId(), sender.getSubId(),
273                 barringInfo);
274     }
275 
276     /**
277      * Convert the {@link DataActivityState} enum into the TelephonyManager.DATA_* constants for the
278      * public API.
279      */
convertDataActivityState(DataActivityState state)280     public static int convertDataActivityState(DataActivityState state) {
281         switch (state) {
282             case DATAIN:
283                 return TelephonyManager.DATA_ACTIVITY_IN;
284             case DATAOUT:
285                 return TelephonyManager.DATA_ACTIVITY_OUT;
286             case DATAINANDOUT:
287                 return TelephonyManager.DATA_ACTIVITY_INOUT;
288             case DORMANT:
289                 return TelephonyManager.DATA_ACTIVITY_DORMANT;
290             default:
291                 return TelephonyManager.DATA_ACTIVITY_NONE;
292         }
293     }
294 
295     /**
296      * Convert the {@link Call.State} enum into the PreciseCallState.PRECISE_CALL_STATE_* constants
297      * for the public API.
298      */
convertPreciseCallState(Call.State state)299     public static int convertPreciseCallState(Call.State state) {
300         switch (state) {
301             case ACTIVE:
302                 return PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
303             case HOLDING:
304                 return PreciseCallState.PRECISE_CALL_STATE_HOLDING;
305             case DIALING:
306                 return PreciseCallState.PRECISE_CALL_STATE_DIALING;
307             case ALERTING:
308                 return PreciseCallState.PRECISE_CALL_STATE_ALERTING;
309             case INCOMING:
310                 return PreciseCallState.PRECISE_CALL_STATE_INCOMING;
311             case WAITING:
312                 return PreciseCallState.PRECISE_CALL_STATE_WAITING;
313             case DISCONNECTED:
314                 return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED;
315             case DISCONNECTING:
316                 return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING;
317             default:
318                 return PreciseCallState.PRECISE_CALL_STATE_IDLE;
319         }
320     }
321 
log(String s)322     private void log(String s) {
323         Rlog.d(LOG_TAG, s);
324     }
325 }
326