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