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.os.Bundle;
20import android.os.RemoteException;
21import android.os.ServiceManager;
22import android.telephony.TelephonyManager;
23import android.util.Log;
24
25import com.android.internal.telephony.ITelephonyRegistry;
26
27/**
28 * broadcast intents
29 */
30public class DefaultPhoneNotifier implements PhoneNotifier {
31
32    static final String LOG_TAG = "GSM";
33    private static final boolean DBG = true;
34    private ITelephonyRegistry mRegistry;
35
36    /*package*/
37    DefaultPhoneNotifier() {
38        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
39                    "telephony.registry"));
40    }
41
42    public void notifyPhoneState(Phone sender) {
43        Call ringingCall = sender.getRingingCall();
44        String incomingNumber = "";
45        if (ringingCall != null && ringingCall.getEarliestConnection() != null){
46            incomingNumber = ringingCall.getEarliestConnection().getAddress();
47        }
48        try {
49            mRegistry.notifyCallState(convertCallState(sender.getState()), incomingNumber);
50        } catch (RemoteException ex) {
51            // system process is dead
52        }
53    }
54
55    public void notifyServiceState(Phone sender) {
56        try {
57            mRegistry.notifyServiceState(sender.getServiceState());
58        } catch (RemoteException ex) {
59            // system process is dead
60        }
61    }
62
63    public void notifySignalStrength(Phone sender) {
64        try {
65            mRegistry.notifySignalStrength(sender.getSignalStrength());
66        } catch (RemoteException ex) {
67            // system process is dead
68        }
69    }
70
71    public void notifyMessageWaitingChanged(Phone sender) {
72        try {
73            mRegistry.notifyMessageWaitingChanged(sender.getMessageWaitingIndicator());
74        } catch (RemoteException ex) {
75            // system process is dead
76        }
77    }
78
79    public void notifyCallForwardingChanged(Phone sender) {
80        try {
81            mRegistry.notifyCallForwardingChanged(sender.getCallForwardingIndicator());
82        } catch (RemoteException ex) {
83            // system process is dead
84        }
85    }
86
87    public void notifyDataActivity(Phone sender) {
88        try {
89            mRegistry.notifyDataActivity(convertDataActivityState(sender.getDataActivityState()));
90        } catch (RemoteException ex) {
91            // system process is dead
92        }
93    }
94
95    public void notifyDataConnection(Phone sender, String reason) {
96        TelephonyManager telephony = TelephonyManager.getDefault();
97        try {
98            mRegistry.notifyDataConnection(
99                    convertDataState(sender.getDataConnectionState()),
100                    sender.isDataConnectivityPossible(), reason,
101                    sender.getActiveApn(),
102                    sender.getActiveApnTypes(),
103                    sender.getInterfaceName(null),
104                    ((telephony!=null) ? telephony.getNetworkType() :
105                    TelephonyManager.NETWORK_TYPE_UNKNOWN),
106                    sender.getGateway(null));
107        } catch (RemoteException ex) {
108            // system process is dead
109        }
110    }
111
112    public void notifyDataConnectionFailed(Phone sender, String reason) {
113        try {
114            mRegistry.notifyDataConnectionFailed(reason);
115        } catch (RemoteException ex) {
116            // system process is dead
117        }
118    }
119
120    public void notifyCellLocation(Phone sender) {
121        Bundle data = new Bundle();
122        sender.getCellLocation().fillInNotifierBundle(data);
123        try {
124            mRegistry.notifyCellLocation(data);
125        } catch (RemoteException ex) {
126            // system process is dead
127        }
128    }
129
130    private void log(String s) {
131        Log.d(LOG_TAG, "[PhoneNotifier] " + s);
132    }
133
134    /**
135     * Convert the {@link State} enum into the TelephonyManager.CALL_STATE_* constants
136     * for the public API.
137     */
138    public static int convertCallState(Phone.State state) {
139        switch (state) {
140            case RINGING:
141                return TelephonyManager.CALL_STATE_RINGING;
142            case OFFHOOK:
143                return TelephonyManager.CALL_STATE_OFFHOOK;
144            default:
145                return TelephonyManager.CALL_STATE_IDLE;
146        }
147    }
148
149    /**
150     * Convert the TelephonyManager.CALL_STATE_* constants into the {@link State} enum
151     * for the public API.
152     */
153    public static Phone.State convertCallState(int state) {
154        switch (state) {
155            case TelephonyManager.CALL_STATE_RINGING:
156                return Phone.State.RINGING;
157            case TelephonyManager.CALL_STATE_OFFHOOK:
158                return Phone.State.OFFHOOK;
159            default:
160                return Phone.State.IDLE;
161        }
162    }
163
164    /**
165     * Convert the {@link DataState} enum into the TelephonyManager.DATA_* constants
166     * for the public API.
167     */
168    public static int convertDataState(Phone.DataState state) {
169        switch (state) {
170            case CONNECTING:
171                return TelephonyManager.DATA_CONNECTING;
172            case CONNECTED:
173                return TelephonyManager.DATA_CONNECTED;
174            case SUSPENDED:
175                return TelephonyManager.DATA_SUSPENDED;
176            default:
177                return TelephonyManager.DATA_DISCONNECTED;
178        }
179    }
180
181    /**
182     * Convert the TelephonyManager.DATA_* constants into {@link DataState} enum
183     * for the public API.
184     */
185    public static Phone.DataState convertDataState(int state) {
186        switch (state) {
187            case TelephonyManager.DATA_CONNECTING:
188                return Phone.DataState.CONNECTING;
189            case TelephonyManager.DATA_CONNECTED:
190                return Phone.DataState.CONNECTED;
191            case TelephonyManager.DATA_SUSPENDED:
192                return Phone.DataState.SUSPENDED;
193            default:
194                return Phone.DataState.DISCONNECTED;
195        }
196    }
197
198    /**
199     * Convert the {@link DataState} enum into the TelephonyManager.DATA_* constants
200     * for the public API.
201     */
202    public static int convertDataActivityState(Phone.DataActivityState state) {
203        switch (state) {
204            case DATAIN:
205                return TelephonyManager.DATA_ACTIVITY_IN;
206            case DATAOUT:
207                return TelephonyManager.DATA_ACTIVITY_OUT;
208            case DATAINANDOUT:
209                return TelephonyManager.DATA_ACTIVITY_INOUT;
210            case DORMANT:
211                return TelephonyManager.DATA_ACTIVITY_DORMANT;
212            default:
213                return TelephonyManager.DATA_ACTIVITY_NONE;
214        }
215    }
216
217    /**
218     * Convert the TelephonyManager.DATA_* constants into the {@link DataState} enum
219     * for the public API.
220     */
221    public static Phone.DataActivityState convertDataActivityState(int state) {
222        switch (state) {
223            case TelephonyManager.DATA_ACTIVITY_IN:
224                return Phone.DataActivityState.DATAIN;
225            case TelephonyManager.DATA_ACTIVITY_OUT:
226                return Phone.DataActivityState.DATAOUT;
227            case TelephonyManager.DATA_ACTIVITY_INOUT:
228                return Phone.DataActivityState.DATAINANDOUT;
229            case TelephonyManager.DATA_ACTIVITY_DORMANT:
230                return Phone.DataActivityState.DORMANT;
231            default:
232                return Phone.DataActivityState.NONE;
233        }
234    }
235}
236