blob: 59eb9965a44b95f5c1823e537ee97b26c1138df9 [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;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Derek Tan97ebb422014-09-05 16:55:38 -070036import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080037import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080038import android.telecom.PhoneAccount;
Andrew Lee9431b832015-03-09 18:46:45 -070039import android.telecom.TelecomManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070041import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080042import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070043import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080045import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080046import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080047import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080049import android.util.ArrayMap;
50import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080052import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080053import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054
Andrew Lee312e8172014-10-23 17:01:36 -070055import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070056import com.android.internal.telephony.CallManager;
57import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080060import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070062import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070063import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import com.android.internal.telephony.PhoneConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080065import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070066import com.android.internal.telephony.uicc.IccIoResult;
67import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070068import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080070import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071
Wink Saville36469e72014-06-11 15:17:00 -070072import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
73
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080075import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080076import java.util.HashMap;
77import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080078import java.util.List;
Jeff Sharkey85190e62014-12-05 09:40:12 -080079import java.util.Map;
80import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081
82/**
83 * Implementation of the ITelephony interface.
84 */
Santos Cordon117fee72014-05-16 17:56:12 -070085public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086 private static final String LOG_TAG = "PhoneInterfaceManager";
87 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
88 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080089 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090
91 // Message codes used with mMainThreadHandler
92 private static final int CMD_HANDLE_PIN_MMI = 1;
93 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
94 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
95 private static final int CMD_ANSWER_RINGING_CALL = 4;
96 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070097 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
98 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -070099 private static final int CMD_OPEN_CHANNEL = 9;
100 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
101 private static final int CMD_CLOSE_CHANNEL = 11;
102 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800103 private static final int CMD_NV_READ_ITEM = 13;
104 private static final int EVENT_NV_READ_ITEM_DONE = 14;
105 private static final int CMD_NV_WRITE_ITEM = 15;
106 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
107 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
108 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
109 private static final int CMD_NV_RESET_CONFIG = 19;
110 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800111 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
112 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
113 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
114 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800115 private static final int CMD_SEND_ENVELOPE = 25;
116 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700117 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
118 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
119 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
120 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
121 private static final int CMD_EXCHANGE_SIM_IO = 31;
122 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800123 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
124 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700125 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
126 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127
128 /** The singleton instance. */
129 private static PhoneInterfaceManager sInstance;
130
Wink Saville3ab207e2014-11-20 13:07:20 -0800131 private PhoneGlobals mApp;
132 private Phone mPhone;
133 private CallManager mCM;
134 private AppOpsManager mAppOps;
135 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800136 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800137 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138
Derek Tan97ebb422014-09-05 16:55:38 -0700139 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
140 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800141 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700142 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700143
144 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700145 * A request object to use for transmitting data to an ICC.
146 */
147 private static final class IccAPDUArgument {
148 public int channel, cla, command, p1, p2, p3;
149 public String data;
150
151 public IccAPDUArgument(int channel, int cla, int command,
152 int p1, int p2, int p3, String data) {
153 this.channel = channel;
154 this.cla = cla;
155 this.command = command;
156 this.p1 = p1;
157 this.p2 = p2;
158 this.p3 = p3;
159 this.data = data;
160 }
161 }
162
163 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
165 * request after sending. The main thread will notify the request when it is complete.
166 */
167 private static final class MainThreadRequest {
168 /** The argument to use for the request */
169 public Object argument;
170 /** The result of the request that is run on the main thread */
171 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800172 /** The subscriber id that this request applies to. Null if default. */
173 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174
175 public MainThreadRequest(Object argument) {
176 this.argument = argument;
177 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800178
179 public MainThreadRequest(Object argument, Integer subId) {
180 this.argument = argument;
181 this.subId = subId;
182 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183 }
184
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800185 private static final class IncomingThirdPartyCallArgs {
186 public final ComponentName component;
187 public final String callId;
188 public final String callerDisplayName;
189
190 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
191 String callerDisplayName) {
192 this.component = component;
193 this.callId = callId;
194 this.callerDisplayName = callerDisplayName;
195 }
196 }
197
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198 /**
199 * A handler that processes messages on the main thread in the phone process. Since many
200 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
201 * inbound binder threads to the main thread in the phone process. The Binder thread
202 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
203 * on, which will be notified when the operation completes and will contain the result of the
204 * request.
205 *
206 * <p>If a MainThreadRequest object is provided in the msg.obj field,
207 * note that request.result must be set to something non-null for the calling thread to
208 * unblock.
209 */
210 private final class MainThreadHandler extends Handler {
211 @Override
212 public void handleMessage(Message msg) {
213 MainThreadRequest request;
214 Message onCompleted;
215 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800216 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700217 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218
219 switch (msg.what) {
220 case CMD_HANDLE_PIN_MMI:
221 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800222 request.result = getPhoneFromRequest(request).handlePinMmi(
223 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 // Wake up the requesting thread
225 synchronized (request) {
226 request.notifyAll();
227 }
228 break;
229
230 case CMD_HANDLE_NEIGHBORING_CELL:
231 request = (MainThreadRequest) msg.obj;
232 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
233 request);
234 mPhone.getNeighboringCids(onCompleted);
235 break;
236
237 case EVENT_NEIGHBORING_CELL_DONE:
238 ar = (AsyncResult) msg.obj;
239 request = (MainThreadRequest) ar.userObj;
240 if (ar.exception == null && ar.result != null) {
241 request.result = ar.result;
242 } else {
243 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800244 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245 }
246 // Wake up the requesting thread
247 synchronized (request) {
248 request.notifyAll();
249 }
250 break;
251
252 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700253 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800254 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700255 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256 break;
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 case CMD_END_CALL:
259 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800260 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700261 final boolean hungUp;
262 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
264 // CDMA: If the user presses the Power button we treat it as
265 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700266 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
268 // GSM: End the call as per the Phone state
269 hungUp = PhoneUtils.hangup(mCM);
270 } else {
271 throw new IllegalStateException("Unexpected phone type: " + phoneType);
272 }
273 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
274 request.result = hungUp;
275 // Wake up the requesting thread
276 synchronized (request) {
277 request.notifyAll();
278 }
279 break;
280
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700281 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700282 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700283 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700284 if (uiccCard == null) {
285 loge("iccTransmitApduLogicalChannel: No UICC");
286 request.result = new IccIoResult(0x6F, 0, (byte[])null);
287 synchronized (request) {
288 request.notifyAll();
289 }
290 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700291 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
292 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700293 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700294 iccArgument.channel, iccArgument.cla, iccArgument.command,
295 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700296 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700297 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700298 break;
299
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700300 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700301 ar = (AsyncResult) msg.obj;
302 request = (MainThreadRequest) ar.userObj;
303 if (ar.exception == null && ar.result != null) {
304 request.result = ar.result;
305 } else {
306 request.result = new IccIoResult(0x6F, 0, (byte[])null);
307 if (ar.result == null) {
308 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800309 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700310 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800311 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700312 } else {
313 loge("iccTransmitApduLogicalChannel: Unknown exception");
314 }
315 }
316 synchronized (request) {
317 request.notifyAll();
318 }
319 break;
320
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700321 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
322 request = (MainThreadRequest) msg.obj;
323 iccArgument = (IccAPDUArgument) request.argument;
324 if (uiccCard == null) {
325 loge("iccTransmitApduBasicChannel: No UICC");
326 request.result = new IccIoResult(0x6F, 0, (byte[])null);
327 synchronized (request) {
328 request.notifyAll();
329 }
330 } else {
331 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
332 request);
333 uiccCard.iccTransmitApduBasicChannel(
334 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
335 iccArgument.p3, iccArgument.data, onCompleted);
336 }
337 break;
338
339 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
340 ar = (AsyncResult) msg.obj;
341 request = (MainThreadRequest) ar.userObj;
342 if (ar.exception == null && ar.result != null) {
343 request.result = ar.result;
344 } else {
345 request.result = new IccIoResult(0x6F, 0, (byte[])null);
346 if (ar.result == null) {
347 loge("iccTransmitApduBasicChannel: Empty response");
348 } else if (ar.exception instanceof CommandException) {
349 loge("iccTransmitApduBasicChannel: CommandException: " +
350 ar.exception);
351 } else {
352 loge("iccTransmitApduBasicChannel: Unknown exception");
353 }
354 }
355 synchronized (request) {
356 request.notifyAll();
357 }
358 break;
359
360 case CMD_EXCHANGE_SIM_IO:
361 request = (MainThreadRequest) msg.obj;
362 iccArgument = (IccAPDUArgument) request.argument;
363 if (uiccCard == null) {
364 loge("iccExchangeSimIO: No UICC");
365 request.result = new IccIoResult(0x6F, 0, (byte[])null);
366 synchronized (request) {
367 request.notifyAll();
368 }
369 } else {
370 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
371 request);
372 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
373 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
374 iccArgument.data, onCompleted);
375 }
376 break;
377
378 case EVENT_EXCHANGE_SIM_IO_DONE:
379 ar = (AsyncResult) msg.obj;
380 request = (MainThreadRequest) ar.userObj;
381 if (ar.exception == null && ar.result != null) {
382 request.result = ar.result;
383 } else {
384 request.result = new IccIoResult(0x6f, 0, (byte[])null);
385 }
386 synchronized (request) {
387 request.notifyAll();
388 }
389 break;
390
Derek Tan4d5e5c12014-02-04 11:54:58 -0800391 case CMD_SEND_ENVELOPE:
392 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700393 if (uiccCard == null) {
394 loge("sendEnvelopeWithStatus: No UICC");
395 request.result = new IccIoResult(0x6F, 0, (byte[])null);
396 synchronized (request) {
397 request.notifyAll();
398 }
399 } else {
400 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
401 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
402 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800403 break;
404
405 case EVENT_SEND_ENVELOPE_DONE:
406 ar = (AsyncResult) msg.obj;
407 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700408 if (ar.exception == null && ar.result != null) {
409 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800410 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700411 request.result = new IccIoResult(0x6F, 0, (byte[])null);
412 if (ar.result == null) {
413 loge("sendEnvelopeWithStatus: Empty response");
414 } else if (ar.exception instanceof CommandException) {
415 loge("sendEnvelopeWithStatus: CommandException: " +
416 ar.exception);
417 } else {
418 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
419 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800420 }
421 synchronized (request) {
422 request.notifyAll();
423 }
424 break;
425
Shishir Agrawal566b7612013-10-28 14:41:00 -0700426 case CMD_OPEN_CHANNEL:
427 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700428 if (uiccCard == null) {
429 loge("iccOpenLogicalChannel: No UICC");
430 request.result = new IccIoResult(0x6F, 0, (byte[])null);
431 synchronized (request) {
432 request.notifyAll();
433 }
434 } else {
435 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
436 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
437 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700438 break;
439
440 case EVENT_OPEN_CHANNEL_DONE:
441 ar = (AsyncResult) msg.obj;
442 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700443 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700444 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700445 int[] result = (int[]) ar.result;
446 int channelId = result[0];
447 byte[] selectResponse = null;
448 if (result.length > 1) {
449 selectResponse = new byte[result.length - 1];
450 for (int i = 1; i < result.length; ++i) {
451 selectResponse[i - 1] = (byte) result[i];
452 }
453 }
454 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700455 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700456 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700457 if (ar.result == null) {
458 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700459 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700460 if (ar.exception != null) {
461 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
462 }
463
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700464 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700465 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
466 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700467 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700468 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700469 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700470 }
471 }
472 openChannelResp = new IccOpenLogicalChannelResponse(
473 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700475 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 synchronized (request) {
477 request.notifyAll();
478 }
479 break;
480
481 case CMD_CLOSE_CHANNEL:
482 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700483 if (uiccCard == null) {
484 loge("iccCloseLogicalChannel: No UICC");
485 request.result = new IccIoResult(0x6F, 0, (byte[])null);
486 synchronized (request) {
487 request.notifyAll();
488 }
489 } else {
490 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
491 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
492 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 break;
494
495 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800496 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
497 break;
498
499 case CMD_NV_READ_ITEM:
500 request = (MainThreadRequest) msg.obj;
501 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
502 mPhone.nvReadItem((Integer) request.argument, onCompleted);
503 break;
504
505 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700506 ar = (AsyncResult) msg.obj;
507 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800508 if (ar.exception == null && ar.result != null) {
509 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800511 request.result = "";
512 if (ar.result == null) {
513 loge("nvReadItem: Empty response");
514 } else if (ar.exception instanceof CommandException) {
515 loge("nvReadItem: CommandException: " +
516 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800518 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 }
520 }
521 synchronized (request) {
522 request.notifyAll();
523 }
524 break;
525
Jake Hambye994d462014-02-03 13:10:13 -0800526 case CMD_NV_WRITE_ITEM:
527 request = (MainThreadRequest) msg.obj;
528 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
529 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
530 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
531 break;
532
533 case EVENT_NV_WRITE_ITEM_DONE:
534 handleNullReturnEvent(msg, "nvWriteItem");
535 break;
536
537 case CMD_NV_WRITE_CDMA_PRL:
538 request = (MainThreadRequest) msg.obj;
539 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
540 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
541 break;
542
543 case EVENT_NV_WRITE_CDMA_PRL_DONE:
544 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
545 break;
546
547 case CMD_NV_RESET_CONFIG:
548 request = (MainThreadRequest) msg.obj;
549 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
550 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
551 break;
552
553 case EVENT_NV_RESET_CONFIG_DONE:
554 handleNullReturnEvent(msg, "nvResetConfig");
555 break;
556
Jake Hamby7c27be32014-03-03 13:25:59 -0800557 case CMD_GET_PREFERRED_NETWORK_TYPE:
558 request = (MainThreadRequest) msg.obj;
559 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700560 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800561 break;
562
563 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
564 ar = (AsyncResult) msg.obj;
565 request = (MainThreadRequest) ar.userObj;
566 if (ar.exception == null && ar.result != null) {
567 request.result = ar.result; // Integer
568 } else {
569 request.result = -1;
570 if (ar.result == null) {
571 loge("getPreferredNetworkType: Empty response");
572 } else if (ar.exception instanceof CommandException) {
573 loge("getPreferredNetworkType: CommandException: " +
574 ar.exception);
575 } else {
576 loge("getPreferredNetworkType: Unknown exception");
577 }
578 }
579 synchronized (request) {
580 request.notifyAll();
581 }
582 break;
583
584 case CMD_SET_PREFERRED_NETWORK_TYPE:
585 request = (MainThreadRequest) msg.obj;
586 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
587 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700588 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800589 break;
590
591 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
592 handleNullReturnEvent(msg, "setPreferredNetworkType");
593 break;
594
Steven Liu4bf01bc2014-07-17 11:05:29 -0500595 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
596 request = (MainThreadRequest)msg.obj;
597 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
598 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
599 break;
600
601 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
602 ar = (AsyncResult)msg.obj;
603 request = (MainThreadRequest)ar.userObj;
604 request.result = ar;
605 synchronized (request) {
606 request.notifyAll();
607 }
608 break;
609
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800610 case CMD_SET_VOICEMAIL_NUMBER:
611 request = (MainThreadRequest) msg.obj;
612 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
613 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800614 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
615 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800616 break;
617
618 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
619 handleNullReturnEvent(msg, "setVoicemailNumber");
620 break;
621
Stuart Scott54788802015-03-30 13:18:01 -0700622 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
625 request);
626 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
627 break;
628
629 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
630 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
631 break;
632
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700633 default:
634 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
635 break;
636 }
637 }
Jake Hambye994d462014-02-03 13:10:13 -0800638
639 private void handleNullReturnEvent(Message msg, String command) {
640 AsyncResult ar = (AsyncResult) msg.obj;
641 MainThreadRequest request = (MainThreadRequest) ar.userObj;
642 if (ar.exception == null) {
643 request.result = true;
644 } else {
645 request.result = false;
646 if (ar.exception instanceof CommandException) {
647 loge(command + ": CommandException: " + ar.exception);
648 } else {
649 loge(command + ": Unknown exception");
650 }
651 }
652 synchronized (request) {
653 request.notifyAll();
654 }
655 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700656 }
657
658 /**
659 * Posts the specified command to be executed on the main thread,
660 * waits for the request to complete, and returns the result.
661 * @see #sendRequestAsync
662 */
663 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700664 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700665 }
666
667 /**
668 * Posts the specified command to be executed on the main thread,
669 * waits for the request to complete, and returns the result.
670 * @see #sendRequestAsync
671 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800672 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
674 throw new RuntimeException("This method will deadlock if called from the main thread.");
675 }
676
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800677 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700678 Message msg = mMainThreadHandler.obtainMessage(command, request);
679 msg.sendToTarget();
680
681 // Wait for the request to complete
682 synchronized (request) {
683 while (request.result == null) {
684 try {
685 request.wait();
686 } catch (InterruptedException e) {
687 // Do nothing, go back and wait until the request is complete
688 }
689 }
690 }
691 return request.result;
692 }
693
694 /**
695 * Asynchronous ("fire and forget") version of sendRequest():
696 * Posts the specified command to be executed on the main thread, and
697 * returns immediately.
698 * @see #sendRequest
699 */
700 private void sendRequestAsync(int command) {
701 mMainThreadHandler.sendEmptyMessage(command);
702 }
703
704 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700705 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
706 * @see {@link #sendRequest(int,Object)}
707 */
708 private void sendRequestAsync(int command, Object argument) {
709 MainThreadRequest request = new MainThreadRequest(argument);
710 Message msg = mMainThreadHandler.obtainMessage(command, request);
711 msg.sendToTarget();
712 }
713
714 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 * Initialize the singleton PhoneInterfaceManager instance.
716 * This is only done once, at startup, from PhoneApp.onCreate().
717 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700718 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 synchronized (PhoneInterfaceManager.class) {
720 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700721 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 } else {
723 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
724 }
725 return sInstance;
726 }
727 }
728
729 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700730 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 mApp = app;
732 mPhone = phone;
733 mCM = PhoneGlobals.getInstance().mCM;
734 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
735 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700736 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700737 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800738 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800739
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 publish();
741 }
742
743 private void publish() {
744 if (DBG) log("publish: " + this);
745
746 ServiceManager.addService("phone", this);
747 }
748
Stuart Scott584921c2015-01-15 17:10:34 -0800749 private Phone getPhoneFromRequest(MainThreadRequest request) {
750 return (request.subId == null) ? mPhone : getPhone(request.subId);
751 }
752
Wink Saville36469e72014-06-11 15:17:00 -0700753 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700754 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800755 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700756 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 //
758 // Implementation of the ITelephony interface.
759 //
760
761 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700762 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700763 }
764
Wink Savilleb564aae2014-10-23 10:18:09 -0700765 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 if (DBG) log("dial: " + number);
767 // No permission check needed here: This is just a wrapper around the
768 // ACTION_DIAL intent, which is available to any app since it puts up
769 // the UI before it does anything.
770
771 String url = createTelUrl(number);
772 if (url == null) {
773 return;
774 }
775
776 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700777 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
779 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
780 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
781 mApp.startActivity(intent);
782 }
783 }
784
785 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700786 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700787 }
788
Wink Savilleb564aae2014-10-23 10:18:09 -0700789 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 if (DBG) log("call: " + number);
791
792 // This is just a wrapper around the ACTION_CALL intent, but we still
793 // need to do a permission check since we're calling startActivity()
794 // from the context of the phone app.
795 enforceCallPermission();
796
797 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
798 != AppOpsManager.MODE_ALLOWED) {
799 return;
800 }
801
802 String url = createTelUrl(number);
803 if (url == null) {
804 return;
805 }
806
Wink Saville08874612014-08-31 19:19:58 -0700807 boolean isValid = false;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800808 List<SubscriptionInfo> slist = mSubscriptionController.getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800809 if (slist != null) {
810 for (SubscriptionInfo subInfoRecord : slist) {
811 if (subInfoRecord.getSubscriptionId() == subId) {
812 isValid = true;
813 break;
814 }
Wink Saville08874612014-08-31 19:19:58 -0700815 }
816 }
817 if (isValid == false) {
818 return;
819 }
820
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700822 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700823 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
824 mApp.startActivity(intent);
825 }
826
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 /**
828 * End a call based on call state
829 * @return true is a call was ended
830 */
831 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700832 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700833 }
834
835 /**
836 * End a call based on the call state of the subId
837 * @return true is a call was ended
838 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700839 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800841 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 }
843
844 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700845 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700846 }
847
Wink Savilleb564aae2014-10-23 10:18:09 -0700848 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 if (DBG) log("answerRingingCall...");
850 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
851 // but that can probably wait till the big TelephonyManager API overhaul.
852 // For now, protect this call with the MODIFY_PHONE_STATE permission.
853 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800854 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 }
856
857 /**
858 * Make the actual telephony calls to implement answerRingingCall().
859 * This should only be called from the main thread of the Phone app.
860 * @see #answerRingingCall
861 *
862 * TODO: it would be nice to return true if we answered the call, or
863 * false if there wasn't actually a ringing incoming call, or some
864 * other error occurred. (In other words, pass back the return value
865 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
866 * But that would require calling this method via sendRequest() rather
867 * than sendRequestAsync(), and right now we don't actually *need* that
868 * return value, so let's just return void for now.
869 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700870 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700871 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700873 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
874 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 if (hasActiveCall && hasHoldingCall) {
876 // Both lines are in use!
877 // TODO: provide a flag to let the caller specify what
878 // policy to use if both lines are in use. (The current
879 // behavior is hardwired to "answer incoming, end ongoing",
880 // which is how the CALL button is specced to behave.)
881 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
882 return;
883 } else {
884 // answerCall() will automatically hold the current active
885 // call, if there is one.
886 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
887 return;
888 }
889 } else {
890 // No call was ringing.
891 return;
892 }
893 }
894
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700895 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700896 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700898 public void silenceRinger() {
899 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700900 }
901
902 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700903 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700904 }
905
Wink Savilleb564aae2014-10-23 10:18:09 -0700906 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700907 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 }
909
910 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700911 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700912 }
913
Wink Savilleb564aae2014-10-23 10:18:09 -0700914 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700915 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 }
917
918 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700919 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700920 }
921
Wink Savilleb564aae2014-10-23 10:18:09 -0700922 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700923 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 }
925
926 public boolean isSimPinEnabled() {
927 enforceReadPermission();
928 return (PhoneGlobals.getInstance().isSimPinEnabled());
929 }
930
931 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700932 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700933 }
934
Wink Savilleb564aae2014-10-23 10:18:09 -0700935 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700936 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700937 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
938 }
939
940 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700941 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700942 }
943
Wink Savilleb564aae2014-10-23 10:18:09 -0700944 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700945 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700946 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
947 }
948
949 /** {@hide} */
950 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700951 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700952 }
953
Wink Savilleb564aae2014-10-23 10:18:09 -0700954 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700956 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 checkSimPin.start();
958 return checkSimPin.unlockSim(null, pin);
959 }
960
Wink Saville9de0f752013-10-22 19:04:03 -0700961 /** {@hide} */
962 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700963 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700964 }
965
Wink Savilleb564aae2014-10-23 10:18:09 -0700966 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700968 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 checkSimPuk.start();
970 return checkSimPuk.unlockSim(puk, pin);
971 }
972
973 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700974 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 * a synchronous one.
976 */
977 private static class UnlockSim extends Thread {
978
979 private final IccCard mSimCard;
980
981 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700982 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
983 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984
985 // For replies from SimCard interface
986 private Handler mHandler;
987
988 // For async handler to identify request type
989 private static final int SUPPLY_PIN_COMPLETE = 100;
990
991 public UnlockSim(IccCard simCard) {
992 mSimCard = simCard;
993 }
994
995 @Override
996 public void run() {
997 Looper.prepare();
998 synchronized (UnlockSim.this) {
999 mHandler = new Handler() {
1000 @Override
1001 public void handleMessage(Message msg) {
1002 AsyncResult ar = (AsyncResult) msg.obj;
1003 switch (msg.what) {
1004 case SUPPLY_PIN_COMPLETE:
1005 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1006 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001007 mRetryCount = msg.arg1;
1008 if (ar.exception != null) {
1009 if (ar.exception instanceof CommandException &&
1010 ((CommandException)(ar.exception)).getCommandError()
1011 == CommandException.Error.PASSWORD_INCORRECT) {
1012 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1013 } else {
1014 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1015 }
1016 } else {
1017 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1018 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 mDone = true;
1020 UnlockSim.this.notifyAll();
1021 }
1022 break;
1023 }
1024 }
1025 };
1026 UnlockSim.this.notifyAll();
1027 }
1028 Looper.loop();
1029 }
1030
1031 /*
1032 * Use PIN or PUK to unlock SIM card
1033 *
1034 * If PUK is null, unlock SIM card with PIN
1035 *
1036 * If PUK is not null, unlock SIM card with PUK and set PIN code
1037 */
Wink Saville9de0f752013-10-22 19:04:03 -07001038 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001039
1040 while (mHandler == null) {
1041 try {
1042 wait();
1043 } catch (InterruptedException e) {
1044 Thread.currentThread().interrupt();
1045 }
1046 }
1047 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1048
1049 if (puk == null) {
1050 mSimCard.supplyPin(pin, callback);
1051 } else {
1052 mSimCard.supplyPuk(puk, pin, callback);
1053 }
1054
1055 while (!mDone) {
1056 try {
1057 Log.d(LOG_TAG, "wait for done");
1058 wait();
1059 } catch (InterruptedException e) {
1060 // Restore the interrupted status
1061 Thread.currentThread().interrupt();
1062 }
1063 }
1064 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001065 int[] resultArray = new int[2];
1066 resultArray[0] = mResult;
1067 resultArray[1] = mRetryCount;
1068 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001069 }
1070 }
1071
1072 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001073 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001074
1075 }
1076
Wink Savilleb564aae2014-10-23 10:18:09 -07001077 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 // No permission check needed here: this call is harmless, and it's
1079 // needed for the ServiceState.requestStateUpdate() call (which is
1080 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001081 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 }
1083
1084 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001085 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001086 }
1087
Wink Savilleb564aae2014-10-23 10:18:09 -07001088 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001089 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001090 }
1091
1092 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001093 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001094
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001095 }
Wink Saville36469e72014-06-11 15:17:00 -07001096
Wink Savilleb564aae2014-10-23 10:18:09 -07001097 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001099 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001100 }
1101
1102 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001103 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001104 }
1105
Wink Savilleb564aae2014-10-23 10:18:09 -07001106 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001107 enforceModifyPermission();
1108 if ((getPhone(subId).getServiceState().getState() !=
1109 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001110 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 }
1112 return true;
1113 }
Wink Saville36469e72014-06-11 15:17:00 -07001114
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001115 public boolean needMobileRadioShutdown() {
1116 /*
1117 * If any of the Radios are available, it will need to be
1118 * shutdown. So return true if any Radio is available.
1119 */
1120 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1121 Phone phone = PhoneFactory.getPhone(i);
1122 if (phone != null && phone.isRadioAvailable()) return true;
1123 }
1124 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1125 return false;
1126 }
1127
1128 public void shutdownMobileRadios() {
1129 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1130 logv("Shutting down Phone " + i);
1131 shutdownRadioUsingPhoneId(i);
1132 }
1133 }
1134
1135 private void shutdownRadioUsingPhoneId(int phoneId) {
1136 enforceModifyPermission();
1137 Phone phone = PhoneFactory.getPhone(phoneId);
1138 if (phone != null && phone.isRadioAvailable()) {
1139 phone.shutdownRadio();
1140 }
1141 }
1142
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001143 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001144 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001145 }
1146
Wink Savilleb564aae2014-10-23 10:18:09 -07001147 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001149 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001150 return true;
1151 }
1152
Wink Saville36469e72014-06-11 15:17:00 -07001153 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 public boolean enableDataConnectivity() {
1155 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001156 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001157 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001158 return true;
1159 }
1160
Wink Saville36469e72014-06-11 15:17:00 -07001161 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162 public boolean disableDataConnectivity() {
1163 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001164 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001165 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 return true;
1167 }
1168
Wink Saville36469e72014-06-11 15:17:00 -07001169 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001171 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001172 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 }
1174
1175 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001176 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001177 }
1178
Wink Savilleb564aae2014-10-23 10:18:09 -07001179 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001181 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001182 }
1183
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001185 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001186 }
1187
Wink Savilleb564aae2014-10-23 10:18:09 -07001188 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001189 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 }
1191
1192 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001193 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001194 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
1197 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001198 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001199 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 }
1201
1202 @Override
1203 public Bundle getCellLocation() {
1204 try {
1205 mApp.enforceCallingOrSelfPermission(
1206 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1207 } catch (SecurityException e) {
1208 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1209 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1210 // is the weaker precondition
1211 mApp.enforceCallingOrSelfPermission(
1212 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1213 }
1214
Jake Hambye994d462014-02-03 13:10:13 -08001215 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 if (DBG_LOC) log("getCellLocation: is active user");
1217 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001218 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1219 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 return data;
1221 } else {
1222 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1223 return null;
1224 }
1225 }
1226
1227 @Override
1228 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001229 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001230 }
1231
Wink Savilleb564aae2014-10-23 10:18:09 -07001232 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 mApp.enforceCallingOrSelfPermission(
1234 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001235 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 }
1237
1238 @Override
1239 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001240 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001241 }
1242
Wink Savilleb564aae2014-10-23 10:18:09 -07001243 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244 mApp.enforceCallingOrSelfPermission(
1245 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001246 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001247 }
1248
1249 @Override
1250 @SuppressWarnings("unchecked")
1251 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
1252 try {
1253 mApp.enforceCallingOrSelfPermission(
1254 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1255 } catch (SecurityException e) {
1256 // If we have ACCESS_FINE_LOCATION permission, skip the check
1257 // for ACCESS_COARSE_LOCATION
1258 // A failure should throw the SecurityException from
1259 // ACCESS_COARSE_LOCATION since this is the weaker precondition
1260 mApp.enforceCallingOrSelfPermission(
1261 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1262 }
1263
1264 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1265 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1266 return null;
1267 }
Jake Hambye994d462014-02-03 13:10:13 -08001268 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1270
1271 ArrayList<NeighboringCellInfo> cells = null;
1272
1273 try {
1274 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001275 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001277 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 }
1279 return cells;
1280 } else {
1281 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1282 return null;
1283 }
1284 }
1285
1286
1287 @Override
1288 public List<CellInfo> getAllCellInfo() {
1289 try {
1290 mApp.enforceCallingOrSelfPermission(
1291 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1292 } catch (SecurityException e) {
1293 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1294 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1295 // is the weaker precondition
1296 mApp.enforceCallingOrSelfPermission(
1297 android.Manifest.permission.ACCESS_COARSE_LOCATION, null);
1298 }
1299
Jake Hambye994d462014-02-03 13:10:13 -08001300 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001302 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1303 for (Phone phone : PhoneFactory.getPhones()) {
1304 cellInfos.addAll(phone.getAllCellInfo());
1305 }
1306 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307 } else {
1308 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1309 return null;
1310 }
1311 }
1312
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001313 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314 public void setCellInfoListRate(int rateInMillis) {
1315 mPhone.setCellInfoListRate(rateInMillis);
1316 }
1317
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318 //
1319 // Internal helper methods.
1320 //
1321
Jake Hambye994d462014-02-03 13:10:13 -08001322 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 boolean ok;
1324
1325 boolean self = Binder.getCallingUid() == Process.myUid();
1326 if (!self) {
1327 // Get the caller's user id then clear the calling identity
1328 // which will be restored in the finally clause.
1329 int callingUser = UserHandle.getCallingUserId();
1330 long ident = Binder.clearCallingIdentity();
1331
1332 try {
1333 // With calling identity cleared the current user is the foreground user.
1334 int foregroundUser = ActivityManager.getCurrentUser();
1335 ok = (foregroundUser == callingUser);
1336 if (DBG_LOC) {
1337 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1338 + " callingUser=" + callingUser + " ok=" + ok);
1339 }
1340 } catch (Exception ex) {
1341 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1342 ok = false;
1343 } finally {
1344 Binder.restoreCallingIdentity(ident);
1345 }
1346 } else {
1347 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1348 ok = true;
1349 }
1350 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1351 return ok;
1352 }
1353
1354 /**
1355 * Make sure the caller has the READ_PHONE_STATE permission.
1356 *
1357 * @throws SecurityException if the caller does not have the required permission
1358 */
1359 private void enforceReadPermission() {
1360 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
1361 }
1362
1363 /**
1364 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1365 *
1366 * @throws SecurityException if the caller does not have the required permission
1367 */
1368 private void enforceModifyPermission() {
1369 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1370 }
1371
1372 /**
Junda Liua2e36012014-07-09 18:30:01 -07001373 * Make sure either system app or the caller has carrier privilege.
1374 *
1375 * @throws SecurityException if the caller does not have the required permission/privilege
1376 */
1377 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001378 int permission = mApp.checkCallingOrSelfPermission(
1379 android.Manifest.permission.MODIFY_PHONE_STATE);
1380 if (permission == PackageManager.PERMISSION_GRANTED) {
1381 return;
1382 }
1383
1384 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001385 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001386 loge("No Carrier Privilege.");
1387 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001388 }
1389 }
1390
1391 /**
1392 * Make sure the caller has carrier privilege.
1393 *
1394 * @throws SecurityException if the caller does not have the required permission
1395 */
1396 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001397 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001398 loge("No Carrier Privilege.");
1399 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001400 }
1401 }
1402
1403 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 * Make sure the caller has the CALL_PHONE permission.
1405 *
1406 * @throws SecurityException if the caller does not have the required permission
1407 */
1408 private void enforceCallPermission() {
1409 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1410 }
1411
Shishir Agrawal566b7612013-10-28 14:41:00 -07001412 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001413 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1414 *
1415 * @throws SecurityException if the caller does not have the required permission
1416 */
1417 private void enforcePrivilegedPhoneStatePermission() {
1418 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1419 null);
1420 }
1421
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 private String createTelUrl(String number) {
1423 if (TextUtils.isEmpty(number)) {
1424 return null;
1425 }
1426
Jake Hambye994d462014-02-03 13:10:13 -08001427 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 }
1429
Ihab Awadf9e92732013-12-05 18:02:52 -08001430 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1432 }
1433
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001434 private static void logv(String msg) {
1435 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1436 }
1437
Ihab Awadf9e92732013-12-05 18:02:52 -08001438 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1440 }
1441
1442 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001443 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001444 }
1445
Wink Savilleb564aae2014-10-23 10:18:09 -07001446 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001447 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 }
1449
1450 /**
1451 * Returns the CDMA ERI icon index to display
1452 */
1453 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001454 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001455
1456 }
1457
Wink Savilleb564aae2014-10-23 10:18:09 -07001458 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001459 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 }
1461
1462 /**
1463 * Returns the CDMA ERI icon mode,
1464 * 0 - ON
1465 * 1 - FLASHING
1466 */
1467 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001468 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001469 }
1470
Wink Savilleb564aae2014-10-23 10:18:09 -07001471 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001472 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001473 }
1474
1475 /**
1476 * Returns the CDMA ERI text,
1477 */
1478 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001479 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001480 }
1481
Wink Savilleb564aae2014-10-23 10:18:09 -07001482 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001483 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 }
1485
1486 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001487 * Returns the CDMA MDN.
1488 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001489 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001490 enforceModifyPermissionOrCarrierPrivilege();
1491 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1492 return getPhone(subId).getLine1Number();
1493 } else {
1494 return null;
1495 }
1496 }
1497
1498 /**
1499 * Returns the CDMA MIN.
1500 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001501 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001502 enforceModifyPermissionOrCarrierPrivilege();
1503 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1504 return getPhone(subId).getCdmaMin();
1505 } else {
1506 return null;
1507 }
1508 }
1509
1510 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511 * Returns true if CDMA provisioning needs to run.
1512 */
1513 public boolean needsOtaServiceProvisioning() {
1514 return mPhone.needsOtaServiceProvisioning();
1515 }
1516
1517 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001518 * Sets the voice mail number of a given subId.
1519 */
1520 @Override
1521 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001522 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001523 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1524 new Pair<String, String>(alphaTag, number), new Integer(subId));
1525 return success;
1526 }
1527
1528 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 * Returns the unread count of voicemails
1530 */
1531 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001532 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001533 }
1534
1535 /**
1536 * Returns the unread count of voicemails for a subId
1537 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001538 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001539 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 }
1541
1542 /**
1543 * Returns the data network type
1544 *
1545 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1546 */
1547 @Override
1548 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001549 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001550 }
1551
1552 /**
1553 * Returns the network type for a subId
1554 */
1555 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001556 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001557 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001558 }
1559
1560 /**
1561 * Returns the data network type
1562 */
1563 @Override
1564 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001565 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001566 }
1567
1568 /**
1569 * Returns the data network type for a subId
1570 */
1571 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001572 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001573 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 }
1575
1576 /**
1577 * Returns the data network type
1578 */
1579 @Override
1580 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001581 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001582 }
1583
1584 /**
1585 * Returns the Voice network type for a subId
1586 */
1587 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001588 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001589 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
1592 /**
1593 * @return true if a ICC card is present
1594 */
1595 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001596 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001597 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001598 }
1599
1600 /**
1601 * @return true if a ICC card is present for a slotId
1602 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001603 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001604 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1605 if (subId != null) {
1606 return getPhone(subId[0]).getIccCard().hasIccCard();
1607 } else {
1608 return false;
1609 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001610 }
1611
1612 /**
1613 * Return if the current radio is LTE on CDMA. This
1614 * is a tri-state return value as for a period of time
1615 * the mode may be unknown.
1616 *
1617 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001618 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 */
1620 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001621 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001622 }
1623
Wink Savilleb564aae2014-10-23 10:18:09 -07001624 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001625 return getPhone(subId).getLteOnCdmaMode();
1626 }
1627
1628 public void setPhone(Phone phone) {
1629 mPhone = phone;
1630 }
1631
1632 /**
1633 * {@hide}
1634 * Returns Default subId, 0 in the case of single standby.
1635 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001636 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001637 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001638 }
1639
Wink Savilleb564aae2014-10-23 10:18:09 -07001640 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001641 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001643
1644 /**
1645 * @see android.telephony.TelephonyManager.WifiCallingChoices
1646 */
1647 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001648 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1649 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001650 }
1651
1652 /**
1653 * @see android.telephony.TelephonyManager.WifiCallingChoices
1654 */
1655 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001656 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1657 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1658 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001659 }
1660
Sailesh Nepald1e68152013-12-12 19:08:02 -08001661 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001662 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001663 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001664 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001665
Shishir Agrawal566b7612013-10-28 14:41:00 -07001666 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001667 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001668 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001669
1670 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001671 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1672 CMD_OPEN_CHANNEL, AID);
1673 if (DBG) log("iccOpenLogicalChannel: " + response);
1674 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001675 }
1676
1677 @Override
1678 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001679 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001680
1681 if (DBG) log("iccCloseLogicalChannel: " + channel);
1682 if (channel < 0) {
1683 return false;
1684 }
Jake Hambye994d462014-02-03 13:10:13 -08001685 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001686 if (DBG) log("iccCloseLogicalChannel: " + success);
1687 return success;
1688 }
1689
1690 @Override
1691 public String iccTransmitApduLogicalChannel(int channel, int cla,
1692 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001693 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001694
1695 if (DBG) {
1696 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1697 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1698 " data=" + data);
1699 }
1700
1701 if (channel < 0) {
1702 return "";
1703 }
1704
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001705 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001706 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1707 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1708
Shishir Agrawal566b7612013-10-28 14:41:00 -07001709 // Append the returned status code to the end of the response payload.
1710 String s = Integer.toHexString(
1711 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001712 if (response.payload != null) {
1713 s = IccUtils.bytesToHexString(response.payload) + s;
1714 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001715 return s;
1716 }
Jake Hambye994d462014-02-03 13:10:13 -08001717
Evan Charltonc66da362014-05-16 14:06:40 -07001718 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001719 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1720 int p3, String data) {
1721 enforceModifyPermissionOrCarrierPrivilege();
1722
1723 if (DBG) {
1724 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1725 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1726 }
1727
1728 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1729 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1730 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1731
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001732 // Append the returned status code to the end of the response payload.
1733 String s = Integer.toHexString(
1734 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001735 if (response.payload != null) {
1736 s = IccUtils.bytesToHexString(response.payload) + s;
1737 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001738 return s;
1739 }
1740
1741 @Override
1742 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1743 String filePath) {
1744 enforceModifyPermissionOrCarrierPrivilege();
1745
1746 if (DBG) {
1747 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1748 p1 + " " + p2 + " " + p3 + ":" + filePath);
1749 }
1750
1751 IccIoResult response =
1752 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001753 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001754
1755 if (DBG) {
1756 log("Exchange SIM_IO [R]" + response);
1757 }
1758
1759 byte[] result = null;
1760 int length = 2;
1761 if (response.payload != null) {
1762 length = 2 + response.payload.length;
1763 result = new byte[length];
1764 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1765 } else {
1766 result = new byte[length];
1767 }
1768
1769 result[length - 1] = (byte) response.sw2;
1770 result[length - 2] = (byte) response.sw1;
1771 return result;
1772 }
1773
1774 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001775 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001776 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001777
1778 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1779 if (response.payload == null) {
1780 return "";
1781 }
1782
1783 // Append the returned status code to the end of the response payload.
1784 String s = Integer.toHexString(
1785 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1786 s = IccUtils.bytesToHexString(response.payload) + s;
1787 return s;
1788 }
1789
Jake Hambye994d462014-02-03 13:10:13 -08001790 /**
1791 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1792 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1793 *
1794 * @param itemID the ID of the item to read
1795 * @return the NV item as a String, or null on error.
1796 */
1797 @Override
1798 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001799 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001800 if (DBG) log("nvReadItem: item " + itemID);
1801 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1802 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1803 return value;
1804 }
1805
1806 /**
1807 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1808 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1809 *
1810 * @param itemID the ID of the item to read
1811 * @param itemValue the value to write, as a String
1812 * @return true on success; false on any failure
1813 */
1814 @Override
1815 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001816 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001817 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1818 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1819 new Pair<Integer, String>(itemID, itemValue));
1820 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1821 return success;
1822 }
1823
1824 /**
1825 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1826 * Used for device configuration by some CDMA operators.
1827 *
1828 * @param preferredRoamingList byte array containing the new PRL
1829 * @return true on success; false on any failure
1830 */
1831 @Override
1832 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001833 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001834 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1835 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1836 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1837 return success;
1838 }
1839
1840 /**
1841 * Perform the specified type of NV config reset.
1842 * Used for device configuration by some CDMA operators.
1843 *
1844 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1845 * @return true on success; false on any failure
1846 */
1847 @Override
1848 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001849 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001850 if (DBG) log("nvResetConfig: type " + resetType);
1851 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1852 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1853 return success;
1854 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001855
1856 /**
Wink Saville36469e72014-06-11 15:17:00 -07001857 * {@hide}
1858 * Returns Default sim, 0 in the case of single standby.
1859 */
1860 public int getDefaultSim() {
1861 //TODO Need to get it from Telephony Devcontroller
1862 return 0;
1863 }
1864
ram87fca6f2014-07-18 18:58:44 +05301865 public String[] getPcscfAddress(String apnType) {
Wink Saville36469e72014-06-11 15:17:00 -07001866 enforceReadPermission();
ram87fca6f2014-07-18 18:58:44 +05301867 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001868 }
1869
1870 public void setImsRegistrationState(boolean registered) {
1871 enforceModifyPermission();
1872 mPhone.setImsRegistrationState(registered);
1873 }
1874
1875 /**
Stuart Scott54788802015-03-30 13:18:01 -07001876 * Set the network selection mode to automatic.
1877 *
1878 */
1879 @Override
1880 public void setNetworkSelectionModeAutomatic(int subId) {
1881 enforceModifyPermissionOrCarrierPrivilege();
1882 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1883 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1884 }
1885
1886 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001887 * Get the calculated preferred network type.
1888 * Used for debugging incorrect network type.
1889 *
1890 * @return the preferred network type, defined in RILConstants.java.
1891 */
1892 @Override
1893 public int getCalculatedPreferredNetworkType() {
1894 enforceReadPermission();
Amit Mahajan43330e02014-11-18 11:54:45 -08001895 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001896 }
1897
1898 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001899 * Get the preferred network type.
1900 * Used for device configuration by some CDMA operators.
1901 *
1902 * @return the preferred network type, defined in RILConstants.java.
1903 */
1904 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001905 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001906 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001907 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001908 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001909 int networkType = (result != null ? result[0] : -1);
1910 if (DBG) log("getPreferredNetworkType: " + networkType);
1911 return networkType;
1912 }
1913
1914 /**
1915 * Set the preferred network type.
1916 * Used for device configuration by some CDMA operators.
1917 *
1918 * @param networkType the preferred network type, defined in RILConstants.java.
1919 * @return true on success; false on any failure.
1920 */
1921 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001922 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001923 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001924 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1925 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001926 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001927 if (success) {
1928 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001929 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001930 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001931 return success;
1932 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001933
1934 /**
Junda Liu475951f2014-11-07 16:45:03 -08001935 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1936 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1937 * tethering.
1938 *
1939 * @return 0: Not required. 1: required. 2: Not set.
1940 * @hide
1941 */
1942 @Override
1943 public int getTetherApnRequired() {
1944 enforceModifyPermissionOrCarrierPrivilege();
1945 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1946 Settings.Global.TETHER_DUN_REQUIRED, 2);
1947 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1948 // config_tether_apndata.
1949 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1950 dunRequired = 1;
1951 }
1952 return dunRequired;
1953 }
1954
1955 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001956 * Set mobile data enabled
1957 * Used by the user through settings etc to turn on/off mobile data
1958 *
1959 * @param enable {@code true} turn turn data on, else {@code false}
1960 */
1961 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001962 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001963 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001964 int phoneId = mSubscriptionController.getPhoneId(subId);
1965 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1966 Phone phone = PhoneFactory.getPhone(phoneId);
1967 if (phone != null) {
1968 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1969 phone.setDataEnabled(enable);
1970 } else {
1971 loge("setDataEnabled: no phone for subId=" + subId);
1972 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001973 }
1974
1975 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001976 * Get whether mobile data is enabled.
1977 *
1978 * Note that this used to be available from ConnectivityService, gated by
1979 * ACCESS_NETWORK_STATE permission, so this will accept either that or
1980 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07001981 *
1982 * @return {@code true} if data is enabled else {@code false}
1983 */
1984 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001985 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07001986 try {
1987 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
1988 null);
1989 } catch (Exception e) {
1990 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
1991 null);
1992 }
Wink Savillee7353bb2014-12-05 14:21:41 -08001993 int phoneId = mSubscriptionController.getPhoneId(subId);
1994 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1995 Phone phone = PhoneFactory.getPhone(phoneId);
1996 if (phone != null) {
1997 boolean retVal = phone.getDataEnabled();
1998 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
1999 return retVal;
2000 } else {
2001 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2002 return false;
2003 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002004 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002005
2006 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002007 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002008 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002009 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002010 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002011 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2012 }
2013 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002014 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002015 }
Junda Liu29340342014-07-10 15:23:27 -07002016
2017 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002018 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002019 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002020 if (card == null) {
2021 loge("checkCarrierPrivilegesForPackage: No UICC");
2022 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2023 }
2024 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002025 }
Derek Tan89e89d42014-07-08 17:00:10 -07002026
2027 @Override
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002028 public List<String> getCarrierPackageNamesForIntent(Intent intent) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002029 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002030 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002031 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002032 return null ;
2033 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002034 return card.getCarrierPackageNamesForIntent(
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002035 mPhone.getContext().getPackageManager(), intent);
2036 }
2037
Wink Savilleb564aae2014-10-23 10:18:09 -07002038 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002039 UiccCard card = getPhone(subId).getUiccCard();
2040 if (card == null) {
2041 loge("getIccId: No UICC");
2042 return null;
2043 }
2044 String iccId = card.getIccId();
2045 if (TextUtils.isEmpty(iccId)) {
2046 loge("getIccId: ICC ID is null or empty.");
2047 return null;
2048 }
2049 return iccId;
2050 }
2051
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002052 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002053 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2054 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002055 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002056
Jeff Sharkey85190e62014-12-05 09:40:12 -08002057 final String iccId = getIccId(subId);
2058 final String subscriberId = getPhone(subId).getSubscriberId();
2059
2060 if (DBG_MERGE) {
2061 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2062 + subscriberId + " to " + number);
2063 }
2064
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002065 if (TextUtils.isEmpty(iccId)) {
2066 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002067 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002068
Jeff Sharkey85190e62014-12-05 09:40:12 -08002069 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2070
2071 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002072 if (alphaTag == null) {
2073 editor.remove(alphaTagPrefKey);
2074 } else {
2075 editor.putString(alphaTagPrefKey, alphaTag);
2076 }
2077
Jeff Sharkey85190e62014-12-05 09:40:12 -08002078 // Record both the line number and IMSI for this ICCID, since we need to
2079 // track all merged IMSIs based on line number
2080 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2081 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002082 if (number == null) {
2083 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002084 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002085 } else {
2086 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002087 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002088 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002089
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002090 editor.commit();
2091 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002092 }
2093
2094 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002095 public String getLine1NumberForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002096 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002097
2098 String iccId = getIccId(subId);
2099 if (iccId != null) {
2100 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002101 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002102 }
Derek Tan97ebb422014-09-05 16:55:38 -07002103 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002104 }
2105
2106 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002107 public String getLine1AlphaTagForDisplay(int subId) {
Derek Tan7226c842014-07-02 17:42:23 -07002108 enforceReadPermission();
Derek Tan97ebb422014-09-05 16:55:38 -07002109
2110 String iccId = getIccId(subId);
2111 if (iccId != null) {
2112 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002113 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002114 }
Derek Tan97ebb422014-09-05 16:55:38 -07002115 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002116 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002117
2118 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002119 public String[] getMergedSubscriberIds() {
2120 final Context context = mPhone.getContext();
2121 final TelephonyManager tele = TelephonyManager.from(context);
2122 final SubscriptionManager sub = SubscriptionManager.from(context);
2123
2124 // Figure out what subscribers are currently active
2125 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2126 final int[] subIds = sub.getActiveSubscriptionIdList();
2127 for (int subId : subIds) {
2128 activeSubscriberIds.add(tele.getSubscriberId(subId));
2129 }
2130
2131 // First pass, find a number override for an active subscriber
2132 String mergeNumber = null;
2133 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2134 for (String key : prefs.keySet()) {
2135 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2136 final String subscriberId = (String) prefs.get(key);
2137 if (activeSubscriberIds.contains(subscriberId)) {
2138 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2139 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2140 mergeNumber = (String) prefs.get(numberKey);
2141 if (DBG_MERGE) {
2142 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2143 + " for active subscriber " + subscriberId);
2144 }
2145 if (!TextUtils.isEmpty(mergeNumber)) {
2146 break;
2147 }
2148 }
2149 }
2150 }
2151
2152 // Shortcut when no active merged subscribers
2153 if (TextUtils.isEmpty(mergeNumber)) {
2154 return null;
2155 }
2156
2157 // Second pass, find all subscribers under that line override
2158 final ArraySet<String> result = new ArraySet<>();
2159 for (String key : prefs.keySet()) {
2160 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2161 final String number = (String) prefs.get(key);
2162 if (mergeNumber.equals(number)) {
2163 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2164 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2165 final String subscriberId = (String) prefs.get(subscriberKey);
2166 if (!TextUtils.isEmpty(subscriberId)) {
2167 result.add(subscriberId);
2168 }
2169 }
2170 }
2171 }
2172
2173 final String[] resultArray = result.toArray(new String[result.size()]);
2174 Arrays.sort(resultArray);
2175 if (DBG_MERGE) {
2176 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2177 }
2178 return resultArray;
2179 }
2180
2181 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002182 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002183 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002184 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002185 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002186
2187 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002188 public boolean setRoamingOverride(List<String> gsmRoamingList,
2189 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2190 List<String> cdmaNonRoamingList) {
2191 enforceCarrierPrivilege();
2192 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2193 cdmaNonRoamingList);
2194 }
2195
2196 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002197 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2198 enforceModifyPermission();
2199
2200 int returnValue = 0;
2201 try {
2202 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2203 if(result.exception == null) {
2204 if (result.result != null) {
2205 byte[] responseData = (byte[])(result.result);
2206 if(responseData.length > oemResp.length) {
2207 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2208 responseData.length + "bytes. Buffer Size is " +
2209 oemResp.length + "bytes.");
2210 }
2211 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2212 returnValue = responseData.length;
2213 }
2214 } else {
2215 CommandException ex = (CommandException) result.exception;
2216 returnValue = ex.getCommandError().ordinal();
2217 if(returnValue > 0) returnValue *= -1;
2218 }
2219 } catch (RuntimeException e) {
2220 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2221 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2222 if(returnValue > 0) returnValue *= -1;
2223 }
2224
2225 return returnValue;
2226 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002227
2228 @Override
2229 public void setRadioCapability(RadioAccessFamily[] rafs) {
2230 try {
2231 ProxyController.getInstance().setRadioCapability(rafs);
2232 } catch (RuntimeException e) {
2233 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2234 }
2235 }
2236
2237 @Override
2238 public int getRadioAccessFamily(int phoneId) {
2239 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2240 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002241
2242 @Override
2243 public void enableVideoCalling(boolean enable) {
2244 enforceModifyPermission();
2245 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2246 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2247 editor.commit();
2248 }
2249
2250 @Override
2251 public boolean isVideoCallingEnabled() {
2252 enforceReadPermission();
Andrew Lee77527ac2014-10-21 16:57:39 -07002253 // Check the user preference and the system-level IMS setting. Even if the user has
2254 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2255 // In the long run, we may instead need to check if there exists a connection service
2256 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002257 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2258 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2259 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002260 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002261
Andrew Leea1239f22015-03-02 17:44:07 -08002262 @Override
2263 public boolean canChangeDtmfToneLength() {
2264 return mPhone.getContext().getResources().getBoolean(R.bool.dtmf_type_enabled);
2265 }
2266
2267 @Override
2268 public boolean isWorldPhone() {
2269 return mPhone.getContext().getResources().getBoolean(R.bool.world_phone);
2270 }
2271
Andrew Lee9431b832015-03-09 18:46:45 -07002272 @Override
2273 public boolean isTtyModeSupported() {
2274 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2275 TelephonyManager telephonyManager =
2276 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2277 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2278 }
2279
2280 @Override
2281 public boolean isHearingAidCompatibilitySupported() {
2282 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2283 }
2284
Sanket Padawe7310cc72015-01-14 09:53:20 -08002285 /**
2286 * Returns the unique device ID of phone, for example, the IMEI for
2287 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2288 *
2289 * <p>Requires Permission:
2290 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2291 */
2292 @Override
2293 public String getDeviceId() {
2294 enforceReadPermission();
2295 final Phone phone = PhoneFactory.getPhone(0);
2296 if (phone != null) {
2297 return phone.getDeviceId();
2298 } else {
2299 return null;
2300 }
2301 }
2302
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002303 /*
2304 * {@hide}
2305 * Returns the IMS Registration Status
2306 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002307 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002308 public boolean isImsRegistered() {
2309 return mPhone.isImsRegistered();
2310 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002311
2312 @Override
2313 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2314 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2315 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002316
Nathan Haroldc55097a2015-03-11 18:14:50 -07002317 /*
2318 * {@hide}
2319 * Returns the IMS Registration Status
2320 */
2321 public boolean isWifiCallingEnabled() {
2322 return mPhone.isWifiCallingEnabled();
2323 }
2324
2325 /*
2326 * {@hide}
2327 * Returns the IMS Registration Status
2328 */
2329 public boolean isVolteEnabled() {
2330 return mPhone.isVolteEnabled();
2331 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332}