blob: 71574b6024b8f1d99aa85dc2b49236bcf73badad [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Wink Saville36469e72014-06-11 15:17:00 -070024import android.net.ConnectivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.net.Uri;
26import android.os.AsyncResult;
27import android.os.Binder;
28import android.os.Bundle;
29import android.os.Handler;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070030import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070034import android.os.RemoteException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.os.ServiceManager;
36import android.os.UserHandle;
Ihab Awadf2177b72013-11-25 13:33:23 -080037import android.provider.Settings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.telephony.CellInfo;
Jake Hambye994d462014-02-03 13:10:13 -080039import android.telephony.NeighboringCellInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.ServiceState;
Wink Saville36469e72014-06-11 15:17:00 -070041import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.text.TextUtils;
44import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080045import android.util.Pair;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046
Shishir Agrawal566b7612013-10-28 14:41:00 -070047import com.android.internal.telephony.CallManager;
48import com.android.internal.telephony.CommandException;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070049import com.android.internal.telephony.Connection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080052import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070054import com.android.internal.telephony.PhoneFactory;
55import com.android.internal.telephony.CallManager;
56import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070058import com.android.internal.telephony.dataconnection.DctController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070059import com.android.internal.telephony.uicc.IccIoResult;
60import com.android.internal.telephony.uicc.IccUtils;
61import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080062import com.android.internal.util.HexDump;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070063import com.android.services.telephony.common.Call;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064
Wink Saville36469e72014-06-11 15:17:00 -070065import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
66
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import java.util.ArrayList;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070068import java.util.HashMap;
69import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080070import java.util.List;
Gabriel Peal36ebb0d2014-03-20 09:20:43 -070071import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
73/**
74 * Implementation of the ITelephony interface.
75 */
Santos Cordon117fee72014-05-16 17:56:12 -070076public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077 private static final String LOG_TAG = "PhoneInterfaceManager";
78 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
79 private static final boolean DBG_LOC = false;
80
81 // Message codes used with mMainThreadHandler
82 private static final int CMD_HANDLE_PIN_MMI = 1;
83 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
84 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
85 private static final int CMD_ANSWER_RINGING_CALL = 4;
86 private static final int CMD_END_CALL = 5; // not used yet
87 private static final int CMD_SILENCE_RINGER = 6;
Shishir Agrawal566b7612013-10-28 14:41:00 -070088 private static final int CMD_TRANSMIT_APDU = 7;
89 private static final int EVENT_TRANSMIT_APDU_DONE = 8;
90 private static final int CMD_OPEN_CHANNEL = 9;
91 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
92 private static final int CMD_CLOSE_CHANNEL = 11;
93 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -080094 private static final int CMD_NV_READ_ITEM = 13;
95 private static final int EVENT_NV_READ_ITEM_DONE = 14;
96 private static final int CMD_NV_WRITE_ITEM = 15;
97 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
98 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
99 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
100 private static final int CMD_NV_RESET_CONFIG = 19;
101 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800102 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
103 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
104 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
105 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800106 private static final int CMD_SEND_ENVELOPE = 25;
107 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108
109 /** The singleton instance. */
110 private static PhoneInterfaceManager sInstance;
111
112 PhoneGlobals mApp;
113 Phone mPhone;
114 CallManager mCM;
115 AppOpsManager mAppOps;
116 MainThreadHandler mMainThreadHandler;
Santos Cordon406c0342013-08-28 00:07:47 -0700117 CallHandlerServiceProxy mCallHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118
119 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700120 * A request object to use for transmitting data to an ICC.
121 */
122 private static final class IccAPDUArgument {
123 public int channel, cla, command, p1, p2, p3;
124 public String data;
125
126 public IccAPDUArgument(int channel, int cla, int command,
127 int p1, int p2, int p3, String data) {
128 this.channel = channel;
129 this.cla = cla;
130 this.command = command;
131 this.p1 = p1;
132 this.p2 = p2;
133 this.p3 = p3;
134 this.data = data;
135 }
136 }
137
138 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
140 * request after sending. The main thread will notify the request when it is complete.
141 */
142 private static final class MainThreadRequest {
143 /** The argument to use for the request */
144 public Object argument;
145 /** The result of the request that is run on the main thread */
146 public Object result;
147
148 public MainThreadRequest(Object argument) {
149 this.argument = argument;
150 }
151 }
152
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800153 private static final class IncomingThirdPartyCallArgs {
154 public final ComponentName component;
155 public final String callId;
156 public final String callerDisplayName;
157
158 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
159 String callerDisplayName) {
160 this.component = component;
161 this.callId = callId;
162 this.callerDisplayName = callerDisplayName;
163 }
164 }
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 /**
167 * A handler that processes messages on the main thread in the phone process. Since many
168 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
169 * inbound binder threads to the main thread in the phone process. The Binder thread
170 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
171 * on, which will be notified when the operation completes and will contain the result of the
172 * request.
173 *
174 * <p>If a MainThreadRequest object is provided in the msg.obj field,
175 * note that request.result must be set to something non-null for the calling thread to
176 * unblock.
177 */
178 private final class MainThreadHandler extends Handler {
179 @Override
180 public void handleMessage(Message msg) {
181 MainThreadRequest request;
182 Message onCompleted;
183 AsyncResult ar;
184
185 switch (msg.what) {
186 case CMD_HANDLE_PIN_MMI:
187 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800188 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 // Wake up the requesting thread
190 synchronized (request) {
191 request.notifyAll();
192 }
193 break;
194
195 case CMD_HANDLE_NEIGHBORING_CELL:
196 request = (MainThreadRequest) msg.obj;
197 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
198 request);
199 mPhone.getNeighboringCids(onCompleted);
200 break;
201
202 case EVENT_NEIGHBORING_CELL_DONE:
203 ar = (AsyncResult) msg.obj;
204 request = (MainThreadRequest) ar.userObj;
205 if (ar.exception == null && ar.result != null) {
206 request.result = ar.result;
207 } else {
208 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800209 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 }
211 // Wake up the requesting thread
212 synchronized (request) {
213 request.notifyAll();
214 }
215 break;
216
217 case CMD_ANSWER_RINGING_CALL:
218 answerRingingCallInternal();
219 break;
220
221 case CMD_SILENCE_RINGER:
222 silenceRingerInternal();
223 break;
224
225 case CMD_END_CALL:
226 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800227 boolean hungUp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228 int phoneType = mPhone.getPhoneType();
229 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
230 // CDMA: If the user presses the Power button we treat it as
231 // ending the complete call session
232 hungUp = PhoneUtils.hangupRingingAndActive(mPhone);
233 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
234 // GSM: End the call as per the Phone state
235 hungUp = PhoneUtils.hangup(mCM);
236 } else {
237 throw new IllegalStateException("Unexpected phone type: " + phoneType);
238 }
239 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
240 request.result = hungUp;
241 // Wake up the requesting thread
242 synchronized (request) {
243 request.notifyAll();
244 }
245 break;
246
Shishir Agrawal566b7612013-10-28 14:41:00 -0700247 case CMD_TRANSMIT_APDU:
248 request = (MainThreadRequest) msg.obj;
249 IccAPDUArgument argument = (IccAPDUArgument) request.argument;
250 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_DONE, request);
251 UiccController.getInstance().getUiccCard().iccTransmitApduLogicalChannel(
252 argument.channel, argument.cla, argument.command,
253 argument.p1, argument.p2, argument.p3, argument.data,
254 onCompleted);
255 break;
256
257 case EVENT_TRANSMIT_APDU_DONE:
258 ar = (AsyncResult) msg.obj;
259 request = (MainThreadRequest) ar.userObj;
260 if (ar.exception == null && ar.result != null) {
261 request.result = ar.result;
262 } else {
263 request.result = new IccIoResult(0x6F, 0, (byte[])null);
264 if (ar.result == null) {
265 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800266 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700267 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800268 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700269 } else {
270 loge("iccTransmitApduLogicalChannel: Unknown exception");
271 }
272 }
273 synchronized (request) {
274 request.notifyAll();
275 }
276 break;
277
Derek Tan4d5e5c12014-02-04 11:54:58 -0800278 case CMD_SEND_ENVELOPE:
279 request = (MainThreadRequest) msg.obj;
280 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700281 UiccController.getInstance().getUiccCard().sendEnvelopeWithStatus(
Derek Tan4d5e5c12014-02-04 11:54:58 -0800282 (String)request.argument, onCompleted);
283 break;
284
285 case EVENT_SEND_ENVELOPE_DONE:
286 ar = (AsyncResult) msg.obj;
287 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700288 if (ar.exception == null && ar.result != null) {
289 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800290 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700291 request.result = new IccIoResult(0x6F, 0, (byte[])null);
292 if (ar.result == null) {
293 loge("sendEnvelopeWithStatus: Empty response");
294 } else if (ar.exception instanceof CommandException) {
295 loge("sendEnvelopeWithStatus: CommandException: " +
296 ar.exception);
297 } else {
298 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
299 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800300 }
301 synchronized (request) {
302 request.notifyAll();
303 }
304 break;
305
Shishir Agrawal566b7612013-10-28 14:41:00 -0700306 case CMD_OPEN_CHANNEL:
307 request = (MainThreadRequest) msg.obj;
308 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
309 UiccController.getInstance().getUiccCard().iccOpenLogicalChannel(
310 (String)request.argument, onCompleted);
311 break;
312
313 case EVENT_OPEN_CHANNEL_DONE:
314 ar = (AsyncResult) msg.obj;
315 request = (MainThreadRequest) ar.userObj;
316 if (ar.exception == null && ar.result != null) {
Jake Hambye994d462014-02-03 13:10:13 -0800317 request.result = ((int[]) ar.result)[0];
Shishir Agrawal566b7612013-10-28 14:41:00 -0700318 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800319 request.result = -1;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700320 if (ar.result == null) {
321 loge("iccOpenLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800322 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700323 loge("iccOpenLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800324 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700325 } else {
326 loge("iccOpenLogicalChannel: Unknown exception");
327 }
328 }
329 synchronized (request) {
330 request.notifyAll();
331 }
332 break;
333
334 case CMD_CLOSE_CHANNEL:
335 request = (MainThreadRequest) msg.obj;
336 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE,
337 request);
338 UiccController.getInstance().getUiccCard().iccCloseLogicalChannel(
Jake Hambye994d462014-02-03 13:10:13 -0800339 (Integer) request.argument,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700340 onCompleted);
341 break;
342
343 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800344 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
345 break;
346
347 case CMD_NV_READ_ITEM:
348 request = (MainThreadRequest) msg.obj;
349 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
350 mPhone.nvReadItem((Integer) request.argument, onCompleted);
351 break;
352
353 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700354 ar = (AsyncResult) msg.obj;
355 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800356 if (ar.exception == null && ar.result != null) {
357 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700358 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800359 request.result = "";
360 if (ar.result == null) {
361 loge("nvReadItem: Empty response");
362 } else if (ar.exception instanceof CommandException) {
363 loge("nvReadItem: CommandException: " +
364 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700365 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800366 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700367 }
368 }
369 synchronized (request) {
370 request.notifyAll();
371 }
372 break;
373
Jake Hambye994d462014-02-03 13:10:13 -0800374 case CMD_NV_WRITE_ITEM:
375 request = (MainThreadRequest) msg.obj;
376 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
377 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
378 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
379 break;
380
381 case EVENT_NV_WRITE_ITEM_DONE:
382 handleNullReturnEvent(msg, "nvWriteItem");
383 break;
384
385 case CMD_NV_WRITE_CDMA_PRL:
386 request = (MainThreadRequest) msg.obj;
387 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
388 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
389 break;
390
391 case EVENT_NV_WRITE_CDMA_PRL_DONE:
392 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
393 break;
394
395 case CMD_NV_RESET_CONFIG:
396 request = (MainThreadRequest) msg.obj;
397 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
398 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
399 break;
400
401 case EVENT_NV_RESET_CONFIG_DONE:
402 handleNullReturnEvent(msg, "nvResetConfig");
403 break;
404
Jake Hamby7c27be32014-03-03 13:25:59 -0800405 case CMD_GET_PREFERRED_NETWORK_TYPE:
406 request = (MainThreadRequest) msg.obj;
407 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
408 mPhone.getPreferredNetworkType(onCompleted);
409 break;
410
411 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
412 ar = (AsyncResult) msg.obj;
413 request = (MainThreadRequest) ar.userObj;
414 if (ar.exception == null && ar.result != null) {
415 request.result = ar.result; // Integer
416 } else {
417 request.result = -1;
418 if (ar.result == null) {
419 loge("getPreferredNetworkType: Empty response");
420 } else if (ar.exception instanceof CommandException) {
421 loge("getPreferredNetworkType: CommandException: " +
422 ar.exception);
423 } else {
424 loge("getPreferredNetworkType: Unknown exception");
425 }
426 }
427 synchronized (request) {
428 request.notifyAll();
429 }
430 break;
431
432 case CMD_SET_PREFERRED_NETWORK_TYPE:
433 request = (MainThreadRequest) msg.obj;
434 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
435 int networkType = (Integer) request.argument;
436 mPhone.setPreferredNetworkType(networkType, onCompleted);
437 break;
438
439 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
440 handleNullReturnEvent(msg, "setPreferredNetworkType");
441 break;
442
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700443 default:
444 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
445 break;
446 }
447 }
Jake Hambye994d462014-02-03 13:10:13 -0800448
449 private void handleNullReturnEvent(Message msg, String command) {
450 AsyncResult ar = (AsyncResult) msg.obj;
451 MainThreadRequest request = (MainThreadRequest) ar.userObj;
452 if (ar.exception == null) {
453 request.result = true;
454 } else {
455 request.result = false;
456 if (ar.exception instanceof CommandException) {
457 loge(command + ": CommandException: " + ar.exception);
458 } else {
459 loge(command + ": Unknown exception");
460 }
461 }
462 synchronized (request) {
463 request.notifyAll();
464 }
465 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700466 }
467
468 /**
469 * Posts the specified command to be executed on the main thread,
470 * waits for the request to complete, and returns the result.
471 * @see #sendRequestAsync
472 */
473 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700474 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700475 }
476
477 /**
478 * Posts the specified command to be executed on the main thread,
479 * waits for the request to complete, and returns the result.
480 * @see #sendRequestAsync
481 */
482 private Object sendRequest(int command, Object argument, Object argument2) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700483 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
484 throw new RuntimeException("This method will deadlock if called from the main thread.");
485 }
486
487 MainThreadRequest request = new MainThreadRequest(argument);
488 Message msg = mMainThreadHandler.obtainMessage(command, request);
489 msg.sendToTarget();
490
491 // Wait for the request to complete
492 synchronized (request) {
493 while (request.result == null) {
494 try {
495 request.wait();
496 } catch (InterruptedException e) {
497 // Do nothing, go back and wait until the request is complete
498 }
499 }
500 }
501 return request.result;
502 }
503
504 /**
505 * Asynchronous ("fire and forget") version of sendRequest():
506 * Posts the specified command to be executed on the main thread, and
507 * returns immediately.
508 * @see #sendRequest
509 */
510 private void sendRequestAsync(int command) {
511 mMainThreadHandler.sendEmptyMessage(command);
512 }
513
514 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700515 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
516 * @see {@link #sendRequest(int,Object)}
517 */
518 private void sendRequestAsync(int command, Object argument) {
519 MainThreadRequest request = new MainThreadRequest(argument);
520 Message msg = mMainThreadHandler.obtainMessage(command, request);
521 msg.sendToTarget();
522 }
523
524 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700525 * Initialize the singleton PhoneInterfaceManager instance.
526 * This is only done once, at startup, from PhoneApp.onCreate().
527 */
Santos Cordon406c0342013-08-28 00:07:47 -0700528 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone,
Santos Cordon117fee72014-05-16 17:56:12 -0700529 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700530 synchronized (PhoneInterfaceManager.class) {
531 if (sInstance == null) {
Santos Cordon117fee72014-05-16 17:56:12 -0700532 sInstance = new PhoneInterfaceManager(app, phone, callHandlerService);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700533 } else {
534 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
535 }
536 return sInstance;
537 }
538 }
539
540 /** Private constructor; @see init() */
Santos Cordon406c0342013-08-28 00:07:47 -0700541 private PhoneInterfaceManager(PhoneGlobals app, Phone phone,
Santos Cordon117fee72014-05-16 17:56:12 -0700542 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 mApp = app;
544 mPhone = phone;
545 mCM = PhoneGlobals.getInstance().mCM;
546 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
547 mMainThreadHandler = new MainThreadHandler();
Santos Cordon406c0342013-08-28 00:07:47 -0700548 mCallHandlerService = callHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700549 publish();
550 }
551
552 private void publish() {
553 if (DBG) log("publish: " + this);
554
555 ServiceManager.addService("phone", this);
556 }
557
Wink Saville36469e72014-06-11 15:17:00 -0700558 // returns phone associated with the subId.
559 // getPhone(0) returns default phone in single SIM mode.
560 private Phone getPhone(long subId) {
561 // FIXME: hack for the moment
562 return mPhone;
563 // return PhoneUtils.getPhoneUsingSubId(subId);
564 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700565 //
566 // Implementation of the ITelephony interface.
567 //
568
569 public void dial(String number) {
Wink Saville36469e72014-06-11 15:17:00 -0700570 dialUsingSubId(getPreferredVoiceSubscription(), number);
571 }
572
573 public void dialUsingSubId(long subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700574 if (DBG) log("dial: " + number);
575 // No permission check needed here: This is just a wrapper around the
576 // ACTION_DIAL intent, which is available to any app since it puts up
577 // the UI before it does anything.
578
579 String url = createTelUrl(number);
580 if (url == null) {
581 return;
582 }
583
584 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700585 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700586 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
587 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
588 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Wink Saville36469e72014-06-11 15:17:00 -0700589 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 mApp.startActivity(intent);
591 }
592 }
593
594 public void call(String callingPackage, String number) {
Wink Saville36469e72014-06-11 15:17:00 -0700595 callUsingSubId(getPreferredVoiceSubscription(), callingPackage, number);
596 }
597
598 public void callUsingSubId(long subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 if (DBG) log("call: " + number);
600
601 // This is just a wrapper around the ACTION_CALL intent, but we still
602 // need to do a permission check since we're calling startActivity()
603 // from the context of the phone app.
604 enforceCallPermission();
605
606 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
607 != AppOpsManager.MODE_ALLOWED) {
608 return;
609 }
610
611 String url = createTelUrl(number);
612 if (url == null) {
613 return;
614 }
615
616 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700617 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700618 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
619 mApp.startActivity(intent);
620 }
621
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700622 /**
623 * End a call based on call state
624 * @return true is a call was ended
625 */
626 public boolean endCall() {
Wink Saville36469e72014-06-11 15:17:00 -0700627 return endCallUsingSubId(getDefaultSubscription());
628 }
629
630 /**
631 * End a call based on the call state of the subId
632 * @return true is a call was ended
633 */
634 public boolean endCallUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 enforceCallPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700636 return (Boolean) sendRequest(CMD_END_CALL, subId, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700637 }
638
639 public void answerRingingCall() {
Wink Saville36469e72014-06-11 15:17:00 -0700640 answerRingingCallUsingSubId(getDefaultSubscription());
641 }
642
643 public void answerRingingCallUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700644 if (DBG) log("answerRingingCall...");
645 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
646 // but that can probably wait till the big TelephonyManager API overhaul.
647 // For now, protect this call with the MODIFY_PHONE_STATE permission.
648 enforceModifyPermission();
649 sendRequestAsync(CMD_ANSWER_RINGING_CALL);
650 }
651
652 /**
653 * Make the actual telephony calls to implement answerRingingCall().
654 * This should only be called from the main thread of the Phone app.
655 * @see #answerRingingCall
656 *
657 * TODO: it would be nice to return true if we answered the call, or
658 * false if there wasn't actually a ringing incoming call, or some
659 * other error occurred. (In other words, pass back the return value
660 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
661 * But that would require calling this method via sendRequest() rather
662 * than sendRequestAsync(), and right now we don't actually *need* that
663 * return value, so let's just return void for now.
664 */
665 private void answerRingingCallInternal() {
666 final boolean hasRingingCall = !mPhone.getRingingCall().isIdle();
667 if (hasRingingCall) {
668 final boolean hasActiveCall = !mPhone.getForegroundCall().isIdle();
669 final boolean hasHoldingCall = !mPhone.getBackgroundCall().isIdle();
670 if (hasActiveCall && hasHoldingCall) {
671 // Both lines are in use!
672 // TODO: provide a flag to let the caller specify what
673 // policy to use if both lines are in use. (The current
674 // behavior is hardwired to "answer incoming, end ongoing",
675 // which is how the CALL button is specced to behave.)
676 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
677 return;
678 } else {
679 // answerCall() will automatically hold the current active
680 // call, if there is one.
681 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
682 return;
683 }
684 } else {
685 // No call was ringing.
686 return;
687 }
688 }
689
690 public void silenceRinger() {
691 if (DBG) log("silenceRinger...");
692 // TODO: find a more appropriate permission to check here.
693 // (That can probably wait till the big TelephonyManager API overhaul.
694 // For now, protect this call with the MODIFY_PHONE_STATE permission.)
695 enforceModifyPermission();
696 sendRequestAsync(CMD_SILENCE_RINGER);
697 }
698
699 /**
700 * Internal implemenation of silenceRinger().
701 * This should only be called from the main thread of the Phone app.
702 * @see #silenceRinger
703 */
704 private void silenceRingerInternal() {
705 if ((mCM.getState() == PhoneConstants.State.RINGING)
706 && mApp.notifier.isRinging()) {
707 // Ringer is actually playing, so silence it.
708 if (DBG) log("silenceRingerInternal: silencing...");
709 mApp.notifier.silenceRinger();
710 }
711 }
712
713 public boolean isOffhook() {
Wink Saville36469e72014-06-11 15:17:00 -0700714 return isOffhookUsingSubId(getDefaultSubscription());
715 }
716
717 public boolean isOffhookUsingSubId(long subId) {
718 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 }
720
721 public boolean isRinging() {
Wink Saville36469e72014-06-11 15:17:00 -0700722 return (isRingingUsingSubId(getDefaultSubscription()));
723 }
724
725 public boolean isRingingUsingSubId(long subId) {
726 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700727 }
728
729 public boolean isIdle() {
Wink Saville36469e72014-06-11 15:17:00 -0700730 return isIdleUsingSubId(getDefaultSubscription());
731 }
732
733 public boolean isIdleUsingSubId(long subId) {
734 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 }
736
737 public boolean isSimPinEnabled() {
738 enforceReadPermission();
739 return (PhoneGlobals.getInstance().isSimPinEnabled());
740 }
741
742 public boolean supplyPin(String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700743 return supplyPinUsingSubId(getDefaultSubscription(), pin);
744 }
745
746 public boolean supplyPinUsingSubId(long subId, String pin) {
747 int [] resultArray = supplyPinReportResultUsingSubId(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700748 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
749 }
750
751 public boolean supplyPuk(String puk, String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700752 return supplyPukUsingSubId(getDefaultSubscription(), puk, pin);
753 }
754
755 public boolean supplyPukUsingSubId(long subId, String puk, String pin) {
756 int [] resultArray = supplyPukReportResultUsingSubId(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700757 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
758 }
759
760 /** {@hide} */
761 public int[] supplyPinReportResult(String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700762 return supplyPinReportResultUsingSubId(getDefaultSubscription(), pin);
763 }
764
765 public int[] supplyPinReportResultUsingSubId(long subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700767 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 checkSimPin.start();
769 return checkSimPin.unlockSim(null, pin);
770 }
771
Wink Saville9de0f752013-10-22 19:04:03 -0700772 /** {@hide} */
773 public int[] supplyPukReportResult(String puk, String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700774 return supplyPukReportResultUsingSubId(getDefaultSubscription(), puk, pin);
775 }
776
777 public int[] supplyPukReportResultUsingSubId(long subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700779 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 checkSimPuk.start();
781 return checkSimPuk.unlockSim(puk, pin);
782 }
783
784 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700785 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700786 * a synchronous one.
787 */
788 private static class UnlockSim extends Thread {
789
790 private final IccCard mSimCard;
791
792 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700793 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
794 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795
796 // For replies from SimCard interface
797 private Handler mHandler;
798
799 // For async handler to identify request type
800 private static final int SUPPLY_PIN_COMPLETE = 100;
801
802 public UnlockSim(IccCard simCard) {
803 mSimCard = simCard;
804 }
805
806 @Override
807 public void run() {
808 Looper.prepare();
809 synchronized (UnlockSim.this) {
810 mHandler = new Handler() {
811 @Override
812 public void handleMessage(Message msg) {
813 AsyncResult ar = (AsyncResult) msg.obj;
814 switch (msg.what) {
815 case SUPPLY_PIN_COMPLETE:
816 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
817 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700818 mRetryCount = msg.arg1;
819 if (ar.exception != null) {
820 if (ar.exception instanceof CommandException &&
821 ((CommandException)(ar.exception)).getCommandError()
822 == CommandException.Error.PASSWORD_INCORRECT) {
823 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
824 } else {
825 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
826 }
827 } else {
828 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
829 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 mDone = true;
831 UnlockSim.this.notifyAll();
832 }
833 break;
834 }
835 }
836 };
837 UnlockSim.this.notifyAll();
838 }
839 Looper.loop();
840 }
841
842 /*
843 * Use PIN or PUK to unlock SIM card
844 *
845 * If PUK is null, unlock SIM card with PIN
846 *
847 * If PUK is not null, unlock SIM card with PUK and set PIN code
848 */
Wink Saville9de0f752013-10-22 19:04:03 -0700849 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700850
851 while (mHandler == null) {
852 try {
853 wait();
854 } catch (InterruptedException e) {
855 Thread.currentThread().interrupt();
856 }
857 }
858 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
859
860 if (puk == null) {
861 mSimCard.supplyPin(pin, callback);
862 } else {
863 mSimCard.supplyPuk(puk, pin, callback);
864 }
865
866 while (!mDone) {
867 try {
868 Log.d(LOG_TAG, "wait for done");
869 wait();
870 } catch (InterruptedException e) {
871 // Restore the interrupted status
872 Thread.currentThread().interrupt();
873 }
874 }
875 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -0700876 int[] resultArray = new int[2];
877 resultArray[0] = mResult;
878 resultArray[1] = mRetryCount;
879 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 }
881 }
882
883 public void updateServiceLocation() {
Wink Saville36469e72014-06-11 15:17:00 -0700884 updateServiceLocationUsingSubId(getDefaultSubscription());
885
886 }
887
888 public void updateServiceLocationUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700889 // No permission check needed here: this call is harmless, and it's
890 // needed for the ServiceState.requestStateUpdate() call (which is
891 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -0700892 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 }
894
895 public boolean isRadioOn() {
Wink Saville36469e72014-06-11 15:17:00 -0700896 return isRadioOnUsingSubId(getDefaultSubscription());
897 }
898
899 public boolean isRadioOnUsingSubId(long subId) {
900 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 }
902
903 public void toggleRadioOnOff() {
Wink Saville36469e72014-06-11 15:17:00 -0700904 toggleRadioOnOffUsingSubId(getDefaultSubscription());
905
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 }
Wink Saville36469e72014-06-11 15:17:00 -0700907
908 public void toggleRadioOnOffUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700910 getPhone(subId).setRadioPower(!isRadioOnUsingSubId(subId));
911 }
912
913 public boolean setRadio(boolean turnOn) {
914 return setRadioUsingSubId(getDefaultSubscription(), turnOn);
915 }
916
917 public boolean setRadioUsingSubId(long subId, boolean turnOn) {
918 enforceModifyPermission();
919 if ((getPhone(subId).getServiceState().getState() !=
920 ServiceState.STATE_POWER_OFF) != turnOn) {
921 toggleRadioOnOffUsingSubId(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 }
923 return true;
924 }
Wink Saville36469e72014-06-11 15:17:00 -0700925
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 public boolean setRadioPower(boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -0700927 return setRadioPowerUsingSubId(getDefaultSubscription(), turnOn);
928 }
929
930 public boolean setRadioPowerUsingSubId(long subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700931 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700932 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 return true;
934 }
935
Wink Saville36469e72014-06-11 15:17:00 -0700936 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 public boolean enableDataConnectivity() {
938 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700939 long subId = SubscriptionManager.getDefaultDataSubId();
940 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 return true;
942 }
943
Wink Saville36469e72014-06-11 15:17:00 -0700944 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 public boolean disableDataConnectivity() {
946 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700947 long subId = SubscriptionManager.getDefaultDataSubId();
948 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 return true;
950 }
951
Wink Saville36469e72014-06-11 15:17:00 -0700952 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700953 public boolean isDataConnectivityPossible() {
Wink Saville36469e72014-06-11 15:17:00 -0700954 long subId = SubscriptionManager.getDefaultDataSubId();
955 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 }
957
958 public boolean handlePinMmi(String dialString) {
Wink Saville36469e72014-06-11 15:17:00 -0700959 return handlePinMmiUsingSubId(getDefaultSubscription(), dialString);
960 }
961
962 public boolean handlePinMmiUsingSubId(long subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700964 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 }
966
967 public void cancelMissedCallsNotification() {
Wink Saville36469e72014-06-11 15:17:00 -0700968 cancelMissedCallsNotificationUsingSubId(getDefaultSubscription());
969 }
970
971 public void cancelMissedCallsNotificationUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 enforceModifyPermission();
973 mApp.notificationMgr.cancelMissedCallNotification();
974 }
975
976 public int getCallState() {
Wink Saville36469e72014-06-11 15:17:00 -0700977 return getCallStateUsingSubId(getDefaultSubscription());
978 }
979
980 public int getCallStateUsingSubId(long subId) {
981 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 }
983
984 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -0700985 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
986 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 }
988
989 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -0700990 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
991 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992 }
993
994 @Override
995 public Bundle getCellLocation() {
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("getCellLocation: is active user");
1009 Bundle data = new Bundle();
1010 mPhone.getCellLocation().fillInNotifierBundle(data);
1011 return data;
1012 } else {
1013 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1014 return null;
1015 }
1016 }
1017
1018 @Override
1019 public void enableLocationUpdates() {
Wink Saville36469e72014-06-11 15:17:00 -07001020 enableLocationUpdatesUsingSubId(getDefaultSubscription());
1021 }
1022
1023 public void enableLocationUpdatesUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 mApp.enforceCallingOrSelfPermission(
1025 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001026 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001027 }
1028
1029 @Override
1030 public void disableLocationUpdates() {
Wink Saville36469e72014-06-11 15:17:00 -07001031 disableLocationUpdatesUsingSubId(getDefaultSubscription());
1032 }
1033
1034 public void disableLocationUpdatesUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 mApp.enforceCallingOrSelfPermission(
1036 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001037 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 }
1039
1040 @Override
1041 @SuppressWarnings("unchecked")
1042 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1043 try {
1044 mApp.enforceCallingOrSelfPermission(
1045 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1046 } catch (SecurityException e) {
1047 // If we have ACCESS_FINE_LOCATION permission, skip the check
1048 // for ACCESS_COARSE_LOCATION
1049 // A failure should throw the SecurityException from
1050 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1051 mApp.enforceCallingOrSelfPermission(
1052 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1053 }
1054
1055 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1056 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1057 return null;
1058 }
Jake Hambye994d462014-02-03 13:10:13 -08001059 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1061
1062 ArrayList<NeighboringCellInfo> cells = null;
1063
1064 try {
1065 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001066 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001067 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001068 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001069 }
1070 return cells;
1071 } else {
1072 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1073 return null;
1074 }
1075 }
1076
1077
1078 @Override
1079 public List<CellInfo> getAllCellInfo() {
1080 try {
1081 mApp.enforceCallingOrSelfPermission(
1082 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1083 } catch (SecurityException e) {
1084 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1085 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1086 // is the weaker precondition
1087 mApp.enforceCallingOrSelfPermission(
1088 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1089 }
1090
Jake Hambye994d462014-02-03 13:10:13 -08001091 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 if (DBG_LOC) log("getAllCellInfo: is active user");
1093 return mPhone.getAllCellInfo();
1094 } else {
1095 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1096 return null;
1097 }
1098 }
1099
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001100 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 public void setCellInfoListRate(int rateInMillis) {
1102 mPhone.setCellInfoListRate(rateInMillis);
1103 }
1104
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 //
1106 // Internal helper methods.
1107 //
1108
Jake Hambye994d462014-02-03 13:10:13 -08001109 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 boolean ok;
1111
1112 boolean self = Binder.getCallingUid() == Process.myUid();
1113 if (!self) {
1114 // Get the caller's user id then clear the calling identity
1115 // which will be restored in the finally clause.
1116 int callingUser = UserHandle.getCallingUserId();
1117 long ident = Binder.clearCallingIdentity();
1118
1119 try {
1120 // With calling identity cleared the current user is the foreground user.
1121 int foregroundUser = ActivityManager.getCurrentUser();
1122 ok = (foregroundUser == callingUser);
1123 if (DBG_LOC) {
1124 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1125 + " callingUser=" + callingUser + " ok=" + ok);
1126 }
1127 } catch (Exception ex) {
1128 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1129 ok = false;
1130 } finally {
1131 Binder.restoreCallingIdentity(ident);
1132 }
1133 } else {
1134 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1135 ok = true;
1136 }
1137 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1138 return ok;
1139 }
1140
1141 /**
1142 * Make sure the caller has the READ_PHONE_STATE permission.
1143 *
1144 * @throws SecurityException if the caller does not have the required permission
1145 */
1146 private void enforceReadPermission() {
1147 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1148 }
1149
1150 /**
1151 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1152 *
1153 * @throws SecurityException if the caller does not have the required permission
1154 */
1155 private void enforceModifyPermission() {
1156 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1157 }
1158
1159 /**
1160 * Make sure the caller has the CALL_PHONE permission.
1161 *
1162 * @throws SecurityException if the caller does not have the required permission
1163 */
1164 private void enforceCallPermission() {
1165 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1166 }
1167
Shishir Agrawal566b7612013-10-28 14:41:00 -07001168 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001169 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1170 *
1171 * @throws SecurityException if the caller does not have the required permission
1172 */
1173 private void enforcePrivilegedPhoneStatePermission() {
1174 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1175 null);
1176 }
1177
1178 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -07001179 * Make sure the caller has SIM_COMMUNICATION permission.
1180 *
1181 * @throws SecurityException if the caller does not have the required permission.
1182 */
1183 private void enforceSimCommunicationPermission() {
1184 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.SIM_COMMUNICATION, null);
1185 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186
1187 private String createTelUrl(String number) {
1188 if (TextUtils.isEmpty(number)) {
1189 return null;
1190 }
1191
Jake Hambye994d462014-02-03 13:10:13 -08001192 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 }
1194
Ihab Awadf9e92732013-12-05 18:02:52 -08001195 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1197 }
1198
Ihab Awadf9e92732013-12-05 18:02:52 -08001199 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1201 }
1202
1203 public int getActivePhoneType() {
Wink Saville36469e72014-06-11 15:17:00 -07001204 return getActivePhoneTypeUsingSubId(getDefaultSubscription());
1205 }
1206
1207 public int getActivePhoneTypeUsingSubId(long subId) {
1208 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 }
1210
1211 /**
1212 * Returns the CDMA ERI icon index to display
1213 */
1214 public int getCdmaEriIconIndex() {
Wink Saville36469e72014-06-11 15:17:00 -07001215 return getCdmaEriIconIndexUsingSubId(getDefaultSubscription());
1216
1217 }
1218
1219 public int getCdmaEriIconIndexUsingSubId(long subId) {
1220 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 }
1222
1223 /**
1224 * Returns the CDMA ERI icon mode,
1225 * 0 - ON
1226 * 1 - FLASHING
1227 */
1228 public int getCdmaEriIconMode() {
Wink Saville36469e72014-06-11 15:17:00 -07001229 return getCdmaEriIconModeUsingSubId(getDefaultSubscription());
1230 }
1231
1232 public int getCdmaEriIconModeUsingSubId(long subId) {
1233 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001234 }
1235
1236 /**
1237 * Returns the CDMA ERI text,
1238 */
1239 public String getCdmaEriText() {
Wink Saville36469e72014-06-11 15:17:00 -07001240 return getCdmaEriTextUsingSubId(getDefaultSubscription());
1241 }
1242
1243 public String getCdmaEriTextUsingSubId(long subId) {
1244 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001245 }
1246
1247 /**
1248 * Returns true if CDMA provisioning needs to run.
1249 */
1250 public boolean needsOtaServiceProvisioning() {
1251 return mPhone.needsOtaServiceProvisioning();
1252 }
1253
1254 /**
1255 * Returns the unread count of voicemails
1256 */
1257 public int getVoiceMessageCount() {
Wink Saville36469e72014-06-11 15:17:00 -07001258 return getVoiceMessageCountUsingSubId(getDefaultSubscription());
1259 }
1260
1261 /**
1262 * Returns the unread count of voicemails for a subId
1263 */
1264 public int getVoiceMessageCountUsingSubId( long subId) {
1265 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 }
1267
1268 /**
1269 * Returns the data network type
1270 *
1271 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1272 */
1273 @Override
1274 public int getNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001275 return getNetworkTypeUsingSubId(getDefaultSubscription());
1276 }
1277
1278 /**
1279 * Returns the network type for a subId
1280 */
1281 @Override
1282 public int getNetworkTypeUsingSubId(long subId) {
1283 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001284 }
1285
1286 /**
1287 * Returns the data network type
1288 */
1289 @Override
1290 public int getDataNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001291 return getDataNetworkTypeUsingSubId(getDefaultSubscription());
1292 }
1293
1294 /**
1295 * Returns the data network type for a subId
1296 */
1297 @Override
1298 public int getDataNetworkTypeUsingSubId(long subId) {
1299 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 }
1301
1302 /**
1303 * Returns the data network type
1304 */
1305 @Override
1306 public int getVoiceNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001307 return getVoiceNetworkTypeUsingSubId(getDefaultSubscription());
1308 }
1309
1310 /**
1311 * Returns the Voice network type for a subId
1312 */
1313 @Override
1314 public int getVoiceNetworkTypeUsingSubId(long subId) {
1315 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 }
1317
1318 /**
1319 * @return true if a ICC card is present
1320 */
1321 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001322 // FIXME Make changes to pass defaultSimId of type int
1323 return hasIccCardUsingSlotId(getDefaultSubscription());
1324 }
1325
1326 /**
1327 * @return true if a ICC card is present for a slotId
1328 */
1329 public boolean hasIccCardUsingSlotId(long slotId) {
1330 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 }
1332
1333 /**
1334 * Return if the current radio is LTE on CDMA. This
1335 * is a tri-state return value as for a period of time
1336 * the mode may be unknown.
1337 *
1338 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001339 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340 */
1341 public int getLteOnCdmaMode() {
Wink Saville36469e72014-06-11 15:17:00 -07001342 return getLteOnCdmaModeUsingSubId(getDefaultSubscription());
1343 }
1344
1345 public int getLteOnCdmaModeUsingSubId(long subId) {
1346 return getPhone(subId).getLteOnCdmaMode();
1347 }
1348
1349 public void setPhone(Phone phone) {
1350 mPhone = phone;
1351 }
1352
1353 /**
1354 * {@hide}
1355 * Returns Default subId, 0 in the case of single standby.
1356 */
1357 private long getDefaultSubscription() {
1358 return SubscriptionManager.getDefaultSubId();
1359 }
1360
1361 private long getPreferredVoiceSubscription() {
1362 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001364
1365 /**
1366 * @see android.telephony.TelephonyManager.WifiCallingChoices
1367 */
1368 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001369 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1370 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001371 }
1372
1373 /**
1374 * @see android.telephony.TelephonyManager.WifiCallingChoices
1375 */
1376 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001377 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1378 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1379 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001380 }
1381
Sailesh Nepald1e68152013-12-12 19:08:02 -08001382 private static int getWhenToMakeWifiCallsDefaultPreference() {
1383 // TODO(sail): Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001384 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001385 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001386
Shishir Agrawal566b7612013-10-28 14:41:00 -07001387 @Override
1388 public int iccOpenLogicalChannel(String AID) {
1389 enforceSimCommunicationPermission();
1390
1391 if (DBG) log("iccOpenLogicalChannel: " + AID);
1392 Integer channel = (Integer)sendRequest(CMD_OPEN_CHANNEL, AID);
1393 if (DBG) log("iccOpenLogicalChannel: " + channel);
Jake Hambye994d462014-02-03 13:10:13 -08001394 return channel;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001395 }
1396
1397 @Override
1398 public boolean iccCloseLogicalChannel(int channel) {
1399 enforceSimCommunicationPermission();
1400
1401 if (DBG) log("iccCloseLogicalChannel: " + channel);
1402 if (channel < 0) {
1403 return false;
1404 }
Jake Hambye994d462014-02-03 13:10:13 -08001405 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001406 if (DBG) log("iccCloseLogicalChannel: " + success);
1407 return success;
1408 }
1409
1410 @Override
1411 public String iccTransmitApduLogicalChannel(int channel, int cla,
1412 int command, int p1, int p2, int p3, String data) {
1413 enforceSimCommunicationPermission();
1414
1415 if (DBG) {
1416 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1417 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1418 " data=" + data);
1419 }
1420
1421 if (channel < 0) {
1422 return "";
1423 }
1424
1425 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU,
1426 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1427 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1428
1429 // If the payload is null, there was an error. Indicate that by returning
1430 // an empty string.
1431 if (response.payload == null) {
1432 return "";
1433 }
1434
1435 // Append the returned status code to the end of the response payload.
1436 String s = Integer.toHexString(
1437 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1438 s = IccUtils.bytesToHexString(response.payload) + s;
1439 return s;
1440 }
Jake Hambye994d462014-02-03 13:10:13 -08001441
Evan Charltonc66da362014-05-16 14:06:40 -07001442 @Override
1443 public String sendEnvelopeWithStatus(String content) {
1444 enforceSimCommunicationPermission();
1445
1446 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1447 if (response.payload == null) {
1448 return "";
1449 }
1450
1451 // Append the returned status code to the end of the response payload.
1452 String s = Integer.toHexString(
1453 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1454 s = IccUtils.bytesToHexString(response.payload) + s;
1455 return s;
1456 }
1457
Jake Hambye994d462014-02-03 13:10:13 -08001458 /**
1459 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1460 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1461 *
1462 * @param itemID the ID of the item to read
1463 * @return the NV item as a String, or null on error.
1464 */
1465 @Override
1466 public String nvReadItem(int itemID) {
1467 enforceModifyPermission();
1468 if (DBG) log("nvReadItem: item " + itemID);
1469 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1470 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1471 return value;
1472 }
1473
1474 /**
1475 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1476 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1477 *
1478 * @param itemID the ID of the item to read
1479 * @param itemValue the value to write, as a String
1480 * @return true on success; false on any failure
1481 */
1482 @Override
1483 public boolean nvWriteItem(int itemID, String itemValue) {
1484 enforceModifyPermission();
1485 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1486 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1487 new Pair<Integer, String>(itemID, itemValue));
1488 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1489 return success;
1490 }
1491
1492 /**
1493 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1494 * Used for device configuration by some CDMA operators.
1495 *
1496 * @param preferredRoamingList byte array containing the new PRL
1497 * @return true on success; false on any failure
1498 */
1499 @Override
1500 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
1501 enforceModifyPermission();
1502 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1503 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1504 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1505 return success;
1506 }
1507
1508 /**
1509 * Perform the specified type of NV config reset.
1510 * Used for device configuration by some CDMA operators.
1511 *
1512 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1513 * @return true on success; false on any failure
1514 */
1515 @Override
1516 public boolean nvResetConfig(int resetType) {
1517 enforceModifyPermission();
1518 if (DBG) log("nvResetConfig: type " + resetType);
1519 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1520 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1521 return success;
1522 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001523
1524 /**
Wink Saville36469e72014-06-11 15:17:00 -07001525 * {@hide}
1526 * Returns Default sim, 0 in the case of single standby.
1527 */
1528 public int getDefaultSim() {
1529 //TODO Need to get it from Telephony Devcontroller
1530 return 0;
1531 }
1532
1533 public String[] getPcscfAddress() {
1534 enforceReadPermission();
1535 return mPhone.getPcscfAddress();
1536 }
1537
1538 public void setImsRegistrationState(boolean registered) {
1539 enforceModifyPermission();
1540 mPhone.setImsRegistrationState(registered);
1541 }
1542
1543 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001544 * Get the preferred network type.
1545 * Used for device configuration by some CDMA operators.
1546 *
1547 * @return the preferred network type, defined in RILConstants.java.
1548 */
1549 @Override
1550 public int getPreferredNetworkType() {
1551 enforceModifyPermission();
1552 if (DBG) log("getPreferredNetworkType");
1553 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1554 int networkType = (result != null ? result[0] : -1);
1555 if (DBG) log("getPreferredNetworkType: " + networkType);
1556 return networkType;
1557 }
1558
1559 /**
1560 * Set the preferred network type.
1561 * Used for device configuration by some CDMA operators.
1562 *
1563 * @param networkType the preferred network type, defined in RILConstants.java.
1564 * @return true on success; false on any failure.
1565 */
1566 @Override
1567 public boolean setPreferredNetworkType(int networkType) {
1568 enforceModifyPermission();
1569 if (DBG) log("setPreferredNetworkType: type " + networkType);
1570 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1571 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
1572 return success;
1573 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001574
1575 /**
1576 * Set mobile data enabled
1577 * Used by the user through settings etc to turn on/off mobile data
1578 *
1579 * @param enable {@code true} turn turn data on, else {@code false}
1580 */
1581 @Override
1582 public void setDataEnabled(boolean enable) {
1583 enforceModifyPermission();
1584 mPhone.setDataEnabled(enable);
1585 }
1586
1587 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001588 * Get whether mobile data is enabled.
1589 *
1590 * Note that this used to be available from ConnectivityService, gated by
1591 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1592 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001593 *
1594 * @return {@code true} if data is enabled else {@code false}
1595 */
1596 @Override
1597 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001598 try {
1599 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1600 null);
1601 } catch (Exception e) {
1602 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1603 null);
1604 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001605 return mPhone.getDataEnabled();
1606 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001607}