DefaultPhoneNotifier.java revision 9af99511111eeb5e9930185dc44259502811e3b6
1cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann/*
2cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * Copyright (C) 2006 The Android Open Source Project
3cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann *
4cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * Licensed under the Apache License, Version 2.0 (the "License");
5cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * you may not use this file except in compliance with the License.
6cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * You may obtain a copy of the License at
7cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann *
8cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann *      http://www.apache.org/licenses/LICENSE-2.0
9cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann *
10cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * Unless required by applicable law or agreed to in writing, software
11cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * distributed under the License is distributed on an "AS IS" BASIS,
12cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * See the License for the specific language governing permissions and
14cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * limitations under the License.
15cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann */
16cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
17cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannpackage com.android.internal.telephony;
18cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
19cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.net.LinkProperties;
20cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.net.NetworkCapabilities;
21cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.os.Bundle;
22cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.os.RemoteException;
23cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.os.ServiceManager;
24cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.CellInfo;
25cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.Rlog;
26cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.VoLteServiceState;
27cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.ServiceState;
28cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.SubscriptionManager;
29cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.TelephonyManager;
30cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport android.telephony.PreciseCallState;
31cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
32cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannimport java.util.List;
33cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
34cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann/**
35cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann * broadcast intents
36cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann */
37cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmannpublic class DefaultPhoneNotifier implements PhoneNotifier {
38cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    private static final String LOG_TAG = "DefaultPhoneNotifier";
39cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    private static final boolean DBG = false; // STOPSHIP if true
40cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
41cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    protected ITelephonyRegistry mRegistry;
42cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
43cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    public DefaultPhoneNotifier() {
44cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
45cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann                    "telephony.registry"));
46cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    }
47cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann
48cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    @Override
49cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann    public void notifyPhoneState(Phone sender) {
50cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann        Call ringingCall = sender.getRingingCall();
51cc021c9a204a17109c9ddf4cb03fc5b174f748b0Philip P. Moltmann        int subId = sender.getSubId();
52        int phoneId = sender.getPhoneId();
53        String incomingNumber = "";
54        if (ringingCall != null && ringingCall.getEarliestConnection() != null) {
55            incomingNumber = ringingCall.getEarliestConnection().getAddress();
56        }
57        try {
58            if (mRegistry != null) {
59                  mRegistry.notifyCallStateForPhoneId(phoneId, subId,
60                        convertCallState(sender.getState()), incomingNumber);
61            }
62        } catch (RemoteException ex) {
63            // system process is dead
64        }
65    }
66
67    @Override
68    public void notifyServiceState(Phone sender) {
69        ServiceState ss = sender.getServiceState();
70        int phoneId = sender.getPhoneId();
71        int subId = sender.getSubId();
72
73        Rlog.d(LOG_TAG, "nofityServiceState: mRegistry=" + mRegistry + " ss=" + ss
74                + " sender=" + sender + " phondId=" + phoneId + " subId=" + subId);
75        if (ss == null) {
76            ss = new ServiceState();
77            ss.setStateOutOfService();
78        }
79        try {
80            if (mRegistry != null) {
81                mRegistry.notifyServiceStateForPhoneId(phoneId, subId, ss);
82            }
83        } catch (RemoteException ex) {
84            // system process is dead
85        }
86    }
87
88    @Override
89    public void notifySignalStrength(Phone sender) {
90        int phoneId = sender.getPhoneId();
91        int subId = sender.getSubId();
92        if (DBG) {
93            // too chatty to log constantly
94            Rlog.d(LOG_TAG, "notifySignalStrength: mRegistry=" + mRegistry
95                    + " ss=" + sender.getSignalStrength() + " sender=" + sender);
96        }
97        try {
98            if (mRegistry != null) {
99                mRegistry.notifySignalStrengthForPhoneId(phoneId, subId,
100                        sender.getSignalStrength());
101            }
102        } catch (RemoteException ex) {
103            // system process is dead
104        }
105    }
106
107    @Override
108    public void notifyMessageWaitingChanged(Phone sender) {
109        int phoneId = sender.getPhoneId();
110        int subId = sender.getSubId();
111
112        try {
113            if (mRegistry != null) {
114                mRegistry.notifyMessageWaitingChangedForPhoneId(phoneId, subId,
115                        sender.getMessageWaitingIndicator());
116            }
117        } catch (RemoteException ex) {
118            // system process is dead
119        }
120    }
121
122    @Override
123    public void notifyCallForwardingChanged(Phone sender) {
124        int subId = sender.getSubId();
125        try {
126            if (mRegistry != null) {
127                mRegistry.notifyCallForwardingChangedForSubscriber(subId,
128                        sender.getCallForwardingIndicator());
129            }
130        } catch (RemoteException ex) {
131            // system process is dead
132        }
133    }
134
135    @Override
136    public void notifyDataActivity(Phone sender) {
137        int subId = sender.getSubId();
138        try {
139            if (mRegistry != null) {
140                mRegistry.notifyDataActivityForSubscriber(subId,
141                        convertDataActivityState(sender.getDataActivityState()));
142            }
143        } catch (RemoteException ex) {
144            // system process is dead
145        }
146    }
147
148    @Override
149    public void notifyDataConnection(Phone sender, String reason, String apnType,
150            PhoneConstants.DataState state) {
151        doNotifyDataConnection(sender, reason, apnType, state);
152    }
153
154    private void doNotifyDataConnection(Phone sender, String reason, String apnType,
155            PhoneConstants.DataState state) {
156        int subId = sender.getSubId();
157        long dds = SubscriptionManager.getDefaultDataSubscriptionId();
158        if (DBG) log("subId = " + subId + ", DDS = " + dds);
159
160        // TODO
161        // use apnType as the key to which connection we're talking about.
162        // pass apnType back up to fetch particular for this one.
163        TelephonyManager telephony = TelephonyManager.getDefault();
164        LinkProperties linkProperties = null;
165        NetworkCapabilities networkCapabilities = null;
166        boolean roaming = false;
167
168        if (state == PhoneConstants.DataState.CONNECTED) {
169            linkProperties = sender.getLinkProperties(apnType);
170            networkCapabilities = sender.getNetworkCapabilities(apnType);
171        }
172        ServiceState ss = sender.getServiceState();
173        if (ss != null) roaming = ss.getDataRoaming();
174
175        try {
176            if (mRegistry != null) {
177                mRegistry.notifyDataConnectionForSubscriber(subId,
178                    convertDataState(state),
179                    sender.isDataConnectivityPossible(apnType), reason,
180                    sender.getActiveApnHost(apnType),
181                    apnType,
182                    linkProperties,
183                    networkCapabilities,
184                    ((telephony!=null) ? telephony.getDataNetworkType(subId) :
185                    TelephonyManager.NETWORK_TYPE_UNKNOWN),
186                    roaming);
187            }
188        } catch (RemoteException ex) {
189            // system process is dead
190        }
191    }
192
193    @Override
194    public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) {
195        int subId = sender.getSubId();
196        try {
197            if (mRegistry != null) {
198                mRegistry.notifyDataConnectionFailedForSubscriber(subId, reason, apnType);
199            }
200        } catch (RemoteException ex) {
201            // system process is dead
202        }
203    }
204
205    @Override
206    public void notifyCellLocation(Phone sender) {
207        int subId = sender.getSubId();
208        Bundle data = new Bundle();
209        sender.getCellLocation().fillInNotifierBundle(data);
210        try {
211            if (mRegistry != null) {
212                mRegistry.notifyCellLocationForSubscriber(subId, data);
213            }
214        } catch (RemoteException ex) {
215            // system process is dead
216        }
217    }
218
219    @Override
220    public void notifyCellInfo(Phone sender, List<CellInfo> cellInfo) {
221        int subId = sender.getSubId();
222        try {
223            if (mRegistry != null) {
224                mRegistry.notifyCellInfoForSubscriber(subId, cellInfo);
225            }
226        } catch (RemoteException ex) {
227
228        }
229    }
230
231    @Override
232    public void notifyOtaspChanged(Phone sender, int otaspMode) {
233        // FIXME: subId?
234        try {
235            if (mRegistry != null) {
236                mRegistry.notifyOtaspChanged(otaspMode);
237            }
238        } catch (RemoteException ex) {
239            // system process is dead
240        }
241    }
242
243    public void notifyPreciseCallState(Phone sender) {
244        // FIXME: subId?
245        Call ringingCall = sender.getRingingCall();
246        Call foregroundCall = sender.getForegroundCall();
247        Call backgroundCall = sender.getBackgroundCall();
248        if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
249            try {
250                mRegistry.notifyPreciseCallState(
251                        convertPreciseCallState(ringingCall.getState()),
252                        convertPreciseCallState(foregroundCall.getState()),
253                        convertPreciseCallState(backgroundCall.getState()));
254            } catch (RemoteException ex) {
255                // system process is dead
256            }
257        }
258    }
259
260    public void notifyDisconnectCause(int cause, int preciseCause) {
261        // FIXME: subId?
262        try {
263            mRegistry.notifyDisconnectCause(cause, preciseCause);
264        } catch (RemoteException ex) {
265            // system process is dead
266        }
267    }
268
269    public void notifyPreciseDataConnectionFailed(Phone sender, String reason, String apnType,
270            String apn, String failCause) {
271        // FIXME: subId?
272        try {
273            mRegistry.notifyPreciseDataConnectionFailed(reason, apnType, apn, failCause);
274        } catch (RemoteException ex) {
275            // system process is dead
276        }
277    }
278
279    @Override
280    public void notifyVoLteServiceStateChanged(Phone sender, VoLteServiceState lteState) {
281        // FIXME: subID
282        try {
283            mRegistry.notifyVoLteServiceStateChanged(lteState);
284        } catch (RemoteException ex) {
285            // system process is dead
286        }
287    }
288
289    @Override
290    public void notifyDataActivationStateChanged(Phone sender, int activationState) {
291        try {
292            mRegistry.notifySimActivationStateChangedForPhoneId(sender.getPhoneId(),
293                    sender.getSubId(), PhoneConstants.SIM_ACTIVATION_TYPE_DATA, activationState);
294        } catch (RemoteException ex) {
295            // system process is dead
296        }
297    }
298
299    @Override
300    public void notifyVoiceActivationStateChanged(Phone sender, int activationState) {
301        try {
302            mRegistry.notifySimActivationStateChangedForPhoneId(sender.getPhoneId(),
303                    sender.getSubId(), PhoneConstants.SIM_ACTIVATION_TYPE_VOICE, activationState);
304        } catch (RemoteException ex) {
305            // system process is dead
306        }
307    }
308
309    @Override
310    public void notifyOemHookRawEventForSubscriber(int subId, byte[] rawData) {
311        try {
312            mRegistry.notifyOemHookRawEventForSubscriber(subId, rawData);
313        } catch (RemoteException ex) {
314            // system process is dead
315        }
316    }
317
318    /**
319     * Convert the {@link PhoneConstants.State} enum into the TelephonyManager.CALL_STATE_*
320     * constants for the public API.
321     */
322    public static int convertCallState(PhoneConstants.State state) {
323        switch (state) {
324            case RINGING:
325                return TelephonyManager.CALL_STATE_RINGING;
326            case OFFHOOK:
327                return TelephonyManager.CALL_STATE_OFFHOOK;
328            default:
329                return TelephonyManager.CALL_STATE_IDLE;
330        }
331    }
332
333    /**
334     * Convert the TelephonyManager.CALL_STATE_* constants into the
335     * {@link PhoneConstants.State} enum for the public API.
336     */
337    public static PhoneConstants.State convertCallState(int state) {
338        switch (state) {
339            case TelephonyManager.CALL_STATE_RINGING:
340                return PhoneConstants.State.RINGING;
341            case TelephonyManager.CALL_STATE_OFFHOOK:
342                return PhoneConstants.State.OFFHOOK;
343            default:
344                return PhoneConstants.State.IDLE;
345        }
346    }
347
348    /**
349     * Convert the {@link PhoneConstants.DataState} enum into the TelephonyManager.DATA_* constants
350     * for the public API.
351     */
352    public static int convertDataState(PhoneConstants.DataState state) {
353        switch (state) {
354            case CONNECTING:
355                return TelephonyManager.DATA_CONNECTING;
356            case CONNECTED:
357                return TelephonyManager.DATA_CONNECTED;
358            case SUSPENDED:
359                return TelephonyManager.DATA_SUSPENDED;
360            default:
361                return TelephonyManager.DATA_DISCONNECTED;
362        }
363    }
364
365    /**
366     * Convert the TelephonyManager.DATA_* constants into {@link PhoneConstants.DataState} enum
367     * for the public API.
368     */
369    public static PhoneConstants.DataState convertDataState(int state) {
370        switch (state) {
371            case TelephonyManager.DATA_CONNECTING:
372                return PhoneConstants.DataState.CONNECTING;
373            case TelephonyManager.DATA_CONNECTED:
374                return PhoneConstants.DataState.CONNECTED;
375            case TelephonyManager.DATA_SUSPENDED:
376                return PhoneConstants.DataState.SUSPENDED;
377            default:
378                return PhoneConstants.DataState.DISCONNECTED;
379        }
380    }
381
382    /**
383     * Convert the {@link Phone.DataActivityState} enum into the TelephonyManager.DATA_* constants
384     * for the public API.
385     */
386    public static int convertDataActivityState(Phone.DataActivityState state) {
387        switch (state) {
388            case DATAIN:
389                return TelephonyManager.DATA_ACTIVITY_IN;
390            case DATAOUT:
391                return TelephonyManager.DATA_ACTIVITY_OUT;
392            case DATAINANDOUT:
393                return TelephonyManager.DATA_ACTIVITY_INOUT;
394            case DORMANT:
395                return TelephonyManager.DATA_ACTIVITY_DORMANT;
396            default:
397                return TelephonyManager.DATA_ACTIVITY_NONE;
398        }
399    }
400
401    /**
402     * Convert the {@link Call.State} enum into the PreciseCallState.PRECISE_CALL_STATE_* constants
403     * for the public API.
404     */
405    public static int convertPreciseCallState(Call.State state) {
406        switch (state) {
407            case ACTIVE:
408                return PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
409            case HOLDING:
410                return PreciseCallState.PRECISE_CALL_STATE_HOLDING;
411            case DIALING:
412                return PreciseCallState.PRECISE_CALL_STATE_DIALING;
413            case ALERTING:
414                return PreciseCallState.PRECISE_CALL_STATE_ALERTING;
415            case INCOMING:
416                return PreciseCallState.PRECISE_CALL_STATE_INCOMING;
417            case WAITING:
418                return PreciseCallState.PRECISE_CALL_STATE_WAITING;
419            case DISCONNECTED:
420                return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED;
421            case DISCONNECTING:
422                return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING;
423            default:
424                return PreciseCallState.PRECISE_CALL_STATE_IDLE;
425        }
426    }
427
428    private void log(String s) {
429        Rlog.d(LOG_TAG, s);
430    }
431}
432