• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 
17 package com.googlecode.android_scripting.facade.telephony;
18 
19 import android.annotation.Nullable;
20 import android.app.Service;
21 import android.content.ContentResolver;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.database.Cursor;
25 import android.net.TrafficStats;
26 import android.net.Uri;
27 import android.os.RemoteException;
28 import android.provider.Telephony;
29 import android.sysprop.TelephonyProperties;
30 import android.telephony.AvailableNetworkInfo;
31 import android.telephony.CellInfo;
32 import android.telephony.CellLocation;
33 import android.telephony.NeighboringCellInfo;
34 import android.telephony.PhoneStateListener;
35 import android.telephony.ServiceState;
36 import android.telephony.SignalStrength;
37 import android.telephony.SubscriptionManager;
38 import android.telephony.TelephonyManager;
39 
40 import com.android.internal.telephony.RILConstants;
41 
42 import com.google.common.io.BaseEncoding;
43 import com.googlecode.android_scripting.Log;
44 import com.googlecode.android_scripting.facade.AndroidFacade;
45 import com.googlecode.android_scripting.facade.EventFacade;
46 import com.googlecode.android_scripting.facade.FacadeManager;
47 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
48                                                    .ActiveDataSubIdChangeListener;
49 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
50                                                    .CallStateChangeListener;
51 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
52                                                    .CellInfoChangeListener;
53 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
54                                                    .DataConnectionRealTimeInfoChangeListener;
55 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
56                                                    .DataConnectionStateChangeListener;
57 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
58                                                    .DisplayInfoStateChangeListener;
59 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
60                                                    .ServiceStateChangeListener;
61 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
62                                                    .SignalStrengthChangeListener;
63 import com.googlecode.android_scripting.facade.telephony.TelephonyStateListeners
64                                                    .VoiceMailStateChangeListener;
65 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
66 import com.googlecode.android_scripting.rpc.Rpc;
67 import com.googlecode.android_scripting.rpc.RpcDefault;
68 import com.googlecode.android_scripting.rpc.RpcOptional;
69 import com.googlecode.android_scripting.rpc.RpcParameter;
70 
71 import java.util.ArrayList;
72 import java.util.Arrays;
73 import java.util.HashMap;
74 import java.util.List;
75 import java.util.concurrent.Executor;
76 
77 /**
78  * Exposes TelephonyManager functionality.
79  *
80  */
81 public class TelephonyManagerFacade extends RpcReceiver {
82 
83     private final Service mService;
84     private final AndroidFacade mAndroidFacade;
85     private final EventFacade mEventFacade;
86     private final TelephonyManager mTelephonyManager;
87     private final SubscriptionManager mSubscriptionManager;
88     private List<AvailableNetworkInfo> availableNetworkList;
89     private HashMap<Integer, StateChangeListener> mStateChangeListeners =
90                              new HashMap<Integer, StateChangeListener>();
91 
92     private static final String[] sProjection = new String[] {
93             Telephony.Carriers._ID, // 0
94             Telephony.Carriers.NAME, // 1
95             Telephony.Carriers.APN, // 2
96             Telephony.Carriers.PROXY, // 3
97             Telephony.Carriers.PORT, // 4
98             Telephony.Carriers.USER, // 5
99             Telephony.Carriers.SERVER, // 6
100             Telephony.Carriers.PASSWORD, // 7
101             Telephony.Carriers.MMSC, // 8
102             Telephony.Carriers.MCC, // 9
103             Telephony.Carriers.MNC, // 10
104             Telephony.Carriers.NUMERIC, // 11
105             Telephony.Carriers.MMSPROXY,// 12
106             Telephony.Carriers.MMSPORT, // 13
107             Telephony.Carriers.AUTH_TYPE, // 14
108             Telephony.Carriers.TYPE, // 15
109             Telephony.Carriers.PROTOCOL, // 16
110             Telephony.Carriers.CARRIER_ENABLED, // 17
111             Telephony.Carriers.BEARER_BITMASK, // 18
112             Telephony.Carriers.ROAMING_PROTOCOL, // 19
113             Telephony.Carriers.MVNO_TYPE, // 20
114             Telephony.Carriers.MVNO_MATCH_DATA // 21
115     };
116 
TelephonyManagerFacade(FacadeManager manager)117     public TelephonyManagerFacade(FacadeManager manager) {
118         super(manager);
119         mService = manager.getService();
120         mTelephonyManager =
121                 (TelephonyManager) mService.getSystemService(Context.TELEPHONY_SERVICE);
122         mAndroidFacade = manager.getReceiver(AndroidFacade.class);
123         mEventFacade = manager.getReceiver(EventFacade.class);
124         mSubscriptionManager = SubscriptionManager.from(mService);
125     }
126 
127     /**
128     * Reset TelephonyManager settings to factory default.
129     * @param subId the subriber id to be reset, use default id if not provided.
130     */
131     @Rpc(description = "Resets TelephonyManager settings to factory default.")
telephonyFactoryReset( @pcOptional @pcParametername = "subId") Integer subId)132     public void telephonyFactoryReset(
133             @RpcOptional @RpcParameter(name = "subId") Integer subId) {
134         if (subId == null) {
135             subId = SubscriptionManager.getDefaultVoiceSubscriptionId();
136         }
137         mTelephonyManager.factoryReset(subId);
138     }
139 
140     /**
141     * Reset TelephonyManager settings to factory default.
142     * @param subId the subriber id to be reset, use default id if not provided.
143     */
144     @Rpc(description = "Resets Telephony and IMS settings to factory default.")
telephonyResetSettings( @pcOptional @pcParametername = "subId") Integer subId)145     public void telephonyResetSettings(
146             @RpcOptional @RpcParameter(name = "subId") Integer subId) {
147         if (subId == null) {
148             subId = SubscriptionManager.getDefaultVoiceSubscriptionId();
149         }
150         mTelephonyManager.createForSubscriptionId(subId).resetSettings();
151     }
152 
153     @Rpc(description = "Set network preference.")
telephonySetPreferredNetworkTypes( @pcParametername = "nwPreference") String nwPreference)154     public boolean telephonySetPreferredNetworkTypes(
155         @RpcParameter(name = "nwPreference") String nwPreference) {
156         return telephonySetPreferredNetworkTypesForSubscription(nwPreference,
157                 SubscriptionManager.getDefaultSubscriptionId());
158     }
159 
160     @Rpc(description = "Set network preference for subscription.")
telephonySetPreferredNetworkTypesForSubscription( @pcParametername = "nwPreference") String nwPreference, @RpcParameter(name = "subId") Integer subId)161     public boolean telephonySetPreferredNetworkTypesForSubscription(
162             @RpcParameter(name = "nwPreference") String nwPreference,
163             @RpcParameter(name = "subId") Integer subId) {
164         int networkPreferenceInt = TelephonyUtils.getNetworkModeIntfromString(
165             nwPreference);
166         if (RILConstants.RIL_ERRNO_INVALID_RESPONSE != networkPreferenceInt) {
167             return mTelephonyManager.setPreferredNetworkType(
168                 subId, networkPreferenceInt);
169         } else {
170             return false;
171         }
172     }
173 
174     /**
175     * Set network selection mode to automatic for subscriber.
176     * @param subId the subriber id to be set.
177     */
178     @Rpc(description = "Set network selection mode to automatic for subscriber.")
telephonySetNetworkSelectionModeAutomaticForSubscription( @pcParametername = "subId") Integer subId)179     public void telephonySetNetworkSelectionModeAutomaticForSubscription(
180             @RpcParameter(name = "subId") Integer subId) {
181         mTelephonyManager.setNetworkSelectionModeAutomatic();
182     }
183 
184     @Rpc(description = "Get network preference.")
telephonyGetPreferredNetworkTypes()185     public String telephonyGetPreferredNetworkTypes() {
186         return telephonyGetPreferredNetworkTypesForSubscription(
187                 SubscriptionManager.getDefaultSubscriptionId());
188     }
189 
190     @Rpc(description = "Get network preference for subscription.")
telephonyGetPreferredNetworkTypesForSubscription( @pcParametername = "subId") Integer subId)191     public String telephonyGetPreferredNetworkTypesForSubscription(
192             @RpcParameter(name = "subId") Integer subId) {
193         int networkPreferenceInt = mTelephonyManager.getPreferredNetworkType(subId);
194         return TelephonyUtils.getNetworkModeStringfromInt(networkPreferenceInt);
195     }
196 
197     @Rpc(description = "Get current voice network type")
telephonyGetCurrentVoiceNetworkType()198     public String telephonyGetCurrentVoiceNetworkType() {
199         return telephonyGetCurrentVoiceNetworkTypeForSubscription(
200                 SubscriptionManager.getDefaultSubscriptionId());
201     }
202 
203     @Rpc(description = "Get current voice network type for subscription")
telephonyGetCurrentVoiceNetworkTypeForSubscription( @pcParametername = "subId") Integer subId)204     public String telephonyGetCurrentVoiceNetworkTypeForSubscription(
205             @RpcParameter(name = "subId") Integer subId) {
206         return TelephonyUtils.getNetworkTypeString(
207             mTelephonyManager.getVoiceNetworkType(subId));
208     }
209 
210     @Rpc(description = "Get current data network type")
telephonyGetCurrentDataNetworkType()211     public String telephonyGetCurrentDataNetworkType() {
212         return telephonyGetCurrentDataNetworkTypeForSubscription(
213                 SubscriptionManager.getDefaultSubscriptionId());
214     }
215 
216     @Rpc(description = "Get current data network type for subscription")
telephonyGetCurrentDataNetworkTypeForSubscription( @pcParametername = "subId") Integer subId)217     public String telephonyGetCurrentDataNetworkTypeForSubscription(
218             @RpcParameter(name = "subId") Integer subId) {
219         return TelephonyUtils.getNetworkTypeString(
220             mTelephonyManager.getDataNetworkType(subId));
221     }
222 
223     @Rpc(description = "Get if phone have voice capability")
telephonyIsVoiceCapable()224     public Boolean telephonyIsVoiceCapable() {
225         return mTelephonyManager.isVoiceCapable();
226     }
227 
228     @Rpc(description = "Get preferred network setting for " +
229                        "default subscription ID .Return value is integer.")
telephonyGetPreferredNetworkTypeInteger()230     public int telephonyGetPreferredNetworkTypeInteger() {
231         return telephonyGetPreferredNetworkTypeIntegerForSubscription(
232                                          SubscriptionManager.getDefaultSubscriptionId());
233     }
234 
235     @Rpc(description = "Get preferred network setting for " +
236                        "specified subscription ID .Return value is integer.")
telephonyGetPreferredNetworkTypeIntegerForSubscription( @pcParametername = "subId") Integer subId)237     public int telephonyGetPreferredNetworkTypeIntegerForSubscription(
238                @RpcParameter(name = "subId") Integer subId) {
239         return mTelephonyManager.getPreferredNetworkType(subId);
240     }
241 
242     @Rpc(description = "Starts tracking call state change" +
243                        "for default subscription ID.")
telephonyStartTrackingCallState()244     public Boolean telephonyStartTrackingCallState() {
245         return telephonyStartTrackingCallStateForSubscription(
246                               SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
247     }
248 
249     @Rpc(description = "Starts tracking call state change" +
250                        "for specified subscription ID.")
telephonyStartTrackingCallStateForSubscription( @pcParametername = "subId") Integer subId)251     public Boolean telephonyStartTrackingCallStateForSubscription(
252                 @RpcParameter(name = "subId") Integer subId) {
253         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
254         if(listener == null) {
255             Log.e("Invalid subscription ID");
256             return false;
257         }
258         mTelephonyManager.createForSubscriptionId(subId).listen(
259             listener.mCallStateChangeListener,
260             CallStateChangeListener.sListeningStates);
261         return true;
262     }
263 
264     @Rpc(description = "Starts tracking cell info change" +
265                        "for default subscription ID.")
telephonyStartTrackingCellInfoChange()266     public Boolean telephonyStartTrackingCellInfoChange() {
267         return telephonyStartTrackingCellInfoChangeForSubscription(
268                               SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
269     }
270 
271     @Rpc(description = "Starts tracking cell info change" +
272                        "for specified subscription ID.")
telephonyStartTrackingCellInfoChangeForSubscription( @pcParametername = "subId") Integer subId)273     public Boolean telephonyStartTrackingCellInfoChangeForSubscription(
274                 @RpcParameter(name = "subId") Integer subId) {
275         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
276         if(listener == null) {
277             Log.e("Invalid subscription ID");
278             return false;
279         }
280         mTelephonyManager.createForSubscriptionId(subId).listen(
281             listener.mCellInfoChangeListener,
282             PhoneStateListener.LISTEN_CELL_INFO);
283         return true;
284     }
285 
286     @Rpc(description = "Starts tracking active opportunistic data change" +
287                        "for default subscription ID.")
telephonyStartTrackingActiveDataChange()288     public Boolean telephonyStartTrackingActiveDataChange() {
289         return telephonyStartTrackingActiveDataChangeForSubscription(
290                               SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
291     }
292 
293     @Rpc(description = "Starts tracking active opportunistic data change" +
294                        "for specified subscription ID.")
telephonyStartTrackingActiveDataChangeForSubscription( @pcParametername = "subId") Integer subId)295     public Boolean telephonyStartTrackingActiveDataChangeForSubscription(
296                 @RpcParameter(name = "subId") Integer subId) {
297         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
298         if(listener == null) {
299             Log.e("Invalid subscription ID");
300             return false;
301         }
302         mTelephonyManager.createForSubscriptionId(subId).listen(
303             listener.mActiveDataSubIdChangeListener,
304             PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
305         return true;
306     }
307 
308     @Rpc(description = "Starts tracking display info change" +
309                        "for default subscription ID.")
telephonyStartTrackingDisplayInfoChange()310     public Boolean telephonyStartTrackingDisplayInfoChange() {
311         return telephonyStartTrackingDisplayInfoChangeForSubscription(
312                               SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
313     }
314 
315     @Rpc(description = "Starts tracking display info change" +
316                        "for specified subscription ID.")
telephonyStartTrackingDisplayInfoChangeForSubscription( @pcParametername = "subId") Integer subId)317     public Boolean telephonyStartTrackingDisplayInfoChangeForSubscription(
318                 @RpcParameter(name = "subId") Integer subId) {
319         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
320         if(listener == null) {
321             Log.e("Invalid subscription ID");
322             return false;
323         }
324         mTelephonyManager.createForSubscriptionId(subId).listen(
325             listener.mDisplayInfoStateChangeListener,
326             PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED);
327         return true;
328     }
329 
330     @Rpc(description = "Turn on/off precise listening on fore/background or" +
331                        " ringing calls for default voice subscription ID.")
telephonyAdjustPreciseCallStateListenLevel( @pcParametername = "type") String type, @RpcParameter(name = "listen") Boolean listen)332     public Boolean telephonyAdjustPreciseCallStateListenLevel(
333             @RpcParameter(name = "type") String type,
334             @RpcParameter(name = "listen") Boolean listen) {
335         return telephonyAdjustPreciseCallStateListenLevelForSubscription(type, listen,
336                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
337     }
338 
339     @Rpc(description = "Turn on/off precise listening on fore/background or" +
340                        " ringing calls for specified subscription ID.")
telephonyAdjustPreciseCallStateListenLevelForSubscription( @pcParametername = "type") String type, @RpcParameter(name = "listen") Boolean listen, @RpcParameter(name = "subId") Integer subId)341     public Boolean telephonyAdjustPreciseCallStateListenLevelForSubscription(
342             @RpcParameter(name = "type") String type,
343             @RpcParameter(name = "listen") Boolean listen,
344             @RpcParameter(name = "subId") Integer subId) {
345         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
346         if(listener == null) {
347             Log.e("Invalid subscription ID");
348             return false;
349         }
350 
351         if (type.equals(TelephonyConstants.PRECISE_CALL_STATE_LISTEN_LEVEL_FOREGROUND)) {
352             listener.mCallStateChangeListener.listenForeground = listen;
353         } else if (type.equals(TelephonyConstants.PRECISE_CALL_STATE_LISTEN_LEVEL_RINGING)) {
354             listener.mCallStateChangeListener.listenRinging = listen;
355         } else if (type.equals(TelephonyConstants.PRECISE_CALL_STATE_LISTEN_LEVEL_BACKGROUND)) {
356             listener.mCallStateChangeListener.listenBackground = listen;
357         } else {
358             throw new IllegalArgumentException("Invalid listen level type " + type);
359         }
360 
361         return true;
362     }
363 
364     @Rpc(description = "Stops tracking cell info change " +
365             "for default voice subscription ID.")
telephonyStopTrackingCellInfoChange()366     public Boolean telephonyStopTrackingCellInfoChange() {
367         return telephonyStopTrackingCellInfoChangeForSubscription(
368                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
369     }
370 
371     @Rpc(description = "Stops tracking cell info change " +
372                        "for specified subscription ID.")
telephonyStopTrackingCellInfoChangeForSubscription( @pcParametername = "subId") Integer subId)373     public Boolean telephonyStopTrackingCellInfoChangeForSubscription(
374                    @RpcParameter(name = "subId") Integer subId) {
375         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
376         if(listener == null) {
377             Log.e("Invalid subscription ID");
378             return false;
379         }
380         mTelephonyManager.createForSubscriptionId(subId).listen(
381             listener.mCellInfoChangeListener,
382             PhoneStateListener.LISTEN_NONE);
383         return true;
384     }
385 
386     @Rpc(description = "Stops tracking active opportunistic data " +
387             "for default subscription ID.")
telephonyStopTrackingActiveDataChange()388     public Boolean telephonyStopTrackingActiveDataChange() {
389         return telephonyStopTrackingActiveDataChangeForSubscription(
390                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
391     }
392 
393     @Rpc(description = "Stops tracking active opportunistic data " +
394                        "for specified subscription ID.")
telephonyStopTrackingActiveDataChangeForSubscription( @pcParametername = "subId") Integer subId)395     public Boolean telephonyStopTrackingActiveDataChangeForSubscription(
396                    @RpcParameter(name = "subId") Integer subId) {
397         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
398         if(listener == null) {
399             Log.e("Invalid subscription ID");
400             return false;
401         }
402         mTelephonyManager.createForSubscriptionId(subId).listen(
403             listener.mActiveDataSubIdChangeListener,
404             PhoneStateListener.LISTEN_NONE);
405         return true;
406     }
407 
408     @Rpc(description = "Stops tracking display info change " +
409                        "for default subscription ID.")
telephonyStopTrackingDisplayInfoChange()410     public Boolean telephonyStopTrackingDisplayInfoChange() {
411         return telephonyStopTrackingDisplayInfoChangeForSubscription(
412                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
413     }
414 
415     @Rpc(description = "Stops tracking display info change " +
416                        "for specified subscription ID.")
telephonyStopTrackingDisplayInfoChangeForSubscription( @pcParametername = "subId") Integer subId)417     public Boolean telephonyStopTrackingDisplayInfoChangeForSubscription(
418                    @RpcParameter(name = "subId") Integer subId) {
419         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
420         if(listener == null) {
421             Log.e("Invalid subscription ID");
422             return false;
423         }
424         mTelephonyManager.createForSubscriptionId(subId).listen(
425             listener.mDisplayInfoStateChangeListener,
426             PhoneStateListener.LISTEN_NONE);
427         return true;
428     }
429 
430     @Rpc(description = "Stops tracking call state change " +
431             "for default voice subscription ID.")
telephonyStopTrackingCallStateChange()432     public Boolean telephonyStopTrackingCallStateChange() {
433         return telephonyStopTrackingCallStateChangeForSubscription(
434                 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
435     }
436 
437     @Rpc(description = "Stops tracking call state change " +
438                        "for specified subscription ID.")
telephonyStopTrackingCallStateChangeForSubscription( @pcParametername = "subId") Integer subId)439     public Boolean telephonyStopTrackingCallStateChangeForSubscription(
440                    @RpcParameter(name = "subId") Integer subId) {
441         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
442         if(listener == null) {
443             Log.e("Invalid subscription ID");
444             return false;
445         }
446         mTelephonyManager.createForSubscriptionId(subId).listen(
447             listener.mCallStateChangeListener,
448             PhoneStateListener.LISTEN_NONE);
449         return true;
450     }
451 
452     @Rpc(description = "Starts tracking data connection real time info change" +
453                        "for default subscription ID.")
telephonyStartTrackingDataConnectionRTInfoChange()454     public Boolean telephonyStartTrackingDataConnectionRTInfoChange() {
455         return telephonyStartTrackingDataConnectionRTInfoChangeForSubscription(
456                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
457     }
458 
459     @Rpc(description = "Starts tracking data connection real time info change" +
460                        "for specified subscription ID.")
telephonyStartTrackingDataConnectionRTInfoChangeForSubscription( @pcParametername = "subId") Integer subId)461     public Boolean telephonyStartTrackingDataConnectionRTInfoChangeForSubscription(
462                    @RpcParameter(name = "subId") Integer subId) {
463         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
464         if(listener == null) {
465             Log.e("Invalid subscription ID");
466             return false;
467         }
468         mTelephonyManager.createForSubscriptionId(subId).listen(
469             listener.mDataConnectionRTInfoChangeListener,
470             DataConnectionRealTimeInfoChangeListener.sListeningStates);
471         return true;
472     }
473 
474     @Rpc(description = "Stops tracking data connection real time info change" +
475                        "for default subscription ID.")
telephonyStopTrackingDataConnectionRTInfoChange()476     public Boolean telephonyStopTrackingDataConnectionRTInfoChange() {
477         return telephonyStopTrackingDataConnectionRTInfoChangeForSubscription(
478                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
479     }
480 
481     @Rpc(description = "Stops tracking data connection real time info change" +
482                        "for specified subscription ID.")
telephonyStopTrackingDataConnectionRTInfoChangeForSubscription( @pcParametername = "subId") Integer subId)483     public Boolean telephonyStopTrackingDataConnectionRTInfoChangeForSubscription(
484                    @RpcParameter(name = "subId") Integer subId) {
485         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
486         if(listener == null) {
487             Log.e("Invalid subscription ID");
488             return false;
489         }
490         mTelephonyManager.createForSubscriptionId(subId).listen(
491             listener.mDataConnectionRTInfoChangeListener,
492             PhoneStateListener.LISTEN_NONE);
493         return true;
494     }
495 
496     @Rpc(description = "Starts tracking data connection state change" +
497                        "for default subscription ID..")
telephonyStartTrackingDataConnectionStateChange()498     public Boolean telephonyStartTrackingDataConnectionStateChange() {
499         return telephonyStartTrackingDataConnectionStateChangeForSubscription(
500                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
501     }
502 
503     @Rpc(description = "Starts tracking data connection state change" +
504                        "for specified subscription ID.")
telephonyStartTrackingDataConnectionStateChangeForSubscription( @pcParametername = "subId") Integer subId)505     public Boolean telephonyStartTrackingDataConnectionStateChangeForSubscription(
506                    @RpcParameter(name = "subId") Integer subId) {
507         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
508         if(listener == null) {
509             Log.e("Invalid subscription ID");
510             return false;
511         }
512         mTelephonyManager.createForSubscriptionId(subId).listen(
513             listener.mDataConnectionStateChangeListener,
514             DataConnectionStateChangeListener.sListeningStates);
515         return true;
516     }
517 
518     @Rpc(description = "Stops tracking data connection state change " +
519                        "for default subscription ID..")
telephonyStopTrackingDataConnectionStateChange()520     public Boolean telephonyStopTrackingDataConnectionStateChange() {
521         return telephonyStopTrackingDataConnectionStateChangeForSubscription(
522                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
523     }
524 
525     @Rpc(description = "Stops tracking data connection state change " +
526                        "for specified subscription ID..")
telephonyStopTrackingDataConnectionStateChangeForSubscription( @pcParametername = "subId") Integer subId)527     public Boolean telephonyStopTrackingDataConnectionStateChangeForSubscription(
528                    @RpcParameter(name = "subId") Integer subId) {
529         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
530         if(listener == null) {
531             Log.e("Invalid subscription ID");
532             return false;
533         }
534         mTelephonyManager.createForSubscriptionId(subId).listen(
535             listener.mDataConnectionStateChangeListener,
536             PhoneStateListener.LISTEN_NONE);
537         return true;
538     }
539 
540     @Rpc(description = "Starts tracking service state change " +
541                        "for default subscription ID.")
telephonyStartTrackingServiceStateChange()542     public Boolean telephonyStartTrackingServiceStateChange() {
543         return telephonyStartTrackingServiceStateChangeForSubscription(
544                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
545     }
546 
547     @Rpc(description = "Starts tracking service state change " +
548                        "for specified subscription ID.")
telephonyStartTrackingServiceStateChangeForSubscription( @pcParametername = "subId") Integer subId)549     public Boolean telephonyStartTrackingServiceStateChangeForSubscription(
550                    @RpcParameter(name = "subId") Integer subId) {
551         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
552         if(listener == null) {
553             Log.e("Invalid subscription ID");
554             return false;
555         }
556         mTelephonyManager.createForSubscriptionId(subId).listen(
557             listener.mServiceStateChangeListener,
558             ServiceStateChangeListener.sListeningStates);
559         return true;
560     }
561 
562     @Rpc(description = "Stops tracking service state change " +
563                        "for default subscription ID.")
telephonyStopTrackingServiceStateChange()564     public Boolean telephonyStopTrackingServiceStateChange() {
565         return telephonyStopTrackingServiceStateChangeForSubscription(
566                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
567     }
568 
569     @Rpc(description = "Stops tracking service state change " +
570                        "for specified subscription ID.")
telephonyStopTrackingServiceStateChangeForSubscription( @pcParametername = "subId") Integer subId)571     public Boolean telephonyStopTrackingServiceStateChangeForSubscription(
572                    @RpcParameter(name = "subId") Integer subId) {
573         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
574         if(listener == null) {
575             Log.e("Invalid subscription ID");
576             return false;
577         }
578         mTelephonyManager.createForSubscriptionId(subId).listen(
579             listener.mServiceStateChangeListener,
580             PhoneStateListener.LISTEN_NONE);
581             return true;
582     }
583 
584     @Rpc(description = "Starts tracking signal strength change " +
585                        "for default subscription ID.")
telephonyStartTrackingSignalStrengthChange()586     public Boolean telephonyStartTrackingSignalStrengthChange() {
587         return telephonyStartTrackingSignalStrengthChangeForSubscription(
588                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
589     }
590 
591     @Rpc(description = "Starts tracking signal strength change " +
592                        "for specified subscription ID.")
telephonyStartTrackingSignalStrengthChangeForSubscription( @pcParametername = "subId") Integer subId)593     public Boolean telephonyStartTrackingSignalStrengthChangeForSubscription(
594                    @RpcParameter(name = "subId") Integer subId) {
595         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
596         if(listener == null) {
597             Log.e("Invalid subscription ID");
598             return false;
599         }
600         mTelephonyManager.createForSubscriptionId(subId).listen(
601             listener.mSignalStrengthChangeListener,
602             SignalStrengthChangeListener.sListeningStates);
603         return true;
604     }
605 
606     @Rpc(description = "Stops tracking signal strength change " +
607                        "for default subscription ID.")
telephonyStopTrackingSignalStrengthChange()608     public Boolean telephonyStopTrackingSignalStrengthChange() {
609         return telephonyStopTrackingSignalStrengthChangeForSubscription(
610                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
611     }
612 
613     @Rpc(description = "Stops tracking signal strength change " +
614                        "for specified subscription ID.")
telephonyStopTrackingSignalStrengthChangeForSubscription( @pcParametername = "subId") Integer subId)615     public Boolean telephonyStopTrackingSignalStrengthChangeForSubscription(
616                    @RpcParameter(name = "subId") Integer subId) {
617         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
618         if(listener == null) {
619             Log.e("Invalid subscription ID");
620             return false;
621         }
622         mTelephonyManager.createForSubscriptionId(subId).listen(
623             listener.mSignalStrengthChangeListener,
624             PhoneStateListener.LISTEN_NONE);
625         return true;
626     }
627 
628     @Rpc(description = "Starts tracking voice mail state change " +
629                        "for default subscription ID.")
telephonyStartTrackingVoiceMailStateChange()630     public Boolean telephonyStartTrackingVoiceMailStateChange() {
631         return telephonyStartTrackingVoiceMailStateChangeForSubscription(
632                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
633     }
634 
635     @Rpc(description = "Starts tracking voice mail state change " +
636                        "for specified subscription ID.")
telephonyStartTrackingVoiceMailStateChangeForSubscription( @pcParametername = "subId") Integer subId)637     public Boolean telephonyStartTrackingVoiceMailStateChangeForSubscription(
638                    @RpcParameter(name = "subId") Integer subId) {
639         StateChangeListener listener = getStateChangeListenerForSubscription(subId, true);
640         if(listener == null) {
641             Log.e("Invalid subscription ID");
642             return false;
643         }
644         mTelephonyManager.createForSubscriptionId(subId).listen(
645             listener.mVoiceMailStateChangeListener,
646             VoiceMailStateChangeListener.sListeningStates);
647         return true;
648     }
649 
650     @Rpc(description = "Stops tracking voice mail state change " +
651                        "for default subscription ID.")
telephonyStopTrackingVoiceMailStateChange()652     public Boolean telephonyStopTrackingVoiceMailStateChange() {
653         return telephonyStopTrackingVoiceMailStateChangeForSubscription(
654                                  SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
655     }
656 
657     @Rpc(description = "Stops tracking voice mail state change " +
658                        "for specified subscription ID.")
telephonyStopTrackingVoiceMailStateChangeForSubscription( @pcParametername = "subId") Integer subId)659     public Boolean telephonyStopTrackingVoiceMailStateChangeForSubscription(
660                    @RpcParameter(name = "subId") Integer subId) {
661         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
662         if(listener == null) {
663             Log.e("Invalid subscription ID");
664             return false;
665         }
666         mTelephonyManager.createForSubscriptionId(subId).listen(
667             listener.mVoiceMailStateChangeListener,
668             PhoneStateListener.LISTEN_NONE);
669         return true;
670     }
671 
672     @Rpc(description = "Answers an incoming ringing call.")
telephonyAnswerCall()673     public void telephonyAnswerCall() throws RemoteException {
674         mTelephonyManager.silenceRinger();
675         mTelephonyManager.answerRingingCall();
676     }
677 
678     @Rpc(description = "Returns the radio on/off state.")
telephonyIsRadioOn()679     public Boolean telephonyIsRadioOn() {
680         return mTelephonyManager.isRadioOn();
681     }
682 
683     @Rpc(description = "Sets the radio to an on/off state.")
telephonySetRadioPower( @pcParametername = "turnOn") boolean turnOn)684     public Boolean telephonySetRadioPower(
685         @RpcParameter(name = "turnOn") boolean turnOn) {
686         return mTelephonyManager.setRadioPower(turnOn);
687     }
688 
689     @Rpc(description = "Returns the current cell location.")
telephonyGetCellLocation()690     public CellLocation telephonyGetCellLocation() {
691         return mTelephonyManager.getCellLocation();
692     }
693 
694     /**
695      *  Returns carrier id of the current subscription.
696      * @return Carrier id of the current subscription.
697      */
698     @Rpc(description = "Returns the numeric CarrierId for current subscription")
telephonyGetSimCarrierId()699     public int telephonyGetSimCarrierId() {
700         return mTelephonyManager.getSimCarrierId();
701     }
702 
703     /**
704      *  Returns carrier id name of the current subscription.
705      * @return Carrier id name of the current subscription
706      */
707     @Rpc(description = "Returns Carrier Name for current subscription")
telephonyGetSimCarrierIdName()708     public CharSequence telephonyGetSimCarrierIdName() {
709         return mTelephonyManager.getSimCarrierIdName();
710     }
711 
712     @Rpc(description = "Returns the numeric name (MCC+MNC) of registered operator." +
713                        "for default subscription ID")
telephonyGetNetworkOperator()714     public String telephonyGetNetworkOperator() {
715         return telephonyGetNetworkOperatorForSubscription(
716                         SubscriptionManager.getDefaultSubscriptionId());
717     }
718 
719     @Rpc(description = "Returns the numeric name (MCC+MNC) of registered operator" +
720                        "for specified subscription ID.")
telephonyGetNetworkOperatorForSubscription( @pcParametername = "subId") Integer subId)721     public String telephonyGetNetworkOperatorForSubscription(
722                   @RpcParameter(name = "subId") Integer subId) {
723         return mTelephonyManager.getNetworkOperator(subId);
724     }
725 
726     @Rpc(description = "Returns the alphabetic name of current registered operator" +
727                        "for specified subscription ID.")
telephonyGetNetworkOperatorName()728     public String telephonyGetNetworkOperatorName() {
729         return telephonyGetNetworkOperatorNameForSubscription(
730                         SubscriptionManager.getDefaultSubscriptionId());
731     }
732 
733     @Rpc(description = "Returns the alphabetic name of registered operator " +
734                        "for specified subscription ID.")
telephonyGetNetworkOperatorNameForSubscription( @pcParametername = "subId") Integer subId)735     public String telephonyGetNetworkOperatorNameForSubscription(
736                   @RpcParameter(name = "subId") Integer subId) {
737         return mTelephonyManager.getNetworkOperatorName(subId);
738     }
739 
740     @Rpc(description = "Returns the current RAT in use on the device.+" +
741                        "for default subscription ID")
telephonyGetNetworkType()742     public String telephonyGetNetworkType() {
743 
744         Log.d("sl4a:getNetworkType() is deprecated!" +
745                 "Please use getVoiceNetworkType()" +
746                 " or getDataNetworkTpe()");
747 
748         return telephonyGetNetworkTypeForSubscription(
749                        SubscriptionManager.getDefaultSubscriptionId());
750     }
751 
752     @Rpc(description = "Returns the current RAT in use on the device" +
753             " for a given Subscription.")
telephonyGetNetworkTypeForSubscription( @pcParametername = "subId") Integer subId)754     public String telephonyGetNetworkTypeForSubscription(
755                   @RpcParameter(name = "subId") Integer subId) {
756 
757         Log.d("sl4a:getNetworkTypeForSubscriber() is deprecated!" +
758                 "Please use getVoiceNetworkType()" +
759                 " or getDataNetworkTpe()");
760 
761         return TelephonyUtils.getNetworkTypeString(
762             mTelephonyManager.getNetworkType(subId));
763     }
764 
765     @Rpc(description = "Returns the current voice RAT for" +
766             " the default voice subscription.")
telephonyGetVoiceNetworkType()767     public String telephonyGetVoiceNetworkType() {
768         return telephonyGetVoiceNetworkTypeForSubscription(
769                          SubscriptionManager.getDefaultVoiceSubscriptionId());
770     }
771 
772     @Rpc(description = "Returns the current voice RAT for" +
773             " the specified voice subscription.")
telephonyGetVoiceNetworkTypeForSubscription( @pcParametername = "subId") Integer subId)774     public String telephonyGetVoiceNetworkTypeForSubscription(
775                   @RpcParameter(name = "subId") Integer subId) {
776         return TelephonyUtils.getNetworkTypeString(
777             mTelephonyManager.getVoiceNetworkType(subId));
778     }
779 
780     @Rpc(description = "Returns the current data RAT for" +
781             " the defaut data subscription")
telephonyGetDataNetworkType()782     public String telephonyGetDataNetworkType() {
783         return telephonyGetDataNetworkTypeForSubscription(
784                          SubscriptionManager.getDefaultDataSubscriptionId());
785     }
786 
787     @Rpc(description = "Returns the current data RAT for" +
788             " the specified data subscription")
telephonyGetDataNetworkTypeForSubscription( @pcParametername = "subId") Integer subId)789     public String telephonyGetDataNetworkTypeForSubscription(
790                   @RpcParameter(name = "subId") Integer subId) {
791         return TelephonyUtils.getNetworkTypeString(
792             mTelephonyManager.getDataNetworkType(subId));
793     }
794 
795     @Rpc(description = "Returns the device phone type.")
telephonyGetPhoneType()796     public String telephonyGetPhoneType() {
797         return TelephonyUtils.getPhoneTypeString(
798             mTelephonyManager.getPhoneType());
799     }
800 
801     @Rpc(description = "Return if setAlwaysAllowMMSData is set correctly")
telephonySetAlwaysAllowMmsData( @pcParametername = "subId") Integer subId, @RpcParameter(name = "alwaysAllow") Boolean alwaysAllow)802     public boolean telephonySetAlwaysAllowMmsData(
803             @RpcParameter(name = "subId") Integer subId,
804             @RpcParameter(name = "alwaysAllow") Boolean alwaysAllow) {
805         boolean wasAlwaysAllow = mTelephonyManager.isMobileDataPolicyEnabled(
806                 TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED);
807         mTelephonyManager.createForSubscriptionId(subId)
808                 .setMobileDataPolicyEnabled(
809                         TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, alwaysAllow);
810         return wasAlwaysAllow == alwaysAllow;
811     }
812 
813     /**
814     * Sets Data Roaming flag for a particular sub Id
815     * @param subId the subscriber id
816     * @param isEnabled can you set to true or false
817     */
818     @Rpc(description = "Sets data roaming for a sub Id")
telephonySetDataRoamingEnabled( @pcParametername = "subId") Integer subId, @RpcParameter(name = "isEnabled") Boolean isEnabled)819     public void telephonySetDataRoamingEnabled(
820             @RpcParameter(name = "subId") Integer subId,
821             @RpcParameter(name = "isEnabled") Boolean isEnabled) {
822         mTelephonyManager.createForSubscriptionId(subId).setDataRoamingEnabled(isEnabled);
823     }
824 
825     @Rpc(description = "Returns preferred opportunistic data subscription Id")
telephonyGetPreferredOpportunisticDataSubscription()826     public Integer telephonyGetPreferredOpportunisticDataSubscription() {
827         return mTelephonyManager.getPreferredOpportunisticDataSubscription();
828     }
829 
830     @Rpc(description = "Sets preferred opportunistic data subscription Id")
telephonySetPreferredOpportunisticDataSubscription( @pcParametername = "subId") Integer subId, @RpcParameter(name = "needValidation") Boolean needValidation)831     public void telephonySetPreferredOpportunisticDataSubscription(
832             @RpcParameter(name = "subId") Integer subId,
833             @RpcParameter(name = "needValidation") Boolean needValidation) {
834         mTelephonyManager.setPreferredOpportunisticDataSubscription(
835                    subId, needValidation, null, null);
836     }
837 
838     @Rpc(description = "Updates Available Networks")
telephonyUpdateAvailableNetworks( @pcParametername = "subId") Integer subId)839     public void telephonyUpdateAvailableNetworks(
840             @RpcParameter(name = "subId") Integer subId) {
841 
842         availableNetworkList = new ArrayList<>();
843         List<String> mccmmc = new ArrayList<String>();
844         List<Integer> bands = new ArrayList<Integer>();
845 
846         availableNetworkList.add(
847             new AvailableNetworkInfo(
848                 subId,
849                 AvailableNetworkInfo.PRIORITY_HIGH,
850                 mccmmc,
851                 bands));
852 
853         mTelephonyManager.updateAvailableNetworks(availableNetworkList, null, null);
854     }
855 
856     /**
857     * Get device phone type for a subscription.
858     * @param subId the subscriber id
859     * @return the phone type string for the subscriber.
860     */
861     @Rpc(description = "Returns the device phone type for a subscription.")
telephonyGetPhoneTypeForSubscription( @pcParametername = "subId") Integer subId)862     public String telephonyGetPhoneTypeForSubscription(
863                   @RpcParameter(name = "subId") Integer subId) {
864         return TelephonyUtils.getPhoneTypeString(
865             mTelephonyManager.getCurrentPhoneType(subId));
866     }
867 
868     @Rpc(description = "Returns the MCC for default subscription ID")
telephonyGetSimCountryIso()869     public String telephonyGetSimCountryIso() {
870          return telephonyGetSimCountryIsoForSubscription(
871                       SubscriptionManager.getDefaultSubscriptionId());
872     }
873 
874     @Rpc(description = "Returns the MCC for specified subscription ID")
telephonyGetSimCountryIsoForSubscription( @pcParametername = "subId") Integer subId)875     public String telephonyGetSimCountryIsoForSubscription(
876                   @RpcParameter(name = "subId") Integer subId) {
877         return mTelephonyManager.getSimCountryIso(subId);
878     }
879 
880     @Rpc(description = "Returns the MCC+MNC for default subscription ID")
telephonyGetSimOperator()881     public String telephonyGetSimOperator() {
882         return telephonyGetSimOperatorForSubscription(
883                   SubscriptionManager.getDefaultSubscriptionId());
884     }
885 
886     @Rpc(description = "Returns the MCC+MNC for specified subscription ID")
telephonyGetSimOperatorForSubscription( @pcParametername = "subId") Integer subId)887     public String telephonyGetSimOperatorForSubscription(
888                   @RpcParameter(name = "subId") Integer subId) {
889         return mTelephonyManager.getSimOperator(subId);
890     }
891 
892     @Rpc(description = "Returns the Service Provider Name (SPN)" +
893                        "for default subscription ID")
telephonyGetSimOperatorName()894     public String telephonyGetSimOperatorName() {
895         return telephonyGetSimOperatorNameForSubscription(
896                   SubscriptionManager.getDefaultSubscriptionId());
897     }
898 
899     @Rpc(description = "Returns the Service Provider Name (SPN)" +
900                        " for specified subscription ID.")
telephonyGetSimOperatorNameForSubscription( @pcParametername = "subId") Integer subId)901     public String telephonyGetSimOperatorNameForSubscription(
902                   @RpcParameter(name = "subId") Integer subId) {
903         return mTelephonyManager.getSimOperatorName(subId);
904     }
905 
906     @Rpc(description = "Returns the serial number of the SIM for " +
907                        "default subscription ID, or Null if unavailable")
telephonyGetSimSerialNumber()908     public String telephonyGetSimSerialNumber() {
909         return telephonyGetSimSerialNumberForSubscription(
910                   SubscriptionManager.getDefaultSubscriptionId());
911     }
912 
913     @Rpc(description = "Returns the serial number of the SIM for " +
914                        "specified subscription ID, or Null if unavailable")
telephonyGetSimSerialNumberForSubscription( @pcParametername = "subId") Integer subId)915     public String telephonyGetSimSerialNumberForSubscription(
916                   @RpcParameter(name = "subId") Integer subId) {
917         return mTelephonyManager.getSimSerialNumber(subId);
918     }
919 
920     /**
921      * Set SIM card power state.
922      *
923      * @param state  State of SIM (0: power down, 1: power up, 2: pass through)
924      **/
925     @Rpc(description = "Set the SIM power state of the SIM card for default slot ID.")
telephonySetSimPowerState( @pcParametername = "state") Integer state)926     public void telephonySetSimPowerState(
927                   @RpcParameter(name = "state") Integer state) {
928         mTelephonyManager.setSimPowerState(state);
929     }
930 
931     /**
932      * Set SIM card power state.
933      *
934      * @param slotId SIM slot id
935      * @param state  State of SIM (0: power down, 1: power up, 2: pass through)
936      **/
937     @Rpc(description = "Set the SIM power state for SIM slot slotId.")
telephonySetSimStateForSlotId( @pcParametername = "slotId") Integer slotId, @RpcParameter(name = "state") Integer state)938     public void telephonySetSimStateForSlotId(
939                   @RpcParameter(name = "slotId") Integer slotId,
940                   @RpcParameter(name = "state") Integer state) {
941         mTelephonyManager.setSimPowerStateForSlot(slotId, state);
942     }
943 
944     @Rpc(description = "Returns the state of the SIM card for default slot ID.")
telephonyGetSimState()945     public String telephonyGetSimState() {
946         return telephonyGetSimStateForSlotId(
947                   mTelephonyManager.getSlotIndex());
948     }
949 
950     @Rpc(description = "Returns the state of the SIM card for specified slot ID.")
telephonyGetSimStateForSlotId( @pcParametername = "slotId") Integer slotId)951     public String telephonyGetSimStateForSlotId(
952                   @RpcParameter(name = "slotId") Integer slotId) {
953         return TelephonyUtils.getSimStateString(
954             mTelephonyManager.getSimState(slotId));
955     }
956 
957     /**
958      * Switch device mode multisim
959      *
960      * @param numOfSims (1: single sim, 2: multi sim)
961      **/
962     @Rpc(description = "Switch configs to enable multi-sim or switch back to single-sim")
telephonySwitchMultiSimConfig( @pcParametername = "numOfSims") Integer numOfSims)963     public void telephonySwitchMultiSimConfig(
964             @RpcParameter(name = "numOfSims")
965             Integer numOfSims) {
966         mTelephonyManager.switchMultiSimConfig(numOfSims.intValue());
967     }
968 
969     /**
970      * Gets device mode multisim
971      *
972      * @return phoneCount (1-single sim, 2-dual sim, 3-tri sim)
973      **/
974     @Rpc(description = "Returns if device is in Single, Dual, Tri SIM Mode")
telephonyGetPhoneCount()975     public Integer telephonyGetPhoneCount() {
976         return mTelephonyManager.getPhoneCount();
977     }
978 
979     @Rpc(description = "Get Authentication Challenge Response from a " +
980             "given SIM Application")
telephonyGetIccSimChallengeResponse( @pcParametername = "appType") Integer appType, @RpcParameter(name = "authType") Integer authType, @RpcParameter(name = "hexChallenge") String hexChallenge)981     public String telephonyGetIccSimChallengeResponse(
982             @RpcParameter(name = "appType") Integer appType,
983             @RpcParameter(name = "authType") Integer authType,
984             @RpcParameter(name = "hexChallenge") String hexChallenge) {
985         return telephonyGetIccSimChallengeResponseForSubscription(
986                 SubscriptionManager.getDefaultSubscriptionId(), appType, authType, hexChallenge);
987     }
988 
989     @Rpc(description = "Get Authentication Challenge Response from a " +
990             "given SIM Application for a specified Subscription")
telephonyGetIccSimChallengeResponseForSubscription( @pcParametername = "subId") Integer subId, @RpcParameter(name = "appType") Integer appType, @RpcParameter(name = "authType") Integer authType, @RpcParameter(name = "hexChallenge") String hexChallenge)991     public String telephonyGetIccSimChallengeResponseForSubscription(
992             @RpcParameter(name = "subId") Integer subId,
993             @RpcParameter(name = "appType") Integer appType,
994             @RpcParameter(name = "authType") Integer authType,
995             @RpcParameter(name = "hexChallenge") String hexChallenge) {
996 
997         try {
998             String b64Data = BaseEncoding.base64().encode(BaseEncoding.base16().decode(hexChallenge));
999             String b64Result = mTelephonyManager.getIccAuthentication(subId, appType, authType, b64Data);
1000             return (b64Result != null)
1001                     ? BaseEncoding.base16().encode(BaseEncoding.base64().decode(b64Result)) : null;
1002         } catch(Exception e) {
1003             Log.e("Exception in phoneGetIccSimChallengeResponseForSubscription" + e.toString());
1004             return null;
1005         }
1006     }
1007 
1008     /**
1009     * Supply the puk code and pin for locked SIM.
1010     * @param puk the puk code string
1011     * @param pin the puk pin string
1012     * @return    true or false for supplying the puk code and pin successfully or unsuccessfully.
1013     */
1014     @Rpc(description = "Supply Puk and Pin for locked SIM.")
telephonySupplyPuk( @pcParametername = "puk") String puk, @RpcParameter(name = "pin") String pin)1015     public boolean telephonySupplyPuk(
1016             @RpcParameter(name = "puk") String puk,
1017             @RpcParameter(name = "pin") String pin) {
1018         return mTelephonyManager.supplyPuk(puk, pin);
1019     }
1020 
1021     /**
1022     * Supply pin for locked SIM.
1023     * @param pin the puk pin string
1024     * @return    true or false for supplying the pin successfully or unsuccessfully.
1025     */
1026     @Rpc(description = "Supply Pin for locked SIM.")
telephonySupplyPin( @pcParametername = "pin") String pin)1027     public boolean telephonySupplyPin(
1028             @RpcParameter(name = "pin") String pin) {
1029         return mTelephonyManager.supplyPin(pin);
1030     }
1031 
1032     @Rpc(description = "Returns the unique subscriber ID (such as IMSI) " +
1033             "for default subscription ID, or null if unavailable")
telephonyGetSubscriberId()1034     public String telephonyGetSubscriberId() {
1035         return telephonyGetSubscriberIdForSubscription(
1036                 SubscriptionManager.getDefaultSubscriptionId());
1037     }
1038 
1039     @Rpc(description = "Returns the unique subscriber ID (such as IMSI) " +
1040                        "for specified subscription ID, or null if unavailable")
telephonyGetSubscriberIdForSubscription( @pcParametername = "subId") Integer subId)1041     public String telephonyGetSubscriberIdForSubscription(
1042                   @RpcParameter(name = "subId") Integer subId) {
1043         return mTelephonyManager.getSubscriberId(subId);
1044     }
1045 
1046     @Rpc(description = "Retrieves the alphabetic id associated with the" +
1047                        " voice mail number for default subscription ID.")
telephonyGetVoiceMailAlphaTag()1048     public String telephonyGetVoiceMailAlphaTag() {
1049         return telephonyGetVoiceMailAlphaTagForSubscription(
1050                    SubscriptionManager.getDefaultSubscriptionId());
1051     }
1052 
1053 
1054     @Rpc(description = "Retrieves the alphabetic id associated with the " +
1055                        "voice mail number for specified subscription ID.")
telephonyGetVoiceMailAlphaTagForSubscription( @pcParametername = "subId") Integer subId)1056     public String telephonyGetVoiceMailAlphaTagForSubscription(
1057                   @RpcParameter(name = "subId") Integer subId) {
1058         return mTelephonyManager.getVoiceMailAlphaTag(subId);
1059     }
1060 
1061     @Rpc(description = "Returns the voice mail number " +
1062                        "for default subscription ID; null if unavailable.")
telephonyGetVoiceMailNumber()1063     public String telephonyGetVoiceMailNumber() {
1064         return telephonyGetVoiceMailNumberForSubscription(
1065                    SubscriptionManager.getDefaultSubscriptionId());
1066     }
1067 
1068     @Rpc(description = "Returns the voice mail number " +
1069                         "for specified subscription ID; null if unavailable.")
telephonyGetVoiceMailNumberForSubscription( @pcParametername = "subId") Integer subId)1070     public String telephonyGetVoiceMailNumberForSubscription(
1071                   @RpcParameter(name = "subId") Integer subId) {
1072         return mTelephonyManager.getVoiceMailNumber(subId);
1073     }
1074 
1075     @Rpc(description = "Get voice message count for specified subscription ID.")
telephonyGetVoiceMailCountForSubscription( @pcParametername = "subId") Integer subId)1076     public Integer telephonyGetVoiceMailCountForSubscription(
1077                    @RpcParameter(name = "subId") Integer subId) {
1078         return mTelephonyManager.getVoiceMessageCount(subId);
1079     }
1080 
1081     @Rpc(description = "Get voice message count for default subscription ID.")
telephonyGetVoiceMailCount()1082     public Integer telephonyGetVoiceMailCount() {
1083         return mTelephonyManager.getVoiceMessageCount();
1084     }
1085 
1086     @Rpc(description = "Returns true if the device is in  roaming state" +
1087                        "for default subscription ID")
telephonyCheckNetworkRoaming()1088     public Boolean telephonyCheckNetworkRoaming() {
1089         return telephonyCheckNetworkRoamingForSubscription(
1090                              SubscriptionManager.getDefaultSubscriptionId());
1091     }
1092 
1093     @Rpc(description = "Returns true if the device is in roaming state " +
1094                        "for specified subscription ID")
telephonyCheckNetworkRoamingForSubscription( @pcParametername = "subId") Integer subId)1095     public Boolean telephonyCheckNetworkRoamingForSubscription(
1096                    @RpcParameter(name = "subId") Integer subId) {
1097         return mTelephonyManager.isNetworkRoaming(subId);
1098     }
1099 
1100     @Rpc(description = "Returns the unique device ID such as MEID or IMEI " +
1101                        "for deault sim slot ID, null if unavailable")
telephonyGetDeviceId()1102     public String telephonyGetDeviceId() {
1103         return telephonyGetDeviceIdForSlotId(mTelephonyManager.getSlotIndex());
1104     }
1105 
1106     @Rpc(description = "Returns the unique device ID such as MEID or IMEI for" +
1107                        " specified slot ID, null if unavailable")
telephonyGetDeviceIdForSlotId( @pcParametername = "slotId") Integer slotId)1108     public String telephonyGetDeviceIdForSlotId(
1109                   @RpcParameter(name = "slotId")
1110                   Integer slotId){
1111         return mTelephonyManager.getDeviceId(slotId);
1112     }
1113 
1114     @Rpc(description = "Returns the modem sw version, such as IMEI-SV;" +
1115                        " null if unavailable")
telephonyGetDeviceSoftwareVersion()1116     public String telephonyGetDeviceSoftwareVersion() {
1117         return mTelephonyManager.getDeviceSoftwareVersion();
1118     }
1119 
1120     @Rpc(description = "Returns phone # string \"line 1\", such as MSISDN " +
1121                        "for default subscription ID; null if unavailable")
telephonyGetLine1Number()1122     public String telephonyGetLine1Number() {
1123         return mTelephonyManager.getLine1Number();
1124     }
1125 
1126     @Rpc(description = "Returns phone # string \"line 1\", such as MSISDN " +
1127                        "for specified subscription ID; null if unavailable")
telephonyGetLine1NumberForSubscription( @pcParametername = "subId") Integer subId)1128     public String telephonyGetLine1NumberForSubscription(
1129                   @RpcParameter(name = "subId") Integer subId) {
1130         return mTelephonyManager.getLine1Number(subId);
1131     }
1132 
1133     @Rpc(description = "Returns the Alpha Tag for the default subscription " +
1134                        "ID; null if unavailable")
telephonyGetLine1AlphaTag()1135     public String telephonyGetLine1AlphaTag() {
1136         return mTelephonyManager.getLine1AlphaTag();
1137     }
1138 
1139     @Rpc(description = "Returns the Alpha Tag for the specified subscription " +
1140                        "ID; null if unavailable")
telephonyGetLine1AlphaTagForSubscription( @pcParametername = "subId") Integer subId)1141     public String telephonyGetLine1AlphaTagForSubscription(
1142                   @RpcParameter(name = "subId") Integer subId) {
1143         return mTelephonyManager.getLine1AlphaTag(subId);
1144     }
1145 
1146     @Rpc(description = "Set the Line1-number (phone number) and Alpha Tag" +
1147                        "for the default subscription")
telephonySetLine1Number( @pcParametername = "number") String number, @RpcOptional @RpcParameter(name = "alphaTag") String alphaTag)1148     public Boolean telephonySetLine1Number(
1149                 @RpcParameter(name = "number") String number,
1150                 @RpcOptional
1151                 @RpcParameter(name = "alphaTag") String alphaTag) {
1152         return mTelephonyManager.setLine1NumberForDisplay(alphaTag, number);
1153     }
1154 
1155     @Rpc(description = "Set the Line1-number (phone number) and Alpha Tag" +
1156                        "for the specified subscription")
telephonySetLine1NumberForSubscription( @pcParametername = "subId") Integer subId, @RpcParameter(name = "number") String number, @RpcOptional @RpcParameter(name = "alphaTag") String alphaTag)1157     public Boolean telephonySetLine1NumberForSubscription(
1158                 @RpcParameter(name = "subId") Integer subId,
1159                 @RpcParameter(name = "number") String number,
1160                 @RpcOptional
1161                 @RpcParameter(name = "alphaTag") String alphaTag) {
1162         return mTelephonyManager.setLine1NumberForDisplay(subId, alphaTag, number);
1163     }
1164 
1165     @Rpc(description = "Returns the neighboring cell information of the device.")
telephonyGetNeighboringCellInfo()1166     public List<NeighboringCellInfo> telephonyGetNeighboringCellInfo() {
1167         return mTelephonyManager.getNeighboringCellInfo();
1168     }
1169 
1170     @Rpc(description =  "Sets the minimum reporting interval for CellInfo" +
1171                         "0-as quickly as possible, 0x7FFFFFF-off")
telephonySetCellInfoListRate( @pcParametername = "rate") Integer rate )1172     public void telephonySetCellInfoListRate(
1173                 @RpcParameter(name = "rate") Integer rate
1174             ) {
1175         mTelephonyManager.setCellInfoListRate(rate);
1176     }
1177 
1178     /**
1179      * Request a list of the current (latest) CellInfo.
1180      *
1181      * <p>When invoked on a device running Q or later, this will only return cached info.
1182      */
1183     @Rpc(description = "Returns all observed cell information from all radios"
1184                        + "on the device including the primary and neighboring cells.")
telephonyGetAllCellInfo()1185     public List<CellInfo> telephonyGetAllCellInfo() {
1186         return mTelephonyManager.getAllCellInfo();
1187     }
1188 
1189     private abstract class FacadeCellInfoCallback extends TelephonyManager.CellInfoCallback {
1190         public List<CellInfo> cellInfo;
1191     }
1192 
1193     /** Request an asynchronous update for the latest CellInfo */
1194     @Rpc(description = "Request updated CellInfo scan information for"
1195                        + " primary and neighboring cells.")
telephonyRequestCellInfoUpdate()1196     public List<CellInfo> telephonyRequestCellInfoUpdate() {
1197         FacadeCellInfoCallback tmCiCb = new FacadeCellInfoCallback() {
1198             @Override
1199             public void onCellInfo(List<CellInfo> ci) {
1200                 synchronized (this) {
1201                     this.cellInfo = ci;
1202                     notifyAll();
1203                 }
1204             }
1205 
1206             @Override
1207             public void onError(int errorCode, Throwable detail) {
1208                 Log.d("Error in telephonyRequestCellInfoUpdate(): errorCode=" + errorCode
1209                         + "detail=" + detail);
1210             }
1211         };
1212 
1213         synchronized (tmCiCb) {
1214             mTelephonyManager.requestCellInfoUpdate(
1215                     new Executor() {
1216                         public void execute(Runnable r) {
1217                             Log.d("Running cellInfo Executor");
1218                             r.run();
1219                         }
1220                     }, tmCiCb);
1221             try {
1222                 tmCiCb.wait(3000 /* millis */);
1223             } catch (InterruptedException e) {
1224                 Log.d("Timed out waiting for cellInfo Executor");
1225                 return null;
1226             }
1227         }
1228         return tmCiCb.cellInfo;
1229     }
1230 
1231     @Rpc(description = "Returns True if cellular data is enabled for" +
1232                        "default data subscription ID.")
telephonyIsDataEnabled()1233     public Boolean telephonyIsDataEnabled() {
1234         return telephonyIsDataEnabledForSubscription(
1235                    SubscriptionManager.getDefaultDataSubscriptionId());
1236     }
1237 
1238     @Rpc(description = "Returns True if data connection is enabled.")
telephonyIsDataEnabledForSubscription( @pcParametername = "subId") Integer subId)1239     public Boolean telephonyIsDataEnabledForSubscription(
1240                    @RpcParameter(name = "subId") Integer subId) {
1241         return mTelephonyManager.getDataEnabled(subId);
1242     }
1243 
1244     @Rpc(description = "Toggles data connection on /off for" +
1245                        " default data subscription ID.")
telephonyToggleDataConnection( @pcParametername = "enabled") @pcOptional Boolean enabled)1246     public void telephonyToggleDataConnection(
1247                 @RpcParameter(name = "enabled")
1248                 @RpcOptional Boolean enabled) {
1249         telephonyToggleDataConnectionForSubscription(
1250                          SubscriptionManager.getDefaultDataSubscriptionId(), enabled);
1251     }
1252 
1253     @Rpc(description = "Toggles data connection on/off for" +
1254                        " specified subscription ID")
telephonyToggleDataConnectionForSubscription( @pcParametername = "subId") Integer subId, @RpcParameter(name = "enabled") @RpcOptional Boolean enabled)1255     public void telephonyToggleDataConnectionForSubscription(
1256                 @RpcParameter(name = "subId") Integer subId,
1257                 @RpcParameter(name = "enabled")
1258                 @RpcOptional Boolean enabled) {
1259         if (enabled == null) {
1260             enabled = !telephonyIsDataEnabledForSubscription(subId);
1261         }
1262         mTelephonyManager.setDataEnabled(subId, enabled);
1263     }
1264 
1265     @Rpc(description = "Sets an APN and make that as preferred APN.")
telephonySetAPN(@pcParametername = "name") final String name, @RpcParameter(name = "apn") final String apn, @RpcParameter(name = "type") @RpcOptional @RpcDefault("") final String type, @RpcParameter(name = "subId") @RpcOptional Integer subId)1266     public void telephonySetAPN(@RpcParameter(name = "name") final String name,
1267                        @RpcParameter(name = "apn") final String apn,
1268                        @RpcParameter(name = "type") @RpcOptional @RpcDefault("")
1269                        final String type,
1270                        @RpcParameter(name = "subId") @RpcOptional Integer subId) {
1271         //TODO: b/26273471 Need to find out how to set APN for specific subId
1272         Uri uri;
1273         Cursor cursor;
1274 
1275         String mcc = "";
1276         String mnc = "";
1277 
1278         List<String> numerics = TelephonyProperties.icc_operator_numeric();
1279         String numeric = numerics.isEmpty() ? null : numerics.get(0);
1280         // MCC is first 3 chars and then in 2 - 3 chars of MNC
1281         if (numeric != null && numeric.length() > 4) {
1282             // Country code
1283             mcc = numeric.substring(0, 3);
1284             // Network code
1285             mnc = numeric.substring(3);
1286         }
1287 
1288         uri = mService.getContentResolver().insert(
1289                 Telephony.Carriers.CONTENT_URI, new ContentValues());
1290         if (uri == null) {
1291             Log.w("Failed to insert new provider into " + Telephony.Carriers.CONTENT_URI);
1292             return;
1293         }
1294 
1295         cursor = mService.getContentResolver().query(uri, sProjection, null, null, null);
1296         cursor.moveToFirst();
1297 
1298         ContentValues values = new ContentValues();
1299 
1300         values.put(Telephony.Carriers.NAME, name);
1301         values.put(Telephony.Carriers.APN, apn);
1302         values.put(Telephony.Carriers.PROXY, "");
1303         values.put(Telephony.Carriers.PORT, "");
1304         values.put(Telephony.Carriers.MMSPROXY, "");
1305         values.put(Telephony.Carriers.MMSPORT, "");
1306         values.put(Telephony.Carriers.USER, "");
1307         values.put(Telephony.Carriers.SERVER, "");
1308         values.put(Telephony.Carriers.PASSWORD, "");
1309         values.put(Telephony.Carriers.MMSC, "");
1310         values.put(Telephony.Carriers.TYPE, type);
1311         values.put(Telephony.Carriers.MCC, mcc);
1312         values.put(Telephony.Carriers.MNC, mnc);
1313         values.put(Telephony.Carriers.NUMERIC, mcc + mnc);
1314 
1315         int ret = mService.getContentResolver().update(uri, values, null, null);
1316         Log.d("after update " + ret);
1317         cursor.close();
1318 
1319         // Make this APN as the preferred
1320         String where = "name=\"" + name + "\"";
1321 
1322         Cursor c = mService.getContentResolver().query(
1323                 Telephony.Carriers.CONTENT_URI,
1324                 new String[] {
1325                         "_id", "name", "apn", "type"
1326                 }, where, null,
1327                 Telephony.Carriers.DEFAULT_SORT_ORDER);
1328         if (c != null) {
1329             c.moveToFirst();
1330             String key = c.getString(0);
1331             final String PREFERRED_APN_URI = "content://telephony/carriers/preferapn";
1332             ContentResolver resolver = mService.getContentResolver();
1333             ContentValues prefAPN = new ContentValues();
1334             prefAPN.put("apn_id", key);
1335             resolver.update(Uri.parse(PREFERRED_APN_URI), prefAPN, null, null);
1336         }
1337         c.close();
1338     }
1339 
1340     @Rpc(description = "Returns the number of APNs defined")
telephonyGetNumberOfAPNs( @pcParametername = "subId") @pcOptional Integer subId)1341     public int telephonyGetNumberOfAPNs(
1342                @RpcParameter(name = "subId")
1343                @RpcOptional Integer subId) {
1344         //TODO: b/26273471 Need to find out how to get Number of APNs for specific subId
1345         int result = 0;
1346 
1347         Cursor cursor = mService.getContentResolver().query(
1348                 Telephony.Carriers.SIM_APN_URI,
1349                 new String[] {"_id", "name", "apn", "type"}, null, null,
1350                 Telephony.Carriers.DEFAULT_SORT_ORDER);
1351 
1352         if (cursor != null) {
1353             result = cursor.getCount();
1354         }
1355         cursor.close();
1356         return result;
1357     }
1358 
1359     @Rpc(description = "Returns the currently selected APN name")
telephonyGetSelectedAPN( @pcParametername = "subId") @pcOptional Integer subId)1360     public String telephonyGetSelectedAPN(
1361                   @RpcParameter(name = "subId")
1362                   @RpcOptional Integer subId) {
1363         //TODO: b/26273471 Need to find out how to get selected APN for specific subId
1364         String key = null;
1365         int ID_INDEX = 0;
1366         final String PREFERRED_APN_URI = "content://telephony/carriers/preferapn";
1367 
1368         Cursor cursor = mService.getContentResolver().query(Uri.parse(PREFERRED_APN_URI),
1369                 new String[] {"name"}, null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
1370 
1371         if (cursor.getCount() > 0) {
1372             cursor.moveToFirst();
1373             key = cursor.getString(ID_INDEX);
1374         }
1375         cursor.close();
1376         return key;
1377     }
1378 
1379     @Rpc(description = "Returns the current data connection state")
telephonyGetDataConnectionState()1380     public String telephonyGetDataConnectionState() {
1381         return TelephonyUtils.getDataConnectionStateString(
1382             mTelephonyManager.getDataState());
1383     }
1384 
1385     @Rpc(description = "Returns Total Rx Bytes.")
getTotalRxBytes()1386     public long getTotalRxBytes() {
1387         return TrafficStats.getTotalRxBytes();
1388     }
1389 
1390     @Rpc(description = "Returns Total Tx Bytes.")
getTotalTxBytes()1391     public long getTotalTxBytes() {
1392         return TrafficStats.getTotalTxBytes();
1393     }
1394 
1395     @Rpc(description = "Returns Total Rx Packets.")
getTotalRxPackets()1396     public long getTotalRxPackets() {
1397         return TrafficStats.getTotalRxPackets();
1398     }
1399 
1400     @Rpc(description = "Returns Total Tx Packets.")
getTotalTxPackets()1401     public long getTotalTxPackets() {
1402         return TrafficStats.getTotalTxPackets();
1403     }
1404 
1405     @Rpc(description = "Returns Mobile Network Rx Bytes.")
getMobileRxBytes()1406     public long getMobileRxBytes() {
1407         return TrafficStats.getMobileRxBytes();
1408     }
1409 
1410     @Rpc(description = "Returns Mobile Network Tx Bytes.")
getMobileTxBytes()1411     public long getMobileTxBytes() {
1412         return TrafficStats.getMobileTxBytes();
1413     }
1414 
1415     @Rpc(description = "Returns Mobile Network Packets.")
getMobileRxPackets()1416     public long getMobileRxPackets() {
1417         return TrafficStats.getMobileRxPackets();
1418     }
1419 
1420     @Rpc(description = "Returns Mobile Network Packets.")
getMobileTxPackets()1421     public long getMobileTxPackets() {
1422         return TrafficStats.getMobileTxPackets();
1423     }
1424 
1425     @Rpc(description = "Returns a given UID Rx Bytes.")
getUidRxBytes( @pcParametername = "uid") Integer uid)1426     public long getUidRxBytes(
1427             @RpcParameter(name = "uid") Integer uid) {
1428         return TrafficStats.getUidRxBytes(uid);
1429     }
1430 
1431     @Rpc(description = "Returns a given UID Rx Packets.")
getUidRxPackets( @pcParametername = "uid") Integer uid)1432     public long getUidRxPackets(
1433             @RpcParameter(name = "uid") Integer uid) {
1434         return TrafficStats.getUidRxPackets(uid);
1435     }
1436 
1437     @Rpc(description = "Enables or Disables Video Calling()")
telephonyEnableVideoCalling( @pcParametername = "enable") boolean enable)1438     public void telephonyEnableVideoCalling(
1439             @RpcParameter(name = "enable") boolean enable) {
1440         mTelephonyManager.enableVideoCalling(enable);
1441     }
1442 
1443     @Rpc(description = "Returns a boolean of whether or not " +
1444             "video calling setting is enabled by the user")
telephonyIsVideoCallingEnabled()1445     public Boolean telephonyIsVideoCallingEnabled() {
1446         return mTelephonyManager.isVideoCallingEnabled();
1447     }
1448 
1449     @Rpc(description = "Returns a boolean of whether video calling is available for use")
telephonyIsVideoCallingAvailable()1450     public Boolean telephonyIsVideoCallingAvailable() {
1451         return mTelephonyManager.isVideoTelephonyAvailable();
1452     }
1453 
1454     @Rpc(description = "Returns a boolean of whether or not the device is ims registered")
telephonyIsImsRegistered()1455     public Boolean telephonyIsImsRegistered() {
1456         return mTelephonyManager.isImsRegistered();
1457     }
1458 
1459     @Rpc(description = "Returns a boolean of whether or not volte calling is available for use")
telephonyIsVolteAvailable()1460     public Boolean telephonyIsVolteAvailable() {
1461         return mTelephonyManager.isVolteAvailable();
1462     }
1463 
1464     @Rpc(description = "Returns a boolean of whether or not wifi calling is available for use")
telephonyIsWifiCallingAvailable()1465     public Boolean telephonyIsWifiCallingAvailable() {
1466         return mTelephonyManager.isWifiCallingAvailable();
1467     }
1468 
1469     @Rpc(description = "Returns the service state string for default subscription ID")
telephonyGetServiceState()1470     public ServiceState telephonyGetServiceState() {
1471         return telephonyGetServiceStateForSubscription(
1472                                  SubscriptionManager.getDefaultSubscriptionId());
1473     }
1474 
1475     @Rpc(description = "Returns the service state string for specified subscription ID")
telephonyGetServiceStateForSubscription( @pcParametername = "subId") Integer subId)1476     public ServiceState telephonyGetServiceStateForSubscription(
1477                   @RpcParameter(name = "subId") Integer subId) {
1478         return mTelephonyManager.getServiceStateForSubscriber(subId);
1479     }
1480 
1481     @Rpc(description = "Returns the call state for default subscription ID")
telephonyGetCallState()1482     public String telephonyGetCallState() {
1483         return telephonyGetCallStateForSubscription(
1484                                SubscriptionManager.getDefaultSubscriptionId());
1485     }
1486 
1487     @Rpc(description = "Returns the call state for specified subscription ID")
telephonyGetCallStateForSubscription( @pcParametername = "subId") Integer subId)1488     public String telephonyGetCallStateForSubscription(
1489                   @RpcParameter(name = "subId") Integer subId) {
1490         return TelephonyUtils.getTelephonyCallStateString(
1491             mTelephonyManager.getCallState(subId));
1492     }
1493 
1494     @Rpc(description = "Returns current signal strength for default subscription ID.")
telephonyGetSignalStrength()1495     public SignalStrength telephonyGetSignalStrength() {
1496         return mTelephonyManager.getSignalStrength();
1497     }
1498 
1499     @Rpc(description = "Returns current signal strength for specified subscription ID.")
telephonyGetSignalStrengthForSubscription( @pcParametername = "subId") Integer subId)1500     public SignalStrength telephonyGetSignalStrengthForSubscription(
1501                     @RpcParameter(name = "subId") Integer subId) {
1502         StateChangeListener listener = getStateChangeListenerForSubscription(subId, false);
1503         if(listener == null) {
1504             Log.e("Invalid subscription ID");
1505             return null;
1506         }
1507         return listener.mSignalStrengthChangeListener.mSignalStrengths;
1508     }
1509 
1510     @Rpc(description = "Returns the sim count.")
telephonyGetSimCount()1511     public int telephonyGetSimCount() {
1512         return mTelephonyManager.getSimCount();
1513     }
1514 
1515     /**
1516      * Get the list of Forbidden PLMNs stored on the USIM
1517      * profile of the SIM for the default subscription.
1518      */
1519     @Rpc(description = "Returns a list of forbidden PLMNs")
telephonyGetForbiddenPlmns()1520     public @Nullable List<String> telephonyGetForbiddenPlmns() {
1521         String[] fplmns = mTelephonyManager.getForbiddenPlmns(
1522                 SubscriptionManager.getDefaultSubscriptionId(),
1523                 TelephonyManager.APPTYPE_USIM);
1524 
1525         if (fplmns != null) {
1526             return Arrays.asList(fplmns);
1527         }
1528         return null;
1529     }
1530 
getStateChangeListenerForSubscription( int subId, boolean createIfNeeded)1531     private StateChangeListener getStateChangeListenerForSubscription(
1532             int subId,
1533             boolean createIfNeeded) {
1534 
1535        if(mStateChangeListeners.get(subId) == null) {
1536             if(createIfNeeded == false) {
1537                 return null;
1538             }
1539 
1540             if(mSubscriptionManager.isValidSubscriptionId(subId) == false) {
1541                 Log.e("Cannot get listener for invalid/inactive subId");
1542                 return null;
1543             }
1544 
1545             mStateChangeListeners.put(subId, new StateChangeListener(subId));
1546         }
1547 
1548         return mStateChangeListeners.get(subId);
1549     }
1550 
1551     //FIXME: This whole class needs reworking. Why do we have separate listeners for everything?
1552     //We need one listener that overrides multiple methods.
1553     private final class StateChangeListener {
1554         public ServiceStateChangeListener mServiceStateChangeListener;
1555         public SignalStrengthChangeListener mSignalStrengthChangeListener;
1556         public CallStateChangeListener mCallStateChangeListener;
1557         public CellInfoChangeListener mCellInfoChangeListener;
1558         public DataConnectionStateChangeListener mDataConnectionStateChangeListener;
1559         public ActiveDataSubIdChangeListener mActiveDataSubIdChangeListener;
1560         public DisplayInfoStateChangeListener mDisplayInfoStateChangeListener;
1561         public DataConnectionRealTimeInfoChangeListener mDataConnectionRTInfoChangeListener;
1562         public VoiceMailStateChangeListener mVoiceMailStateChangeListener;
1563 
StateChangeListener(int subId)1564         public StateChangeListener(int subId) {
1565             mServiceStateChangeListener =
1566                 new ServiceStateChangeListener(mEventFacade, subId, mService.getMainLooper());
1567             mSignalStrengthChangeListener =
1568                 new SignalStrengthChangeListener(mEventFacade, subId, mService.getMainLooper());
1569             mDataConnectionStateChangeListener =
1570                 new DataConnectionStateChangeListener(
1571                         mEventFacade, mTelephonyManager, subId, mService.getMainLooper());
1572             mActiveDataSubIdChangeListener =
1573                 new ActiveDataSubIdChangeListener(
1574                         mEventFacade, mTelephonyManager, subId, mService.getMainLooper());
1575             mDisplayInfoStateChangeListener =
1576                 new DisplayInfoStateChangeListener(
1577                         mEventFacade, mTelephonyManager, subId, mService.getMainLooper());
1578             mCallStateChangeListener =
1579                 new CallStateChangeListener(mEventFacade, subId, mService.getMainLooper());
1580             mCellInfoChangeListener =
1581                 new CellInfoChangeListener(mEventFacade, subId, mService.getMainLooper());
1582             mDataConnectionRTInfoChangeListener =
1583                 new DataConnectionRealTimeInfoChangeListener(
1584                         mEventFacade, subId, mService.getMainLooper());
1585             mVoiceMailStateChangeListener =
1586                 new VoiceMailStateChangeListener(mEventFacade, subId, mService.getMainLooper());
1587         }
1588 
shutdown()1589         public void shutdown() {
1590             mTelephonyManager.listen(
1591                     mServiceStateChangeListener,
1592                     PhoneStateListener.LISTEN_NONE);
1593             mTelephonyManager.listen(
1594                     mSignalStrengthChangeListener,
1595                     PhoneStateListener.LISTEN_NONE);
1596             mTelephonyManager.listen(
1597                     mCallStateChangeListener,
1598                     PhoneStateListener.LISTEN_NONE);
1599             mTelephonyManager.listen(
1600                     mActiveDataSubIdChangeListener,
1601                     PhoneStateListener.LISTEN_NONE);
1602             mTelephonyManager.listen(
1603                     mDisplayInfoStateChangeListener,
1604                     PhoneStateListener.LISTEN_NONE);
1605             mTelephonyManager.listen(
1606                     mCellInfoChangeListener,
1607                     PhoneStateListener.LISTEN_NONE);
1608             mTelephonyManager.listen(
1609                     mDataConnectionStateChangeListener,
1610                     PhoneStateListener.LISTEN_NONE);
1611             mTelephonyManager.listen(
1612                     mDataConnectionRTInfoChangeListener,
1613                     PhoneStateListener.LISTEN_NONE);
1614             mTelephonyManager.listen(
1615                     mVoiceMailStateChangeListener,
1616                     PhoneStateListener.LISTEN_NONE);
1617         }
1618 
finalize()1619         protected void finalize() {
1620             try {
1621                 shutdown();
1622             } catch(Exception e) {}
1623         }
1624     }
1625 
1626     @Override
shutdown()1627     public void shutdown() {
1628         for(StateChangeListener listener : mStateChangeListeners.values()) {
1629             listener.shutdown();
1630         }
1631     }
1632 }
1633