blob: 13b1cf2adb67b22e786f0acb75564f8b315e6b0d [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;
Jake Hamby7c27be32014-03-03 13:25:59 -080092 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
93 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
94 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
95 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -080096 private static final int CMD_SEND_ENVELOPE = 25;
97 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
99 /** The singleton instance. */
100 private static PhoneInterfaceManager sInstance;
101
102 PhoneGlobals mApp;
103 Phone mPhone;
104 CallManager mCM;
105 AppOpsManager mAppOps;
106 MainThreadHandler mMainThreadHandler;
Santos Cordon406c0342013-08-28 00:07:47 -0700107 CallHandlerServiceProxy mCallHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108
109 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700110 * A request object to use for transmitting data to an ICC.
111 */
112 private static final class IccAPDUArgument {
113 public int channel, cla, command, p1, p2, p3;
114 public String data;
115
116 public IccAPDUArgument(int channel, int cla, int command,
117 int p1, int p2, int p3, String data) {
118 this.channel = channel;
119 this.cla = cla;
120 this.command = command;
121 this.p1 = p1;
122 this.p2 = p2;
123 this.p3 = p3;
124 this.data = data;
125 }
126 }
127
128 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
130 * request after sending. The main thread will notify the request when it is complete.
131 */
132 private static final class MainThreadRequest {
133 /** The argument to use for the request */
134 public Object argument;
135 /** The result of the request that is run on the main thread */
136 public Object result;
137
138 public MainThreadRequest(Object argument) {
139 this.argument = argument;
140 }
141 }
142
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800143 private static final class IncomingThirdPartyCallArgs {
144 public final ComponentName component;
145 public final String callId;
146 public final String callerDisplayName;
147
148 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
149 String callerDisplayName) {
150 this.component = component;
151 this.callId = callId;
152 this.callerDisplayName = callerDisplayName;
153 }
154 }
155
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156 /**
157 * A handler that processes messages on the main thread in the phone process. Since many
158 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
159 * inbound binder threads to the main thread in the phone process. The Binder thread
160 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
161 * on, which will be notified when the operation completes and will contain the result of the
162 * request.
163 *
164 * <p>If a MainThreadRequest object is provided in the msg.obj field,
165 * note that request.result must be set to something non-null for the calling thread to
166 * unblock.
167 */
168 private final class MainThreadHandler extends Handler {
169 @Override
170 public void handleMessage(Message msg) {
171 MainThreadRequest request;
172 Message onCompleted;
173 AsyncResult ar;
174
175 switch (msg.what) {
176 case CMD_HANDLE_PIN_MMI:
177 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800178 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179 // Wake up the requesting thread
180 synchronized (request) {
181 request.notifyAll();
182 }
183 break;
184
185 case CMD_HANDLE_NEIGHBORING_CELL:
186 request = (MainThreadRequest) msg.obj;
187 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
188 request);
189 mPhone.getNeighboringCids(onCompleted);
190 break;
191
192 case EVENT_NEIGHBORING_CELL_DONE:
193 ar = (AsyncResult) msg.obj;
194 request = (MainThreadRequest) ar.userObj;
195 if (ar.exception == null && ar.result != null) {
196 request.result = ar.result;
197 } else {
198 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800199 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 }
201 // Wake up the requesting thread
202 synchronized (request) {
203 request.notifyAll();
204 }
205 break;
206
207 case CMD_ANSWER_RINGING_CALL:
208 answerRingingCallInternal();
209 break;
210
211 case CMD_SILENCE_RINGER:
212 silenceRingerInternal();
213 break;
214
215 case CMD_END_CALL:
216 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800217 boolean hungUp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 int phoneType = mPhone.getPhoneType();
219 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
220 // CDMA: If the user presses the Power button we treat it as
221 // ending the complete call session
222 hungUp = PhoneUtils.hangupRingingAndActive(mPhone);
223 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
224 // GSM: End the call as per the Phone state
225 hungUp = PhoneUtils.hangup(mCM);
226 } else {
227 throw new IllegalStateException("Unexpected phone type: " + phoneType);
228 }
229 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
230 request.result = hungUp;
231 // Wake up the requesting thread
232 synchronized (request) {
233 request.notifyAll();
234 }
235 break;
236
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700237 case CMD_NEW_INCOMING_THIRD_PARTY_CALL: {
238 request = (MainThreadRequest) msg.obj;
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800239 IncomingThirdPartyCallArgs args = (IncomingThirdPartyCallArgs) request.argument;
Sailesh Nepalbfb68322013-11-07 14:07:41 -0800240 ThirdPartyPhone thirdPartyPhone = (ThirdPartyPhone)
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800241 PhoneUtils.getThirdPartyPhoneFromComponent(mCM, args.component);
242 thirdPartyPhone.takeIncomingCall(args.callId, args.callerDisplayName);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700243 break;
244 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245
Shishir Agrawal566b7612013-10-28 14:41:00 -0700246 case CMD_TRANSMIT_APDU:
247 request = (MainThreadRequest) msg.obj;
248 IccAPDUArgument argument = (IccAPDUArgument) request.argument;
249 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_DONE, request);
250 UiccController.getInstance().getUiccCard().iccTransmitApduLogicalChannel(
251 argument.channel, argument.cla, argument.command,
252 argument.p1, argument.p2, argument.p3, argument.data,
253 onCompleted);
254 break;
255
256 case EVENT_TRANSMIT_APDU_DONE:
257 ar = (AsyncResult) msg.obj;
258 request = (MainThreadRequest) ar.userObj;
259 if (ar.exception == null && ar.result != null) {
260 request.result = ar.result;
261 } else {
262 request.result = new IccIoResult(0x6F, 0, (byte[])null);
263 if (ar.result == null) {
264 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800265 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700266 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800267 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700268 } else {
269 loge("iccTransmitApduLogicalChannel: Unknown exception");
270 }
271 }
272 synchronized (request) {
273 request.notifyAll();
274 }
275 break;
276
Derek Tan4d5e5c12014-02-04 11:54:58 -0800277 case CMD_SEND_ENVELOPE:
278 request = (MainThreadRequest) msg.obj;
279 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
280 UiccController.getInstance().getUiccCard().sendEnvelope(
281 (String)request.argument, onCompleted);
282 break;
283
284 case EVENT_SEND_ENVELOPE_DONE:
285 ar = (AsyncResult) msg.obj;
286 request = (MainThreadRequest) ar.userObj;
287 if (ar.exception == null) {
288 request.result = (ar.result == null) ? "" : ar.result;
289 } else {
290 loge("sendEnvelope: Unknown exception " + ar.exception);
291 }
292 synchronized (request) {
293 request.notifyAll();
294 }
295 break;
296
Shishir Agrawal566b7612013-10-28 14:41:00 -0700297 case CMD_OPEN_CHANNEL:
298 request = (MainThreadRequest) msg.obj;
299 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
300 UiccController.getInstance().getUiccCard().iccOpenLogicalChannel(
301 (String)request.argument, onCompleted);
302 break;
303
304 case EVENT_OPEN_CHANNEL_DONE:
305 ar = (AsyncResult) msg.obj;
306 request = (MainThreadRequest) ar.userObj;
307 if (ar.exception == null && ar.result != null) {
Jake Hambye994d462014-02-03 13:10:13 -0800308 request.result = ((int[]) ar.result)[0];
Shishir Agrawal566b7612013-10-28 14:41:00 -0700309 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800310 request.result = -1;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700311 if (ar.result == null) {
312 loge("iccOpenLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800313 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700314 loge("iccOpenLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800315 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700316 } else {
317 loge("iccOpenLogicalChannel: Unknown exception");
318 }
319 }
320 synchronized (request) {
321 request.notifyAll();
322 }
323 break;
324
325 case CMD_CLOSE_CHANNEL:
326 request = (MainThreadRequest) msg.obj;
327 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE,
328 request);
329 UiccController.getInstance().getUiccCard().iccCloseLogicalChannel(
Jake Hambye994d462014-02-03 13:10:13 -0800330 (Integer) request.argument,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700331 onCompleted);
332 break;
333
334 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800335 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
336 break;
337
338 case CMD_NV_READ_ITEM:
339 request = (MainThreadRequest) msg.obj;
340 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
341 mPhone.nvReadItem((Integer) request.argument, onCompleted);
342 break;
343
344 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700345 ar = (AsyncResult) msg.obj;
346 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800347 if (ar.exception == null && ar.result != null) {
348 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700349 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800350 request.result = "";
351 if (ar.result == null) {
352 loge("nvReadItem: Empty response");
353 } else if (ar.exception instanceof CommandException) {
354 loge("nvReadItem: CommandException: " +
355 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700356 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800357 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700358 }
359 }
360 synchronized (request) {
361 request.notifyAll();
362 }
363 break;
364
Jake Hambye994d462014-02-03 13:10:13 -0800365 case CMD_NV_WRITE_ITEM:
366 request = (MainThreadRequest) msg.obj;
367 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
368 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
369 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
370 break;
371
372 case EVENT_NV_WRITE_ITEM_DONE:
373 handleNullReturnEvent(msg, "nvWriteItem");
374 break;
375
376 case CMD_NV_WRITE_CDMA_PRL:
377 request = (MainThreadRequest) msg.obj;
378 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
379 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
380 break;
381
382 case EVENT_NV_WRITE_CDMA_PRL_DONE:
383 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
384 break;
385
386 case CMD_NV_RESET_CONFIG:
387 request = (MainThreadRequest) msg.obj;
388 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
389 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
390 break;
391
392 case EVENT_NV_RESET_CONFIG_DONE:
393 handleNullReturnEvent(msg, "nvResetConfig");
394 break;
395
Jake Hamby7c27be32014-03-03 13:25:59 -0800396 case CMD_GET_PREFERRED_NETWORK_TYPE:
397 request = (MainThreadRequest) msg.obj;
398 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
399 mPhone.getPreferredNetworkType(onCompleted);
400 break;
401
402 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
403 ar = (AsyncResult) msg.obj;
404 request = (MainThreadRequest) ar.userObj;
405 if (ar.exception == null && ar.result != null) {
406 request.result = ar.result; // Integer
407 } else {
408 request.result = -1;
409 if (ar.result == null) {
410 loge("getPreferredNetworkType: Empty response");
411 } else if (ar.exception instanceof CommandException) {
412 loge("getPreferredNetworkType: CommandException: " +
413 ar.exception);
414 } else {
415 loge("getPreferredNetworkType: Unknown exception");
416 }
417 }
418 synchronized (request) {
419 request.notifyAll();
420 }
421 break;
422
423 case CMD_SET_PREFERRED_NETWORK_TYPE:
424 request = (MainThreadRequest) msg.obj;
425 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
426 int networkType = (Integer) request.argument;
427 mPhone.setPreferredNetworkType(networkType, onCompleted);
428 break;
429
430 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
431 handleNullReturnEvent(msg, "setPreferredNetworkType");
432 break;
433
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 default:
435 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
436 break;
437 }
438 }
Jake Hambye994d462014-02-03 13:10:13 -0800439
440 private void handleNullReturnEvent(Message msg, String command) {
441 AsyncResult ar = (AsyncResult) msg.obj;
442 MainThreadRequest request = (MainThreadRequest) ar.userObj;
443 if (ar.exception == null) {
444 request.result = true;
445 } else {
446 request.result = false;
447 if (ar.exception instanceof CommandException) {
448 loge(command + ": CommandException: " + ar.exception);
449 } else {
450 loge(command + ": Unknown exception");
451 }
452 }
453 synchronized (request) {
454 request.notifyAll();
455 }
456 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700457 }
458
459 /**
460 * Posts the specified command to be executed on the main thread,
461 * waits for the request to complete, and returns the result.
462 * @see #sendRequestAsync
463 */
464 private Object sendRequest(int command, Object argument) {
465 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
466 throw new RuntimeException("This method will deadlock if called from the main thread.");
467 }
468
469 MainThreadRequest request = new MainThreadRequest(argument);
470 Message msg = mMainThreadHandler.obtainMessage(command, request);
471 msg.sendToTarget();
472
473 // Wait for the request to complete
474 synchronized (request) {
475 while (request.result == null) {
476 try {
477 request.wait();
478 } catch (InterruptedException e) {
479 // Do nothing, go back and wait until the request is complete
480 }
481 }
482 }
483 return request.result;
484 }
485
486 /**
487 * Asynchronous ("fire and forget") version of sendRequest():
488 * Posts the specified command to be executed on the main thread, and
489 * returns immediately.
490 * @see #sendRequest
491 */
492 private void sendRequestAsync(int command) {
493 mMainThreadHandler.sendEmptyMessage(command);
494 }
495
496 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700497 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
498 * @see {@link #sendRequest(int,Object)}
499 */
500 private void sendRequestAsync(int command, Object argument) {
501 MainThreadRequest request = new MainThreadRequest(argument);
502 Message msg = mMainThreadHandler.obtainMessage(command, request);
503 msg.sendToTarget();
504 }
505
506 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700507 * Initialize the singleton PhoneInterfaceManager instance.
508 * This is only done once, at startup, from PhoneApp.onCreate().
509 */
Santos Cordon406c0342013-08-28 00:07:47 -0700510 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone,
511 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 synchronized (PhoneInterfaceManager.class) {
513 if (sInstance == null) {
Santos Cordon406c0342013-08-28 00:07:47 -0700514 sInstance = new PhoneInterfaceManager(app, phone, callHandlerService);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 } else {
516 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
517 }
518 return sInstance;
519 }
520 }
521
522 /** Private constructor; @see init() */
Santos Cordon406c0342013-08-28 00:07:47 -0700523 private PhoneInterfaceManager(PhoneGlobals app, Phone phone,
524 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 mApp = app;
526 mPhone = phone;
527 mCM = PhoneGlobals.getInstance().mCM;
528 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
529 mMainThreadHandler = new MainThreadHandler();
Santos Cordon406c0342013-08-28 00:07:47 -0700530 mCallHandlerService = callHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700531 publish();
532 }
533
534 private void publish() {
535 if (DBG) log("publish: " + this);
536
537 ServiceManager.addService("phone", this);
538 }
539
540 //
541 // Implementation of the ITelephony interface.
542 //
543
544 public void dial(String number) {
545 if (DBG) log("dial: " + number);
546 // No permission check needed here: This is just a wrapper around the
547 // ACTION_DIAL intent, which is available to any app since it puts up
548 // the UI before it does anything.
549
550 String url = createTelUrl(number);
551 if (url == null) {
552 return;
553 }
554
555 // PENDING: should we just silently fail if phone is offhook or ringing?
556 PhoneConstants.State state = mCM.getState();
557 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
558 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
559 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
560 mApp.startActivity(intent);
561 }
562 }
563
564 public void call(String callingPackage, String number) {
565 if (DBG) log("call: " + number);
566
567 // This is just a wrapper around the ACTION_CALL intent, but we still
568 // need to do a permission check since we're calling startActivity()
569 // from the context of the phone app.
570 enforceCallPermission();
571
572 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
573 != AppOpsManager.MODE_ALLOWED) {
574 return;
575 }
576
577 String url = createTelUrl(number);
578 if (url == null) {
579 return;
580 }
581
582 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
583 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
584 mApp.startActivity(intent);
585 }
586
587 private boolean showCallScreenInternal(boolean specifyInitialDialpadState,
Makoto Onukibcf20992013-09-12 17:59:30 -0700588 boolean showDialpad) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 if (!PhoneGlobals.sVoiceCapable) {
590 // Never allow the InCallScreen to appear on data-only devices.
591 return false;
592 }
593 if (isIdle()) {
594 return false;
595 }
596 // If the phone isn't idle then go to the in-call screen
597 long callingId = Binder.clearCallingIdentity();
Santos Cordon406c0342013-08-28 00:07:47 -0700598
Makoto Onukibcf20992013-09-12 17:59:30 -0700599 mCallHandlerService.bringToForeground(showDialpad);
Santos Cordon406c0342013-08-28 00:07:47 -0700600
601 Binder.restoreCallingIdentity(callingId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700602 return true;
603 }
604
605 // Show the in-call screen without specifying the initial dialpad state.
606 public boolean showCallScreen() {
607 return showCallScreenInternal(false, false);
608 }
609
610 // The variation of showCallScreen() that specifies the initial dialpad state.
611 // (Ideally this would be called showCallScreen() too, just with a different
612 // signature, but AIDL doesn't allow that.)
613 public boolean showCallScreenWithDialpad(boolean showDialpad) {
614 return showCallScreenInternal(true, showDialpad);
615 }
616
617 /**
618 * End a call based on call state
619 * @return true is a call was ended
620 */
621 public boolean endCall() {
622 enforceCallPermission();
623 return (Boolean) sendRequest(CMD_END_CALL, null);
624 }
625
626 public void answerRingingCall() {
627 if (DBG) log("answerRingingCall...");
628 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
629 // but that can probably wait till the big TelephonyManager API overhaul.
630 // For now, protect this call with the MODIFY_PHONE_STATE permission.
631 enforceModifyPermission();
632 sendRequestAsync(CMD_ANSWER_RINGING_CALL);
633 }
634
635 /**
636 * Make the actual telephony calls to implement answerRingingCall().
637 * This should only be called from the main thread of the Phone app.
638 * @see #answerRingingCall
639 *
640 * TODO: it would be nice to return true if we answered the call, or
641 * false if there wasn't actually a ringing incoming call, or some
642 * other error occurred. (In other words, pass back the return value
643 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
644 * But that would require calling this method via sendRequest() rather
645 * than sendRequestAsync(), and right now we don't actually *need* that
646 * return value, so let's just return void for now.
647 */
648 private void answerRingingCallInternal() {
649 final boolean hasRingingCall = !mPhone.getRingingCall().isIdle();
650 if (hasRingingCall) {
651 final boolean hasActiveCall = !mPhone.getForegroundCall().isIdle();
652 final boolean hasHoldingCall = !mPhone.getBackgroundCall().isIdle();
653 if (hasActiveCall && hasHoldingCall) {
654 // Both lines are in use!
655 // TODO: provide a flag to let the caller specify what
656 // policy to use if both lines are in use. (The current
657 // behavior is hardwired to "answer incoming, end ongoing",
658 // which is how the CALL button is specced to behave.)
659 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
660 return;
661 } else {
662 // answerCall() will automatically hold the current active
663 // call, if there is one.
664 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
665 return;
666 }
667 } else {
668 // No call was ringing.
669 return;
670 }
671 }
672
673 public void silenceRinger() {
674 if (DBG) log("silenceRinger...");
675 // TODO: find a more appropriate permission to check here.
676 // (That can probably wait till the big TelephonyManager API overhaul.
677 // For now, protect this call with the MODIFY_PHONE_STATE permission.)
678 enforceModifyPermission();
679 sendRequestAsync(CMD_SILENCE_RINGER);
680 }
681
682 /**
683 * Internal implemenation of silenceRinger().
684 * This should only be called from the main thread of the Phone app.
685 * @see #silenceRinger
686 */
687 private void silenceRingerInternal() {
688 if ((mCM.getState() == PhoneConstants.State.RINGING)
689 && mApp.notifier.isRinging()) {
690 // Ringer is actually playing, so silence it.
691 if (DBG) log("silenceRingerInternal: silencing...");
692 mApp.notifier.silenceRinger();
693 }
694 }
695
696 public boolean isOffhook() {
697 return (mCM.getState() == PhoneConstants.State.OFFHOOK);
698 }
699
700 public boolean isRinging() {
701 return (mCM.getState() == PhoneConstants.State.RINGING);
702 }
703
704 public boolean isIdle() {
705 return (mCM.getState() == PhoneConstants.State.IDLE);
706 }
707
708 public boolean isSimPinEnabled() {
709 enforceReadPermission();
710 return (PhoneGlobals.getInstance().isSimPinEnabled());
711 }
712
713 public boolean supplyPin(String pin) {
Wink Saville9de0f752013-10-22 19:04:03 -0700714 int [] resultArray = supplyPinReportResult(pin);
715 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
716 }
717
718 public boolean supplyPuk(String puk, String pin) {
719 int [] resultArray = supplyPukReportResult(puk, pin);
720 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
721 }
722
723 /** {@hide} */
724 public int[] supplyPinReportResult(String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 enforceModifyPermission();
726 final UnlockSim checkSimPin = new UnlockSim(mPhone.getIccCard());
727 checkSimPin.start();
728 return checkSimPin.unlockSim(null, pin);
729 }
730
Wink Saville9de0f752013-10-22 19:04:03 -0700731 /** {@hide} */
732 public int[] supplyPukReportResult(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 enforceModifyPermission();
734 final UnlockSim checkSimPuk = new UnlockSim(mPhone.getIccCard());
735 checkSimPuk.start();
736 return checkSimPuk.unlockSim(puk, pin);
737 }
738
739 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700740 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 * a synchronous one.
742 */
743 private static class UnlockSim extends Thread {
744
745 private final IccCard mSimCard;
746
747 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700748 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
749 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750
751 // For replies from SimCard interface
752 private Handler mHandler;
753
754 // For async handler to identify request type
755 private static final int SUPPLY_PIN_COMPLETE = 100;
756
757 public UnlockSim(IccCard simCard) {
758 mSimCard = simCard;
759 }
760
761 @Override
762 public void run() {
763 Looper.prepare();
764 synchronized (UnlockSim.this) {
765 mHandler = new Handler() {
766 @Override
767 public void handleMessage(Message msg) {
768 AsyncResult ar = (AsyncResult) msg.obj;
769 switch (msg.what) {
770 case SUPPLY_PIN_COMPLETE:
771 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
772 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700773 mRetryCount = msg.arg1;
774 if (ar.exception != null) {
775 if (ar.exception instanceof CommandException &&
776 ((CommandException)(ar.exception)).getCommandError()
777 == CommandException.Error.PASSWORD_INCORRECT) {
778 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
779 } else {
780 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
781 }
782 } else {
783 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 mDone = true;
786 UnlockSim.this.notifyAll();
787 }
788 break;
789 }
790 }
791 };
792 UnlockSim.this.notifyAll();
793 }
794 Looper.loop();
795 }
796
797 /*
798 * Use PIN or PUK to unlock SIM card
799 *
800 * If PUK is null, unlock SIM card with PIN
801 *
802 * If PUK is not null, unlock SIM card with PUK and set PIN code
803 */
Wink Saville9de0f752013-10-22 19:04:03 -0700804 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805
806 while (mHandler == null) {
807 try {
808 wait();
809 } catch (InterruptedException e) {
810 Thread.currentThread().interrupt();
811 }
812 }
813 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
814
815 if (puk == null) {
816 mSimCard.supplyPin(pin, callback);
817 } else {
818 mSimCard.supplyPuk(puk, pin, callback);
819 }
820
821 while (!mDone) {
822 try {
823 Log.d(LOG_TAG, "wait for done");
824 wait();
825 } catch (InterruptedException e) {
826 // Restore the interrupted status
827 Thread.currentThread().interrupt();
828 }
829 }
830 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -0700831 int[] resultArray = new int[2];
832 resultArray[0] = mResult;
833 resultArray[1] = mRetryCount;
834 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 }
836 }
837
838 public void updateServiceLocation() {
839 // No permission check needed here: this call is harmless, and it's
840 // needed for the ServiceState.requestStateUpdate() call (which is
841 // already intentionally exposed to 3rd parties.)
842 mPhone.updateServiceLocation();
843 }
844
845 public boolean isRadioOn() {
846 return mPhone.getServiceState().getVoiceRegState() != ServiceState.STATE_POWER_OFF;
847 }
848
849 public void toggleRadioOnOff() {
850 enforceModifyPermission();
851 mPhone.setRadioPower(!isRadioOn());
852 }
853 public boolean setRadio(boolean turnOn) {
854 enforceModifyPermission();
855 if ((mPhone.getServiceState().getVoiceRegState() != ServiceState.STATE_POWER_OFF) != turnOn) {
856 toggleRadioOnOff();
857 }
858 return true;
859 }
860 public boolean setRadioPower(boolean turnOn) {
861 enforceModifyPermission();
862 mPhone.setRadioPower(turnOn);
863 return true;
864 }
865
866 public boolean enableDataConnectivity() {
867 enforceModifyPermission();
868 ConnectivityManager cm =
869 (ConnectivityManager)mApp.getSystemService(Context.CONNECTIVITY_SERVICE);
870 cm.setMobileDataEnabled(true);
871 return true;
872 }
873
874 public int enableApnType(String type) {
875 enforceModifyPermission();
876 return mPhone.enableApnType(type);
877 }
878
879 public int disableApnType(String type) {
880 enforceModifyPermission();
881 return mPhone.disableApnType(type);
882 }
883
884 public boolean disableDataConnectivity() {
885 enforceModifyPermission();
886 ConnectivityManager cm =
887 (ConnectivityManager)mApp.getSystemService(Context.CONNECTIVITY_SERVICE);
888 cm.setMobileDataEnabled(false);
889 return true;
890 }
891
892 public boolean isDataConnectivityPossible() {
893 return mPhone.isDataConnectivityPossible();
894 }
895
896 public boolean handlePinMmi(String dialString) {
897 enforceModifyPermission();
898 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString);
899 }
900
901 public void cancelMissedCallsNotification() {
902 enforceModifyPermission();
903 mApp.notificationMgr.cancelMissedCallNotification();
904 }
905
906 public int getCallState() {
907 return DefaultPhoneNotifier.convertCallState(mCM.getState());
908 }
909
910 public int getDataState() {
911 return DefaultPhoneNotifier.convertDataState(mPhone.getDataConnectionState());
912 }
913
914 public int getDataActivity() {
915 return DefaultPhoneNotifier.convertDataActivityState(mPhone.getDataActivityState());
916 }
917
918 @Override
919 public Bundle getCellLocation() {
920 try {
921 mApp.enforceCallingOrSelfPermission(
922 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
923 } catch (SecurityException e) {
924 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
925 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
926 // is the weaker precondition
927 mApp.enforceCallingOrSelfPermission(
928 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
929 }
930
Jake Hambye994d462014-02-03 13:10:13 -0800931 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 if (DBG_LOC) log("getCellLocation: is active user");
933 Bundle data = new Bundle();
934 mPhone.getCellLocation().fillInNotifierBundle(data);
935 return data;
936 } else {
937 if (DBG_LOC) log("getCellLocation: suppress non-active user");
938 return null;
939 }
940 }
941
942 @Override
943 public void enableLocationUpdates() {
944 mApp.enforceCallingOrSelfPermission(
945 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
946 mPhone.enableLocationUpdates();
947 }
948
949 @Override
950 public void disableLocationUpdates() {
951 mApp.enforceCallingOrSelfPermission(
952 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
953 mPhone.disableLocationUpdates();
954 }
955
956 @Override
957 @SuppressWarnings("unchecked")
958 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
959 try {
960 mApp.enforceCallingOrSelfPermission(
961 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
962 } catch (SecurityException e) {
963 // If we have ACCESS_FINE_LOCATION permission, skip the check
964 // for ACCESS_COARSE_LOCATION
965 // A failure should throw the SecurityException from
966 // ACCESS_COARSE_LOCATION since this is the weaker precondition
967 mApp.enforceCallingOrSelfPermission(
968 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
969 }
970
971 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
972 callingPackage) != AppOpsManager.MODE_ALLOWED) {
973 return null;
974 }
Jake Hambye994d462014-02-03 13:10:13 -0800975 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
977
978 ArrayList<NeighboringCellInfo> cells = null;
979
980 try {
981 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
982 CMD_HANDLE_NEIGHBORING_CELL, null);
983 } catch (RuntimeException e) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700984 loge("getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 }
986 return cells;
987 } else {
988 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
989 return null;
990 }
991 }
992
993
994 @Override
995 public List<CellInfo> getAllCellInfo() {
996 try {
997 mApp.enforceCallingOrSelfPermission(
998 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
999 } catch (SecurityException e) {
1000 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1001 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1002 // is the weaker precondition
1003 mApp.enforceCallingOrSelfPermission(
1004 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1005 }
1006
Jake Hambye994d462014-02-03 13:10:13 -08001007 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 if (DBG_LOC) log("getAllCellInfo: is active user");
1009 return mPhone.getAllCellInfo();
1010 } else {
1011 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1012 return null;
1013 }
1014 }
1015
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001016 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 public void setCellInfoListRate(int rateInMillis) {
1018 mPhone.setCellInfoListRate(rateInMillis);
1019 }
1020
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001021 @Override
Sailesh Nepalcc0375f2013-11-13 09:15:18 -08001022 public void newIncomingThirdPartyCall(ComponentName component, String callId,
1023 String callerDisplayName) {
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001024 // TODO(sail): Enforce that the component belongs to the calling package.
1025 if (DBG) {
1026 log("newIncomingThirdPartyCall: component: " + component + " callId: " + callId);
1027 }
1028 enforceCallPermission();
Sailesh Nepalcc0375f2013-11-13 09:15:18 -08001029 sendRequestAsync(CMD_NEW_INCOMING_THIRD_PARTY_CALL, new IncomingThirdPartyCallArgs(
1030 component, callId, callerDisplayName));
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001031 }
1032
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 //
1034 // Internal helper methods.
1035 //
1036
Jake Hambye994d462014-02-03 13:10:13 -08001037 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 boolean ok;
1039
1040 boolean self = Binder.getCallingUid() == Process.myUid();
1041 if (!self) {
1042 // Get the caller's user id then clear the calling identity
1043 // which will be restored in the finally clause.
1044 int callingUser = UserHandle.getCallingUserId();
1045 long ident = Binder.clearCallingIdentity();
1046
1047 try {
1048 // With calling identity cleared the current user is the foreground user.
1049 int foregroundUser = ActivityManager.getCurrentUser();
1050 ok = (foregroundUser == callingUser);
1051 if (DBG_LOC) {
1052 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1053 + " callingUser=" + callingUser + " ok=" + ok);
1054 }
1055 } catch (Exception ex) {
1056 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1057 ok = false;
1058 } finally {
1059 Binder.restoreCallingIdentity(ident);
1060 }
1061 } else {
1062 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1063 ok = true;
1064 }
1065 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1066 return ok;
1067 }
1068
1069 /**
1070 * Make sure the caller has the READ_PHONE_STATE permission.
1071 *
1072 * @throws SecurityException if the caller does not have the required permission
1073 */
1074 private void enforceReadPermission() {
1075 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1076 }
1077
1078 /**
1079 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1080 *
1081 * @throws SecurityException if the caller does not have the required permission
1082 */
1083 private void enforceModifyPermission() {
1084 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1085 }
1086
1087 /**
1088 * Make sure the caller has the CALL_PHONE permission.
1089 *
1090 * @throws SecurityException if the caller does not have the required permission
1091 */
1092 private void enforceCallPermission() {
1093 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1094 }
1095
Shishir Agrawal566b7612013-10-28 14:41:00 -07001096 /**
1097 * Make sure the caller has SIM_COMMUNICATION permission.
1098 *
1099 * @throws SecurityException if the caller does not have the required permission.
1100 */
1101 private void enforceSimCommunicationPermission() {
1102 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.SIM_COMMUNICATION, null);
1103 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104
1105 private String createTelUrl(String number) {
1106 if (TextUtils.isEmpty(number)) {
1107 return null;
1108 }
1109
Jake Hambye994d462014-02-03 13:10:13 -08001110 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 }
1112
Ihab Awadf9e92732013-12-05 18:02:52 -08001113 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1115 }
1116
Ihab Awadf9e92732013-12-05 18:02:52 -08001117 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1119 }
1120
1121 public int getActivePhoneType() {
1122 return mPhone.getPhoneType();
1123 }
1124
1125 /**
1126 * Returns the CDMA ERI icon index to display
1127 */
1128 public int getCdmaEriIconIndex() {
1129 return mPhone.getCdmaEriIconIndex();
1130 }
1131
1132 /**
1133 * Returns the CDMA ERI icon mode,
1134 * 0 - ON
1135 * 1 - FLASHING
1136 */
1137 public int getCdmaEriIconMode() {
1138 return mPhone.getCdmaEriIconMode();
1139 }
1140
1141 /**
1142 * Returns the CDMA ERI text,
1143 */
1144 public String getCdmaEriText() {
1145 return mPhone.getCdmaEriText();
1146 }
1147
1148 /**
1149 * Returns true if CDMA provisioning needs to run.
1150 */
1151 public boolean needsOtaServiceProvisioning() {
1152 return mPhone.needsOtaServiceProvisioning();
1153 }
1154
1155 /**
1156 * Returns the unread count of voicemails
1157 */
1158 public int getVoiceMessageCount() {
1159 return mPhone.getVoiceMessageCount();
1160 }
1161
1162 /**
1163 * Returns the data network type
1164 *
1165 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1166 */
1167 @Override
1168 public int getNetworkType() {
1169 return mPhone.getServiceState().getDataNetworkType();
1170 }
1171
1172 /**
1173 * Returns the data network type
1174 */
1175 @Override
1176 public int getDataNetworkType() {
1177 return mPhone.getServiceState().getDataNetworkType();
1178 }
1179
1180 /**
1181 * Returns the data network type
1182 */
1183 @Override
1184 public int getVoiceNetworkType() {
1185 return mPhone.getServiceState().getVoiceNetworkType();
1186 }
1187
1188 /**
1189 * @return true if a ICC card is present
1190 */
1191 public boolean hasIccCard() {
1192 return mPhone.getIccCard().hasIccCard();
1193 }
1194
1195 /**
1196 * Return if the current radio is LTE on CDMA. This
1197 * is a tri-state return value as for a period of time
1198 * the mode may be unknown.
1199 *
1200 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001201 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001202 */
1203 public int getLteOnCdmaMode() {
1204 return mPhone.getLteOnCdmaMode();
1205 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001206
1207 /**
1208 * @see android.telephony.TelephonyManager.WifiCallingChoices
1209 */
1210 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001211 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1212 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001213 }
1214
1215 /**
1216 * @see android.telephony.TelephonyManager.WifiCallingChoices
1217 */
1218 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001219 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1220 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1221 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001222 }
1223
Sailesh Nepald1e68152013-12-12 19:08:02 -08001224 private static int getWhenToMakeWifiCallsDefaultPreference() {
1225 // TODO(sail): Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001226 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001227 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001228
Shishir Agrawal566b7612013-10-28 14:41:00 -07001229 @Override
1230 public int iccOpenLogicalChannel(String AID) {
1231 enforceSimCommunicationPermission();
1232
1233 if (DBG) log("iccOpenLogicalChannel: " + AID);
1234 Integer channel = (Integer)sendRequest(CMD_OPEN_CHANNEL, AID);
1235 if (DBG) log("iccOpenLogicalChannel: " + channel);
Jake Hambye994d462014-02-03 13:10:13 -08001236 return channel;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001237 }
1238
1239 @Override
1240 public boolean iccCloseLogicalChannel(int channel) {
1241 enforceSimCommunicationPermission();
1242
1243 if (DBG) log("iccCloseLogicalChannel: " + channel);
1244 if (channel < 0) {
1245 return false;
1246 }
Jake Hambye994d462014-02-03 13:10:13 -08001247 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001248 if (DBG) log("iccCloseLogicalChannel: " + success);
1249 return success;
1250 }
1251
1252 @Override
1253 public String iccTransmitApduLogicalChannel(int channel, int cla,
1254 int command, int p1, int p2, int p3, String data) {
1255 enforceSimCommunicationPermission();
1256
1257 if (DBG) {
1258 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1259 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1260 " data=" + data);
1261 }
1262
1263 if (channel < 0) {
1264 return "";
1265 }
1266
1267 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU,
1268 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1269 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1270
1271 // If the payload is null, there was an error. Indicate that by returning
1272 // an empty string.
1273 if (response.payload == null) {
1274 return "";
1275 }
1276
1277 // Append the returned status code to the end of the response payload.
1278 String s = Integer.toHexString(
1279 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1280 s = IccUtils.bytesToHexString(response.payload) + s;
1281 return s;
1282 }
Jake Hambye994d462014-02-03 13:10:13 -08001283
Derek Tan4d5e5c12014-02-04 11:54:58 -08001284 @Override
1285 public String sendEnvelope(String content) {
1286 enforceSimCommunicationPermission();
1287
1288 String response = (String)sendRequest(CMD_SEND_ENVELOPE, content);
1289
1290 return response;
1291 }
1292
Jake Hambye994d462014-02-03 13:10:13 -08001293 /**
1294 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1295 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1296 *
1297 * @param itemID the ID of the item to read
1298 * @return the NV item as a String, or null on error.
1299 */
1300 @Override
1301 public String nvReadItem(int itemID) {
1302 enforceModifyPermission();
1303 if (DBG) log("nvReadItem: item " + itemID);
1304 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1305 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1306 return value;
1307 }
1308
1309 /**
1310 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1311 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1312 *
1313 * @param itemID the ID of the item to read
1314 * @param itemValue the value to write, as a String
1315 * @return true on success; false on any failure
1316 */
1317 @Override
1318 public boolean nvWriteItem(int itemID, String itemValue) {
1319 enforceModifyPermission();
1320 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1321 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1322 new Pair<Integer, String>(itemID, itemValue));
1323 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1324 return success;
1325 }
1326
1327 /**
1328 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1329 * Used for device configuration by some CDMA operators.
1330 *
1331 * @param preferredRoamingList byte array containing the new PRL
1332 * @return true on success; false on any failure
1333 */
1334 @Override
1335 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
1336 enforceModifyPermission();
1337 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1338 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1339 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1340 return success;
1341 }
1342
1343 /**
1344 * Perform the specified type of NV config reset.
1345 * Used for device configuration by some CDMA operators.
1346 *
1347 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1348 * @return true on success; false on any failure
1349 */
1350 @Override
1351 public boolean nvResetConfig(int resetType) {
1352 enforceModifyPermission();
1353 if (DBG) log("nvResetConfig: type " + resetType);
1354 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1355 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1356 return success;
1357 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001358
1359 /**
1360 * Get the preferred network type.
1361 * Used for device configuration by some CDMA operators.
1362 *
1363 * @return the preferred network type, defined in RILConstants.java.
1364 */
1365 @Override
1366 public int getPreferredNetworkType() {
1367 enforceModifyPermission();
1368 if (DBG) log("getPreferredNetworkType");
1369 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1370 int networkType = (result != null ? result[0] : -1);
1371 if (DBG) log("getPreferredNetworkType: " + networkType);
1372 return networkType;
1373 }
1374
1375 /**
1376 * Set the preferred network type.
1377 * Used for device configuration by some CDMA operators.
1378 *
1379 * @param networkType the preferred network type, defined in RILConstants.java.
1380 * @return true on success; false on any failure.
1381 */
1382 @Override
1383 public boolean setPreferredNetworkType(int networkType) {
1384 enforceModifyPermission();
1385 if (DBG) log("setPreferredNetworkType: type " + networkType);
1386 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1387 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
1388 return success;
1389 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390}