DefaultPhoneNotifier.java revision 0825495a331bb44df395a0cdb79fab85e68db5d5
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.util.Log;
28
29import com.android.internal.telephony.ITelephonyRegistry;
30
31/**
32 * broadcast intents
33 */
34public class DefaultPhoneNotifier implements PhoneNotifier {
35
36    static final String LOG_TAG = "GSM";
37    private static final boolean DBG = true;
38    private ITelephonyRegistry mRegistry;
39
40    /*package*/
41    DefaultPhoneNotifier() {
42        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
43                    "telephony.registry"));
44    }
45
46    public void notifyPhoneState(Phone sender) {
47        Call ringingCall = sender.getRingingCall();
48        String incomingNumber = "";
49        if (ringingCall != null && ringingCall.getEarliestConnection() != null){
50            incomingNumber = ringingCall.getEarliestConnection().getAddress();
51        }
52        try {
53            mRegistry.notifyCallState(convertCallState(sender.getState()), incomingNumber);
54        } catch (RemoteException ex) {
55            // system process is dead
56        }
57    }
58
59    public void notifyServiceState(Phone sender) {
60        ServiceState ss = sender.getServiceState();
61        if (ss == null) {
62            ss = new ServiceState();
63            ss.setStateOutOfService();
64        }
65        try {
66            mRegistry.notifyServiceState(ss);
67        } catch (RemoteException ex) {
68            // system process is dead
69        }
70    }
71
72    public void notifySignalStrength(Phone sender) {
73        try {
74            mRegistry.notifySignalStrength(sender.getSignalStrength());
75        } catch (RemoteException ex) {
76            // system process is dead
77        }
78    }
79
80    public void notifyMessageWaitingChanged(Phone sender) {
81        try {
82            mRegistry.notifyMessageWaitingChanged(sender.getMessageWaitingIndicator());
83        } catch (RemoteException ex) {
84            // system process is dead
85        }
86    }
87
88    public void notifyCallForwardingChanged(Phone sender) {
89        try {
90            mRegistry.notifyCallForwardingChanged(sender.getCallForwardingIndicator());
91        } catch (RemoteException ex) {
92            // system process is dead
93        }
94    }
95
96    public void notifyDataActivity(Phone sender) {
97        try {
98            mRegistry.notifyDataActivity(convertDataActivityState(sender.getDataActivityState()));
99        } catch (RemoteException ex) {
100            // system process is dead
101        }
102    }
103
104    public void notifyDataConnection(Phone sender, String reason, String apnType,
105            PhoneConstants.DataState state) {
106        doNotifyDataConnection(sender, reason, apnType, state);
107    }
108
109    private void doNotifyDataConnection(Phone sender, String reason, String apnType,
110            PhoneConstants.DataState state) {
111        // TODO
112        // use apnType as the key to which connection we're talking about.
113        // pass apnType back up to fetch particular for this one.
114        TelephonyManager telephony = TelephonyManager.getDefault();
115        LinkProperties linkProperties = null;
116        LinkCapabilities linkCapabilities = null;
117        boolean roaming = false;
118
119        if (state == PhoneConstants.DataState.CONNECTED) {
120            linkProperties = sender.getLinkProperties(apnType);
121            linkCapabilities = sender.getLinkCapabilities(apnType);
122        }
123        ServiceState ss = sender.getServiceState();
124        if (ss != null) roaming = ss.getRoaming();
125
126        try {
127            mRegistry.notifyDataConnection(
128                    convertDataState(state),
129                    sender.isDataConnectivityPossible(apnType), reason,
130                    sender.getActiveApnHost(apnType),
131                    apnType,
132                    linkProperties,
133                    linkCapabilities,
134                    ((telephony!=null) ? telephony.getNetworkType() :
135                    TelephonyManager.NETWORK_TYPE_UNKNOWN),
136                    roaming);
137        } catch (RemoteException ex) {
138            // system process is dead
139        }
140    }
141
142    public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) {
143        try {
144            mRegistry.notifyDataConnectionFailed(reason, apnType);
145        } catch (RemoteException ex) {
146            // system process is dead
147        }
148    }
149
150    public void notifyCellLocation(Phone sender) {
151        Bundle data = new Bundle();
152        sender.getCellLocation().fillInNotifierBundle(data);
153        try {
154            mRegistry.notifyCellLocation(data);
155        } catch (RemoteException ex) {
156            // system process is dead
157        }
158    }
159
160    public void notifyCellInfo(Phone sender, CellInfo cellInfo) {
161        try {
162            mRegistry.notifyCellInfo(cellInfo);
163        } catch (RemoteException ex) {
164
165        }
166    }
167
168    public void notifyOtaspChanged(Phone sender, int otaspMode) {
169        try {
170            mRegistry.notifyOtaspChanged(otaspMode);
171        } catch (RemoteException ex) {
172            // system process is dead
173        }
174    }
175
176    private void log(String s) {
177        Log.d(LOG_TAG, "[PhoneNotifier] " + s);
178    }
179
180    /**
181     * Convert the {@link State} enum into the TelephonyManager.CALL_STATE_* constants
182     * for the public API.
183     */
184    public static int convertCallState(PhoneConstants.State state) {
185        switch (state) {
186            case RINGING:
187                return TelephonyManager.CALL_STATE_RINGING;
188            case OFFHOOK:
189                return TelephonyManager.CALL_STATE_OFFHOOK;
190            default:
191                return TelephonyManager.CALL_STATE_IDLE;
192        }
193    }
194
195    /**
196     * Convert the TelephonyManager.CALL_STATE_* constants into the {@link State} enum
197     * for the public API.
198     */
199    public static PhoneConstants.State convertCallState(int state) {
200        switch (state) {
201            case TelephonyManager.CALL_STATE_RINGING:
202                return PhoneConstants.State.RINGING;
203            case TelephonyManager.CALL_STATE_OFFHOOK:
204                return PhoneConstants.State.OFFHOOK;
205            default:
206                return PhoneConstants.State.IDLE;
207        }
208    }
209
210    /**
211     * Convert the {@link DataState} enum into the TelephonyManager.DATA_* constants
212     * for the public API.
213     */
214    public static int convertDataState(PhoneConstants.DataState state) {
215        switch (state) {
216            case CONNECTING:
217                return TelephonyManager.DATA_CONNECTING;
218            case CONNECTED:
219                return TelephonyManager.DATA_CONNECTED;
220            case SUSPENDED:
221                return TelephonyManager.DATA_SUSPENDED;
222            default:
223                return TelephonyManager.DATA_DISCONNECTED;
224        }
225    }
226
227    /**
228     * Convert the TelephonyManager.DATA_* constants into {@link DataState} enum
229     * for the public API.
230     */
231    public static PhoneConstants.DataState convertDataState(int state) {
232        switch (state) {
233            case TelephonyManager.DATA_CONNECTING:
234                return PhoneConstants.DataState.CONNECTING;
235            case TelephonyManager.DATA_CONNECTED:
236                return PhoneConstants.DataState.CONNECTED;
237            case TelephonyManager.DATA_SUSPENDED:
238                return PhoneConstants.DataState.SUSPENDED;
239            default:
240                return PhoneConstants.DataState.DISCONNECTED;
241        }
242    }
243
244    /**
245     * Convert the {@link DataState} enum into the TelephonyManager.DATA_* constants
246     * for the public API.
247     */
248    public static int convertDataActivityState(Phone.DataActivityState state) {
249        switch (state) {
250            case DATAIN:
251                return TelephonyManager.DATA_ACTIVITY_IN;
252            case DATAOUT:
253                return TelephonyManager.DATA_ACTIVITY_OUT;
254            case DATAINANDOUT:
255                return TelephonyManager.DATA_ACTIVITY_INOUT;
256            case DORMANT:
257                return TelephonyManager.DATA_ACTIVITY_DORMANT;
258            default:
259                return TelephonyManager.DATA_ACTIVITY_NONE;
260        }
261    }
262
263    /**
264     * Convert the TelephonyManager.DATA_* constants into the {@link DataState} enum
265     * for the public API.
266     */
267    public static Phone.DataActivityState convertDataActivityState(int state) {
268        switch (state) {
269            case TelephonyManager.DATA_ACTIVITY_IN:
270                return Phone.DataActivityState.DATAIN;
271            case TelephonyManager.DATA_ACTIVITY_OUT:
272                return Phone.DataActivityState.DATAOUT;
273            case TelephonyManager.DATA_ACTIVITY_INOUT:
274                return Phone.DataActivityState.DATAINANDOUT;
275            case TelephonyManager.DATA_ACTIVITY_DORMANT:
276                return Phone.DataActivityState.DORMANT;
277            default:
278                return Phone.DataActivityState.NONE;
279        }
280    }
281}
282