DefaultPhoneNotifier.java revision b7b7a62112b79571adf74372c5f5366fd62d0031
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
17package com.android.internal.telephony;
18
19import android.net.LinkCapabilities;
20import android.net.LinkProperties;
21import android.os.Bundle;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.telephony.CellInfo;
25import android.telephony.ServiceState;
26import android.telephony.TelephonyManager;
27import android.telephony.PreciseCallState;
28import android.telephony.DisconnectCause;
29
30import com.android.internal.telephony.ITelephonyRegistry;
31
32import java.util.List;
33
34/**
35 * broadcast intents
36 */
37public class DefaultPhoneNotifier implements PhoneNotifier {
38
39    private ITelephonyRegistry mRegistry;
40
41    /*package*/
42    DefaultPhoneNotifier() {
43        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
44                    "telephony.registry"));
45    }
46
47    @Override
48    public void notifyPhoneState(Phone sender) {
49        Call ringingCall = sender.getRingingCall();
50        String incomingNumber = "";
51        if (ringingCall != null && ringingCall.getEarliestConnection() != null){
52            incomingNumber = ringingCall.getEarliestConnection().getAddress();
53        }
54        try {
55            mRegistry.notifyCallState(convertCallState(sender.getState()), incomingNumber);
56        } catch (RemoteException ex) {
57            // system process is dead
58        }
59    }
60
61    @Override
62    public void notifyServiceState(Phone sender) {
63        ServiceState ss = sender.getServiceState();
64        if (ss == null) {
65            ss = new ServiceState();
66            ss.setStateOutOfService();
67        }
68        try {
69            mRegistry.notifyServiceState(ss);
70        } catch (RemoteException ex) {
71            // system process is dead
72        }
73    }
74
75    @Override
76    public void notifySignalStrength(Phone sender) {
77        try {
78            mRegistry.notifySignalStrength(sender.getSignalStrength());
79        } catch (RemoteException ex) {
80            // system process is dead
81        }
82    }
83
84    @Override
85    public void notifyMessageWaitingChanged(Phone sender) {
86        try {
87            mRegistry.notifyMessageWaitingChanged(sender.getMessageWaitingIndicator());
88        } catch (RemoteException ex) {
89            // system process is dead
90        }
91    }
92
93    @Override
94    public void notifyCallForwardingChanged(Phone sender) {
95        try {
96            mRegistry.notifyCallForwardingChanged(sender.getCallForwardingIndicator());
97        } catch (RemoteException ex) {
98            // system process is dead
99        }
100    }
101
102    @Override
103    public void notifyDataActivity(Phone sender) {
104        try {
105            mRegistry.notifyDataActivity(convertDataActivityState(sender.getDataActivityState()));
106        } catch (RemoteException ex) {
107            // system process is dead
108        }
109    }
110
111    @Override
112    public void notifyDataConnection(Phone sender, String reason, String apnType,
113            PhoneConstants.DataState state) {
114        doNotifyDataConnection(sender, reason, apnType, state);
115    }
116
117    private void doNotifyDataConnection(Phone sender, String reason, String apnType,
118            PhoneConstants.DataState state) {
119        // TODO
120        // use apnType as the key to which connection we're talking about.
121        // pass apnType back up to fetch particular for this one.
122        TelephonyManager telephony = TelephonyManager.getDefault();
123        LinkProperties linkProperties = null;
124        LinkCapabilities linkCapabilities = null;
125        boolean roaming = false;
126
127        if (state == PhoneConstants.DataState.CONNECTED) {
128            linkProperties = sender.getLinkProperties(apnType);
129            linkCapabilities = sender.getLinkCapabilities(apnType);
130        }
131        ServiceState ss = sender.getServiceState();
132        if (ss != null) roaming = ss.getRoaming();
133
134        try {
135            mRegistry.notifyDataConnection(
136                    convertDataState(state),
137                    sender.isDataConnectivityPossible(apnType), reason,
138                    sender.getActiveApnHost(apnType),
139                    apnType,
140                    linkProperties,
141                    linkCapabilities,
142                    ((telephony!=null) ? telephony.getNetworkType() :
143                    TelephonyManager.NETWORK_TYPE_UNKNOWN),
144                    roaming);
145        } catch (RemoteException ex) {
146            // system process is dead
147        }
148    }
149
150    @Override
151    public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) {
152        try {
153            mRegistry.notifyDataConnectionFailed(reason, apnType);
154        } catch (RemoteException ex) {
155            // system process is dead
156        }
157    }
158
159    @Override
160    public void notifyCellLocation(Phone sender) {
161        Bundle data = new Bundle();
162        sender.getCellLocation().fillInNotifierBundle(data);
163        try {
164            mRegistry.notifyCellLocation(data);
165        } catch (RemoteException ex) {
166            // system process is dead
167        }
168    }
169
170    @Override
171    public void notifyCellInfo(Phone sender, List<CellInfo> cellInfo) {
172        try {
173            mRegistry.notifyCellInfo(cellInfo);
174        } catch (RemoteException ex) {
175
176        }
177    }
178
179    @Override
180    public void notifyOtaspChanged(Phone sender, int otaspMode) {
181        try {
182            mRegistry.notifyOtaspChanged(otaspMode);
183        } catch (RemoteException ex) {
184            // system process is dead
185        }
186    }
187
188    public void notifyPreciseCallState(Phone sender) {
189        Call ringingCall = sender.getRingingCall();
190        Call foregroundCall = sender.getForegroundCall();
191        Call backgroundCall = sender.getBackgroundCall();
192        if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
193            try {
194                mRegistry.notifyPreciseCallState(
195                        convertPreciseCallState(ringingCall.getState()),
196                        convertPreciseCallState(foregroundCall.getState()),
197                        convertPreciseCallState(backgroundCall.getState()));
198            } catch (RemoteException ex) {
199                // system process is dead
200            }
201        }
202    }
203
204    public void notifyDisconnectCause(int cause, int preciseCause) {
205        try {
206            mRegistry.notifyDisconnectCause(cause, preciseCause);
207        } catch (RemoteException ex) {
208            // system process is dead
209        }
210    }
211
212    public void notifyPreciseDataConnectionFailed(Phone sender, String reason, String apnType,
213            String apn, String failCause) {
214        try {
215            mRegistry.notifyPreciseDataConnectionFailed(reason, apnType, apn, failCause);
216        } catch (RemoteException ex) {
217            // system process is dead
218        }
219    }
220
221    /**
222     * Convert the {@link PhoneConstants.State} enum into the TelephonyManager.CALL_STATE_*
223     * constants for the public API.
224     */
225    public static int convertCallState(PhoneConstants.State state) {
226        switch (state) {
227            case RINGING:
228                return TelephonyManager.CALL_STATE_RINGING;
229            case OFFHOOK:
230                return TelephonyManager.CALL_STATE_OFFHOOK;
231            default:
232                return TelephonyManager.CALL_STATE_IDLE;
233        }
234    }
235
236    /**
237     * Convert the TelephonyManager.CALL_STATE_* constants into the
238     * {@link PhoneConstants.State} enum for the public API.
239     */
240    public static PhoneConstants.State convertCallState(int state) {
241        switch (state) {
242            case TelephonyManager.CALL_STATE_RINGING:
243                return PhoneConstants.State.RINGING;
244            case TelephonyManager.CALL_STATE_OFFHOOK:
245                return PhoneConstants.State.OFFHOOK;
246            default:
247                return PhoneConstants.State.IDLE;
248        }
249    }
250
251    /**
252     * Convert the {@link PhoneConstants.DataState} enum into the TelephonyManager.DATA_* constants
253     * for the public API.
254     */
255    public static int convertDataState(PhoneConstants.DataState state) {
256        switch (state) {
257            case CONNECTING:
258                return TelephonyManager.DATA_CONNECTING;
259            case CONNECTED:
260                return TelephonyManager.DATA_CONNECTED;
261            case SUSPENDED:
262                return TelephonyManager.DATA_SUSPENDED;
263            default:
264                return TelephonyManager.DATA_DISCONNECTED;
265        }
266    }
267
268    /**
269     * Convert the TelephonyManager.DATA_* constants into {@link PhoneConstants.DataState} enum
270     * for the public API.
271     */
272    public static PhoneConstants.DataState convertDataState(int state) {
273        switch (state) {
274            case TelephonyManager.DATA_CONNECTING:
275                return PhoneConstants.DataState.CONNECTING;
276            case TelephonyManager.DATA_CONNECTED:
277                return PhoneConstants.DataState.CONNECTED;
278            case TelephonyManager.DATA_SUSPENDED:
279                return PhoneConstants.DataState.SUSPENDED;
280            default:
281                return PhoneConstants.DataState.DISCONNECTED;
282        }
283    }
284
285    /**
286     * Convert the {@link Phone.DataActivityState} enum into the TelephonyManager.DATA_* constants
287     * for the public API.
288     */
289    public static int convertDataActivityState(Phone.DataActivityState state) {
290        switch (state) {
291            case DATAIN:
292                return TelephonyManager.DATA_ACTIVITY_IN;
293            case DATAOUT:
294                return TelephonyManager.DATA_ACTIVITY_OUT;
295            case DATAINANDOUT:
296                return TelephonyManager.DATA_ACTIVITY_INOUT;
297            case DORMANT:
298                return TelephonyManager.DATA_ACTIVITY_DORMANT;
299            default:
300                return TelephonyManager.DATA_ACTIVITY_NONE;
301        }
302    }
303
304    /**
305     * Convert the TelephonyManager.DATA_* constants into the {@link Phone.DataActivityState} enum
306     * for the public API.
307     */
308    public static Phone.DataActivityState convertDataActivityState(int state) {
309        switch (state) {
310            case TelephonyManager.DATA_ACTIVITY_IN:
311                return Phone.DataActivityState.DATAIN;
312            case TelephonyManager.DATA_ACTIVITY_OUT:
313                return Phone.DataActivityState.DATAOUT;
314            case TelephonyManager.DATA_ACTIVITY_INOUT:
315                return Phone.DataActivityState.DATAINANDOUT;
316            case TelephonyManager.DATA_ACTIVITY_DORMANT:
317                return Phone.DataActivityState.DORMANT;
318            default:
319                return Phone.DataActivityState.NONE;
320        }
321    }
322
323    /**
324     * Convert the {@link State} enum into the PreciseCallState.PRECISE_CALL_STATE_* constants
325     * for the public API.
326     */
327    public static int convertPreciseCallState(Call.State state) {
328        switch (state) {
329            case ACTIVE:
330                return PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
331            case HOLDING:
332                return PreciseCallState.PRECISE_CALL_STATE_HOLDING;
333            case DIALING:
334                return PreciseCallState.PRECISE_CALL_STATE_DIALING;
335            case ALERTING:
336                return PreciseCallState.PRECISE_CALL_STATE_ALERTING;
337            case INCOMING:
338                return PreciseCallState.PRECISE_CALL_STATE_INCOMING;
339            case WAITING:
340                return PreciseCallState.PRECISE_CALL_STATE_WAITING;
341            case DISCONNECTED:
342                return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED;
343            case DISCONNECTING:
344                return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING;
345            default:
346                return PreciseCallState.PRECISE_CALL_STATE_IDLE;
347        }
348    }
349
350    /**
351     * Convert the Call.State.* constants into the {@link State} enum
352     * for the public API.
353     */
354    public static Call.State convertPreciseCallState(int state) {
355        switch (state) {
356            case PreciseCallState.PRECISE_CALL_STATE_ACTIVE:
357                return Call.State.ACTIVE;
358            case PreciseCallState.PRECISE_CALL_STATE_HOLDING:
359                return Call.State.HOLDING;
360            case PreciseCallState.PRECISE_CALL_STATE_DIALING:
361                return Call.State.DIALING;
362            case PreciseCallState.PRECISE_CALL_STATE_ALERTING:
363                return Call.State.ALERTING;
364            case PreciseCallState.PRECISE_CALL_STATE_INCOMING:
365                return Call.State.INCOMING;
366            case PreciseCallState.PRECISE_CALL_STATE_WAITING:
367                return Call.State.WAITING;
368            case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED:
369                return Call.State.DISCONNECTED;
370            case PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING:
371                return Call.State.DISCONNECTING;
372            default:
373                return Call.State.IDLE;
374        }
375    }
376}
377