blob: 335ec95b8cccff2ae69c6c35f0586d4a0b9daaf5 [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;
Junda Liu787bc7e2014-06-30 13:38:02 -0700108 private static final int CMD_SET_CDMA_SUBSCRIPTION = 27;
109 private static final int EVENT_SET_CDMA_SUBSCRIPTION_DONE = 28;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110
111 /** The singleton instance. */
112 private static PhoneInterfaceManager sInstance;
113
114 PhoneGlobals mApp;
115 Phone mPhone;
116 CallManager mCM;
117 AppOpsManager mAppOps;
118 MainThreadHandler mMainThreadHandler;
Santos Cordon406c0342013-08-28 00:07:47 -0700119 CallHandlerServiceProxy mCallHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120
121 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700122 * A request object to use for transmitting data to an ICC.
123 */
124 private static final class IccAPDUArgument {
125 public int channel, cla, command, p1, p2, p3;
126 public String data;
127
128 public IccAPDUArgument(int channel, int cla, int command,
129 int p1, int p2, int p3, String data) {
130 this.channel = channel;
131 this.cla = cla;
132 this.command = command;
133 this.p1 = p1;
134 this.p2 = p2;
135 this.p3 = p3;
136 this.data = data;
137 }
138 }
139
140 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
142 * request after sending. The main thread will notify the request when it is complete.
143 */
144 private static final class MainThreadRequest {
145 /** The argument to use for the request */
146 public Object argument;
147 /** The result of the request that is run on the main thread */
148 public Object result;
149
150 public MainThreadRequest(Object argument) {
151 this.argument = argument;
152 }
153 }
154
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800155 private static final class IncomingThirdPartyCallArgs {
156 public final ComponentName component;
157 public final String callId;
158 public final String callerDisplayName;
159
160 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
161 String callerDisplayName) {
162 this.component = component;
163 this.callId = callId;
164 this.callerDisplayName = callerDisplayName;
165 }
166 }
167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 /**
169 * A handler that processes messages on the main thread in the phone process. Since many
170 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
171 * inbound binder threads to the main thread in the phone process. The Binder thread
172 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
173 * on, which will be notified when the operation completes and will contain the result of the
174 * request.
175 *
176 * <p>If a MainThreadRequest object is provided in the msg.obj field,
177 * note that request.result must be set to something non-null for the calling thread to
178 * unblock.
179 */
180 private final class MainThreadHandler extends Handler {
181 @Override
182 public void handleMessage(Message msg) {
183 MainThreadRequest request;
184 Message onCompleted;
185 AsyncResult ar;
186
187 switch (msg.what) {
188 case CMD_HANDLE_PIN_MMI:
189 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800190 request.result = mPhone.handlePinMmi((String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700191 // Wake up the requesting thread
192 synchronized (request) {
193 request.notifyAll();
194 }
195 break;
196
197 case CMD_HANDLE_NEIGHBORING_CELL:
198 request = (MainThreadRequest) msg.obj;
199 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
200 request);
201 mPhone.getNeighboringCids(onCompleted);
202 break;
203
204 case EVENT_NEIGHBORING_CELL_DONE:
205 ar = (AsyncResult) msg.obj;
206 request = (MainThreadRequest) ar.userObj;
207 if (ar.exception == null && ar.result != null) {
208 request.result = ar.result;
209 } else {
210 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800211 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 }
213 // Wake up the requesting thread
214 synchronized (request) {
215 request.notifyAll();
216 }
217 break;
218
219 case CMD_ANSWER_RINGING_CALL:
220 answerRingingCallInternal();
221 break;
222
223 case CMD_SILENCE_RINGER:
224 silenceRingerInternal();
225 break;
226
227 case CMD_END_CALL:
228 request = (MainThreadRequest) msg.obj;
Jake Hambye994d462014-02-03 13:10:13 -0800229 boolean hungUp;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 int phoneType = mPhone.getPhoneType();
231 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
232 // CDMA: If the user presses the Power button we treat it as
233 // ending the complete call session
234 hungUp = PhoneUtils.hangupRingingAndActive(mPhone);
235 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
236 // GSM: End the call as per the Phone state
237 hungUp = PhoneUtils.hangup(mCM);
238 } else {
239 throw new IllegalStateException("Unexpected phone type: " + phoneType);
240 }
241 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
242 request.result = hungUp;
243 // Wake up the requesting thread
244 synchronized (request) {
245 request.notifyAll();
246 }
247 break;
248
Shishir Agrawal566b7612013-10-28 14:41:00 -0700249 case CMD_TRANSMIT_APDU:
250 request = (MainThreadRequest) msg.obj;
251 IccAPDUArgument argument = (IccAPDUArgument) request.argument;
252 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_DONE, request);
253 UiccController.getInstance().getUiccCard().iccTransmitApduLogicalChannel(
254 argument.channel, argument.cla, argument.command,
255 argument.p1, argument.p2, argument.p3, argument.data,
256 onCompleted);
257 break;
258
259 case EVENT_TRANSMIT_APDU_DONE:
260 ar = (AsyncResult) msg.obj;
261 request = (MainThreadRequest) ar.userObj;
262 if (ar.exception == null && ar.result != null) {
263 request.result = ar.result;
264 } else {
265 request.result = new IccIoResult(0x6F, 0, (byte[])null);
266 if (ar.result == null) {
267 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800268 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700269 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800270 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700271 } else {
272 loge("iccTransmitApduLogicalChannel: Unknown exception");
273 }
274 }
275 synchronized (request) {
276 request.notifyAll();
277 }
278 break;
279
Derek Tan4d5e5c12014-02-04 11:54:58 -0800280 case CMD_SEND_ENVELOPE:
281 request = (MainThreadRequest) msg.obj;
282 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700283 UiccController.getInstance().getUiccCard().sendEnvelopeWithStatus(
Derek Tan4d5e5c12014-02-04 11:54:58 -0800284 (String)request.argument, onCompleted);
285 break;
286
287 case EVENT_SEND_ENVELOPE_DONE:
288 ar = (AsyncResult) msg.obj;
289 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700290 if (ar.exception == null && ar.result != null) {
291 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800292 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700293 request.result = new IccIoResult(0x6F, 0, (byte[])null);
294 if (ar.result == null) {
295 loge("sendEnvelopeWithStatus: Empty response");
296 } else if (ar.exception instanceof CommandException) {
297 loge("sendEnvelopeWithStatus: CommandException: " +
298 ar.exception);
299 } else {
300 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
301 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800302 }
303 synchronized (request) {
304 request.notifyAll();
305 }
306 break;
307
Shishir Agrawal566b7612013-10-28 14:41:00 -0700308 case CMD_OPEN_CHANNEL:
309 request = (MainThreadRequest) msg.obj;
310 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
311 UiccController.getInstance().getUiccCard().iccOpenLogicalChannel(
312 (String)request.argument, onCompleted);
313 break;
314
315 case EVENT_OPEN_CHANNEL_DONE:
316 ar = (AsyncResult) msg.obj;
317 request = (MainThreadRequest) ar.userObj;
318 if (ar.exception == null && ar.result != null) {
Jake Hambye994d462014-02-03 13:10:13 -0800319 request.result = ((int[]) ar.result)[0];
Shishir Agrawal566b7612013-10-28 14:41:00 -0700320 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800321 request.result = -1;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700322 if (ar.result == null) {
323 loge("iccOpenLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800324 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700325 loge("iccOpenLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800326 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700327 } else {
328 loge("iccOpenLogicalChannel: Unknown exception");
329 }
330 }
331 synchronized (request) {
332 request.notifyAll();
333 }
334 break;
335
336 case CMD_CLOSE_CHANNEL:
337 request = (MainThreadRequest) msg.obj;
338 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE,
339 request);
340 UiccController.getInstance().getUiccCard().iccCloseLogicalChannel(
Jake Hambye994d462014-02-03 13:10:13 -0800341 (Integer) request.argument,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700342 onCompleted);
343 break;
344
345 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800346 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
347 break;
348
349 case CMD_NV_READ_ITEM:
350 request = (MainThreadRequest) msg.obj;
351 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
352 mPhone.nvReadItem((Integer) request.argument, onCompleted);
353 break;
354
355 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700356 ar = (AsyncResult) msg.obj;
357 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800358 if (ar.exception == null && ar.result != null) {
359 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700360 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800361 request.result = "";
362 if (ar.result == null) {
363 loge("nvReadItem: Empty response");
364 } else if (ar.exception instanceof CommandException) {
365 loge("nvReadItem: CommandException: " +
366 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700367 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800368 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700369 }
370 }
371 synchronized (request) {
372 request.notifyAll();
373 }
374 break;
375
Jake Hambye994d462014-02-03 13:10:13 -0800376 case CMD_NV_WRITE_ITEM:
377 request = (MainThreadRequest) msg.obj;
378 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
379 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
380 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
381 break;
382
383 case EVENT_NV_WRITE_ITEM_DONE:
384 handleNullReturnEvent(msg, "nvWriteItem");
385 break;
386
387 case CMD_NV_WRITE_CDMA_PRL:
388 request = (MainThreadRequest) msg.obj;
389 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
390 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
391 break;
392
393 case EVENT_NV_WRITE_CDMA_PRL_DONE:
394 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
395 break;
396
397 case CMD_NV_RESET_CONFIG:
398 request = (MainThreadRequest) msg.obj;
399 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
400 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
401 break;
402
403 case EVENT_NV_RESET_CONFIG_DONE:
404 handleNullReturnEvent(msg, "nvResetConfig");
405 break;
406
Jake Hamby7c27be32014-03-03 13:25:59 -0800407 case CMD_GET_PREFERRED_NETWORK_TYPE:
408 request = (MainThreadRequest) msg.obj;
409 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
410 mPhone.getPreferredNetworkType(onCompleted);
411 break;
412
413 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
414 ar = (AsyncResult) msg.obj;
415 request = (MainThreadRequest) ar.userObj;
416 if (ar.exception == null && ar.result != null) {
417 request.result = ar.result; // Integer
418 } else {
419 request.result = -1;
420 if (ar.result == null) {
421 loge("getPreferredNetworkType: Empty response");
422 } else if (ar.exception instanceof CommandException) {
423 loge("getPreferredNetworkType: CommandException: " +
424 ar.exception);
425 } else {
426 loge("getPreferredNetworkType: Unknown exception");
427 }
428 }
429 synchronized (request) {
430 request.notifyAll();
431 }
432 break;
433
434 case CMD_SET_PREFERRED_NETWORK_TYPE:
435 request = (MainThreadRequest) msg.obj;
436 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
437 int networkType = (Integer) request.argument;
438 mPhone.setPreferredNetworkType(networkType, onCompleted);
439 break;
440
441 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
442 handleNullReturnEvent(msg, "setPreferredNetworkType");
443 break;
444
Junda Liu787bc7e2014-06-30 13:38:02 -0700445 case CMD_SET_CDMA_SUBSCRIPTION:
446 request = (MainThreadRequest) msg.obj;
447 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_DONE, request);
448 int subscriptionType = (Integer) request.argument;
449 mPhone.setCdmaSubscription(subscriptionType, onCompleted);
450 break;
451
452 case EVENT_SET_CDMA_SUBSCRIPTION_DONE:
453 handleNullReturnEvent(msg, "setCdmaSubscription");
454 break;
455
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456 default:
457 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
458 break;
459 }
460 }
Jake Hambye994d462014-02-03 13:10:13 -0800461
462 private void handleNullReturnEvent(Message msg, String command) {
463 AsyncResult ar = (AsyncResult) msg.obj;
464 MainThreadRequest request = (MainThreadRequest) ar.userObj;
465 if (ar.exception == null) {
466 request.result = true;
467 } else {
468 request.result = false;
469 if (ar.exception instanceof CommandException) {
470 loge(command + ": CommandException: " + ar.exception);
471 } else {
472 loge(command + ": Unknown exception");
473 }
474 }
475 synchronized (request) {
476 request.notifyAll();
477 }
478 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 }
480
481 /**
482 * Posts the specified command to be executed on the main thread,
483 * waits for the request to complete, and returns the result.
484 * @see #sendRequestAsync
485 */
486 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700487 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700488 }
489
490 /**
491 * Posts the specified command to be executed on the main thread,
492 * waits for the request to complete, and returns the result.
493 * @see #sendRequestAsync
494 */
495 private Object sendRequest(int command, Object argument, Object argument2) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700496 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
497 throw new RuntimeException("This method will deadlock if called from the main thread.");
498 }
499
500 MainThreadRequest request = new MainThreadRequest(argument);
501 Message msg = mMainThreadHandler.obtainMessage(command, request);
502 msg.sendToTarget();
503
504 // Wait for the request to complete
505 synchronized (request) {
506 while (request.result == null) {
507 try {
508 request.wait();
509 } catch (InterruptedException e) {
510 // Do nothing, go back and wait until the request is complete
511 }
512 }
513 }
514 return request.result;
515 }
516
517 /**
518 * Asynchronous ("fire and forget") version of sendRequest():
519 * Posts the specified command to be executed on the main thread, and
520 * returns immediately.
521 * @see #sendRequest
522 */
523 private void sendRequestAsync(int command) {
524 mMainThreadHandler.sendEmptyMessage(command);
525 }
526
527 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700528 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
529 * @see {@link #sendRequest(int,Object)}
530 */
531 private void sendRequestAsync(int command, Object argument) {
532 MainThreadRequest request = new MainThreadRequest(argument);
533 Message msg = mMainThreadHandler.obtainMessage(command, request);
534 msg.sendToTarget();
535 }
536
537 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700538 * Initialize the singleton PhoneInterfaceManager instance.
539 * This is only done once, at startup, from PhoneApp.onCreate().
540 */
Santos Cordon406c0342013-08-28 00:07:47 -0700541 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone,
Santos Cordon117fee72014-05-16 17:56:12 -0700542 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700543 synchronized (PhoneInterfaceManager.class) {
544 if (sInstance == null) {
Santos Cordon117fee72014-05-16 17:56:12 -0700545 sInstance = new PhoneInterfaceManager(app, phone, callHandlerService);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700546 } else {
547 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
548 }
549 return sInstance;
550 }
551 }
552
553 /** Private constructor; @see init() */
Santos Cordon406c0342013-08-28 00:07:47 -0700554 private PhoneInterfaceManager(PhoneGlobals app, Phone phone,
Santos Cordon117fee72014-05-16 17:56:12 -0700555 CallHandlerServiceProxy callHandlerService) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700556 mApp = app;
557 mPhone = phone;
558 mCM = PhoneGlobals.getInstance().mCM;
559 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
560 mMainThreadHandler = new MainThreadHandler();
Santos Cordon406c0342013-08-28 00:07:47 -0700561 mCallHandlerService = callHandlerService;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700562 publish();
563 }
564
565 private void publish() {
566 if (DBG) log("publish: " + this);
567
568 ServiceManager.addService("phone", this);
569 }
570
Wink Saville36469e72014-06-11 15:17:00 -0700571 // returns phone associated with the subId.
572 // getPhone(0) returns default phone in single SIM mode.
573 private Phone getPhone(long subId) {
574 // FIXME: hack for the moment
575 return mPhone;
576 // return PhoneUtils.getPhoneUsingSubId(subId);
577 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 //
579 // Implementation of the ITelephony interface.
580 //
581
582 public void dial(String number) {
Wink Saville36469e72014-06-11 15:17:00 -0700583 dialUsingSubId(getPreferredVoiceSubscription(), number);
584 }
585
586 public void dialUsingSubId(long subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 if (DBG) log("dial: " + number);
588 // No permission check needed here: This is just a wrapper around the
589 // ACTION_DIAL intent, which is available to any app since it puts up
590 // the UI before it does anything.
591
592 String url = createTelUrl(number);
593 if (url == null) {
594 return;
595 }
596
597 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700598 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
600 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
601 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Wink Saville36469e72014-06-11 15:17:00 -0700602 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700603 mApp.startActivity(intent);
604 }
605 }
606
607 public void call(String callingPackage, String number) {
Wink Saville36469e72014-06-11 15:17:00 -0700608 callUsingSubId(getPreferredVoiceSubscription(), callingPackage, number);
609 }
610
611 public void callUsingSubId(long subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 if (DBG) log("call: " + number);
613
614 // This is just a wrapper around the ACTION_CALL intent, but we still
615 // need to do a permission check since we're calling startActivity()
616 // from the context of the phone app.
617 enforceCallPermission();
618
619 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
620 != AppOpsManager.MODE_ALLOWED) {
621 return;
622 }
623
624 String url = createTelUrl(number);
625 if (url == null) {
626 return;
627 }
628
629 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700630 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700631 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
632 mApp.startActivity(intent);
633 }
634
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 /**
636 * End a call based on call state
637 * @return true is a call was ended
638 */
639 public boolean endCall() {
Wink Saville36469e72014-06-11 15:17:00 -0700640 return endCallUsingSubId(getDefaultSubscription());
641 }
642
643 /**
644 * End a call based on the call state of the subId
645 * @return true is a call was ended
646 */
647 public boolean endCallUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700648 enforceCallPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700649 return (Boolean) sendRequest(CMD_END_CALL, subId, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 }
651
652 public void answerRingingCall() {
Wink Saville36469e72014-06-11 15:17:00 -0700653 answerRingingCallUsingSubId(getDefaultSubscription());
654 }
655
656 public void answerRingingCallUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 if (DBG) log("answerRingingCall...");
658 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
659 // but that can probably wait till the big TelephonyManager API overhaul.
660 // For now, protect this call with the MODIFY_PHONE_STATE permission.
661 enforceModifyPermission();
662 sendRequestAsync(CMD_ANSWER_RINGING_CALL);
663 }
664
665 /**
666 * Make the actual telephony calls to implement answerRingingCall().
667 * This should only be called from the main thread of the Phone app.
668 * @see #answerRingingCall
669 *
670 * TODO: it would be nice to return true if we answered the call, or
671 * false if there wasn't actually a ringing incoming call, or some
672 * other error occurred. (In other words, pass back the return value
673 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
674 * But that would require calling this method via sendRequest() rather
675 * than sendRequestAsync(), and right now we don't actually *need* that
676 * return value, so let's just return void for now.
677 */
678 private void answerRingingCallInternal() {
679 final boolean hasRingingCall = !mPhone.getRingingCall().isIdle();
680 if (hasRingingCall) {
681 final boolean hasActiveCall = !mPhone.getForegroundCall().isIdle();
682 final boolean hasHoldingCall = !mPhone.getBackgroundCall().isIdle();
683 if (hasActiveCall && hasHoldingCall) {
684 // Both lines are in use!
685 // TODO: provide a flag to let the caller specify what
686 // policy to use if both lines are in use. (The current
687 // behavior is hardwired to "answer incoming, end ongoing",
688 // which is how the CALL button is specced to behave.)
689 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
690 return;
691 } else {
692 // answerCall() will automatically hold the current active
693 // call, if there is one.
694 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
695 return;
696 }
697 } else {
698 // No call was ringing.
699 return;
700 }
701 }
702
703 public void silenceRinger() {
704 if (DBG) log("silenceRinger...");
705 // TODO: find a more appropriate permission to check here.
706 // (That can probably wait till the big TelephonyManager API overhaul.
707 // For now, protect this call with the MODIFY_PHONE_STATE permission.)
708 enforceModifyPermission();
709 sendRequestAsync(CMD_SILENCE_RINGER);
710 }
711
712 /**
713 * Internal implemenation of silenceRinger().
714 * This should only be called from the main thread of the Phone app.
715 * @see #silenceRinger
716 */
717 private void silenceRingerInternal() {
718 if ((mCM.getState() == PhoneConstants.State.RINGING)
719 && mApp.notifier.isRinging()) {
720 // Ringer is actually playing, so silence it.
721 if (DBG) log("silenceRingerInternal: silencing...");
722 mApp.notifier.silenceRinger();
723 }
724 }
725
726 public boolean isOffhook() {
Wink Saville36469e72014-06-11 15:17:00 -0700727 return isOffhookUsingSubId(getDefaultSubscription());
728 }
729
730 public boolean isOffhookUsingSubId(long subId) {
731 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 }
733
734 public boolean isRinging() {
Wink Saville36469e72014-06-11 15:17:00 -0700735 return (isRingingUsingSubId(getDefaultSubscription()));
736 }
737
738 public boolean isRingingUsingSubId(long subId) {
739 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 }
741
742 public boolean isIdle() {
Wink Saville36469e72014-06-11 15:17:00 -0700743 return isIdleUsingSubId(getDefaultSubscription());
744 }
745
746 public boolean isIdleUsingSubId(long subId) {
747 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700748 }
749
750 public boolean isSimPinEnabled() {
751 enforceReadPermission();
752 return (PhoneGlobals.getInstance().isSimPinEnabled());
753 }
754
755 public boolean supplyPin(String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700756 return supplyPinUsingSubId(getDefaultSubscription(), pin);
757 }
758
759 public boolean supplyPinUsingSubId(long subId, String pin) {
760 int [] resultArray = supplyPinReportResultUsingSubId(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700761 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
762 }
763
764 public boolean supplyPuk(String puk, String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700765 return supplyPukUsingSubId(getDefaultSubscription(), puk, pin);
766 }
767
768 public boolean supplyPukUsingSubId(long subId, String puk, String pin) {
769 int [] resultArray = supplyPukReportResultUsingSubId(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700770 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
771 }
772
773 /** {@hide} */
774 public int[] supplyPinReportResult(String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700775 return supplyPinReportResultUsingSubId(getDefaultSubscription(), pin);
776 }
777
778 public int[] supplyPinReportResultUsingSubId(long subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700780 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700781 checkSimPin.start();
782 return checkSimPin.unlockSim(null, pin);
783 }
784
Wink Saville9de0f752013-10-22 19:04:03 -0700785 /** {@hide} */
786 public int[] supplyPukReportResult(String puk, String pin) {
Wink Saville36469e72014-06-11 15:17:00 -0700787 return supplyPukReportResultUsingSubId(getDefaultSubscription(), puk, pin);
788 }
789
790 public int[] supplyPukReportResultUsingSubId(long subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700792 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793 checkSimPuk.start();
794 return checkSimPuk.unlockSim(puk, pin);
795 }
796
797 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700798 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 * a synchronous one.
800 */
801 private static class UnlockSim extends Thread {
802
803 private final IccCard mSimCard;
804
805 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700806 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
807 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808
809 // For replies from SimCard interface
810 private Handler mHandler;
811
812 // For async handler to identify request type
813 private static final int SUPPLY_PIN_COMPLETE = 100;
814
815 public UnlockSim(IccCard simCard) {
816 mSimCard = simCard;
817 }
818
819 @Override
820 public void run() {
821 Looper.prepare();
822 synchronized (UnlockSim.this) {
823 mHandler = new Handler() {
824 @Override
825 public void handleMessage(Message msg) {
826 AsyncResult ar = (AsyncResult) msg.obj;
827 switch (msg.what) {
828 case SUPPLY_PIN_COMPLETE:
829 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
830 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -0700831 mRetryCount = msg.arg1;
832 if (ar.exception != null) {
833 if (ar.exception instanceof CommandException &&
834 ((CommandException)(ar.exception)).getCommandError()
835 == CommandException.Error.PASSWORD_INCORRECT) {
836 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
837 } else {
838 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
839 }
840 } else {
841 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
842 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 mDone = true;
844 UnlockSim.this.notifyAll();
845 }
846 break;
847 }
848 }
849 };
850 UnlockSim.this.notifyAll();
851 }
852 Looper.loop();
853 }
854
855 /*
856 * Use PIN or PUK to unlock SIM card
857 *
858 * If PUK is null, unlock SIM card with PIN
859 *
860 * If PUK is not null, unlock SIM card with PUK and set PIN code
861 */
Wink Saville9de0f752013-10-22 19:04:03 -0700862 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863
864 while (mHandler == null) {
865 try {
866 wait();
867 } catch (InterruptedException e) {
868 Thread.currentThread().interrupt();
869 }
870 }
871 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
872
873 if (puk == null) {
874 mSimCard.supplyPin(pin, callback);
875 } else {
876 mSimCard.supplyPuk(puk, pin, callback);
877 }
878
879 while (!mDone) {
880 try {
881 Log.d(LOG_TAG, "wait for done");
882 wait();
883 } catch (InterruptedException e) {
884 // Restore the interrupted status
885 Thread.currentThread().interrupt();
886 }
887 }
888 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -0700889 int[] resultArray = new int[2];
890 resultArray[0] = mResult;
891 resultArray[1] = mRetryCount;
892 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 }
894 }
895
896 public void updateServiceLocation() {
Wink Saville36469e72014-06-11 15:17:00 -0700897 updateServiceLocationUsingSubId(getDefaultSubscription());
898
899 }
900
901 public void updateServiceLocationUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 // No permission check needed here: this call is harmless, and it's
903 // needed for the ServiceState.requestStateUpdate() call (which is
904 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -0700905 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 }
907
908 public boolean isRadioOn() {
Wink Saville36469e72014-06-11 15:17:00 -0700909 return isRadioOnUsingSubId(getDefaultSubscription());
910 }
911
912 public boolean isRadioOnUsingSubId(long subId) {
913 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 }
915
916 public void toggleRadioOnOff() {
Wink Saville36469e72014-06-11 15:17:00 -0700917 toggleRadioOnOffUsingSubId(getDefaultSubscription());
918
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 }
Wink Saville36469e72014-06-11 15:17:00 -0700920
921 public void toggleRadioOnOffUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700923 getPhone(subId).setRadioPower(!isRadioOnUsingSubId(subId));
924 }
925
926 public boolean setRadio(boolean turnOn) {
927 return setRadioUsingSubId(getDefaultSubscription(), turnOn);
928 }
929
930 public boolean setRadioUsingSubId(long subId, boolean turnOn) {
931 enforceModifyPermission();
932 if ((getPhone(subId).getServiceState().getState() !=
933 ServiceState.STATE_POWER_OFF) != turnOn) {
934 toggleRadioOnOffUsingSubId(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 }
936 return true;
937 }
Wink Saville36469e72014-06-11 15:17:00 -0700938
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 public boolean setRadioPower(boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -0700940 return setRadioPowerUsingSubId(getDefaultSubscription(), turnOn);
941 }
942
943 public boolean setRadioPowerUsingSubId(long subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700945 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 return true;
947 }
948
Wink Saville36469e72014-06-11 15:17:00 -0700949 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 public boolean enableDataConnectivity() {
951 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700952 long subId = SubscriptionManager.getDefaultDataSubId();
953 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700954 return true;
955 }
956
Wink Saville36469e72014-06-11 15:17:00 -0700957 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 public boolean disableDataConnectivity() {
959 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700960 long subId = SubscriptionManager.getDefaultDataSubId();
961 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 return true;
963 }
964
Wink Saville36469e72014-06-11 15:17:00 -0700965 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 public boolean isDataConnectivityPossible() {
Wink Saville36469e72014-06-11 15:17:00 -0700967 long subId = SubscriptionManager.getDefaultDataSubId();
968 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 }
970
971 public boolean handlePinMmi(String dialString) {
Wink Saville36469e72014-06-11 15:17:00 -0700972 return handlePinMmiUsingSubId(getDefaultSubscription(), dialString);
973 }
974
975 public boolean handlePinMmiUsingSubId(long subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700977 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 }
979
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 public int getCallState() {
Wink Saville36469e72014-06-11 15:17:00 -0700981 return getCallStateUsingSubId(getDefaultSubscription());
982 }
983
984 public int getCallStateUsingSubId(long subId) {
985 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 }
987
988 public int getDataState() {
Wink Saville36469e72014-06-11 15:17:00 -0700989 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
990 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 }
992
993 public int getDataActivity() {
Wink Saville36469e72014-06-11 15:17:00 -0700994 Phone phone = getPhone(SubscriptionManager.getDefaultDataSubId());
995 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 }
997
998 @Override
999 public Bundle getCellLocation() {
1000 try {
1001 mApp.enforceCallingOrSelfPermission(
1002 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1003 } catch (SecurityException e) {
1004 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1005 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1006 // is the weaker precondition
1007 mApp.enforceCallingOrSelfPermission(
1008 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1009 }
1010
Jake Hambye994d462014-02-03 13:10:13 -08001011 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 if (DBG_LOC) log("getCellLocation: is active user");
1013 Bundle data = new Bundle();
1014 mPhone.getCellLocation().fillInNotifierBundle(data);
1015 return data;
1016 } else {
1017 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1018 return null;
1019 }
1020 }
1021
1022 @Override
1023 public void enableLocationUpdates() {
Wink Saville36469e72014-06-11 15:17:00 -07001024 enableLocationUpdatesUsingSubId(getDefaultSubscription());
1025 }
1026
1027 public void enableLocationUpdatesUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 mApp.enforceCallingOrSelfPermission(
1029 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001030 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 }
1032
1033 @Override
1034 public void disableLocationUpdates() {
Wink Saville36469e72014-06-11 15:17:00 -07001035 disableLocationUpdatesUsingSubId(getDefaultSubscription());
1036 }
1037
1038 public void disableLocationUpdatesUsingSubId(long subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001039 mApp.enforceCallingOrSelfPermission(
1040 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001041 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001042 }
1043
1044 @Override
1045 @SuppressWarnings("unchecked")
1046 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1047 try {
1048 mApp.enforceCallingOrSelfPermission(
1049 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1050 } catch (SecurityException e) {
1051 // If we have ACCESS_FINE_LOCATION permission, skip the check
1052 // for ACCESS_COARSE_LOCATION
1053 // A failure should throw the SecurityException from
1054 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1055 mApp.enforceCallingOrSelfPermission(
1056 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1057 }
1058
1059 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1060 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1061 return null;
1062 }
Jake Hambye994d462014-02-03 13:10:13 -08001063 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1065
1066 ArrayList<NeighboringCellInfo> cells = null;
1067
1068 try {
1069 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001070 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001072 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 }
1074 return cells;
1075 } else {
1076 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1077 return null;
1078 }
1079 }
1080
1081
1082 @Override
1083 public List<CellInfo> getAllCellInfo() {
1084 try {
1085 mApp.enforceCallingOrSelfPermission(
1086 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1087 } catch (SecurityException e) {
1088 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1089 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1090 // is the weaker precondition
1091 mApp.enforceCallingOrSelfPermission(
1092 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1093 }
1094
Jake Hambye994d462014-02-03 13:10:13 -08001095 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 if (DBG_LOC) log("getAllCellInfo: is active user");
1097 return mPhone.getAllCellInfo();
1098 } else {
1099 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1100 return null;
1101 }
1102 }
1103
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001104 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 public void setCellInfoListRate(int rateInMillis) {
1106 mPhone.setCellInfoListRate(rateInMillis);
1107 }
1108
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 //
1110 // Internal helper methods.
1111 //
1112
Jake Hambye994d462014-02-03 13:10:13 -08001113 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 boolean ok;
1115
1116 boolean self = Binder.getCallingUid() == Process.myUid();
1117 if (!self) {
1118 // Get the caller's user id then clear the calling identity
1119 // which will be restored in the finally clause.
1120 int callingUser = UserHandle.getCallingUserId();
1121 long ident = Binder.clearCallingIdentity();
1122
1123 try {
1124 // With calling identity cleared the current user is the foreground user.
1125 int foregroundUser = ActivityManager.getCurrentUser();
1126 ok = (foregroundUser == callingUser);
1127 if (DBG_LOC) {
1128 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1129 + " callingUser=" + callingUser + " ok=" + ok);
1130 }
1131 } catch (Exception ex) {
1132 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1133 ok = false;
1134 } finally {
1135 Binder.restoreCallingIdentity(ident);
1136 }
1137 } else {
1138 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1139 ok = true;
1140 }
1141 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1142 return ok;
1143 }
1144
1145 /**
1146 * Make sure the caller has the READ_PHONE_STATE permission.
1147 *
1148 * @throws SecurityException if the caller does not have the required permission
1149 */
1150 private void enforceReadPermission() {
1151 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1152 }
1153
1154 /**
1155 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1156 *
1157 * @throws SecurityException if the caller does not have the required permission
1158 */
1159 private void enforceModifyPermission() {
1160 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1161 }
1162
1163 /**
1164 * Make sure the caller has the CALL_PHONE permission.
1165 *
1166 * @throws SecurityException if the caller does not have the required permission
1167 */
1168 private void enforceCallPermission() {
1169 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1170 }
1171
Shishir Agrawal566b7612013-10-28 14:41:00 -07001172 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001173 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1174 *
1175 * @throws SecurityException if the caller does not have the required permission
1176 */
1177 private void enforcePrivilegedPhoneStatePermission() {
1178 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1179 null);
1180 }
1181
1182 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -07001183 * Make sure the caller has SIM_COMMUNICATION permission.
1184 *
1185 * @throws SecurityException if the caller does not have the required permission.
1186 */
1187 private void enforceSimCommunicationPermission() {
1188 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.SIM_COMMUNICATION, null);
1189 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190
1191 private String createTelUrl(String number) {
1192 if (TextUtils.isEmpty(number)) {
1193 return null;
1194 }
1195
Jake Hambye994d462014-02-03 13:10:13 -08001196 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 }
1198
Ihab Awadf9e92732013-12-05 18:02:52 -08001199 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1201 }
1202
Ihab Awadf9e92732013-12-05 18:02:52 -08001203 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1205 }
1206
1207 public int getActivePhoneType() {
Wink Saville36469e72014-06-11 15:17:00 -07001208 return getActivePhoneTypeUsingSubId(getDefaultSubscription());
1209 }
1210
1211 public int getActivePhoneTypeUsingSubId(long subId) {
1212 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 }
1214
1215 /**
1216 * Returns the CDMA ERI icon index to display
1217 */
1218 public int getCdmaEriIconIndex() {
Wink Saville36469e72014-06-11 15:17:00 -07001219 return getCdmaEriIconIndexUsingSubId(getDefaultSubscription());
1220
1221 }
1222
1223 public int getCdmaEriIconIndexUsingSubId(long subId) {
1224 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001225 }
1226
1227 /**
1228 * Returns the CDMA ERI icon mode,
1229 * 0 - ON
1230 * 1 - FLASHING
1231 */
1232 public int getCdmaEriIconMode() {
Wink Saville36469e72014-06-11 15:17:00 -07001233 return getCdmaEriIconModeUsingSubId(getDefaultSubscription());
1234 }
1235
1236 public int getCdmaEriIconModeUsingSubId(long subId) {
1237 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238 }
1239
1240 /**
1241 * Returns the CDMA ERI text,
1242 */
1243 public String getCdmaEriText() {
Wink Saville36469e72014-06-11 15:17:00 -07001244 return getCdmaEriTextUsingSubId(getDefaultSubscription());
1245 }
1246
1247 public String getCdmaEriTextUsingSubId(long subId) {
1248 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001249 }
1250
1251 /**
1252 * Returns true if CDMA provisioning needs to run.
1253 */
1254 public boolean needsOtaServiceProvisioning() {
1255 return mPhone.needsOtaServiceProvisioning();
1256 }
1257
1258 /**
1259 * Returns the unread count of voicemails
1260 */
1261 public int getVoiceMessageCount() {
Wink Saville36469e72014-06-11 15:17:00 -07001262 return getVoiceMessageCountUsingSubId(getDefaultSubscription());
1263 }
1264
1265 /**
1266 * Returns the unread count of voicemails for a subId
1267 */
1268 public int getVoiceMessageCountUsingSubId( long subId) {
1269 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001270 }
1271
1272 /**
1273 * Returns the data network type
1274 *
1275 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1276 */
1277 @Override
1278 public int getNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001279 return getNetworkTypeUsingSubId(getDefaultSubscription());
1280 }
1281
1282 /**
1283 * Returns the network type for a subId
1284 */
1285 @Override
1286 public int getNetworkTypeUsingSubId(long subId) {
1287 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 }
1289
1290 /**
1291 * Returns the data network type
1292 */
1293 @Override
1294 public int getDataNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001295 return getDataNetworkTypeUsingSubId(getDefaultSubscription());
1296 }
1297
1298 /**
1299 * Returns the data network type for a subId
1300 */
1301 @Override
1302 public int getDataNetworkTypeUsingSubId(long subId) {
1303 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304 }
1305
1306 /**
1307 * Returns the data network type
1308 */
1309 @Override
1310 public int getVoiceNetworkType() {
Wink Saville36469e72014-06-11 15:17:00 -07001311 return getVoiceNetworkTypeUsingSubId(getDefaultSubscription());
1312 }
1313
1314 /**
1315 * Returns the Voice network type for a subId
1316 */
1317 @Override
1318 public int getVoiceNetworkTypeUsingSubId(long subId) {
1319 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001320 }
1321
1322 /**
1323 * @return true if a ICC card is present
1324 */
1325 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001326 // FIXME Make changes to pass defaultSimId of type int
1327 return hasIccCardUsingSlotId(getDefaultSubscription());
1328 }
1329
1330 /**
1331 * @return true if a ICC card is present for a slotId
1332 */
1333 public boolean hasIccCardUsingSlotId(long slotId) {
1334 return getPhone(slotId).getIccCard().hasIccCard();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 }
1336
1337 /**
1338 * Return if the current radio is LTE on CDMA. This
1339 * is a tri-state return value as for a period of time
1340 * the mode may be unknown.
1341 *
1342 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001343 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 */
1345 public int getLteOnCdmaMode() {
Wink Saville36469e72014-06-11 15:17:00 -07001346 return getLteOnCdmaModeUsingSubId(getDefaultSubscription());
1347 }
1348
1349 public int getLteOnCdmaModeUsingSubId(long subId) {
1350 return getPhone(subId).getLteOnCdmaMode();
1351 }
1352
1353 public void setPhone(Phone phone) {
1354 mPhone = phone;
1355 }
1356
1357 /**
1358 * {@hide}
1359 * Returns Default subId, 0 in the case of single standby.
1360 */
1361 private long getDefaultSubscription() {
1362 return SubscriptionManager.getDefaultSubId();
1363 }
1364
1365 private long getPreferredVoiceSubscription() {
1366 return SubscriptionManager.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001367 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001368
1369 /**
1370 * @see android.telephony.TelephonyManager.WifiCallingChoices
1371 */
1372 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001373 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1374 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001375 }
1376
1377 /**
1378 * @see android.telephony.TelephonyManager.WifiCallingChoices
1379 */
1380 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001381 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1382 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1383 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001384 }
1385
Sailesh Nepald1e68152013-12-12 19:08:02 -08001386 private static int getWhenToMakeWifiCallsDefaultPreference() {
1387 // TODO(sail): Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001388 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001389 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001390
Shishir Agrawal566b7612013-10-28 14:41:00 -07001391 @Override
1392 public int iccOpenLogicalChannel(String AID) {
1393 enforceSimCommunicationPermission();
1394
1395 if (DBG) log("iccOpenLogicalChannel: " + AID);
1396 Integer channel = (Integer)sendRequest(CMD_OPEN_CHANNEL, AID);
1397 if (DBG) log("iccOpenLogicalChannel: " + channel);
Jake Hambye994d462014-02-03 13:10:13 -08001398 return channel;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001399 }
1400
1401 @Override
1402 public boolean iccCloseLogicalChannel(int channel) {
1403 enforceSimCommunicationPermission();
1404
1405 if (DBG) log("iccCloseLogicalChannel: " + channel);
1406 if (channel < 0) {
1407 return false;
1408 }
Jake Hambye994d462014-02-03 13:10:13 -08001409 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001410 if (DBG) log("iccCloseLogicalChannel: " + success);
1411 return success;
1412 }
1413
1414 @Override
1415 public String iccTransmitApduLogicalChannel(int channel, int cla,
1416 int command, int p1, int p2, int p3, String data) {
1417 enforceSimCommunicationPermission();
1418
1419 if (DBG) {
1420 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1421 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1422 " data=" + data);
1423 }
1424
1425 if (channel < 0) {
1426 return "";
1427 }
1428
1429 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU,
1430 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1431 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1432
1433 // If the payload is null, there was an error. Indicate that by returning
1434 // an empty string.
1435 if (response.payload == null) {
1436 return "";
1437 }
1438
1439 // Append the returned status code to the end of the response payload.
1440 String s = Integer.toHexString(
1441 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1442 s = IccUtils.bytesToHexString(response.payload) + s;
1443 return s;
1444 }
Jake Hambye994d462014-02-03 13:10:13 -08001445
Evan Charltonc66da362014-05-16 14:06:40 -07001446 @Override
1447 public String sendEnvelopeWithStatus(String content) {
1448 enforceSimCommunicationPermission();
1449
1450 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1451 if (response.payload == null) {
1452 return "";
1453 }
1454
1455 // Append the returned status code to the end of the response payload.
1456 String s = Integer.toHexString(
1457 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1458 s = IccUtils.bytesToHexString(response.payload) + s;
1459 return s;
1460 }
1461
Jake Hambye994d462014-02-03 13:10:13 -08001462 /**
1463 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1464 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1465 *
1466 * @param itemID the ID of the item to read
1467 * @return the NV item as a String, or null on error.
1468 */
1469 @Override
1470 public String nvReadItem(int itemID) {
1471 enforceModifyPermission();
1472 if (DBG) log("nvReadItem: item " + itemID);
1473 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1474 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1475 return value;
1476 }
1477
1478 /**
1479 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1480 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1481 *
1482 * @param itemID the ID of the item to read
1483 * @param itemValue the value to write, as a String
1484 * @return true on success; false on any failure
1485 */
1486 @Override
1487 public boolean nvWriteItem(int itemID, String itemValue) {
1488 enforceModifyPermission();
1489 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1490 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1491 new Pair<Integer, String>(itemID, itemValue));
1492 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1493 return success;
1494 }
1495
1496 /**
1497 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1498 * Used for device configuration by some CDMA operators.
1499 *
1500 * @param preferredRoamingList byte array containing the new PRL
1501 * @return true on success; false on any failure
1502 */
1503 @Override
1504 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
1505 enforceModifyPermission();
1506 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1507 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1508 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1509 return success;
1510 }
1511
1512 /**
1513 * Perform the specified type of NV config reset.
1514 * Used for device configuration by some CDMA operators.
1515 *
1516 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1517 * @return true on success; false on any failure
1518 */
1519 @Override
1520 public boolean nvResetConfig(int resetType) {
1521 enforceModifyPermission();
1522 if (DBG) log("nvResetConfig: type " + resetType);
1523 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1524 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1525 return success;
1526 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001527
1528 /**
Wink Saville36469e72014-06-11 15:17:00 -07001529 * {@hide}
1530 * Returns Default sim, 0 in the case of single standby.
1531 */
1532 public int getDefaultSim() {
1533 //TODO Need to get it from Telephony Devcontroller
1534 return 0;
1535 }
1536
1537 public String[] getPcscfAddress() {
1538 enforceReadPermission();
1539 return mPhone.getPcscfAddress();
1540 }
1541
1542 public void setImsRegistrationState(boolean registered) {
1543 enforceModifyPermission();
1544 mPhone.setImsRegistrationState(registered);
1545 }
1546
1547 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001548 * Get the preferred network type.
1549 * Used for device configuration by some CDMA operators.
1550 *
1551 * @return the preferred network type, defined in RILConstants.java.
1552 */
1553 @Override
1554 public int getPreferredNetworkType() {
1555 enforceModifyPermission();
1556 if (DBG) log("getPreferredNetworkType");
1557 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null);
1558 int networkType = (result != null ? result[0] : -1);
1559 if (DBG) log("getPreferredNetworkType: " + networkType);
1560 return networkType;
1561 }
1562
1563 /**
1564 * Set the preferred network type.
1565 * Used for device configuration by some CDMA operators.
1566 *
1567 * @param networkType the preferred network type, defined in RILConstants.java.
1568 * @return true on success; false on any failure.
1569 */
1570 @Override
1571 public boolean setPreferredNetworkType(int networkType) {
1572 enforceModifyPermission();
1573 if (DBG) log("setPreferredNetworkType: type " + networkType);
1574 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType);
1575 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
1576 return success;
1577 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001578
1579 /**
Junda Liu787bc7e2014-06-30 13:38:02 -07001580 * Set the CDMA subscription source.
1581 * Used for device supporting both NV and RUIM for CDMA.
1582 *
1583 * @param subscriptionType the subscription type, 0 for RUIM, 1 for NV.
1584 * @return true on success; false on any failure.
1585 */
1586 @Override
1587 public boolean setCdmaSubscription(int subscriptionType) {
1588 enforceModifyPermission();
1589 if (DBG) log("setCdmaSubscription: type " + subscriptionType);
1590 if (subscriptionType != mPhone.CDMA_SUBSCRIPTION_RUIM_SIM &&
1591 subscriptionType != mPhone.CDMA_SUBSCRIPTION_NV) {
1592 loge("setCdmaSubscription: unsupported subscriptionType.");
1593 return false;
1594 }
1595 Boolean success = (Boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION, subscriptionType);
1596 if (DBG) log("setCdmaSubscription: " + (success ? "ok" : "fail"));
1597 if (success) {
1598 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
1599 Settings.Global.CDMA_SUBSCRIPTION_MODE, subscriptionType);
1600 }
1601 return success;
1602 }
1603
1604 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001605 * Set mobile data enabled
1606 * Used by the user through settings etc to turn on/off mobile data
1607 *
1608 * @param enable {@code true} turn turn data on, else {@code false}
1609 */
1610 @Override
1611 public void setDataEnabled(boolean enable) {
1612 enforceModifyPermission();
1613 mPhone.setDataEnabled(enable);
1614 }
1615
1616 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001617 * Get whether mobile data is enabled.
1618 *
1619 * Note that this used to be available from ConnectivityService, gated by
1620 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1621 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001622 *
1623 * @return {@code true} if data is enabled else {@code false}
1624 */
1625 @Override
1626 public boolean getDataEnabled() {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001627 try {
1628 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1629 null);
1630 } catch (Exception e) {
1631 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1632 null);
1633 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001634 return mPhone.getDataEnabled();
1635 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636}