blob: 507cbcbc772c5f4d2d69e504e40003230861fe57 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
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.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Wink Saville36469e72014-06-11 15:17:00 -070024import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.net.Uri;
26import android.os.AsyncResult;
27import android.os.Binder;
28import android.os.Bundle;
29import android.os.Handler;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070030import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070034import android.os.RemoteException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.ServiceManager;
36import android.os.UserHandle;
Ihab Awadf2177b72013-11-25 13:33:23 -080037import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.telephony.CellInfo;
Jake Hambye994d462014-02-03 13:10:13 -080039import android.telephony.NeighboringCellInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.ServiceState;
Wink Saville36469e72014-06-11 15:17:00 -070041import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.text.TextUtils;
44import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080045import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046
Shishir Agrawal566b7612013-10-28 14:41:00 -070047import com.android.internal.telephony.CallManager;
48import com.android.internal.telephony.CommandException;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070049import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080052import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070054import com.android.internal.telephony.PhoneFactory;
55import com.android.internal.telephony.CallManager;
56import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070058import com.android.internal.telephony.dataconnection.DctController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070059import com.android.internal.telephony.uicc.IccIoResult;
60import com.android.internal.telephony.uicc.IccUtils;
61import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080062import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063
Wink Saville36469e72014-06-11 15:17:00 -070064import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
65
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import java.util.ArrayList;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070067import java.util.HashMap;
68import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080069import java.util.List;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070070import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071
72/**
73 * Implementation of the ITelephony interface.
74 */
Santos Cordon117fee72014-05-16 17:56:12 -070075public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076 private static final String LOG_TAG = "PhoneInterfaceManager";
77 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
78 private static final boolean DBG_LOC = false;
79
80 // Message codes used with mMainThreadHandler
81 private static final int CMD_HANDLE_PIN_MMI = 1;
82 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
83 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
84 private static final int CMD_ANSWER_RINGING_CALL = 4;
85 private static final int CMD_END_CALL = 5; // not used yet
86 private static final int CMD_SILENCE_RINGER = 6;
Shishir Agrawal566b7612013-10-28 14:41:00 -070087 private static final int CMD_TRANSMIT_APDU = 7;
88 private static final int EVENT_TRANSMIT_APDU_DONE = 8;
89 private static final int CMD_OPEN_CHANNEL = 9;
90 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
91 private static final int CMD_CLOSE_CHANNEL = 11;
92 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -080093 private static final int CMD_NV_READ_ITEM = 13;
94 private static final int EVENT_NV_READ_ITEM_DONE = 14;
95 private static final int CMD_NV_WRITE_ITEM = 15;
96 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
97 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
98 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
99 private static final int CMD_NV_RESET_CONFIG = 19;
100 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800101 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
102 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
103 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
104 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800105 private static final int CMD_SEND_ENVELOPE = 25;
106 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Junda Liu787bc7e2014-06-30 13:38:02 -0700107 private static final int CMD_SET_CDMA_SUBSCRIPTION = 27;
108 private static final int EVENT_SET_CDMA_SUBSCRIPTION_DONE = 28;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109
110 /** The singleton instance. */
111 private static PhoneInterfaceManager sInstance;
112
113 PhoneGlobals mApp;
114 Phone mPhone;
115 CallManager mCM;
116 AppOpsManager mAppOps;
117 MainThreadHandler mMainThreadHandler;
118
119 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700120 * A request object to use for transmitting data to an ICC.
121 */
122 private static final class IccAPDUArgument {
123 public int channel, cla, command, p1, p2, p3;
124 public String data;
125
126 public IccAPDUArgument(int channel, int cla, int command,
127 int p1, int p2, int p3, String data) {
128 this.channel = channel;
129 this.cla = cla;
130 this.command = command;
131 this.p1 = p1;
132 this.p2 = p2;
133 this.p3 = p3;
134 this.data = data;
135 }
136 }
137
138 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
140 * request after sending. The main thread will notify the request when it is complete.
141 */
142 private static final class MainThreadRequest {
143 /** The argument to use for the request */
144 public Object argument;
145 /** The result of the request that is run on the main thread */
146 public Object result;
147
148 public MainThreadRequest(Object argument) {
149 this.argument = argument;
150 }
151 }
152
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800153 private static final class IncomingThirdPartyCallArgs {
154 public final ComponentName component;
155 public final String callId;
156 public final String callerDisplayName;
157
158 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
159 String callerDisplayName) {
160 this.component = component;
161 this.callId = callId;
162 this.callerDisplayName = callerDisplayName;
163 }
164 }
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 /**
167 * A handler that processes messages on the main thread in the phone process. Since many
168 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
169 * inbound binder threads to the main thread in the phone process. The Binder thread
170 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
171 * on, which will be notified when the operation completes and will contain the result of the
172 * request.
173 *
174 * <p>If a MainThreadRequest object is provided in the msg.obj field,
175 * note that request.result must be set to something non-null for the calling thread to
176 * unblock.
177 */
178 private final class MainThreadHandler extends Handler {
179 @Override
180 public void handleMessage(Message msg) {
181 MainThreadRequest request;
182 Message onCompleted;
183 AsyncResult ar;
184
185 switch (msg.what) {
186 case CMD_HANDLE_PIN_MMI:
187 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800188 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 // Wake up the requesting thread
190 synchronized (request) {
191 request.notifyAll();
192 }
193 break;
194
195 case CMD_HANDLE_NEIGHBORING_CELL:
196 request = (MainThreadRequest) msg.obj;
197 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
198 request);
199 mPhone.getNeighboringCids(onCompleted);
200 break;
201
202 case EVENT_NEIGHBORING_CELL_DONE:
203 ar = (AsyncResult) msg.obj;
204 request = (MainThreadRequest) ar.userObj;
205 if (ar.exception == null && ar.result != null) {
206 request.result = ar.result;
207 } else {
208 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800209 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 }
211 // Wake up the requesting thread
212 synchronized (request) {
213 request.notifyAll();
214 }
215 break;
216
217 case CMD_ANSWER_RINGING_CALL:
218 answerRingingCallInternal();
219 break;
220
221 case CMD_SILENCE_RINGER:
222 silenceRingerInternal();
223 break;
224
225 case CMD_END_CALL:
226 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800227 boolean hungUp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 int phoneType = mPhone.getPhoneType();
229 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
230 // CDMA: If the user presses the Power button we treat it as
231 // ending the complete call session
232 hungUp = PhoneUtils.hangupRingingAndActive(mPhone);
233 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
234 // GSM: End the call as per the Phone state
235 hungUp = PhoneUtils.hangup(mCM);
236 } else {
237 throw new IllegalStateException("Unexpected phone type: " + phoneType);
238 }
239 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
240 request.result = hungUp;
241 // Wake up the requesting thread
242 synchronized (request) {
243 request.notifyAll();
244 }
245 break;
246
Shishir Agrawal566b7612013-10-28 14:41:00 -0700247 case CMD_TRANSMIT_APDU:
248 request = (MainThreadRequest) msg.obj;
249 IccAPDUArgument argument = (IccAPDUArgument) request.argument;
250 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_DONE, request);
251 UiccController.getInstance().getUiccCard().iccTransmitApduLogicalChannel(
252 argument.channel, argument.cla, argument.command,
253 argument.p1, argument.p2, argument.p3, argument.data,
254 onCompleted);
255 break;
256
257 case EVENT_TRANSMIT_APDU_DONE:
258 ar = (AsyncResult) msg.obj;
259 request = (MainThreadRequest) ar.userObj;
260 if (ar.exception == null && ar.result != null) {
261 request.result = ar.result;
262 } else {
263 request.result = new IccIoResult(0x6F, 0, (byte[])null);
264 if (ar.result == null) {
265 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800266 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700267 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800268 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700269 } else {
270 loge("iccTransmitApduLogicalChannel: Unknown exception");
271 }
272 }
273 synchronized (request) {
274 request.notifyAll();
275 }
276 break;
277
Derek Tan4d5e5c12014-02-04 11:54:58 -0800278 case CMD_SEND_ENVELOPE:
279 request = (MainThreadRequest) msg.obj;
280 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700281 UiccController.getInstance().getUiccCard().sendEnvelopeWithStatus(
Derek Tan4d5e5c12014-02-04 11:54:58 -0800282 (String)request.argument, onCompleted);
283 break;
284
285 case EVENT_SEND_ENVELOPE_DONE:
286 ar = (AsyncResult) msg.obj;
287 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700288 if (ar.exception == null && ar.result != null) {
289 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800290 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700291 request.result = new IccIoResult(0x6F, 0, (byte[])null);
292 if (ar.result == null) {
293 loge("sendEnvelopeWithStatus: Empty response");
294 } else if (ar.exception instanceof CommandException) {
295 loge("sendEnvelopeWithStatus: CommandException: " +
296 ar.exception);
297 } else {
298 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
299 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800300 }
301 synchronized (request) {
302 request.notifyAll();
303 }
304 break;
305
Shishir Agrawal566b7612013-10-28 14:41:00 -0700306 case CMD_OPEN_CHANNEL:
307 request = (MainThreadRequest) msg.obj;
308 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
309 UiccController.getInstance().getUiccCard().iccOpenLogicalChannel(
310 (String)request.argument, onCompleted);
311 break;
312
313 case EVENT_OPEN_CHANNEL_DONE:
314 ar = (AsyncResult) msg.obj;
315 request = (MainThreadRequest) ar.userObj;
316 if (ar.exception == null && ar.result != null) {
Jake Hambye994d462014-02-03 13:10:13 -0800317 request.result = ((int[]) ar.result)[0];
Shishir Agrawal566b7612013-10-28 14:41:00 -0700318 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800319 request.result = -1;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700320 if (ar.result == null) {
321 loge("iccOpenLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800322 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700323 loge("iccOpenLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800324 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700325 } else {
326 loge("iccOpenLogicalChannel: Unknown exception");
327 }
328 }
329 synchronized (request) {
330 request.notifyAll();
331 }
332 break;
333
334 case CMD_CLOSE_CHANNEL:
335 request = (MainThreadRequest) msg.obj;
336 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE,
337 request);
338 UiccController.getInstance().getUiccCard().iccCloseLogicalChannel(
Jake Hambye994d462014-02-03 13:10:13 -0800339 (Integer) request.argument,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700340 onCompleted);
341 break;
342
343 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800344 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
345 break;
346
347 case CMD_NV_READ_ITEM:
348 request = (MainThreadRequest) msg.obj;
349 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
350 mPhone.nvReadItem((Integer) request.argument, onCompleted);
351 break;
352
353 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700354 ar = (AsyncResult) msg.obj;
355 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800356 if (ar.exception == null && ar.result != null) {
357 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700358 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800359 request.result = "";
360 if (ar.result == null) {
361 loge("nvReadItem: Empty response");
362 } else if (ar.exception instanceof CommandException) {
363 loge("nvReadItem: CommandException: " +
364 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700365 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800366 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700367 }
368 }
369 synchronized (request) {
370 request.notifyAll();
371 }
372 break;
373
Jake Hambye994d462014-02-03 13:10:13 -0800374 case CMD_NV_WRITE_ITEM:
375 request = (MainThreadRequest) msg.obj;
376 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
377 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
378 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
379 break;
380
381 case EVENT_NV_WRITE_ITEM_DONE:
382 handleNullReturnEvent(msg, "nvWriteItem");
383 break;
384
385 case CMD_NV_WRITE_CDMA_PRL:
386 request = (MainThreadRequest) msg.obj;
387 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
388 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
389 break;
390
391 case EVENT_NV_WRITE_CDMA_PRL_DONE:
392 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
393 break;
394
395 case CMD_NV_RESET_CONFIG:
396 request = (MainThreadRequest) msg.obj;
397 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
398 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
399 break;
400
401 case EVENT_NV_RESET_CONFIG_DONE:
402 handleNullReturnEvent(msg, "nvResetConfig");
403 break;
404
Jake Hamby7c27be32014-03-03 13:25:59 -0800405 case CMD_GET_PREFERRED_NETWORK_TYPE:
406 request = (MainThreadRequest) msg.obj;
407 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
408 mPhone.getPreferredNetworkType(onCompleted);
409 break;
410
411 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
412 ar = (AsyncResult) msg.obj;
413 request = (MainThreadRequest) ar.userObj;
414 if (ar.exception == null && ar.result != null) {
415 request.result = ar.result; // Integer
416 } else {
417 request.result = -1;
418 if (ar.result == null) {
419 loge("getPreferredNetworkType: Empty response");
420 } else if (ar.exception instanceof CommandException) {
421 loge("getPreferredNetworkType: CommandException: " +
422 ar.exception);
423 } else {
424 loge("getPreferredNetworkType: Unknown exception");
425 }
426 }
427 synchronized (request) {
428 request.notifyAll();
429 }
430 break;
431
432 case CMD_SET_PREFERRED_NETWORK_TYPE:
433 request = (MainThreadRequest) msg.obj;
434 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
435 int networkType = (Integer) request.argument;
436 mPhone.setPreferredNetworkType(networkType, onCompleted);
437 break;
438
439 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
440 handleNullReturnEvent(msg, "setPreferredNetworkType");
441 break;
442
Junda Liu787bc7e2014-06-30 13:38:02 -0700443 case CMD_SET_CDMA_SUBSCRIPTION:
444 request = (MainThreadRequest) msg.obj;
445 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_DONE, request);
446 int subscriptionType = (Integer) request.argument;
447 mPhone.setCdmaSubscription(subscriptionType, onCompleted);
448 break;
449
450 case EVENT_SET_CDMA_SUBSCRIPTION_DONE:
451 handleNullReturnEvent(msg, "setCdmaSubscription");
452 break;
453
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700454 default:
455 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
456 break;
457 }
458 }
Jake Hambye994d462014-02-03 13:10:13 -0800459
460 private void handleNullReturnEvent(Message msg, String command) {
461 AsyncResult ar = (AsyncResult) msg.obj;
462 MainThreadRequest request = (MainThreadRequest) ar.userObj;
463 if (ar.exception == null) {
464 request.result = true;
465 } else {
466 request.result = false;
467 if (ar.exception instanceof CommandException) {
468 loge(command + ": CommandException: " + ar.exception);
469 } else {
470 loge(command + ": Unknown exception");
471 }
472 }
473 synchronized (request) {
474 request.notifyAll();
475 }
476 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700477 }
478
479 /**
480 * Posts the specified command to be executed on the main thread,
481 * waits for the request to complete, and returns the result.
482 * @see #sendRequestAsync
483 */
484 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700485 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700486 }
487
488 /**
489 * Posts the specified command to be executed on the main thread,
490 * waits for the request to complete, and returns the result.
491 * @see #sendRequestAsync
492 */
493 private Object sendRequest(int command, Object argument, Object argument2) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700494 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
495 throw new RuntimeException("This method will deadlock if called from the main thread.");
496 }
497
498 MainThreadRequest request = new MainThreadRequest(argument);
499 Message msg = mMainThreadHandler.obtainMessage(command, request);
500 msg.sendToTarget();
501
502 // Wait for the request to complete
503 synchronized (request) {
504 while (request.result == null) {
505 try {
506 request.wait();
507 } catch (InterruptedException e) {
508 // Do nothing, go back and wait until the request is complete
509 }
510 }
511 }
512 return request.result;
513 }
514
515 /**
516 * Asynchronous ("fire and forget") version of sendRequest():
517 * Posts the specified command to be executed on the main thread, and
518 * returns immediately.
519 * @see #sendRequest
520 */
521 private void sendRequestAsync(int command) {
522 mMainThreadHandler.sendEmptyMessage(command);
523 }
524
525 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700526 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
527 * @see {@link #sendRequest(int,Object)}
528 */
529 private void sendRequestAsync(int command, Object argument) {
530 MainThreadRequest request = new MainThreadRequest(argument);
531 Message msg = mMainThreadHandler.obtainMessage(command, request);
532 msg.sendToTarget();
533 }
534
535 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700536 * Initialize the singleton PhoneInterfaceManager instance.
537 * This is only done once, at startup, from PhoneApp.onCreate().
538 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700539 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 synchronized (PhoneInterfaceManager.class) {
541 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700542 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 } else {
544 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
545 }
546 return sInstance;
547 }
548 }
549
550 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700551 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700552 mApp = app;
553 mPhone = phone;
554 mCM = PhoneGlobals.getInstance().mCM;
555 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
556 mMainThreadHandler = new MainThreadHandler();
557 publish();
558 }
559
560 private void publish() {
561 if (DBG) log("publish: " + this);
562
563 ServiceManager.addService("phone", this);
564 }
565
Wink Saville36469e72014-06-11 15:17:00 -0700566 // returns phone associated with the subId.
567 // getPhone(0) returns default phone in single SIM mode.
568 private Phone getPhone(long subId) {
569 // FIXME: hack for the moment
570 return mPhone;
571 // return PhoneUtils.getPhoneUsingSubId(subId);
572 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 //
574 // Implementation of the ITelephony interface.
575 //
576
577 public void dial(String number) {
Wink Saville36469e72014-06-11 15:17:00 -0700578 dialUsingSubId(getPreferredVoiceSubscription(), number);
579 }
580
581 public void dialUsingSubId(long subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 if (DBG) log("dial: " + number);
583 // No permission check needed here: This is just a wrapper around the
584 // ACTION_DIAL intent, which is available to any app since it puts up
585 // the UI before it does anything.
586
587 String url = createTelUrl(number);
588 if (url == null) {
589 return;
590 }
591
592 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700593 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
595 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
596 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Wink Saville36469e72014-06-11 15:17:00 -0700597 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700598 mApp.startActivity(intent);
599 }
600 }
601
602 public void call(String callingPackage, String number) {
Wink Saville36469e72014-06-11 15:17:00 -0700603 callUsingSubId(getPreferredVoiceSubscription(), callingPackage, number);
604 }
605
606 public void callUsingSubId(long subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700607 if (DBG) log("call: " + number);
608
609 // This is just a wrapper around the ACTION_CALL intent, but we still
610 // need to do a permission check since we're calling startActivity()
611 // from the context of the phone app.
612 enforceCallPermission();
613
614 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
615 != AppOpsManager.MODE_ALLOWED) {
616 return;
617 }
618
619 String url = createTelUrl(number);
620 if (url == null) {
621 return;
622 }
623
624 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700625 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700626 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
627 mApp.startActivity(intent);
628 }
629
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700630 /**
631 * End a call based on call state
632 * @return true is a call was ended
633 */
634 public boolean endCall() {
Wink Saville36469e72014-06-11 15:17:00 -0700635 return endCallUsingSubId(getDefaultSubscription());
636 }
637
638 /**
639 * End a call based on the call state of the subId
640 * @return true is a call was ended
641 */
642 public boolean endCallUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 enforceCallPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700644 return (Boolean) sendRequest(CMD_END_CALL, subId, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 }
646
647 public void answerRingingCall() {
Wink Saville36469e72014-06-11 15:17:00 -0700648 answerRingingCallUsingSubId(getDefaultSubscription());
649 }
650
651 public void answerRingingCallUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700652 if (DBG) log("answerRingingCall...");
653 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
654 // but that can probably wait till the big TelephonyManager API overhaul.
655 // For now, protect this call with the MODIFY_PHONE_STATE permission.
656 enforceModifyPermission();
657 sendRequestAsync(CMD_ANSWER_RINGING_CALL);
658 }
659
660 /**
661 * Make the actual telephony calls to implement answerRingingCall().
662 * This should only be called from the main thread of the Phone app.
663 * @see #answerRingingCall
664 *
665 * TODO: it would be nice to return true if we answered the call, or
666 * false if there wasn't actually a ringing incoming call, or some
667 * other error occurred. (In other words, pass back the return value
668 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
669 * But that would require calling this method via sendRequest() rather
670 * than sendRequestAsync(), and right now we don't actually *need* that
671 * return value, so let's just return void for now.
672 */
673 private void answerRingingCallInternal() {
674 final boolean hasRingingCall = !mPhone.getRingingCall().isIdle();
675 if (hasRingingCall) {
676 final boolean hasActiveCall = !mPhone.getForegroundCall().isIdle();
677 final boolean hasHoldingCall = !mPhone.getBackgroundCall().isIdle();
678 if (hasActiveCall && hasHoldingCall) {
679 // Both lines are in use!
680 // TODO: provide a flag to let the caller specify what
681 // policy to use if both lines are in use. (The current
682 // behavior is hardwired to "answer incoming, end ongoing",
683 // which is how the CALL button is specced to behave.)
684 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
685 return;
686 } else {
687 // answerCall() will automatically hold the current active
688 // call, if there is one.
689 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
690 return;
691 }
692 } else {
693 // No call was ringing.
694 return;
695 }
696 }
697
698 public void silenceRinger() {
699 if (DBG) log("silenceRinger...");
700 // TODO: find a more appropriate permission to check here.
701 // (That can probably wait till the big TelephonyManager API overhaul.
702 // For now, protect this call with the MODIFY_PHONE_STATE permission.)
703 enforceModifyPermission();
704 sendRequestAsync(CMD_SILENCE_RINGER);
705 }
706
707 /**
708 * Internal implemenation of silenceRinger().
709 * This should only be called from the main thread of the Phone app.
710 * @see #silenceRinger
711 */
712 private void silenceRingerInternal() {
713 if ((mCM.getState() == PhoneConstants.State.RINGING)
714 && mApp.notifier.isRinging()) {
715 // Ringer is actually playing, so silence it.
716 if (DBG) log("silenceRingerInternal: silencing...");
717 mApp.notifier.silenceRinger();
718 }
719 }
720
721 public boolean isOffhook() {
Wink Saville36469e72014-06-11 15:17:00 -0700722 return isOffhookUsingSubId(getDefaultSubscription());
723 }
724
725 public boolean isOffhookUsingSubId(long subId) {
726 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 }
728
729 public boolean isRinging() {
Wink Saville36469e72014-06-11 15:17:00 -0700730 return (isRingingUsingSubId(getDefaultSubscription()));
731 }
732
733 public boolean isRingingUsingSubId(long subId) {
734 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 }
736
737 public boolean isIdle() {
Wink Saville36469e72014-06-11 15:17:00 -0700738 return isIdleUsingSubId(getDefaultSubscription());
739 }
740
741 public boolean isIdleUsingSubId(long subId) {
742 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 }
744
745 public boolean isSimPinEnabled() {
746 enforceReadPermission();
747 return (PhoneGlobals.getInstance().isSimPinEnabled());
748 }
749
750 public boolean supplyPin(String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700751 return supplyPinUsingSubId(getDefaultSubscription(), pin);
752 }
753
754 public boolean supplyPinUsingSubId(long subId, String pin) {
755 int [] resultArray = supplyPinReportResultUsingSubId(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700756 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
757 }
758
759 public boolean supplyPuk(String puk, String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700760 return supplyPukUsingSubId(getDefaultSubscription(), puk, pin);
761 }
762
763 public boolean supplyPukUsingSubId(long subId, String puk, String pin) {
764 int [] resultArray = supplyPukReportResultUsingSubId(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700765 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
766 }
767
768 /** {@hide} */
769 public int[] supplyPinReportResult(String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700770 return supplyPinReportResultUsingSubId(getDefaultSubscription(), pin);
771 }
772
773 public int[] supplyPinReportResultUsingSubId(long subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700775 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 checkSimPin.start();
777 return checkSimPin.unlockSim(null, pin);
778 }
779
Wink Saville9de0f752013-10-22 19:04:03 -0700780 /** {@hide} */
781 public int[] supplyPukReportResult(String puk, String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700782 return supplyPukReportResultUsingSubId(getDefaultSubscription(), puk, pin);
783 }
784
785 public int[] supplyPukReportResultUsingSubId(long subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700787 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 checkSimPuk.start();
789 return checkSimPuk.unlockSim(puk, pin);
790 }
791
792 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700793 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 * a synchronous one.
795 */
796 private static class UnlockSim extends Thread {
797
798 private final IccCard mSimCard;
799
800 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700801 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
802 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803
804 // For replies from SimCard interface
805 private Handler mHandler;
806
807 // For async handler to identify request type
808 private static final int SUPPLY_PIN_COMPLETE = 100;
809
810 public UnlockSim(IccCard simCard) {
811 mSimCard = simCard;
812 }
813
814 @Override
815 public void run() {
816 Looper.prepare();
817 synchronized (UnlockSim.this) {
818 mHandler = new Handler() {
819 @Override
820 public void handleMessage(Message msg) {
821 AsyncResult ar = (AsyncResult) msg.obj;
822 switch (msg.what) {
823 case SUPPLY_PIN_COMPLETE:
824 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
825 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700826 mRetryCount = msg.arg1;
827 if (ar.exception != null) {
828 if (ar.exception instanceof CommandException &&
829 ((CommandException)(ar.exception)).getCommandError()
830 == CommandException.Error.PASSWORD_INCORRECT) {
831 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
832 } else {
833 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
834 }
835 } else {
836 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
837 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 mDone = true;
839 UnlockSim.this.notifyAll();
840 }
841 break;
842 }
843 }
844 };
845 UnlockSim.this.notifyAll();
846 }
847 Looper.loop();
848 }
849
850 /*
851 * Use PIN or PUK to unlock SIM card
852 *
853 * If PUK is null, unlock SIM card with PIN
854 *
855 * If PUK is not null, unlock SIM card with PUK and set PIN code
856 */
Wink Saville9de0f752013-10-22 19:04:03 -0700857 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858
859 while (mHandler == null) {
860 try {
861 wait();
862 } catch (InterruptedException e) {
863 Thread.currentThread().interrupt();
864 }
865 }
866 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
867
868 if (puk == null) {
869 mSimCard.supplyPin(pin, callback);
870 } else {
871 mSimCard.supplyPuk(puk, pin, callback);
872 }
873
874 while (!mDone) {
875 try {
876 Log.d(LOG_TAG, "wait for done");
877 wait();
878 } catch (InterruptedException e) {
879 // Restore the interrupted status
880 Thread.currentThread().interrupt();
881 }
882 }
883 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -0700884 int[] resultArray = new int[2];
885 resultArray[0] = mResult;
886 resultArray[1] = mRetryCount;
887 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 }
889 }
890
891 public void updateServiceLocation() {
Wink Saville36469e72014-06-11 15:17:00 -0700892 updateServiceLocationUsingSubId(getDefaultSubscription());
893
894 }
895
896 public void updateServiceLocationUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 // No permission check needed here: this call is harmless, and it's
898 // needed for the ServiceState.requestStateUpdate() call (which is
899 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -0700900 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 }
902
903 public boolean isRadioOn() {
Wink Saville36469e72014-06-11 15:17:00 -0700904 return isRadioOnUsingSubId(getDefaultSubscription());
905 }
906
907 public boolean isRadioOnUsingSubId(long subId) {
908 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 }
910
911 public void toggleRadioOnOff() {
Wink Saville36469e72014-06-11 15:17:00 -0700912 toggleRadioOnOffUsingSubId(getDefaultSubscription());
913
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 }
Wink Saville36469e72014-06-11 15:17:00 -0700915
916 public void toggleRadioOnOffUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700918 getPhone(subId).setRadioPower(!isRadioOnUsingSubId(subId));
919 }
920
921 public boolean setRadio(boolean turnOn) {
922 return setRadioUsingSubId(getDefaultSubscription(), turnOn);
923 }
924
925 public boolean setRadioUsingSubId(long subId, boolean turnOn) {
926 enforceModifyPermission();
927 if ((getPhone(subId).getServiceState().getState() !=
928 ServiceState.STATE_POWER_OFF) != turnOn) {
929 toggleRadioOnOffUsingSubId(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700930 }
931 return true;
932 }
Wink Saville36469e72014-06-11 15:17:00 -0700933
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 public boolean setRadioPower(boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -0700935 return setRadioPowerUsingSubId(getDefaultSubscription(), turnOn);
936 }
937
938 public boolean setRadioPowerUsingSubId(long subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700940 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 return true;
942 }
943
Wink Saville36469e72014-06-11 15:17:00 -0700944 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 public boolean enableDataConnectivity() {
946 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700947 long subId = SubscriptionManager.getDefaultDataSubId();
948 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 return true;
950 }
951
Wink Saville36469e72014-06-11 15:17:00 -0700952 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 public boolean disableDataConnectivity() {
954 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700955 long subId = SubscriptionManager.getDefaultDataSubId();
956 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 return true;
958 }
959
Wink Saville36469e72014-06-11 15:17:00 -0700960 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 public boolean isDataConnectivityPossible() {
Wink Saville36469e72014-06-11 15:17:00 -0700962 long subId = SubscriptionManager.getDefaultDataSubId();
963 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 }
965
966 public boolean handlePinMmi(String dialString) {
Wink Saville36469e72014-06-11 15:17:00 -0700967 return handlePinMmiUsingSubId(getDefaultSubscription(), dialString);
968 }
969
970 public boolean handlePinMmiUsingSubId(long subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700972 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 }
974
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 public int getCallState() {
Wink Saville36469e72014-06-11 15:17:00 -0700976 return getCallStateUsingSubId(getDefaultSubscription());
977 }
978
979 public int getCallStateUsingSubId(long subId) {
980 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 }
982
983 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -0700984 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
985 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 }
987
988 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -0700989 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
990 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 }
992
993 @Override
994 public Bundle getCellLocation() {
995 try {
996 mApp.enforceCallingOrSelfPermission(
997 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
998 } catch (SecurityException e) {
999 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1000 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1001 // is the weaker precondition
1002 mApp.enforceCallingOrSelfPermission(
1003 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1004 }
1005
Jake Hambye994d462014-02-03 13:10:13 -08001006 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 if (DBG_LOC) log("getCellLocation: is active user");
1008 Bundle data = new Bundle();
1009 mPhone.getCellLocation().fillInNotifierBundle(data);
1010 return data;
1011 } else {
1012 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1013 return null;
1014 }
1015 }
1016
1017 @Override
1018 public void enableLocationUpdates() {
Wink Saville36469e72014-06-11 15:17:00 -07001019 enableLocationUpdatesUsingSubId(getDefaultSubscription());
1020 }
1021
1022 public void enableLocationUpdatesUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 mApp.enforceCallingOrSelfPermission(
1024 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001025 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 }
1027
1028 @Override
1029 public void disableLocationUpdates() {
Wink Saville36469e72014-06-11 15:17:00 -07001030 disableLocationUpdatesUsingSubId(getDefaultSubscription());
1031 }
1032
1033 public void disableLocationUpdatesUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 mApp.enforceCallingOrSelfPermission(
1035 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001036 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 }
1038
1039 @Override
1040 @SuppressWarnings("unchecked")
1041 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1042 try {
1043 mApp.enforceCallingOrSelfPermission(
1044 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1045 } catch (SecurityException e) {
1046 // If we have ACCESS_FINE_LOCATION permission, skip the check
1047 // for ACCESS_COARSE_LOCATION
1048 // A failure should throw the SecurityException from
1049 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1050 mApp.enforceCallingOrSelfPermission(
1051 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1052 }
1053
1054 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1055 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1056 return null;
1057 }
Jake Hambye994d462014-02-03 13:10:13 -08001058 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1060
1061 ArrayList<NeighboringCellInfo> cells = null;
1062
1063 try {
1064 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001065 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001066 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001067 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 }
1069 return cells;
1070 } else {
1071 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1072 return null;
1073 }
1074 }
1075
1076
1077 @Override
1078 public List<CellInfo> getAllCellInfo() {
1079 try {
1080 mApp.enforceCallingOrSelfPermission(
1081 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1082 } catch (SecurityException e) {
1083 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1084 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1085 // is the weaker precondition
1086 mApp.enforceCallingOrSelfPermission(
1087 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1088 }
1089
Jake Hambye994d462014-02-03 13:10:13 -08001090 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001091 if (DBG_LOC) log("getAllCellInfo: is active user");
1092 return mPhone.getAllCellInfo();
1093 } else {
1094 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1095 return null;
1096 }
1097 }
1098
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001099 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100 public void setCellInfoListRate(int rateInMillis) {
1101 mPhone.setCellInfoListRate(rateInMillis);
1102 }
1103
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 //
1105 // Internal helper methods.
1106 //
1107
Jake Hambye994d462014-02-03 13:10:13 -08001108 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 boolean ok;
1110
1111 boolean self = Binder.getCallingUid() == Process.myUid();
1112 if (!self) {
1113 // Get the caller's user id then clear the calling identity
1114 // which will be restored in the finally clause.
1115 int callingUser = UserHandle.getCallingUserId();
1116 long ident = Binder.clearCallingIdentity();
1117
1118 try {
1119 // With calling identity cleared the current user is the foreground user.
1120 int foregroundUser = ActivityManager.getCurrentUser();
1121 ok = (foregroundUser == callingUser);
1122 if (DBG_LOC) {
1123 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1124 + " callingUser=" + callingUser + " ok=" + ok);
1125 }
1126 } catch (Exception ex) {
1127 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1128 ok = false;
1129 } finally {
1130 Binder.restoreCallingIdentity(ident);
1131 }
1132 } else {
1133 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1134 ok = true;
1135 }
1136 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1137 return ok;
1138 }
1139
1140 /**
1141 * Make sure the caller has the READ_PHONE_STATE permission.
1142 *
1143 * @throws SecurityException if the caller does not have the required permission
1144 */
1145 private void enforceReadPermission() {
1146 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1147 }
1148
1149 /**
1150 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1151 *
1152 * @throws SecurityException if the caller does not have the required permission
1153 */
1154 private void enforceModifyPermission() {
1155 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1156 }
1157
1158 /**
1159 * Make sure the caller has the CALL_PHONE permission.
1160 *
1161 * @throws SecurityException if the caller does not have the required permission
1162 */
1163 private void enforceCallPermission() {
1164 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1165 }
1166
Shishir Agrawal566b7612013-10-28 14:41:00 -07001167 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001168 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1169 *
1170 * @throws SecurityException if the caller does not have the required permission
1171 */
1172 private void enforcePrivilegedPhoneStatePermission() {
1173 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1174 null);
1175 }
1176
1177 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -07001178 * Make sure the caller has SIM_COMMUNICATION permission.
1179 *
1180 * @throws SecurityException if the caller does not have the required permission.
1181 */
1182 private void enforceSimCommunicationPermission() {
1183 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.SIM_COMMUNICATION, null);
1184 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001185
1186 private String createTelUrl(String number) {
1187 if (TextUtils.isEmpty(number)) {
1188 return null;
1189 }
1190
Jake Hambye994d462014-02-03 13:10:13 -08001191 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192 }
1193
Ihab Awadf9e92732013-12-05 18:02:52 -08001194 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1196 }
1197
Ihab Awadf9e92732013-12-05 18:02:52 -08001198 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1200 }
1201
1202 public int getActivePhoneType() {
Wink Saville36469e72014-06-11 15:17:00 -07001203 return getActivePhoneTypeUsingSubId(getDefaultSubscription());
1204 }
1205
1206 public int getActivePhoneTypeUsingSubId(long subId) {
1207 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001208 }
1209
1210 /**
1211 * Returns the CDMA ERI icon index to display
1212 */
1213 public int getCdmaEriIconIndex() {
Wink Saville36469e72014-06-11 15:17:00 -07001214 return getCdmaEriIconIndexUsingSubId(getDefaultSubscription());
1215
1216 }
1217
1218 public int getCdmaEriIconIndexUsingSubId(long subId) {
1219 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 }
1221
1222 /**
1223 * Returns the CDMA ERI icon mode,
1224 * 0 - ON
1225 * 1 - FLASHING
1226 */
1227 public int getCdmaEriIconMode() {
Wink Saville36469e72014-06-11 15:17:00 -07001228 return getCdmaEriIconModeUsingSubId(getDefaultSubscription());
1229 }
1230
1231 public int getCdmaEriIconModeUsingSubId(long subId) {
1232 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 }
1234
1235 /**
1236 * Returns the CDMA ERI text,
1237 */
1238 public String getCdmaEriText() {
Wink Saville36469e72014-06-11 15:17:00 -07001239 return getCdmaEriTextUsingSubId(getDefaultSubscription());
1240 }
1241
1242 public String getCdmaEriTextUsingSubId(long subId) {
1243 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244 }
1245
1246 /**
1247 * Returns true if CDMA provisioning needs to run.
1248 */
1249 public boolean needsOtaServiceProvisioning() {
1250 return mPhone.needsOtaServiceProvisioning();
1251 }
1252
1253 /**
1254 * Returns the unread count of voicemails
1255 */
1256 public int getVoiceMessageCount() {
Wink Saville36469e72014-06-11 15:17:00 -07001257 return getVoiceMessageCountUsingSubId(getDefaultSubscription());
1258 }
1259
1260 /**
1261 * Returns the unread count of voicemails for a subId
1262 */
1263 public int getVoiceMessageCountUsingSubId( long subId) {
1264 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 }
1266
1267 /**
1268 * Returns the data network type
1269 *
1270 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1271 */
1272 @Override
1273 public int getNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001274 return getNetworkTypeUsingSubId(getDefaultSubscription());
1275 }
1276
1277 /**
1278 * Returns the network type for a subId
1279 */
1280 @Override
1281 public int getNetworkTypeUsingSubId(long subId) {
1282 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 }
1284
1285 /**
1286 * Returns the data network type
1287 */
1288 @Override
1289 public int getDataNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001290 return getDataNetworkTypeUsingSubId(getDefaultSubscription());
1291 }
1292
1293 /**
1294 * Returns the data network type for a subId
1295 */
1296 @Override
1297 public int getDataNetworkTypeUsingSubId(long subId) {
1298 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001299 }
1300
1301 /**
1302 * Returns the data network type
1303 */
1304 @Override
1305 public int getVoiceNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001306 return getVoiceNetworkTypeUsingSubId(getDefaultSubscription());
1307 }
1308
1309 /**
1310 * Returns the Voice network type for a subId
1311 */
1312 @Override
1313 public int getVoiceNetworkTypeUsingSubId(long subId) {
1314 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315 }
1316
1317 /**
1318 * @return true if a ICC card is present
1319 */
1320 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001321 // FIXME Make changes to pass defaultSimId of type int
1322 return hasIccCardUsingSlotId(getDefaultSubscription());
1323 }
1324
1325 /**
1326 * @return true if a ICC card is present for a slotId
1327 */
1328 public boolean hasIccCardUsingSlotId(long slotId) {
1329 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001330 }
1331
1332 /**
1333 * Return if the current radio is LTE on CDMA. This
1334 * is a tri-state return value as for a period of time
1335 * the mode may be unknown.
1336 *
1337 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001338 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 */
1340 public int getLteOnCdmaMode() {
Wink Saville36469e72014-06-11 15:17:00 -07001341 return getLteOnCdmaModeUsingSubId(getDefaultSubscription());
1342 }
1343
1344 public int getLteOnCdmaModeUsingSubId(long subId) {
1345 return getPhone(subId).getLteOnCdmaMode();
1346 }
1347
1348 public void setPhone(Phone phone) {
1349 mPhone = phone;
1350 }
1351
1352 /**
1353 * {@hide}
1354 * Returns Default subId, 0 in the case of single standby.
1355 */
1356 private long getDefaultSubscription() {
1357 return SubscriptionManager.getDefaultSubId();
1358 }
1359
1360 private long getPreferredVoiceSubscription() {
1361 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001363
1364 /**
1365 * @see android.telephony.TelephonyManager.WifiCallingChoices
1366 */
1367 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001368 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1369 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001370 }
1371
1372 /**
1373 * @see android.telephony.TelephonyManager.WifiCallingChoices
1374 */
1375 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001376 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1377 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1378 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001379 }
1380
Sailesh Nepald1e68152013-12-12 19:08:02 -08001381 private static int getWhenToMakeWifiCallsDefaultPreference() {
1382 // TODO(sail): Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001383 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001384 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001385
Shishir Agrawal566b7612013-10-28 14:41:00 -07001386 @Override
1387 public int iccOpenLogicalChannel(String AID) {
1388 enforceSimCommunicationPermission();
1389
1390 if (DBG) log("iccOpenLogicalChannel: " + AID);
1391 Integer channel = (Integer)sendRequest(CMD_OPEN_CHANNEL, AID);
1392 if (DBG) log("iccOpenLogicalChannel: " + channel);
Jake Hambye994d462014-02-03 13:10:13 -08001393 return channel;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001394 }
1395
1396 @Override
1397 public boolean iccCloseLogicalChannel(int channel) {
1398 enforceSimCommunicationPermission();
1399
1400 if (DBG) log("iccCloseLogicalChannel: " + channel);
1401 if (channel < 0) {
1402 return false;
1403 }
Jake Hambye994d462014-02-03 13:10:13 -08001404 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001405 if (DBG) log("iccCloseLogicalChannel: " + success);
1406 return success;
1407 }
1408
1409 @Override
1410 public String iccTransmitApduLogicalChannel(int channel, int cla,
1411 int command, int p1, int p2, int p3, String data) {
1412 enforceSimCommunicationPermission();
1413
1414 if (DBG) {
1415 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1416 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1417 " data=" + data);
1418 }
1419
1420 if (channel < 0) {
1421 return "";
1422 }
1423
1424 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU,
1425 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1426 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1427
1428 // If the payload is null, there was an error. Indicate that by returning
1429 // an empty string.
1430 if (response.payload == null) {
1431 return "";
1432 }
1433
1434 // Append the returned status code to the end of the response payload.
1435 String s = Integer.toHexString(
1436 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1437 s = IccUtils.bytesToHexString(response.payload) + s;
1438 return s;
1439 }
Jake Hambye994d462014-02-03 13:10:13 -08001440
Evan Charltonc66da362014-05-16 14:06:40 -07001441 @Override
1442 public String sendEnvelopeWithStatus(String content) {
1443 enforceSimCommunicationPermission();
1444
1445 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1446 if (response.payload == null) {
1447 return "";
1448 }
1449
1450 // Append the returned status code to the end of the response payload.
1451 String s = Integer.toHexString(
1452 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1453 s = IccUtils.bytesToHexString(response.payload) + s;
1454 return s;
1455 }
1456
Jake Hambye994d462014-02-03 13:10:13 -08001457 /**
1458 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1459 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1460 *
1461 * @param itemID the ID of the item to read
1462 * @return the NV item as a String, or null on error.
1463 */
1464 @Override
1465 public String nvReadItem(int itemID) {
1466 enforceModifyPermission();
1467 if (DBG) log("nvReadItem: item " + itemID);
1468 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1469 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1470 return value;
1471 }
1472
1473 /**
1474 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1475 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1476 *
1477 * @param itemID the ID of the item to read
1478 * @param itemValue the value to write, as a String
1479 * @return true on success; false on any failure
1480 */
1481 @Override
1482 public boolean nvWriteItem(int itemID, String itemValue) {
1483 enforceModifyPermission();
1484 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1485 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1486 new Pair<Integer, String>(itemID, itemValue));
1487 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1488 return success;
1489 }
1490
1491 /**
1492 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1493 * Used for device configuration by some CDMA operators.
1494 *
1495 * @param preferredRoamingList byte array containing the new PRL
1496 * @return true on success; false on any failure
1497 */
1498 @Override
1499 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
1500 enforceModifyPermission();
1501 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1502 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1503 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1504 return success;
1505 }
1506
1507 /**
1508 * Perform the specified type of NV config reset.
1509 * Used for device configuration by some CDMA operators.
1510 *
1511 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1512 * @return true on success; false on any failure
1513 */
1514 @Override
1515 public boolean nvResetConfig(int resetType) {
1516 enforceModifyPermission();
1517 if (DBG) log("nvResetConfig: type " + resetType);
1518 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1519 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1520 return success;
1521 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001522
1523 /**
Wink Saville36469e72014-06-11 15:17:00 -07001524 * {@hide}
1525 * Returns Default sim, 0 in the case of single standby.
1526 */
1527 public int getDefaultSim() {
1528 //TODO Need to get it from Telephony Devcontroller
1529 return 0;
1530 }
1531
1532 public String[] getPcscfAddress() {
1533 enforceReadPermission();
1534 return mPhone.getPcscfAddress();
1535 }
1536
1537 public void setImsRegistrationState(boolean registered) {
1538 enforceModifyPermission();
1539 mPhone.setImsRegistrationState(registered);
1540 }
1541
1542 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001543 * Get the preferred network type.
1544 * Used for device configuration by some CDMA operators.
1545 *
1546 * @return the preferred network type, defined in RILConstants.java.
1547 */
1548 @Override
1549 public int getPreferredNetworkType() {
1550 enforceModifyPermission();
1551 if (DBG) log("getPreferredNetworkType");
1552 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1553 int networkType = (result != null ? result[0] : -1);
1554 if (DBG) log("getPreferredNetworkType: " + networkType);
1555 return networkType;
1556 }
1557
1558 /**
1559 * Set the preferred network type.
1560 * Used for device configuration by some CDMA operators.
1561 *
1562 * @param networkType the preferred network type, defined in RILConstants.java.
1563 * @return true on success; false on any failure.
1564 */
1565 @Override
1566 public boolean setPreferredNetworkType(int networkType) {
1567 enforceModifyPermission();
1568 if (DBG) log("setPreferredNetworkType: type " + networkType);
1569 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1570 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
1571 return success;
1572 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001573
1574 /**
Junda Liu787bc7e2014-06-30 13:38:02 -07001575 * Set the CDMA subscription source.
1576 * Used for device supporting both NV and RUIM for CDMA.
1577 *
1578 * @param subscriptionType the subscription type, 0 for RUIM, 1 for NV.
1579 * @return true on success; false on any failure.
1580 */
1581 @Override
1582 public boolean setCdmaSubscription(int subscriptionType) {
1583 enforceModifyPermission();
1584 if (DBG) log("setCdmaSubscription: type " + subscriptionType);
1585 if (subscriptionType != mPhone.CDMA_SUBSCRIPTION_RUIM_SIM &&
1586 subscriptionType != mPhone.CDMA_SUBSCRIPTION_NV) {
1587 loge("setCdmaSubscription: unsupported subscriptionType.");
1588 return false;
1589 }
1590 Boolean success = (Boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION, subscriptionType);
1591 if (DBG) log("setCdmaSubscription: " + (success ? "ok" : "fail"));
1592 if (success) {
1593 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
1594 Settings.Global.CDMA_SUBSCRIPTION_MODE, subscriptionType);
1595 }
1596 return success;
1597 }
1598
1599 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001600 * Set mobile data enabled
1601 * Used by the user through settings etc to turn on/off mobile data
1602 *
1603 * @param enable {@code true} turn turn data on, else {@code false}
1604 */
1605 @Override
1606 public void setDataEnabled(boolean enable) {
1607 enforceModifyPermission();
1608 mPhone.setDataEnabled(enable);
1609 }
1610
1611 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001612 * Get whether mobile data is enabled.
1613 *
1614 * Note that this used to be available from ConnectivityService, gated by
1615 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1616 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001617 *
1618 * @return {@code true} if data is enabled else {@code false}
1619 */
1620 @Override
1621 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001622 try {
1623 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1624 null);
1625 } catch (Exception e) {
1626 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1627 null);
1628 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001629 return mPhone.getDataEnabled();
1630 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631}