blob: 5fe8cd4e73aba4e5f5018f4d2ddeff793bc9685b [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;
24import android.net.ConnectivityManager;
25import android.net.Uri;
26import android.os.AsyncResult;
27import android.os.Binder;
28import android.os.Bundle;
29import android.os.Handler;
30import android.os.Looper;
31import android.os.Message;
32import android.os.Process;
33import android.os.ServiceManager;
34import android.os.UserHandle;
Ihab Awadf2177b72013-11-25 13:33:23 -080035import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.telephony.CellInfo;
Jake Hambye994d462014-02-03 13:10:13 -080037import android.telephony.NeighboringCellInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.telephony.ServiceState;
Ihab Awadf2177b72013-11-25 13:33:23 -080039import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.text.TextUtils;
41import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080042import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043
Shishir Agrawal566b7612013-10-28 14:41:00 -070044import com.android.internal.telephony.CallManager;
45import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import com.android.internal.telephony.ITelephony;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070048import com.android.internal.telephony.IThirdPartyCallProvider;
Jake Hambye994d462014-02-03 13:10:13 -080049import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import com.android.internal.telephony.Phone;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.PhoneConstants;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070052import com.android.internal.telephony.thirdpartyphone.ThirdPartyPhone;
Shishir Agrawal566b7612013-10-28 14:41:00 -070053import com.android.internal.telephony.uicc.IccIoResult;
54import com.android.internal.telephony.uicc.IccUtils;
55import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080056import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import java.util.ArrayList;
Jake Hambye994d462014-02-03 13:10:13 -080059import java.util.List;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060
61/**
62 * Implementation of the ITelephony interface.
63 */
64public class PhoneInterfaceManager extends ITelephony.Stub {
65 private static final String LOG_TAG = "PhoneInterfaceManager";
66 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
67 private static final boolean DBG_LOC = false;
68
69 // Message codes used with mMainThreadHandler
70 private static final int CMD_HANDLE_PIN_MMI = 1;
71 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
72 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
73 private static final int CMD_ANSWER_RINGING_CALL = 4;
74 private static final int CMD_END_CALL = 5; // not used yet
75 private static final int CMD_SILENCE_RINGER = 6;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076 private static final int CMD_TRANSMIT_APDU = 7;
77 private static final int EVENT_TRANSMIT_APDU_DONE = 8;
78 private static final int CMD_OPEN_CHANNEL = 9;
79 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
80 private static final int CMD_CLOSE_CHANNEL = 11;
81 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Evan Charlton62e3eac2014-02-05 09:17:29 -080082 // TODO: Remove this.
83 private static final int CMD_NEW_INCOMING_THIRD_PARTY_CALL = 100;
Jake Hambye994d462014-02-03 13:10:13 -080084 private static final int CMD_NV_READ_ITEM = 13;
85 private static final int EVENT_NV_READ_ITEM_DONE = 14;
86 private static final int CMD_NV_WRITE_ITEM = 15;
87 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
88 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
89 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
90 private static final int CMD_NV_RESET_CONFIG = 19;
91 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
92 private static final int CMD_SET_RADIO_MODE = 21;
93 private static final int EVENT_SET_RADIO_MODE_DONE = 22;
Derek Tan4d5e5c12014-02-04 11:54:58 -080094 private static final int CMD_SEND_ENVELOPE = 23;
95 private static final int EVENT_SEND_ENVELOPE_DONE = 24;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096
97 /** The singleton instance. */
98 private static PhoneInterfaceManager sInstance;
99
100 PhoneGlobals mApp;
101 Phone mPhone;
102 CallManager mCM;
103 AppOpsManager mAppOps;
104 MainThreadHandler mMainThreadHandler;
Santos Cordon406c0342013-08-28 00:07:47 -0700105 CallHandlerServiceProxy mCallHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700108 * A request object to use for transmitting data to an ICC.
109 */
110 private static final class IccAPDUArgument {
111 public int channel, cla, command, p1, p2, p3;
112 public String data;
113
114 public IccAPDUArgument(int channel, int cla, int command,
115 int p1, int p2, int p3, String data) {
116 this.channel = channel;
117 this.cla = cla;
118 this.command = command;
119 this.p1 = p1;
120 this.p2 = p2;
121 this.p3 = p3;
122 this.data = data;
123 }
124 }
125
126 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
128 * request after sending. The main thread will notify the request when it is complete.
129 */
130 private static final class MainThreadRequest {
131 /** The argument to use for the request */
132 public Object argument;
133 /** The result of the request that is run on the main thread */
134 public Object result;
135
136 public MainThreadRequest(Object argument) {
137 this.argument = argument;
138 }
139 }
140
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800141 private static final class IncomingThirdPartyCallArgs {
142 public final ComponentName component;
143 public final String callId;
144 public final String callerDisplayName;
145
146 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
147 String callerDisplayName) {
148 this.component = component;
149 this.callId = callId;
150 this.callerDisplayName = callerDisplayName;
151 }
152 }
153
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 /**
155 * A handler that processes messages on the main thread in the phone process. Since many
156 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
157 * inbound binder threads to the main thread in the phone process. The Binder thread
158 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
159 * on, which will be notified when the operation completes and will contain the result of the
160 * request.
161 *
162 * <p>If a MainThreadRequest object is provided in the msg.obj field,
163 * note that request.result must be set to something non-null for the calling thread to
164 * unblock.
165 */
166 private final class MainThreadHandler extends Handler {
167 @Override
168 public void handleMessage(Message msg) {
169 MainThreadRequest request;
170 Message onCompleted;
171 AsyncResult ar;
172
173 switch (msg.what) {
174 case CMD_HANDLE_PIN_MMI:
175 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800176 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177 // Wake up the requesting thread
178 synchronized (request) {
179 request.notifyAll();
180 }
181 break;
182
183 case CMD_HANDLE_NEIGHBORING_CELL:
184 request = (MainThreadRequest) msg.obj;
185 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
186 request);
187 mPhone.getNeighboringCids(onCompleted);
188 break;
189
190 case EVENT_NEIGHBORING_CELL_DONE:
191 ar = (AsyncResult) msg.obj;
192 request = (MainThreadRequest) ar.userObj;
193 if (ar.exception == null && ar.result != null) {
194 request.result = ar.result;
195 } else {
196 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800197 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 }
199 // Wake up the requesting thread
200 synchronized (request) {
201 request.notifyAll();
202 }
203 break;
204
205 case CMD_ANSWER_RINGING_CALL:
206 answerRingingCallInternal();
207 break;
208
209 case CMD_SILENCE_RINGER:
210 silenceRingerInternal();
211 break;
212
213 case CMD_END_CALL:
214 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800215 boolean hungUp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 int phoneType = mPhone.getPhoneType();
217 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
218 // CDMA: If the user presses the Power button we treat it as
219 // ending the complete call session
220 hungUp = PhoneUtils.hangupRingingAndActive(mPhone);
221 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
222 // GSM: End the call as per the Phone state
223 hungUp = PhoneUtils.hangup(mCM);
224 } else {
225 throw new IllegalStateException("Unexpected phone type: " + phoneType);
226 }
227 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
228 request.result = hungUp;
229 // Wake up the requesting thread
230 synchronized (request) {
231 request.notifyAll();
232 }
233 break;
234
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700235 case CMD_NEW_INCOMING_THIRD_PARTY_CALL: {
236 request = (MainThreadRequest) msg.obj;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800237 IncomingThirdPartyCallArgs args = (IncomingThirdPartyCallArgs) request.argument;
Sailesh Nepalbfb68322013-11-07 14:07:41 -0800238 ThirdPartyPhone thirdPartyPhone = (ThirdPartyPhone)
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800239 PhoneUtils.getThirdPartyPhoneFromComponent(mCM, args.component);
240 thirdPartyPhone.takeIncomingCall(args.callId, args.callerDisplayName);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700241 break;
242 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243
Shishir Agrawal566b7612013-10-28 14:41:00 -0700244 case CMD_TRANSMIT_APDU:
245 request = (MainThreadRequest) msg.obj;
246 IccAPDUArgument argument = (IccAPDUArgument) request.argument;
247 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_DONE, request);
248 UiccController.getInstance().getUiccCard().iccTransmitApduLogicalChannel(
249 argument.channel, argument.cla, argument.command,
250 argument.p1, argument.p2, argument.p3, argument.data,
251 onCompleted);
252 break;
253
254 case EVENT_TRANSMIT_APDU_DONE:
255 ar = (AsyncResult) msg.obj;
256 request = (MainThreadRequest) ar.userObj;
257 if (ar.exception == null && ar.result != null) {
258 request.result = ar.result;
259 } else {
260 request.result = new IccIoResult(0x6F, 0, (byte[])null);
261 if (ar.result == null) {
262 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800263 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700264 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800265 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700266 } else {
267 loge("iccTransmitApduLogicalChannel: Unknown exception");
268 }
269 }
270 synchronized (request) {
271 request.notifyAll();
272 }
273 break;
274
Derek Tan4d5e5c12014-02-04 11:54:58 -0800275 case CMD_SEND_ENVELOPE:
276 request = (MainThreadRequest) msg.obj;
277 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
278 UiccController.getInstance().getUiccCard().sendEnvelope(
279 (String)request.argument, onCompleted);
280 break;
281
282 case EVENT_SEND_ENVELOPE_DONE:
283 ar = (AsyncResult) msg.obj;
284 request = (MainThreadRequest) ar.userObj;
285 if (ar.exception == null) {
286 request.result = (ar.result == null) ? "" : ar.result;
287 } else {
288 loge("sendEnvelope: Unknown exception " + ar.exception);
289 }
290 synchronized (request) {
291 request.notifyAll();
292 }
293 break;
294
Shishir Agrawal566b7612013-10-28 14:41:00 -0700295 case CMD_OPEN_CHANNEL:
296 request = (MainThreadRequest) msg.obj;
297 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
298 UiccController.getInstance().getUiccCard().iccOpenLogicalChannel(
299 (String)request.argument, onCompleted);
300 break;
301
302 case EVENT_OPEN_CHANNEL_DONE:
303 ar = (AsyncResult) msg.obj;
304 request = (MainThreadRequest) ar.userObj;
305 if (ar.exception == null && ar.result != null) {
Jake Hambye994d462014-02-03 13:10:13 -0800306 request.result = ((int[]) ar.result)[0];
Shishir Agrawal566b7612013-10-28 14:41:00 -0700307 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800308 request.result = -1;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700309 if (ar.result == null) {
310 loge("iccOpenLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800311 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700312 loge("iccOpenLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800313 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700314 } else {
315 loge("iccOpenLogicalChannel: Unknown exception");
316 }
317 }
318 synchronized (request) {
319 request.notifyAll();
320 }
321 break;
322
323 case CMD_CLOSE_CHANNEL:
324 request = (MainThreadRequest) msg.obj;
325 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE,
326 request);
327 UiccController.getInstance().getUiccCard().iccCloseLogicalChannel(
Jake Hambye994d462014-02-03 13:10:13 -0800328 (Integer) request.argument,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700329 onCompleted);
330 break;
331
332 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800333 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
334 break;
335
336 case CMD_NV_READ_ITEM:
337 request = (MainThreadRequest) msg.obj;
338 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
339 mPhone.nvReadItem((Integer) request.argument, onCompleted);
340 break;
341
342 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700343 ar = (AsyncResult) msg.obj;
344 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800345 if (ar.exception == null && ar.result != null) {
346 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700347 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800348 request.result = "";
349 if (ar.result == null) {
350 loge("nvReadItem: Empty response");
351 } else if (ar.exception instanceof CommandException) {
352 loge("nvReadItem: CommandException: " +
353 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700354 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800355 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700356 }
357 }
358 synchronized (request) {
359 request.notifyAll();
360 }
361 break;
362
Jake Hambye994d462014-02-03 13:10:13 -0800363 case CMD_NV_WRITE_ITEM:
364 request = (MainThreadRequest) msg.obj;
365 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
366 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
367 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
368 break;
369
370 case EVENT_NV_WRITE_ITEM_DONE:
371 handleNullReturnEvent(msg, "nvWriteItem");
372 break;
373
374 case CMD_NV_WRITE_CDMA_PRL:
375 request = (MainThreadRequest) msg.obj;
376 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
377 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
378 break;
379
380 case EVENT_NV_WRITE_CDMA_PRL_DONE:
381 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
382 break;
383
384 case CMD_NV_RESET_CONFIG:
385 request = (MainThreadRequest) msg.obj;
386 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
387 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
388 break;
389
390 case EVENT_NV_RESET_CONFIG_DONE:
391 handleNullReturnEvent(msg, "nvResetConfig");
392 break;
393
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700394 default:
395 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
396 break;
397 }
398 }
Jake Hambye994d462014-02-03 13:10:13 -0800399
400 private void handleNullReturnEvent(Message msg, String command) {
401 AsyncResult ar = (AsyncResult) msg.obj;
402 MainThreadRequest request = (MainThreadRequest) ar.userObj;
403 if (ar.exception == null) {
404 request.result = true;
405 } else {
406 request.result = false;
407 if (ar.exception instanceof CommandException) {
408 loge(command + ": CommandException: " + ar.exception);
409 } else {
410 loge(command + ": Unknown exception");
411 }
412 }
413 synchronized (request) {
414 request.notifyAll();
415 }
416 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700417 }
418
419 /**
420 * Posts the specified command to be executed on the main thread,
421 * waits for the request to complete, and returns the result.
422 * @see #sendRequestAsync
423 */
424 private Object sendRequest(int command, Object argument) {
425 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
426 throw new RuntimeException("This method will deadlock if called from the main thread.");
427 }
428
429 MainThreadRequest request = new MainThreadRequest(argument);
430 Message msg = mMainThreadHandler.obtainMessage(command, request);
431 msg.sendToTarget();
432
433 // Wait for the request to complete
434 synchronized (request) {
435 while (request.result == null) {
436 try {
437 request.wait();
438 } catch (InterruptedException e) {
439 // Do nothing, go back and wait until the request is complete
440 }
441 }
442 }
443 return request.result;
444 }
445
446 /**
447 * Asynchronous ("fire and forget") version of sendRequest():
448 * Posts the specified command to be executed on the main thread, and
449 * returns immediately.
450 * @see #sendRequest
451 */
452 private void sendRequestAsync(int command) {
453 mMainThreadHandler.sendEmptyMessage(command);
454 }
455
456 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700457 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
458 * @see {@link #sendRequest(int,Object)}
459 */
460 private void sendRequestAsync(int command, Object argument) {
461 MainThreadRequest request = new MainThreadRequest(argument);
462 Message msg = mMainThreadHandler.obtainMessage(command, request);
463 msg.sendToTarget();
464 }
465
466 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700467 * Initialize the singleton PhoneInterfaceManager instance.
468 * This is only done once, at startup, from PhoneApp.onCreate().
469 */
Santos Cordon406c0342013-08-28 00:07:47 -0700470 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone,
471 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700472 synchronized (PhoneInterfaceManager.class) {
473 if (sInstance == null) {
Santos Cordon406c0342013-08-28 00:07:47 -0700474 sInstance = new PhoneInterfaceManager(app, phone, callHandlerService);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 } else {
476 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
477 }
478 return sInstance;
479 }
480 }
481
482 /** Private constructor; @see init() */
Santos Cordon406c0342013-08-28 00:07:47 -0700483 private PhoneInterfaceManager(PhoneGlobals app, Phone phone,
484 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485 mApp = app;
486 mPhone = phone;
487 mCM = PhoneGlobals.getInstance().mCM;
488 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
489 mMainThreadHandler = new MainThreadHandler();
Santos Cordon406c0342013-08-28 00:07:47 -0700490 mCallHandlerService = callHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 publish();
492 }
493
494 private void publish() {
495 if (DBG) log("publish: " + this);
496
497 ServiceManager.addService("phone", this);
498 }
499
500 //
501 // Implementation of the ITelephony interface.
502 //
503
504 public void dial(String number) {
505 if (DBG) log("dial: " + number);
506 // No permission check needed here: This is just a wrapper around the
507 // ACTION_DIAL intent, which is available to any app since it puts up
508 // the UI before it does anything.
509
510 String url = createTelUrl(number);
511 if (url == null) {
512 return;
513 }
514
515 // PENDING: should we just silently fail if phone is offhook or ringing?
516 PhoneConstants.State state = mCM.getState();
517 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
518 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
519 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
520 mApp.startActivity(intent);
521 }
522 }
523
524 public void call(String callingPackage, String number) {
525 if (DBG) log("call: " + number);
526
527 // This is just a wrapper around the ACTION_CALL intent, but we still
528 // need to do a permission check since we're calling startActivity()
529 // from the context of the phone app.
530 enforceCallPermission();
531
532 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
533 != AppOpsManager.MODE_ALLOWED) {
534 return;
535 }
536
537 String url = createTelUrl(number);
538 if (url == null) {
539 return;
540 }
541
542 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
543 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
544 mApp.startActivity(intent);
545 }
546
547 private boolean showCallScreenInternal(boolean specifyInitialDialpadState,
Makoto Onukibcf20992013-09-12 17:59:30 -0700548 boolean showDialpad) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 if (!PhoneGlobals.sVoiceCapable) {
550 // Never allow the InCallScreen to appear on data-only devices.
551 return false;
552 }
553 if (isIdle()) {
554 return false;
555 }
556 // If the phone isn't idle then go to the in-call screen
557 long callingId = Binder.clearCallingIdentity();
Santos Cordon406c0342013-08-28 00:07:47 -0700558
Makoto Onukibcf20992013-09-12 17:59:30 -0700559 mCallHandlerService.bringToForeground(showDialpad);
Santos Cordon406c0342013-08-28 00:07:47 -0700560
561 Binder.restoreCallingIdentity(callingId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 return true;
563 }
564
565 // Show the in-call screen without specifying the initial dialpad state.
566 public boolean showCallScreen() {
567 return showCallScreenInternal(false, false);
568 }
569
570 // The variation of showCallScreen() that specifies the initial dialpad state.
571 // (Ideally this would be called showCallScreen() too, just with a different
572 // signature, but AIDL doesn't allow that.)
573 public boolean showCallScreenWithDialpad(boolean showDialpad) {
574 return showCallScreenInternal(true, showDialpad);
575 }
576
577 /**
578 * End a call based on call state
579 * @return true is a call was ended
580 */
581 public boolean endCall() {
582 enforceCallPermission();
583 return (Boolean) sendRequest(CMD_END_CALL, null);
584 }
585
586 public void answerRingingCall() {
587 if (DBG) log("answerRingingCall...");
588 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
589 // but that can probably wait till the big TelephonyManager API overhaul.
590 // For now, protect this call with the MODIFY_PHONE_STATE permission.
591 enforceModifyPermission();
592 sendRequestAsync(CMD_ANSWER_RINGING_CALL);
593 }
594
595 /**
596 * Make the actual telephony calls to implement answerRingingCall().
597 * This should only be called from the main thread of the Phone app.
598 * @see #answerRingingCall
599 *
600 * TODO: it would be nice to return true if we answered the call, or
601 * false if there wasn't actually a ringing incoming call, or some
602 * other error occurred. (In other words, pass back the return value
603 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
604 * But that would require calling this method via sendRequest() rather
605 * than sendRequestAsync(), and right now we don't actually *need* that
606 * return value, so let's just return void for now.
607 */
608 private void answerRingingCallInternal() {
609 final boolean hasRingingCall = !mPhone.getRingingCall().isIdle();
610 if (hasRingingCall) {
611 final boolean hasActiveCall = !mPhone.getForegroundCall().isIdle();
612 final boolean hasHoldingCall = !mPhone.getBackgroundCall().isIdle();
613 if (hasActiveCall && hasHoldingCall) {
614 // Both lines are in use!
615 // TODO: provide a flag to let the caller specify what
616 // policy to use if both lines are in use. (The current
617 // behavior is hardwired to "answer incoming, end ongoing",
618 // which is how the CALL button is specced to behave.)
619 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
620 return;
621 } else {
622 // answerCall() will automatically hold the current active
623 // call, if there is one.
624 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
625 return;
626 }
627 } else {
628 // No call was ringing.
629 return;
630 }
631 }
632
633 public void silenceRinger() {
634 if (DBG) log("silenceRinger...");
635 // TODO: find a more appropriate permission to check here.
636 // (That can probably wait till the big TelephonyManager API overhaul.
637 // For now, protect this call with the MODIFY_PHONE_STATE permission.)
638 enforceModifyPermission();
639 sendRequestAsync(CMD_SILENCE_RINGER);
640 }
641
642 /**
643 * Internal implemenation of silenceRinger().
644 * This should only be called from the main thread of the Phone app.
645 * @see #silenceRinger
646 */
647 private void silenceRingerInternal() {
648 if ((mCM.getState() == PhoneConstants.State.RINGING)
649 && mApp.notifier.isRinging()) {
650 // Ringer is actually playing, so silence it.
651 if (DBG) log("silenceRingerInternal: silencing...");
652 mApp.notifier.silenceRinger();
653 }
654 }
655
656 public boolean isOffhook() {
657 return (mCM.getState() == PhoneConstants.State.OFFHOOK);
658 }
659
660 public boolean isRinging() {
661 return (mCM.getState() == PhoneConstants.State.RINGING);
662 }
663
664 public boolean isIdle() {
665 return (mCM.getState() == PhoneConstants.State.IDLE);
666 }
667
668 public boolean isSimPinEnabled() {
669 enforceReadPermission();
670 return (PhoneGlobals.getInstance().isSimPinEnabled());
671 }
672
673 public boolean supplyPin(String pin) {
Wink Saville9de0f752013-10-22 19:04:03 -0700674 int [] resultArray = supplyPinReportResult(pin);
675 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
676 }
677
678 public boolean supplyPuk(String puk, String pin) {
679 int [] resultArray = supplyPukReportResult(puk, pin);
680 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
681 }
682
683 /** {@hide} */
684 public int[] supplyPinReportResult(String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 enforceModifyPermission();
686 final UnlockSim checkSimPin = new UnlockSim(mPhone.getIccCard());
687 checkSimPin.start();
688 return checkSimPin.unlockSim(null, pin);
689 }
690
Wink Saville9de0f752013-10-22 19:04:03 -0700691 /** {@hide} */
692 public int[] supplyPukReportResult(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700693 enforceModifyPermission();
694 final UnlockSim checkSimPuk = new UnlockSim(mPhone.getIccCard());
695 checkSimPuk.start();
696 return checkSimPuk.unlockSim(puk, pin);
697 }
698
699 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700700 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700701 * a synchronous one.
702 */
703 private static class UnlockSim extends Thread {
704
705 private final IccCard mSimCard;
706
707 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700708 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
709 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710
711 // For replies from SimCard interface
712 private Handler mHandler;
713
714 // For async handler to identify request type
715 private static final int SUPPLY_PIN_COMPLETE = 100;
716
717 public UnlockSim(IccCard simCard) {
718 mSimCard = simCard;
719 }
720
721 @Override
722 public void run() {
723 Looper.prepare();
724 synchronized (UnlockSim.this) {
725 mHandler = new Handler() {
726 @Override
727 public void handleMessage(Message msg) {
728 AsyncResult ar = (AsyncResult) msg.obj;
729 switch (msg.what) {
730 case SUPPLY_PIN_COMPLETE:
731 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
732 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700733 mRetryCount = msg.arg1;
734 if (ar.exception != null) {
735 if (ar.exception instanceof CommandException &&
736 ((CommandException)(ar.exception)).getCommandError()
737 == CommandException.Error.PASSWORD_INCORRECT) {
738 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
739 } else {
740 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
741 }
742 } else {
743 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
744 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 mDone = true;
746 UnlockSim.this.notifyAll();
747 }
748 break;
749 }
750 }
751 };
752 UnlockSim.this.notifyAll();
753 }
754 Looper.loop();
755 }
756
757 /*
758 * Use PIN or PUK to unlock SIM card
759 *
760 * If PUK is null, unlock SIM card with PIN
761 *
762 * If PUK is not null, unlock SIM card with PUK and set PIN code
763 */
Wink Saville9de0f752013-10-22 19:04:03 -0700764 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700765
766 while (mHandler == null) {
767 try {
768 wait();
769 } catch (InterruptedException e) {
770 Thread.currentThread().interrupt();
771 }
772 }
773 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
774
775 if (puk == null) {
776 mSimCard.supplyPin(pin, callback);
777 } else {
778 mSimCard.supplyPuk(puk, pin, callback);
779 }
780
781 while (!mDone) {
782 try {
783 Log.d(LOG_TAG, "wait for done");
784 wait();
785 } catch (InterruptedException e) {
786 // Restore the interrupted status
787 Thread.currentThread().interrupt();
788 }
789 }
790 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -0700791 int[] resultArray = new int[2];
792 resultArray[0] = mResult;
793 resultArray[1] = mRetryCount;
794 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 }
796 }
797
798 public void updateServiceLocation() {
799 // No permission check needed here: this call is harmless, and it's
800 // needed for the ServiceState.requestStateUpdate() call (which is
801 // already intentionally exposed to 3rd parties.)
802 mPhone.updateServiceLocation();
803 }
804
805 public boolean isRadioOn() {
806 return mPhone.getServiceState().getVoiceRegState() != ServiceState.STATE_POWER_OFF;
807 }
808
809 public void toggleRadioOnOff() {
810 enforceModifyPermission();
811 mPhone.setRadioPower(!isRadioOn());
812 }
813 public boolean setRadio(boolean turnOn) {
814 enforceModifyPermission();
815 if ((mPhone.getServiceState().getVoiceRegState() != ServiceState.STATE_POWER_OFF) != turnOn) {
816 toggleRadioOnOff();
817 }
818 return true;
819 }
820 public boolean setRadioPower(boolean turnOn) {
821 enforceModifyPermission();
822 mPhone.setRadioPower(turnOn);
823 return true;
824 }
825
826 public boolean enableDataConnectivity() {
827 enforceModifyPermission();
828 ConnectivityManager cm =
829 (ConnectivityManager)mApp.getSystemService(Context.CONNECTIVITY_SERVICE);
830 cm.setMobileDataEnabled(true);
831 return true;
832 }
833
834 public int enableApnType(String type) {
835 enforceModifyPermission();
836 return mPhone.enableApnType(type);
837 }
838
839 public int disableApnType(String type) {
840 enforceModifyPermission();
841 return mPhone.disableApnType(type);
842 }
843
844 public boolean disableDataConnectivity() {
845 enforceModifyPermission();
846 ConnectivityManager cm =
847 (ConnectivityManager)mApp.getSystemService(Context.CONNECTIVITY_SERVICE);
848 cm.setMobileDataEnabled(false);
849 return true;
850 }
851
852 public boolean isDataConnectivityPossible() {
853 return mPhone.isDataConnectivityPossible();
854 }
855
856 public boolean handlePinMmi(String dialString) {
857 enforceModifyPermission();
858 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString);
859 }
860
861 public void cancelMissedCallsNotification() {
862 enforceModifyPermission();
863 mApp.notificationMgr.cancelMissedCallNotification();
864 }
865
866 public int getCallState() {
867 return DefaultPhoneNotifier.convertCallState(mCM.getState());
868 }
869
870 public int getDataState() {
871 return DefaultPhoneNotifier.convertDataState(mPhone.getDataConnectionState());
872 }
873
874 public int getDataActivity() {
875 return DefaultPhoneNotifier.convertDataActivityState(mPhone.getDataActivityState());
876 }
877
878 @Override
879 public Bundle getCellLocation() {
880 try {
881 mApp.enforceCallingOrSelfPermission(
882 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
883 } catch (SecurityException e) {
884 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
885 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
886 // is the weaker precondition
887 mApp.enforceCallingOrSelfPermission(
888 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
889 }
890
Jake Hambye994d462014-02-03 13:10:13 -0800891 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 if (DBG_LOC) log("getCellLocation: is active user");
893 Bundle data = new Bundle();
894 mPhone.getCellLocation().fillInNotifierBundle(data);
895 return data;
896 } else {
897 if (DBG_LOC) log("getCellLocation: suppress non-active user");
898 return null;
899 }
900 }
901
902 @Override
903 public void enableLocationUpdates() {
904 mApp.enforceCallingOrSelfPermission(
905 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
906 mPhone.enableLocationUpdates();
907 }
908
909 @Override
910 public void disableLocationUpdates() {
911 mApp.enforceCallingOrSelfPermission(
912 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
913 mPhone.disableLocationUpdates();
914 }
915
916 @Override
917 @SuppressWarnings("unchecked")
918 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
919 try {
920 mApp.enforceCallingOrSelfPermission(
921 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
922 } catch (SecurityException e) {
923 // If we have ACCESS_FINE_LOCATION permission, skip the check
924 // for ACCESS_COARSE_LOCATION
925 // A failure should throw the SecurityException from
926 // ACCESS_COARSE_LOCATION since this is the weaker precondition
927 mApp.enforceCallingOrSelfPermission(
928 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
929 }
930
931 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
932 callingPackage) != AppOpsManager.MODE_ALLOWED) {
933 return null;
934 }
Jake Hambye994d462014-02-03 13:10:13 -0800935 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
937
938 ArrayList<NeighboringCellInfo> cells = null;
939
940 try {
941 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
942 CMD_HANDLE_NEIGHBORING_CELL, null);
943 } catch (RuntimeException e) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700944 loge("getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 }
946 return cells;
947 } else {
948 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
949 return null;
950 }
951 }
952
953
954 @Override
955 public List<CellInfo> getAllCellInfo() {
956 try {
957 mApp.enforceCallingOrSelfPermission(
958 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
959 } catch (SecurityException e) {
960 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
961 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
962 // is the weaker precondition
963 mApp.enforceCallingOrSelfPermission(
964 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
965 }
966
Jake Hambye994d462014-02-03 13:10:13 -0800967 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 if (DBG_LOC) log("getAllCellInfo: is active user");
969 return mPhone.getAllCellInfo();
970 } else {
971 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
972 return null;
973 }
974 }
975
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700976 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 public void setCellInfoListRate(int rateInMillis) {
978 mPhone.setCellInfoListRate(rateInMillis);
979 }
980
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700981 @Override
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800982 public void newIncomingThirdPartyCall(ComponentName component, String callId,
983 String callerDisplayName) {
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700984 // TODO(sail): Enforce that the component belongs to the calling package.
985 if (DBG) {
986 log("newIncomingThirdPartyCall: component: " + component + " callId: " + callId);
987 }
988 enforceCallPermission();
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800989 sendRequestAsync(CMD_NEW_INCOMING_THIRD_PARTY_CALL, new IncomingThirdPartyCallArgs(
990 component, callId, callerDisplayName));
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700991 }
992
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 //
994 // Internal helper methods.
995 //
996
Jake Hambye994d462014-02-03 13:10:13 -0800997 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 boolean ok;
999
1000 boolean self = Binder.getCallingUid() == Process.myUid();
1001 if (!self) {
1002 // Get the caller's user id then clear the calling identity
1003 // which will be restored in the finally clause.
1004 int callingUser = UserHandle.getCallingUserId();
1005 long ident = Binder.clearCallingIdentity();
1006
1007 try {
1008 // With calling identity cleared the current user is the foreground user.
1009 int foregroundUser = ActivityManager.getCurrentUser();
1010 ok = (foregroundUser == callingUser);
1011 if (DBG_LOC) {
1012 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1013 + " callingUser=" + callingUser + " ok=" + ok);
1014 }
1015 } catch (Exception ex) {
1016 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1017 ok = false;
1018 } finally {
1019 Binder.restoreCallingIdentity(ident);
1020 }
1021 } else {
1022 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1023 ok = true;
1024 }
1025 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1026 return ok;
1027 }
1028
1029 /**
1030 * Make sure the caller has the READ_PHONE_STATE permission.
1031 *
1032 * @throws SecurityException if the caller does not have the required permission
1033 */
1034 private void enforceReadPermission() {
1035 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1036 }
1037
1038 /**
1039 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1040 *
1041 * @throws SecurityException if the caller does not have the required permission
1042 */
1043 private void enforceModifyPermission() {
1044 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1045 }
1046
1047 /**
1048 * Make sure the caller has the CALL_PHONE permission.
1049 *
1050 * @throws SecurityException if the caller does not have the required permission
1051 */
1052 private void enforceCallPermission() {
1053 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1054 }
1055
Shishir Agrawal566b7612013-10-28 14:41:00 -07001056 /**
1057 * Make sure the caller has SIM_COMMUNICATION permission.
1058 *
1059 * @throws SecurityException if the caller does not have the required permission.
1060 */
1061 private void enforceSimCommunicationPermission() {
1062 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.SIM_COMMUNICATION, null);
1063 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064
1065 private String createTelUrl(String number) {
1066 if (TextUtils.isEmpty(number)) {
1067 return null;
1068 }
1069
Jake Hambye994d462014-02-03 13:10:13 -08001070 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 }
1072
Ihab Awadf9e92732013-12-05 18:02:52 -08001073 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001074 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1075 }
1076
Ihab Awadf9e92732013-12-05 18:02:52 -08001077 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1079 }
1080
1081 public int getActivePhoneType() {
1082 return mPhone.getPhoneType();
1083 }
1084
1085 /**
1086 * Returns the CDMA ERI icon index to display
1087 */
1088 public int getCdmaEriIconIndex() {
1089 return mPhone.getCdmaEriIconIndex();
1090 }
1091
1092 /**
1093 * Returns the CDMA ERI icon mode,
1094 * 0 - ON
1095 * 1 - FLASHING
1096 */
1097 public int getCdmaEriIconMode() {
1098 return mPhone.getCdmaEriIconMode();
1099 }
1100
1101 /**
1102 * Returns the CDMA ERI text,
1103 */
1104 public String getCdmaEriText() {
1105 return mPhone.getCdmaEriText();
1106 }
1107
1108 /**
1109 * Returns true if CDMA provisioning needs to run.
1110 */
1111 public boolean needsOtaServiceProvisioning() {
1112 return mPhone.needsOtaServiceProvisioning();
1113 }
1114
1115 /**
1116 * Returns the unread count of voicemails
1117 */
1118 public int getVoiceMessageCount() {
1119 return mPhone.getVoiceMessageCount();
1120 }
1121
1122 /**
1123 * Returns the data network type
1124 *
1125 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1126 */
1127 @Override
1128 public int getNetworkType() {
1129 return mPhone.getServiceState().getDataNetworkType();
1130 }
1131
1132 /**
1133 * Returns the data network type
1134 */
1135 @Override
1136 public int getDataNetworkType() {
1137 return mPhone.getServiceState().getDataNetworkType();
1138 }
1139
1140 /**
1141 * Returns the data network type
1142 */
1143 @Override
1144 public int getVoiceNetworkType() {
1145 return mPhone.getServiceState().getVoiceNetworkType();
1146 }
1147
1148 /**
1149 * @return true if a ICC card is present
1150 */
1151 public boolean hasIccCard() {
1152 return mPhone.getIccCard().hasIccCard();
1153 }
1154
1155 /**
1156 * Return if the current radio is LTE on CDMA. This
1157 * is a tri-state return value as for a period of time
1158 * the mode may be unknown.
1159 *
1160 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001161 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162 */
1163 public int getLteOnCdmaMode() {
1164 return mPhone.getLteOnCdmaMode();
1165 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001166
1167 /**
1168 * @see android.telephony.TelephonyManager.WifiCallingChoices
1169 */
1170 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001171 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1172 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001173 }
1174
1175 /**
1176 * @see android.telephony.TelephonyManager.WifiCallingChoices
1177 */
1178 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001179 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1180 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1181 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001182 }
1183
Sailesh Nepald1e68152013-12-12 19:08:02 -08001184 private static int getWhenToMakeWifiCallsDefaultPreference() {
1185 // TODO(sail): Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001186 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001187 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001188
Shishir Agrawal566b7612013-10-28 14:41:00 -07001189 @Override
1190 public int iccOpenLogicalChannel(String AID) {
1191 enforceSimCommunicationPermission();
1192
1193 if (DBG) log("iccOpenLogicalChannel: " + AID);
1194 Integer channel = (Integer)sendRequest(CMD_OPEN_CHANNEL, AID);
1195 if (DBG) log("iccOpenLogicalChannel: " + channel);
Jake Hambye994d462014-02-03 13:10:13 -08001196 return channel;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001197 }
1198
1199 @Override
1200 public boolean iccCloseLogicalChannel(int channel) {
1201 enforceSimCommunicationPermission();
1202
1203 if (DBG) log("iccCloseLogicalChannel: " + channel);
1204 if (channel < 0) {
1205 return false;
1206 }
Jake Hambye994d462014-02-03 13:10:13 -08001207 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001208 if (DBG) log("iccCloseLogicalChannel: " + success);
1209 return success;
1210 }
1211
1212 @Override
1213 public String iccTransmitApduLogicalChannel(int channel, int cla,
1214 int command, int p1, int p2, int p3, String data) {
1215 enforceSimCommunicationPermission();
1216
1217 if (DBG) {
1218 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1219 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1220 " data=" + data);
1221 }
1222
1223 if (channel < 0) {
1224 return "";
1225 }
1226
1227 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU,
1228 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1229 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1230
1231 // If the payload is null, there was an error. Indicate that by returning
1232 // an empty string.
1233 if (response.payload == null) {
1234 return "";
1235 }
1236
1237 // Append the returned status code to the end of the response payload.
1238 String s = Integer.toHexString(
1239 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1240 s = IccUtils.bytesToHexString(response.payload) + s;
1241 return s;
1242 }
Jake Hambye994d462014-02-03 13:10:13 -08001243
Derek Tan4d5e5c12014-02-04 11:54:58 -08001244 @Override
1245 public String sendEnvelope(String content) {
1246 enforceSimCommunicationPermission();
1247
1248 String response = (String)sendRequest(CMD_SEND_ENVELOPE, content);
1249
1250 return response;
1251 }
1252
Jake Hambye994d462014-02-03 13:10:13 -08001253 /**
1254 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1255 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1256 *
1257 * @param itemID the ID of the item to read
1258 * @return the NV item as a String, or null on error.
1259 */
1260 @Override
1261 public String nvReadItem(int itemID) {
1262 enforceModifyPermission();
1263 if (DBG) log("nvReadItem: item " + itemID);
1264 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1265 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1266 return value;
1267 }
1268
1269 /**
1270 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1271 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1272 *
1273 * @param itemID the ID of the item to read
1274 * @param itemValue the value to write, as a String
1275 * @return true on success; false on any failure
1276 */
1277 @Override
1278 public boolean nvWriteItem(int itemID, String itemValue) {
1279 enforceModifyPermission();
1280 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1281 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1282 new Pair<Integer, String>(itemID, itemValue));
1283 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1284 return success;
1285 }
1286
1287 /**
1288 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1289 * Used for device configuration by some CDMA operators.
1290 *
1291 * @param preferredRoamingList byte array containing the new PRL
1292 * @return true on success; false on any failure
1293 */
1294 @Override
1295 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
1296 enforceModifyPermission();
1297 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1298 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1299 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1300 return success;
1301 }
1302
1303 /**
1304 * Perform the specified type of NV config reset.
1305 * Used for device configuration by some CDMA operators.
1306 *
1307 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1308 * @return true on success; false on any failure
1309 */
1310 @Override
1311 public boolean nvResetConfig(int resetType) {
1312 enforceModifyPermission();
1313 if (DBG) log("nvResetConfig: type " + resetType);
1314 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1315 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1316 return success;
1317 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318}