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