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