DefaultPhoneNotifier.java revision ec6e2f9f13bac23cdfa0de0a56826b21050b6487
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.LinkProperties;
20import android.net.NetworkCapabilities;
21import android.os.Bundle;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.telephony.CellInfo;
25import android.telephony.Rlog;
26import android.telephony.VoLteServiceState;
27import android.telephony.ServiceState;
28import android.telephony.SubscriptionManager;
29import android.telephony.TelephonyManager;
30import android.telephony.PreciseCallState;
31import android.telephony.DisconnectCause;
32
33import com.android.internal.telephony.Call;
34import com.android.internal.telephony.CallManager;
35import com.android.internal.telephony.Phone;
36import com.android.internal.telephony.ITelephonyRegistry;
37import com.android.internal.telephony.PhoneConstants;
38
39import java.util.List;
40
41/**
42 * broadcast intents
43 */
44public class DefaultPhoneNotifier implements PhoneNotifier {
45    private static final String LOG_TAG = "DefaultPhoneNotifier";
46    private static final boolean DBG = false; // STOPSHIP if true
47
48    protected ITelephonyRegistry mRegistry;
49
50    public DefaultPhoneNotifier() {
51        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
52                    "telephony.registry"));
53    }
54
55    @Override
56    public void notifyPhoneState(Phone sender) {
57        Call ringingCall = sender.getRingingCall();
58        int subId = sender.getSubId();
59        String incomingNumber = "";
60        if (ringingCall != null && ringingCall.getEarliestConnection() != null){
61            incomingNumber = ringingCall.getEarliestConnection().getAddress();
62        }
63        try {
64            if (mRegistry != null) {
65                  mRegistry.notifyCallStateForSubscriber(subId,
66                        convertCallState(sender.getState()), incomingNumber);
67            }
68        } catch (RemoteException ex) {
69            // system process is dead
70        }
71    }
72
73    @Override
74    public void notifyServiceState(Phone sender) {
75        ServiceState ss = sender.getServiceState();
76        int phoneId = sender.getPhoneId();
77        int subId = sender.getSubId();
78
79        Rlog.d(LOG_TAG, "nofityServiceState: mRegistry=" + mRegistry + " ss=" + ss
80                + " sender=" + sender + " phondId=" + phoneId + " subId=" + subId);
81        if (ss == null) {
82            ss = new ServiceState();
83            ss.setStateOutOfService();
84        }
85        try {
86            if (mRegistry != null) {
87                mRegistry.notifyServiceStateForPhoneId(phoneId, subId, ss);
88            }
89        } catch (RemoteException ex) {
90            // system process is dead
91        }
92    }
93
94    @Override
95    public void notifySignalStrength(Phone sender) {
96        int subId = sender.getSubId();
97        if (DBG) {
98            // too chatty to log constantly
99            Rlog.d(LOG_TAG, "notifySignalStrength: mRegistry=" + mRegistry
100                    + " ss=" + sender.getSignalStrength() + " sender=" + sender);
101        }
102        try {
103            if (mRegistry != null) {
104                mRegistry.notifySignalStrengthForSubscriber(subId, sender.getSignalStrength());
105            }
106        } catch (RemoteException ex) {
107            // system process is dead
108        }
109    }
110
111    @Override
112    public void notifyMessageWaitingChanged(Phone sender) {
113        int phoneId = sender.getPhoneId();
114        int subId = sender.getSubId();
115
116        try {
117            if (mRegistry != null) {
118                mRegistry.notifyMessageWaitingChangedForPhoneId(phoneId, subId,
119                        sender.getMessageWaitingIndicator());
120            }
121        } catch (RemoteException ex) {
122            // system process is dead
123        }
124    }
125
126    @Override
127    public void notifyCallForwardingChanged(Phone sender) {
128        int subId = sender.getSubId();
129        try {
130            if (mRegistry != null) {
131                mRegistry.notifyCallForwardingChangedForSubscriber(subId,
132                        sender.getCallForwardingIndicator());
133            }
134        } catch (RemoteException ex) {
135            // system process is dead
136        }
137    }
138
139    @Override
140    public void notifyDataActivity(Phone sender) {
141        int subId = sender.getSubId();
142        try {
143            if (mRegistry != null) {
144                mRegistry.notifyDataActivityForSubscriber(subId,
145                        convertDataActivityState(sender.getDataActivityState()));
146            }
147        } catch (RemoteException ex) {
148            // system process is dead
149        }
150    }
151
152    @Override
153    public void notifyDataConnection(Phone sender, String reason, String apnType,
154            PhoneConstants.DataState state) {
155        doNotifyDataConnection(sender, reason, apnType, state);
156    }
157
158    private void doNotifyDataConnection(Phone sender, String reason, String apnType,
159            PhoneConstants.DataState state) {
160        int subId = sender.getSubId();
161        long dds = SubscriptionManager.getDefaultDataSubscriptionId();
162        if (DBG) log("subId = " + subId + ", DDS = " + dds);
163
164        // TODO
165        // use apnType as the key to which connection we're talking about.
166        // pass apnType back up to fetch particular for this one.
167        TelephonyManager telephony = TelephonyManager.getDefault();
168        LinkProperties linkProperties = null;
169        NetworkCapabilities networkCapabilities = null;
170        boolean roaming = false;
171
172        if (state == PhoneConstants.DataState.CONNECTED) {
173            linkProperties = sender.getLinkProperties(apnType);
174            networkCapabilities = sender.getNetworkCapabilities(apnType);
175        }
176        ServiceState ss = sender.getServiceState();
177        if (ss != null) roaming = ss.getDataRoaming();
178
179        try {
180            if (mRegistry != null) {
181                mRegistry.notifyDataConnectionForSubscriber(subId,
182                    convertDataState(state),
183                    sender.isDataConnectivityPossible(apnType), reason,
184                    sender.getActiveApnHost(apnType),
185                    apnType,
186                    linkProperties,
187                    networkCapabilities,
188                    ((telephony!=null) ? telephony.getDataNetworkType(subId) :
189                    TelephonyManager.NETWORK_TYPE_UNKNOWN),
190                    roaming);
191            }
192        } catch (RemoteException ex) {
193            // system process is dead
194        }
195    }
196
197    @Override
198    public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) {
199        int subId = sender.getSubId();
200        try {
201            if (mRegistry != null) {
202                mRegistry.notifyDataConnectionFailedForSubscriber(subId, reason, apnType);
203            }
204        } catch (RemoteException ex) {
205            // system process is dead
206        }
207    }
208
209    @Override
210    public void notifyCellLocation(Phone sender) {
211        int subId = sender.getSubId();
212        Bundle data = new Bundle();
213        sender.getCellLocation().fillInNotifierBundle(data);
214        try {
215            if (mRegistry != null) {
216                mRegistry.notifyCellLocationForSubscriber(subId, data);
217            }
218        } catch (RemoteException ex) {
219            // system process is dead
220        }
221    }
222
223    @Override
224    public void notifyCellInfo(Phone sender, List<CellInfo> cellInfo) {
225        int subId = sender.getSubId();
226        try {
227            if (mRegistry != null) {
228                mRegistry.notifyCellInfoForSubscriber(subId, cellInfo);
229            }
230        } catch (RemoteException ex) {
231
232        }
233    }
234
235    @Override
236    public void notifyOtaspChanged(Phone sender, int otaspMode) {
237        // FIXME: subId?
238        try {
239            if (mRegistry != null) {
240                mRegistry.notifyOtaspChanged(otaspMode);
241            }
242        } catch (RemoteException ex) {
243            // system process is dead
244        }
245    }
246
247    public void notifyPreciseCallState(Phone sender) {
248        // FIXME: subId?
249        Call ringingCall = sender.getRingingCall();
250        Call foregroundCall = sender.getForegroundCall();
251        Call backgroundCall = sender.getBackgroundCall();
252        if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
253            try {
254                mRegistry.notifyPreciseCallState(
255                        convertPreciseCallState(ringingCall.getState()),
256                        convertPreciseCallState(foregroundCall.getState()),
257                        convertPreciseCallState(backgroundCall.getState()));
258            } catch (RemoteException ex) {
259                // system process is dead
260            }
261        }
262    }
263
264    public void notifyDisconnectCause(int cause, int preciseCause) {
265        // FIXME: subId?
266        try {
267            mRegistry.notifyDisconnectCause(cause, preciseCause);
268        } catch (RemoteException ex) {
269            // system process is dead
270        }
271    }
272
273    public void notifyPreciseDataConnectionFailed(Phone sender, String reason, String apnType,
274            String apn, String failCause) {
275        // FIXME: subId?
276        try {
277            mRegistry.notifyPreciseDataConnectionFailed(reason, apnType, apn, failCause);
278        } catch (RemoteException ex) {
279            // system process is dead
280        }
281    }
282
283    @Override
284    public void notifyVoLteServiceStateChanged(Phone sender, VoLteServiceState lteState) {
285        // FIXME: subID
286        try {
287            mRegistry.notifyVoLteServiceStateChanged(lteState);
288        } catch (RemoteException ex) {
289            // system process is dead
290        }
291    }
292
293    @Override
294    public void notifyOemHookRawEventForSubscriber(int subId, byte[] rawData) {
295        try {
296            mRegistry.notifyOemHookRawEventForSubscriber(subId, rawData);
297        } catch (RemoteException ex) {
298            // system process is dead
299        }
300    }
301
302    /**
303     * Convert the {@link PhoneConstants.State} enum into the TelephonyManager.CALL_STATE_*
304     * constants for the public API.
305     */
306    public static int convertCallState(PhoneConstants.State state) {
307        switch (state) {
308            case RINGING:
309                return TelephonyManager.CALL_STATE_RINGING;
310            case OFFHOOK:
311                return TelephonyManager.CALL_STATE_OFFHOOK;
312            default:
313                return TelephonyManager.CALL_STATE_IDLE;
314        }
315    }
316
317    /**
318     * Convert the TelephonyManager.CALL_STATE_* constants into the
319     * {@link PhoneConstants.State} enum for the public API.
320     */
321    public static PhoneConstants.State convertCallState(int state) {
322        switch (state) {
323            case TelephonyManager.CALL_STATE_RINGING:
324                return PhoneConstants.State.RINGING;
325            case TelephonyManager.CALL_STATE_OFFHOOK:
326                return PhoneConstants.State.OFFHOOK;
327            default:
328                return PhoneConstants.State.IDLE;
329        }
330    }
331
332    /**
333     * Convert the {@link PhoneConstants.DataState} enum into the TelephonyManager.DATA_* constants
334     * for the public API.
335     */
336    public static int convertDataState(PhoneConstants.DataState state) {
337        switch (state) {
338            case CONNECTING:
339                return TelephonyManager.DATA_CONNECTING;
340            case CONNECTED:
341                return TelephonyManager.DATA_CONNECTED;
342            case SUSPENDED:
343                return TelephonyManager.DATA_SUSPENDED;
344            default:
345                return TelephonyManager.DATA_DISCONNECTED;
346        }
347    }
348
349    /**
350     * Convert the TelephonyManager.DATA_* constants into {@link PhoneConstants.DataState} enum
351     * for the public API.
352     */
353    public static PhoneConstants.DataState convertDataState(int state) {
354        switch (state) {
355            case TelephonyManager.DATA_CONNECTING:
356                return PhoneConstants.DataState.CONNECTING;
357            case TelephonyManager.DATA_CONNECTED:
358                return PhoneConstants.DataState.CONNECTED;
359            case TelephonyManager.DATA_SUSPENDED:
360                return PhoneConstants.DataState.SUSPENDED;
361            default:
362                return PhoneConstants.DataState.DISCONNECTED;
363        }
364    }
365
366    /**
367     * Convert the {@link Phone.DataActivityState} enum into the TelephonyManager.DATA_* constants
368     * for the public API.
369     */
370    public static int convertDataActivityState(Phone.DataActivityState state) {
371        switch (state) {
372            case DATAIN:
373                return TelephonyManager.DATA_ACTIVITY_IN;
374            case DATAOUT:
375                return TelephonyManager.DATA_ACTIVITY_OUT;
376            case DATAINANDOUT:
377                return TelephonyManager.DATA_ACTIVITY_INOUT;
378            case DORMANT:
379                return TelephonyManager.DATA_ACTIVITY_DORMANT;
380            default:
381                return TelephonyManager.DATA_ACTIVITY_NONE;
382        }
383    }
384
385    /**
386     * Convert the {@link Call.State} enum into the PreciseCallState.PRECISE_CALL_STATE_* constants
387     * for the public API.
388     */
389    public static int convertPreciseCallState(Call.State state) {
390        switch (state) {
391            case ACTIVE:
392                return PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
393            case HOLDING:
394                return PreciseCallState.PRECISE_CALL_STATE_HOLDING;
395            case DIALING:
396                return PreciseCallState.PRECISE_CALL_STATE_DIALING;
397            case ALERTING:
398                return PreciseCallState.PRECISE_CALL_STATE_ALERTING;
399            case INCOMING:
400                return PreciseCallState.PRECISE_CALL_STATE_INCOMING;
401            case WAITING:
402                return PreciseCallState.PRECISE_CALL_STATE_WAITING;
403            case DISCONNECTED:
404                return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED;
405            case DISCONNECTING:
406                return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING;
407            default:
408                return PreciseCallState.PRECISE_CALL_STATE_IDLE;
409        }
410    }
411
412    private void log(String s) {
413        Rlog.d(LOG_TAG, s);
414    }
415}
416