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