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