blob: 3045903df60c5821b5250e1d406c5670a9dd94cf [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;
Svet Ganovb320e182015-04-16 12:30:10 -070065import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080066import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070067import com.android.internal.telephony.uicc.IccIoResult;
68import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070069import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070070import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080071import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072
Wink Saville36469e72014-06-11 15:17:00 -070073import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
74
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080076import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080077import java.util.HashMap;
78import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080079import java.util.List;
Jeff Sharkey85190e62014-12-05 09:40:12 -080080import java.util.Map;
81import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082
83/**
84 * Implementation of the ITelephony interface.
85 */
Santos Cordon117fee72014-05-16 17:56:12 -070086public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087 private static final String LOG_TAG = "PhoneInterfaceManager";
88 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
89 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080090 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091
92 // Message codes used with mMainThreadHandler
93 private static final int CMD_HANDLE_PIN_MMI = 1;
94 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
95 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
96 private static final int CMD_ANSWER_RINGING_CALL = 4;
97 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070098 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
99 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700100 private static final int CMD_OPEN_CHANNEL = 9;
101 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
102 private static final int CMD_CLOSE_CHANNEL = 11;
103 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800104 private static final int CMD_NV_READ_ITEM = 13;
105 private static final int EVENT_NV_READ_ITEM_DONE = 14;
106 private static final int CMD_NV_WRITE_ITEM = 15;
107 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
108 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
109 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
110 private static final int CMD_NV_RESET_CONFIG = 19;
111 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800112 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
113 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
114 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
115 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800116 private static final int CMD_SEND_ENVELOPE = 25;
117 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700118 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
119 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
120 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
121 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
122 private static final int CMD_EXCHANGE_SIM_IO = 31;
123 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800124 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
125 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700126 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
127 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
129 /** The singleton instance. */
130 private static PhoneInterfaceManager sInstance;
131
Wink Saville3ab207e2014-11-20 13:07:20 -0800132 private PhoneGlobals mApp;
133 private Phone mPhone;
134 private CallManager mCM;
135 private AppOpsManager mAppOps;
136 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800137 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800138 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139
Derek Tan97ebb422014-09-05 16:55:38 -0700140 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
141 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800142 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700143 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700144
145 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700146 * A request object to use for transmitting data to an ICC.
147 */
148 private static final class IccAPDUArgument {
149 public int channel, cla, command, p1, p2, p3;
150 public String data;
151
152 public IccAPDUArgument(int channel, int cla, int command,
153 int p1, int p2, int p3, String data) {
154 this.channel = channel;
155 this.cla = cla;
156 this.command = command;
157 this.p1 = p1;
158 this.p2 = p2;
159 this.p3 = p3;
160 this.data = data;
161 }
162 }
163
164 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
166 * request after sending. The main thread will notify the request when it is complete.
167 */
168 private static final class MainThreadRequest {
169 /** The argument to use for the request */
170 public Object argument;
171 /** The result of the request that is run on the main thread */
172 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800173 /** The subscriber id that this request applies to. Null if default. */
174 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175
176 public MainThreadRequest(Object argument) {
177 this.argument = argument;
178 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800179
180 public MainThreadRequest(Object argument, Integer subId) {
181 this.argument = argument;
182 this.subId = subId;
183 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 }
185
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800186 private static final class IncomingThirdPartyCallArgs {
187 public final ComponentName component;
188 public final String callId;
189 public final String callerDisplayName;
190
191 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
192 String callerDisplayName) {
193 this.component = component;
194 this.callId = callId;
195 this.callerDisplayName = callerDisplayName;
196 }
197 }
198
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 /**
200 * A handler that processes messages on the main thread in the phone process. Since many
201 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
202 * inbound binder threads to the main thread in the phone process. The Binder thread
203 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
204 * on, which will be notified when the operation completes and will contain the result of the
205 * request.
206 *
207 * <p>If a MainThreadRequest object is provided in the msg.obj field,
208 * note that request.result must be set to something non-null for the calling thread to
209 * unblock.
210 */
211 private final class MainThreadHandler extends Handler {
212 @Override
213 public void handleMessage(Message msg) {
214 MainThreadRequest request;
215 Message onCompleted;
216 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800217 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700218 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219
220 switch (msg.what) {
221 case CMD_HANDLE_PIN_MMI:
222 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800223 request.result = getPhoneFromRequest(request).handlePinMmi(
224 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 // Wake up the requesting thread
226 synchronized (request) {
227 request.notifyAll();
228 }
229 break;
230
231 case CMD_HANDLE_NEIGHBORING_CELL:
232 request = (MainThreadRequest) msg.obj;
233 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
234 request);
235 mPhone.getNeighboringCids(onCompleted);
236 break;
237
238 case EVENT_NEIGHBORING_CELL_DONE:
239 ar = (AsyncResult) msg.obj;
240 request = (MainThreadRequest) ar.userObj;
241 if (ar.exception == null && ar.result != null) {
242 request.result = ar.result;
243 } else {
244 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800245 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 }
247 // Wake up the requesting thread
248 synchronized (request) {
249 request.notifyAll();
250 }
251 break;
252
253 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700254 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800255 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700256 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 break;
258
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 case CMD_END_CALL:
260 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800261 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700262 final boolean hungUp;
263 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
265 // CDMA: If the user presses the Power button we treat it as
266 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700267 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700268 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
269 // GSM: End the call as per the Phone state
270 hungUp = PhoneUtils.hangup(mCM);
271 } else {
272 throw new IllegalStateException("Unexpected phone type: " + phoneType);
273 }
274 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
275 request.result = hungUp;
276 // Wake up the requesting thread
277 synchronized (request) {
278 request.notifyAll();
279 }
280 break;
281
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700282 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700283 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700284 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700285 if (uiccCard == null) {
286 loge("iccTransmitApduLogicalChannel: No UICC");
287 request.result = new IccIoResult(0x6F, 0, (byte[])null);
288 synchronized (request) {
289 request.notifyAll();
290 }
291 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700292 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
293 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700294 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700295 iccArgument.channel, iccArgument.cla, iccArgument.command,
296 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700297 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700298 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700299 break;
300
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700301 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700302 ar = (AsyncResult) msg.obj;
303 request = (MainThreadRequest) ar.userObj;
304 if (ar.exception == null && ar.result != null) {
305 request.result = ar.result;
306 } else {
307 request.result = new IccIoResult(0x6F, 0, (byte[])null);
308 if (ar.result == null) {
309 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800310 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700311 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800312 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700313 } else {
314 loge("iccTransmitApduLogicalChannel: Unknown exception");
315 }
316 }
317 synchronized (request) {
318 request.notifyAll();
319 }
320 break;
321
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700322 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
323 request = (MainThreadRequest) msg.obj;
324 iccArgument = (IccAPDUArgument) request.argument;
325 if (uiccCard == null) {
326 loge("iccTransmitApduBasicChannel: No UICC");
327 request.result = new IccIoResult(0x6F, 0, (byte[])null);
328 synchronized (request) {
329 request.notifyAll();
330 }
331 } else {
332 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
333 request);
334 uiccCard.iccTransmitApduBasicChannel(
335 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
336 iccArgument.p3, iccArgument.data, onCompleted);
337 }
338 break;
339
340 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
341 ar = (AsyncResult) msg.obj;
342 request = (MainThreadRequest) ar.userObj;
343 if (ar.exception == null && ar.result != null) {
344 request.result = ar.result;
345 } else {
346 request.result = new IccIoResult(0x6F, 0, (byte[])null);
347 if (ar.result == null) {
348 loge("iccTransmitApduBasicChannel: Empty response");
349 } else if (ar.exception instanceof CommandException) {
350 loge("iccTransmitApduBasicChannel: CommandException: " +
351 ar.exception);
352 } else {
353 loge("iccTransmitApduBasicChannel: Unknown exception");
354 }
355 }
356 synchronized (request) {
357 request.notifyAll();
358 }
359 break;
360
361 case CMD_EXCHANGE_SIM_IO:
362 request = (MainThreadRequest) msg.obj;
363 iccArgument = (IccAPDUArgument) request.argument;
364 if (uiccCard == null) {
365 loge("iccExchangeSimIO: No UICC");
366 request.result = new IccIoResult(0x6F, 0, (byte[])null);
367 synchronized (request) {
368 request.notifyAll();
369 }
370 } else {
371 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
372 request);
373 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
374 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
375 iccArgument.data, onCompleted);
376 }
377 break;
378
379 case EVENT_EXCHANGE_SIM_IO_DONE:
380 ar = (AsyncResult) msg.obj;
381 request = (MainThreadRequest) ar.userObj;
382 if (ar.exception == null && ar.result != null) {
383 request.result = ar.result;
384 } else {
385 request.result = new IccIoResult(0x6f, 0, (byte[])null);
386 }
387 synchronized (request) {
388 request.notifyAll();
389 }
390 break;
391
Derek Tan4d5e5c12014-02-04 11:54:58 -0800392 case CMD_SEND_ENVELOPE:
393 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700394 if (uiccCard == null) {
395 loge("sendEnvelopeWithStatus: No UICC");
396 request.result = new IccIoResult(0x6F, 0, (byte[])null);
397 synchronized (request) {
398 request.notifyAll();
399 }
400 } else {
401 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
402 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
403 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800404 break;
405
406 case EVENT_SEND_ENVELOPE_DONE:
407 ar = (AsyncResult) msg.obj;
408 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700409 if (ar.exception == null && ar.result != null) {
410 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800411 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700412 request.result = new IccIoResult(0x6F, 0, (byte[])null);
413 if (ar.result == null) {
414 loge("sendEnvelopeWithStatus: Empty response");
415 } else if (ar.exception instanceof CommandException) {
416 loge("sendEnvelopeWithStatus: CommandException: " +
417 ar.exception);
418 } else {
419 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
420 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800421 }
422 synchronized (request) {
423 request.notifyAll();
424 }
425 break;
426
Shishir Agrawal566b7612013-10-28 14:41:00 -0700427 case CMD_OPEN_CHANNEL:
428 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700429 if (uiccCard == null) {
430 loge("iccOpenLogicalChannel: No UICC");
431 request.result = new IccIoResult(0x6F, 0, (byte[])null);
432 synchronized (request) {
433 request.notifyAll();
434 }
435 } else {
436 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
437 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
438 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700439 break;
440
441 case EVENT_OPEN_CHANNEL_DONE:
442 ar = (AsyncResult) msg.obj;
443 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700444 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700445 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700446 int[] result = (int[]) ar.result;
447 int channelId = result[0];
448 byte[] selectResponse = null;
449 if (result.length > 1) {
450 selectResponse = new byte[result.length - 1];
451 for (int i = 1; i < result.length; ++i) {
452 selectResponse[i - 1] = (byte) result[i];
453 }
454 }
455 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700456 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700457 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 if (ar.result == null) {
459 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700460 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700461 if (ar.exception != null) {
462 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
463 }
464
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700465 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700466 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
467 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700468 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700469 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700470 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700471 }
472 }
473 openChannelResp = new IccOpenLogicalChannelResponse(
474 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700475 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700476 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700477 synchronized (request) {
478 request.notifyAll();
479 }
480 break;
481
482 case CMD_CLOSE_CHANNEL:
483 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700484 if (uiccCard == null) {
485 loge("iccCloseLogicalChannel: No UICC");
486 request.result = new IccIoResult(0x6F, 0, (byte[])null);
487 synchronized (request) {
488 request.notifyAll();
489 }
490 } else {
491 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
492 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
493 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700494 break;
495
496 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800497 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
498 break;
499
500 case CMD_NV_READ_ITEM:
501 request = (MainThreadRequest) msg.obj;
502 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
503 mPhone.nvReadItem((Integer) request.argument, onCompleted);
504 break;
505
506 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700507 ar = (AsyncResult) msg.obj;
508 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800509 if (ar.exception == null && ar.result != null) {
510 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800512 request.result = "";
513 if (ar.result == null) {
514 loge("nvReadItem: Empty response");
515 } else if (ar.exception instanceof CommandException) {
516 loge("nvReadItem: CommandException: " +
517 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700518 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800519 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700520 }
521 }
522 synchronized (request) {
523 request.notifyAll();
524 }
525 break;
526
Jake Hambye994d462014-02-03 13:10:13 -0800527 case CMD_NV_WRITE_ITEM:
528 request = (MainThreadRequest) msg.obj;
529 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
530 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
531 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
532 break;
533
534 case EVENT_NV_WRITE_ITEM_DONE:
535 handleNullReturnEvent(msg, "nvWriteItem");
536 break;
537
538 case CMD_NV_WRITE_CDMA_PRL:
539 request = (MainThreadRequest) msg.obj;
540 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
541 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
542 break;
543
544 case EVENT_NV_WRITE_CDMA_PRL_DONE:
545 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
546 break;
547
548 case CMD_NV_RESET_CONFIG:
549 request = (MainThreadRequest) msg.obj;
550 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
551 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
552 break;
553
554 case EVENT_NV_RESET_CONFIG_DONE:
555 handleNullReturnEvent(msg, "nvResetConfig");
556 break;
557
Jake Hamby7c27be32014-03-03 13:25:59 -0800558 case CMD_GET_PREFERRED_NETWORK_TYPE:
559 request = (MainThreadRequest) msg.obj;
560 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700561 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800562 break;
563
564 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
565 ar = (AsyncResult) msg.obj;
566 request = (MainThreadRequest) ar.userObj;
567 if (ar.exception == null && ar.result != null) {
568 request.result = ar.result; // Integer
569 } else {
570 request.result = -1;
571 if (ar.result == null) {
572 loge("getPreferredNetworkType: Empty response");
573 } else if (ar.exception instanceof CommandException) {
574 loge("getPreferredNetworkType: CommandException: " +
575 ar.exception);
576 } else {
577 loge("getPreferredNetworkType: Unknown exception");
578 }
579 }
580 synchronized (request) {
581 request.notifyAll();
582 }
583 break;
584
585 case CMD_SET_PREFERRED_NETWORK_TYPE:
586 request = (MainThreadRequest) msg.obj;
587 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
588 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700589 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800590 break;
591
592 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
593 handleNullReturnEvent(msg, "setPreferredNetworkType");
594 break;
595
Steven Liu4bf01bc2014-07-17 11:05:29 -0500596 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
597 request = (MainThreadRequest)msg.obj;
598 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
599 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
600 break;
601
602 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
603 ar = (AsyncResult)msg.obj;
604 request = (MainThreadRequest)ar.userObj;
605 request.result = ar;
606 synchronized (request) {
607 request.notifyAll();
608 }
609 break;
610
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800611 case CMD_SET_VOICEMAIL_NUMBER:
612 request = (MainThreadRequest) msg.obj;
613 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
614 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800615 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
616 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800617 break;
618
619 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
620 handleNullReturnEvent(msg, "setVoicemailNumber");
621 break;
622
Stuart Scott54788802015-03-30 13:18:01 -0700623 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
624 request = (MainThreadRequest) msg.obj;
625 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
626 request);
627 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
628 break;
629
630 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
631 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
632 break;
633
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700634 default:
635 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
636 break;
637 }
638 }
Jake Hambye994d462014-02-03 13:10:13 -0800639
640 private void handleNullReturnEvent(Message msg, String command) {
641 AsyncResult ar = (AsyncResult) msg.obj;
642 MainThreadRequest request = (MainThreadRequest) ar.userObj;
643 if (ar.exception == null) {
644 request.result = true;
645 } else {
646 request.result = false;
647 if (ar.exception instanceof CommandException) {
648 loge(command + ": CommandException: " + ar.exception);
649 } else {
650 loge(command + ": Unknown exception");
651 }
652 }
653 synchronized (request) {
654 request.notifyAll();
655 }
656 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 }
658
659 /**
660 * Posts the specified command to be executed on the main thread,
661 * waits for the request to complete, and returns the result.
662 * @see #sendRequestAsync
663 */
664 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700665 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700666 }
667
668 /**
669 * Posts the specified command to be executed on the main thread,
670 * waits for the request to complete, and returns the result.
671 * @see #sendRequestAsync
672 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800673 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
675 throw new RuntimeException("This method will deadlock if called from the main thread.");
676 }
677
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800678 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700679 Message msg = mMainThreadHandler.obtainMessage(command, request);
680 msg.sendToTarget();
681
682 // Wait for the request to complete
683 synchronized (request) {
684 while (request.result == null) {
685 try {
686 request.wait();
687 } catch (InterruptedException e) {
688 // Do nothing, go back and wait until the request is complete
689 }
690 }
691 }
692 return request.result;
693 }
694
695 /**
696 * Asynchronous ("fire and forget") version of sendRequest():
697 * Posts the specified command to be executed on the main thread, and
698 * returns immediately.
699 * @see #sendRequest
700 */
701 private void sendRequestAsync(int command) {
702 mMainThreadHandler.sendEmptyMessage(command);
703 }
704
705 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700706 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
707 * @see {@link #sendRequest(int,Object)}
708 */
709 private void sendRequestAsync(int command, Object argument) {
710 MainThreadRequest request = new MainThreadRequest(argument);
711 Message msg = mMainThreadHandler.obtainMessage(command, request);
712 msg.sendToTarget();
713 }
714
715 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700716 * Initialize the singleton PhoneInterfaceManager instance.
717 * This is only done once, at startup, from PhoneApp.onCreate().
718 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700719 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700720 synchronized (PhoneInterfaceManager.class) {
721 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700722 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 } else {
724 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
725 }
726 return sInstance;
727 }
728 }
729
730 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700731 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732 mApp = app;
733 mPhone = phone;
734 mCM = PhoneGlobals.getInstance().mCM;
735 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
736 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700737 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700738 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800739 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800740
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 publish();
742 }
743
744 private void publish() {
745 if (DBG) log("publish: " + this);
746
747 ServiceManager.addService("phone", this);
748 }
749
Stuart Scott584921c2015-01-15 17:10:34 -0800750 private Phone getPhoneFromRequest(MainThreadRequest request) {
751 return (request.subId == null) ? mPhone : getPhone(request.subId);
752 }
753
Wink Saville36469e72014-06-11 15:17:00 -0700754 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700755 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800756 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700757 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 //
759 // Implementation of the ITelephony interface.
760 //
761
762 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700763 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700764 }
765
Wink Savilleb564aae2014-10-23 10:18:09 -0700766 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 if (DBG) log("dial: " + number);
768 // No permission check needed here: This is just a wrapper around the
769 // ACTION_DIAL intent, which is available to any app since it puts up
770 // the UI before it does anything.
771
772 String url = createTelUrl(number);
773 if (url == null) {
774 return;
775 }
776
777 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700778 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
780 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
781 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
782 mApp.startActivity(intent);
783 }
784 }
785
786 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700787 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700788 }
789
Wink Savilleb564aae2014-10-23 10:18:09 -0700790 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 if (DBG) log("call: " + number);
792
793 // This is just a wrapper around the ACTION_CALL intent, but we still
794 // need to do a permission check since we're calling startActivity()
795 // from the context of the phone app.
796 enforceCallPermission();
797
798 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
799 != AppOpsManager.MODE_ALLOWED) {
800 return;
801 }
802
803 String url = createTelUrl(number);
804 if (url == null) {
805 return;
806 }
807
Wink Saville08874612014-08-31 19:19:58 -0700808 boolean isValid = false;
Svetoslav483aff72015-04-21 14:16:07 -0700809 List<SubscriptionInfo> slist;
810
811 final long identity = Binder.clearCallingIdentity();
812 try {
813 slist = mSubscriptionController.getActiveSubscriptionInfoList(
814 mPhone.getContext().getOpPackageName());
815 } finally {
816 Binder.restoreCallingIdentity(identity);
817 }
818
Wink Saville3ab207e2014-11-20 13:07:20 -0800819 if (slist != null) {
820 for (SubscriptionInfo subInfoRecord : slist) {
821 if (subInfoRecord.getSubscriptionId() == subId) {
822 isValid = true;
823 break;
824 }
Wink Saville08874612014-08-31 19:19:58 -0700825 }
826 }
827 if (isValid == false) {
828 return;
829 }
830
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700831 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700832 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
834 mApp.startActivity(intent);
835 }
836
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 /**
838 * End a call based on call state
839 * @return true is a call was ended
840 */
841 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700842 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700843 }
844
845 /**
846 * End a call based on the call state of the subId
847 * @return true is a call was ended
848 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700849 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700850 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800851 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 }
853
854 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700855 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700856 }
857
Wink Savilleb564aae2014-10-23 10:18:09 -0700858 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 if (DBG) log("answerRingingCall...");
860 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
861 // but that can probably wait till the big TelephonyManager API overhaul.
862 // For now, protect this call with the MODIFY_PHONE_STATE permission.
863 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800864 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 }
866
867 /**
868 * Make the actual telephony calls to implement answerRingingCall().
869 * This should only be called from the main thread of the Phone app.
870 * @see #answerRingingCall
871 *
872 * TODO: it would be nice to return true if we answered the call, or
873 * false if there wasn't actually a ringing incoming call, or some
874 * other error occurred. (In other words, pass back the return value
875 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
876 * But that would require calling this method via sendRequest() rather
877 * than sendRequestAsync(), and right now we don't actually *need* that
878 * return value, so let's just return void for now.
879 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700880 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700881 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700883 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
884 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 if (hasActiveCall && hasHoldingCall) {
886 // Both lines are in use!
887 // TODO: provide a flag to let the caller specify what
888 // policy to use if both lines are in use. (The current
889 // behavior is hardwired to "answer incoming, end ongoing",
890 // which is how the CALL button is specced to behave.)
891 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
892 return;
893 } else {
894 // answerCall() will automatically hold the current active
895 // call, if there is one.
896 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
897 return;
898 }
899 } else {
900 // No call was ringing.
901 return;
902 }
903 }
904
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700905 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700906 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700907 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700908 public void silenceRinger() {
909 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700910 }
911
912 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700913 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700914 }
915
Wink Savilleb564aae2014-10-23 10:18:09 -0700916 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700917 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 }
919
920 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700921 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700922 }
923
Wink Savilleb564aae2014-10-23 10:18:09 -0700924 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700925 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 }
927
928 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700929 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700930 }
931
Wink Savilleb564aae2014-10-23 10:18:09 -0700932 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700933 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 }
935
Svet Ganovb320e182015-04-16 12:30:10 -0700936 public boolean isSimPinEnabled(String callingPackage) {
937 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
938 return false;
939 }
940
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 return (PhoneGlobals.getInstance().isSimPinEnabled());
942 }
943
944 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700945 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700946 }
947
Wink Savilleb564aae2014-10-23 10:18:09 -0700948 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700949 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700950 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
951 }
952
953 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700954 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700955 }
956
Wink Savilleb564aae2014-10-23 10:18:09 -0700957 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700958 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700959 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
960 }
961
962 /** {@hide} */
963 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700964 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700965 }
966
Wink Savilleb564aae2014-10-23 10:18:09 -0700967 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700969 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 checkSimPin.start();
971 return checkSimPin.unlockSim(null, pin);
972 }
973
Wink Saville9de0f752013-10-22 19:04:03 -0700974 /** {@hide} */
975 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700976 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700977 }
978
Wink Savilleb564aae2014-10-23 10:18:09 -0700979 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700981 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 checkSimPuk.start();
983 return checkSimPuk.unlockSim(puk, pin);
984 }
985
986 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700987 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 * a synchronous one.
989 */
990 private static class UnlockSim extends Thread {
991
992 private final IccCard mSimCard;
993
994 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700995 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
996 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997
998 // For replies from SimCard interface
999 private Handler mHandler;
1000
1001 // For async handler to identify request type
1002 private static final int SUPPLY_PIN_COMPLETE = 100;
1003
1004 public UnlockSim(IccCard simCard) {
1005 mSimCard = simCard;
1006 }
1007
1008 @Override
1009 public void run() {
1010 Looper.prepare();
1011 synchronized (UnlockSim.this) {
1012 mHandler = new Handler() {
1013 @Override
1014 public void handleMessage(Message msg) {
1015 AsyncResult ar = (AsyncResult) msg.obj;
1016 switch (msg.what) {
1017 case SUPPLY_PIN_COMPLETE:
1018 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1019 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001020 mRetryCount = msg.arg1;
1021 if (ar.exception != null) {
1022 if (ar.exception instanceof CommandException &&
1023 ((CommandException)(ar.exception)).getCommandError()
1024 == CommandException.Error.PASSWORD_INCORRECT) {
1025 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1026 } else {
1027 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1028 }
1029 } else {
1030 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1031 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 mDone = true;
1033 UnlockSim.this.notifyAll();
1034 }
1035 break;
1036 }
1037 }
1038 };
1039 UnlockSim.this.notifyAll();
1040 }
1041 Looper.loop();
1042 }
1043
1044 /*
1045 * Use PIN or PUK to unlock SIM card
1046 *
1047 * If PUK is null, unlock SIM card with PIN
1048 *
1049 * If PUK is not null, unlock SIM card with PUK and set PIN code
1050 */
Wink Saville9de0f752013-10-22 19:04:03 -07001051 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052
1053 while (mHandler == null) {
1054 try {
1055 wait();
1056 } catch (InterruptedException e) {
1057 Thread.currentThread().interrupt();
1058 }
1059 }
1060 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1061
1062 if (puk == null) {
1063 mSimCard.supplyPin(pin, callback);
1064 } else {
1065 mSimCard.supplyPuk(puk, pin, callback);
1066 }
1067
1068 while (!mDone) {
1069 try {
1070 Log.d(LOG_TAG, "wait for done");
1071 wait();
1072 } catch (InterruptedException e) {
1073 // Restore the interrupted status
1074 Thread.currentThread().interrupt();
1075 }
1076 }
1077 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001078 int[] resultArray = new int[2];
1079 resultArray[0] = mResult;
1080 resultArray[1] = mRetryCount;
1081 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 }
1083 }
1084
1085 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001086 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001087
1088 }
1089
Wink Savilleb564aae2014-10-23 10:18:09 -07001090 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001091 // No permission check needed here: this call is harmless, and it's
1092 // needed for the ServiceState.requestStateUpdate() call (which is
1093 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001094 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001095 }
1096
1097 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001098 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001099 }
1100
Wink Savilleb564aae2014-10-23 10:18:09 -07001101 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001102 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001103 }
1104
1105 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001106 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001107
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 }
Wink Saville36469e72014-06-11 15:17:00 -07001109
Wink Savilleb564aae2014-10-23 10:18:09 -07001110 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001112 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001113 }
1114
1115 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001116 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001117 }
1118
Wink Savilleb564aae2014-10-23 10:18:09 -07001119 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001120 enforceModifyPermission();
1121 if ((getPhone(subId).getServiceState().getState() !=
1122 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001123 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124 }
1125 return true;
1126 }
Wink Saville36469e72014-06-11 15:17:00 -07001127
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001128 public boolean needMobileRadioShutdown() {
1129 /*
1130 * If any of the Radios are available, it will need to be
1131 * shutdown. So return true if any Radio is available.
1132 */
1133 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1134 Phone phone = PhoneFactory.getPhone(i);
1135 if (phone != null && phone.isRadioAvailable()) return true;
1136 }
1137 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1138 return false;
1139 }
1140
1141 public void shutdownMobileRadios() {
1142 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1143 logv("Shutting down Phone " + i);
1144 shutdownRadioUsingPhoneId(i);
1145 }
1146 }
1147
1148 private void shutdownRadioUsingPhoneId(int phoneId) {
1149 enforceModifyPermission();
1150 Phone phone = PhoneFactory.getPhone(phoneId);
1151 if (phone != null && phone.isRadioAvailable()) {
1152 phone.shutdownRadio();
1153 }
1154 }
1155
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001156 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001157 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001158 }
1159
Wink Savilleb564aae2014-10-23 10:18:09 -07001160 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001162 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001163 return true;
1164 }
1165
Wink Saville36469e72014-06-11 15:17:00 -07001166 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 public boolean enableDataConnectivity() {
1168 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001169 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001170 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 return true;
1172 }
1173
Wink Saville36469e72014-06-11 15:17:00 -07001174 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001175 public boolean disableDataConnectivity() {
1176 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001177 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001178 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001179 return true;
1180 }
1181
Wink Saville36469e72014-06-11 15:17:00 -07001182 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001184 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001185 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186 }
1187
1188 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001189 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001190 }
1191
Wink Savilleb564aae2014-10-23 10:18:09 -07001192 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001194 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001198 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001199 }
1200
Wink Savilleb564aae2014-10-23 10:18:09 -07001201 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001202 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 }
1204
1205 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001206 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001207 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001208 }
1209
1210 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001211 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001212 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 }
1214
1215 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001216 public Bundle getCellLocation(String callingPackage) {
1217 enforceFineOrCoarseLocationPermission("getCellLocation");
1218
1219 // OP_COARSE_LOCATION controls both fine and coarse location.
1220 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1221 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1222 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 }
1224
Jake Hambye994d462014-02-03 13:10:13 -08001225 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001226 if (DBG_LOC) log("getCellLocation: is active user");
1227 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001228 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1229 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230 return data;
1231 } else {
1232 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1233 return null;
1234 }
1235 }
1236
Svetoslav64fad262015-04-14 14:35:21 -07001237 private void enforceFineOrCoarseLocationPermission(String message) {
1238 try {
1239 mApp.enforceCallingOrSelfPermission(
1240 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1241 } catch (SecurityException e) {
1242 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1243 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1244 // is the weaker precondition
1245 mApp.enforceCallingOrSelfPermission(
1246 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1247 }
1248 }
1249
1250
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251 @Override
1252 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001253 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001254 }
1255
Wink Savilleb564aae2014-10-23 10:18:09 -07001256 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 mApp.enforceCallingOrSelfPermission(
1258 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001259 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 }
1261
1262 @Override
1263 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001264 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001265 }
1266
Wink Savilleb564aae2014-10-23 10:18:09 -07001267 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 mApp.enforceCallingOrSelfPermission(
1269 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001270 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001271 }
1272
1273 @Override
1274 @SuppressWarnings("unchecked")
1275 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001276 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1277
1278 // OP_COARSE_LOCATION controls both fine and coarse location.
1279 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1280 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1281 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 }
1283
1284 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1285 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1286 return null;
1287 }
Svetoslav64fad262015-04-14 14:35:21 -07001288
Jake Hambye994d462014-02-03 13:10:13 -08001289 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1291
1292 ArrayList<NeighboringCellInfo> cells = null;
1293
1294 try {
1295 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001296 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001298 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001299 }
1300 return cells;
1301 } else {
1302 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1303 return null;
1304 }
1305 }
1306
1307
1308 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001309 public List<CellInfo> getAllCellInfo(String callingPackage) {
1310 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1311
1312 // OP_COARSE_LOCATION controls both fine and coarse location.
1313 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1314 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1315 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 }
1317
Jake Hambye994d462014-02-03 13:10:13 -08001318 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001320 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1321 for (Phone phone : PhoneFactory.getPhones()) {
1322 cellInfos.addAll(phone.getAllCellInfo());
1323 }
1324 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 } else {
1326 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1327 return null;
1328 }
1329 }
1330
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001331 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 public void setCellInfoListRate(int rateInMillis) {
1333 mPhone.setCellInfoListRate(rateInMillis);
1334 }
1335
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001336 //
1337 // Internal helper methods.
1338 //
1339
Jake Hambye994d462014-02-03 13:10:13 -08001340 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 boolean ok;
1342
1343 boolean self = Binder.getCallingUid() == Process.myUid();
1344 if (!self) {
1345 // Get the caller's user id then clear the calling identity
1346 // which will be restored in the finally clause.
1347 int callingUser = UserHandle.getCallingUserId();
1348 long ident = Binder.clearCallingIdentity();
1349
1350 try {
1351 // With calling identity cleared the current user is the foreground user.
1352 int foregroundUser = ActivityManager.getCurrentUser();
1353 ok = (foregroundUser == callingUser);
1354 if (DBG_LOC) {
1355 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1356 + " callingUser=" + callingUser + " ok=" + ok);
1357 }
1358 } catch (Exception ex) {
1359 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1360 ok = false;
1361 } finally {
1362 Binder.restoreCallingIdentity(ident);
1363 }
1364 } else {
1365 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1366 ok = true;
1367 }
1368 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1369 return ok;
1370 }
1371
1372 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1374 *
1375 * @throws SecurityException if the caller does not have the required permission
1376 */
1377 private void enforceModifyPermission() {
1378 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1379 }
1380
1381 /**
Junda Liua2e36012014-07-09 18:30:01 -07001382 * Make sure either system app or the caller has carrier privilege.
1383 *
1384 * @throws SecurityException if the caller does not have the required permission/privilege
1385 */
1386 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001387 int permission = mApp.checkCallingOrSelfPermission(
1388 android.Manifest.permission.MODIFY_PHONE_STATE);
1389 if (permission == PackageManager.PERMISSION_GRANTED) {
1390 return;
1391 }
1392
1393 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001394 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001395 loge("No Carrier Privilege.");
1396 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001397 }
1398 }
1399
1400 /**
1401 * Make sure the caller has carrier privilege.
1402 *
1403 * @throws SecurityException if the caller does not have the required permission
1404 */
1405 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001406 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001407 loge("No Carrier Privilege.");
1408 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001409 }
1410 }
1411
1412 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001413 * Make sure the caller has the CALL_PHONE permission.
1414 *
1415 * @throws SecurityException if the caller does not have the required permission
1416 */
1417 private void enforceCallPermission() {
1418 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1419 }
1420
Shishir Agrawal566b7612013-10-28 14:41:00 -07001421 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001422 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1423 *
1424 * @throws SecurityException if the caller does not have the required permission
1425 */
1426 private void enforcePrivilegedPhoneStatePermission() {
1427 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1428 null);
1429 }
1430
Stuart Scott8eef64f2015-04-08 15:13:54 -07001431 private void enforceConnectivityInternalPermission() {
1432 mApp.enforceCallingOrSelfPermission(
1433 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1434 "ConnectivityService");
1435 }
1436
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 private String createTelUrl(String number) {
1438 if (TextUtils.isEmpty(number)) {
1439 return null;
1440 }
1441
Jake Hambye994d462014-02-03 13:10:13 -08001442 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
1444
Ihab Awadf9e92732013-12-05 18:02:52 -08001445 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1447 }
1448
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001449 private static void logv(String msg) {
1450 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1451 }
1452
Ihab Awadf9e92732013-12-05 18:02:52 -08001453 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1455 }
1456
1457 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001458 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001459 }
1460
Wink Savilleb564aae2014-10-23 10:18:09 -07001461 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001462 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001463 }
1464
1465 /**
1466 * Returns the CDMA ERI icon index to display
1467 */
1468 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001469 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001470
1471 }
1472
Wink Savilleb564aae2014-10-23 10:18:09 -07001473 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001474 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 }
1476
1477 /**
1478 * Returns the CDMA ERI icon mode,
1479 * 0 - ON
1480 * 1 - FLASHING
1481 */
1482 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001483 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001484 }
1485
Wink Savilleb564aae2014-10-23 10:18:09 -07001486 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001487 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489
1490 /**
1491 * Returns the CDMA ERI text,
1492 */
1493 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001494 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001495 }
1496
Wink Savilleb564aae2014-10-23 10:18:09 -07001497 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001498 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001499 }
1500
1501 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001502 * Returns the CDMA MDN.
1503 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001504 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001505 enforceModifyPermissionOrCarrierPrivilege();
1506 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1507 return getPhone(subId).getLine1Number();
1508 } else {
1509 return null;
1510 }
1511 }
1512
1513 /**
1514 * Returns the CDMA MIN.
1515 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001516 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001517 enforceModifyPermissionOrCarrierPrivilege();
1518 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1519 return getPhone(subId).getCdmaMin();
1520 } else {
1521 return null;
1522 }
1523 }
1524
1525 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 * Returns true if CDMA provisioning needs to run.
1527 */
1528 public boolean needsOtaServiceProvisioning() {
1529 return mPhone.needsOtaServiceProvisioning();
1530 }
1531
1532 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001533 * Sets the voice mail number of a given subId.
1534 */
1535 @Override
1536 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001537 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001538 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1539 new Pair<String, String>(alphaTag, number), new Integer(subId));
1540 return success;
1541 }
1542
1543 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 * Returns the unread count of voicemails
1545 */
1546 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001547 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001548 }
1549
1550 /**
1551 * Returns the unread count of voicemails for a subId
1552 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001553 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001554 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 }
1556
1557 /**
1558 * Returns the data network type
1559 *
1560 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1561 */
1562 @Override
1563 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001564 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001565 }
1566
1567 /**
1568 * Returns the network type for a subId
1569 */
1570 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001571 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001572 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 }
1574
1575 /**
1576 * Returns the data network type
1577 */
1578 @Override
1579 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001580 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001581 }
1582
1583 /**
1584 * Returns the data network type for a subId
1585 */
1586 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001587 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001588 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 }
1590
1591 /**
1592 * Returns the data network type
1593 */
1594 @Override
1595 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001596 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001597 }
1598
1599 /**
1600 * Returns the Voice network type for a subId
1601 */
1602 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001603 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001604 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 }
1606
1607 /**
1608 * @return true if a ICC card is present
1609 */
1610 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001611 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001612 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001613 }
1614
1615 /**
1616 * @return true if a ICC card is present for a slotId
1617 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001618 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001619 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1620 if (subId != null) {
1621 return getPhone(subId[0]).getIccCard().hasIccCard();
1622 } else {
1623 return false;
1624 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001625 }
1626
1627 /**
1628 * Return if the current radio is LTE on CDMA. This
1629 * is a tri-state return value as for a period of time
1630 * the mode may be unknown.
1631 *
1632 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001633 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001634 */
1635 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001636 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001637 }
1638
Wink Savilleb564aae2014-10-23 10:18:09 -07001639 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001640 return getPhone(subId).getLteOnCdmaMode();
1641 }
1642
1643 public void setPhone(Phone phone) {
1644 mPhone = phone;
1645 }
1646
1647 /**
1648 * {@hide}
1649 * Returns Default subId, 0 in the case of single standby.
1650 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001651 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001652 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001653 }
1654
Wink Savilleb564aae2014-10-23 10:18:09 -07001655 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001656 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001658
1659 /**
1660 * @see android.telephony.TelephonyManager.WifiCallingChoices
1661 */
1662 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001663 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1664 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001665 }
1666
1667 /**
1668 * @see android.telephony.TelephonyManager.WifiCallingChoices
1669 */
1670 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001671 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1672 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1673 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001674 }
1675
Sailesh Nepald1e68152013-12-12 19:08:02 -08001676 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001677 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001678 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001679 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001680
Shishir Agrawal566b7612013-10-28 14:41:00 -07001681 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001682 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001683 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001684
1685 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001686 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1687 CMD_OPEN_CHANNEL, AID);
1688 if (DBG) log("iccOpenLogicalChannel: " + response);
1689 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001690 }
1691
1692 @Override
1693 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001694 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001695
1696 if (DBG) log("iccCloseLogicalChannel: " + channel);
1697 if (channel < 0) {
1698 return false;
1699 }
Jake Hambye994d462014-02-03 13:10:13 -08001700 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001701 if (DBG) log("iccCloseLogicalChannel: " + success);
1702 return success;
1703 }
1704
1705 @Override
1706 public String iccTransmitApduLogicalChannel(int channel, int cla,
1707 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001708 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001709
1710 if (DBG) {
1711 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1712 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1713 " data=" + data);
1714 }
1715
1716 if (channel < 0) {
1717 return "";
1718 }
1719
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001720 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001721 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1722 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1723
Shishir Agrawal566b7612013-10-28 14:41:00 -07001724 // Append the returned status code to the end of the response payload.
1725 String s = Integer.toHexString(
1726 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001727 if (response.payload != null) {
1728 s = IccUtils.bytesToHexString(response.payload) + s;
1729 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001730 return s;
1731 }
Jake Hambye994d462014-02-03 13:10:13 -08001732
Evan Charltonc66da362014-05-16 14:06:40 -07001733 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001734 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1735 int p3, String data) {
1736 enforceModifyPermissionOrCarrierPrivilege();
1737
1738 if (DBG) {
1739 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1740 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1741 }
1742
1743 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1744 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1745 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1746
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001747 // Append the returned status code to the end of the response payload.
1748 String s = Integer.toHexString(
1749 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001750 if (response.payload != null) {
1751 s = IccUtils.bytesToHexString(response.payload) + s;
1752 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001753 return s;
1754 }
1755
1756 @Override
1757 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1758 String filePath) {
1759 enforceModifyPermissionOrCarrierPrivilege();
1760
1761 if (DBG) {
1762 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1763 p1 + " " + p2 + " " + p3 + ":" + filePath);
1764 }
1765
1766 IccIoResult response =
1767 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001768 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001769
1770 if (DBG) {
1771 log("Exchange SIM_IO [R]" + response);
1772 }
1773
1774 byte[] result = null;
1775 int length = 2;
1776 if (response.payload != null) {
1777 length = 2 + response.payload.length;
1778 result = new byte[length];
1779 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1780 } else {
1781 result = new byte[length];
1782 }
1783
1784 result[length - 1] = (byte) response.sw2;
1785 result[length - 2] = (byte) response.sw1;
1786 return result;
1787 }
1788
1789 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001790 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001791 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001792
1793 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1794 if (response.payload == null) {
1795 return "";
1796 }
1797
1798 // Append the returned status code to the end of the response payload.
1799 String s = Integer.toHexString(
1800 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1801 s = IccUtils.bytesToHexString(response.payload) + s;
1802 return s;
1803 }
1804
Jake Hambye994d462014-02-03 13:10:13 -08001805 /**
1806 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1807 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1808 *
1809 * @param itemID the ID of the item to read
1810 * @return the NV item as a String, or null on error.
1811 */
1812 @Override
1813 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001814 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001815 if (DBG) log("nvReadItem: item " + itemID);
1816 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1817 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1818 return value;
1819 }
1820
1821 /**
1822 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1823 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1824 *
1825 * @param itemID the ID of the item to read
1826 * @param itemValue the value to write, as a String
1827 * @return true on success; false on any failure
1828 */
1829 @Override
1830 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001831 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001832 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1833 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1834 new Pair<Integer, String>(itemID, itemValue));
1835 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1836 return success;
1837 }
1838
1839 /**
1840 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1841 * Used for device configuration by some CDMA operators.
1842 *
1843 * @param preferredRoamingList byte array containing the new PRL
1844 * @return true on success; false on any failure
1845 */
1846 @Override
1847 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001848 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001849 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1850 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1851 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1852 return success;
1853 }
1854
1855 /**
1856 * Perform the specified type of NV config reset.
1857 * Used for device configuration by some CDMA operators.
1858 *
1859 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1860 * @return true on success; false on any failure
1861 */
1862 @Override
1863 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001864 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001865 if (DBG) log("nvResetConfig: type " + resetType);
1866 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1867 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1868 return success;
1869 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001870
1871 /**
Wink Saville36469e72014-06-11 15:17:00 -07001872 * {@hide}
1873 * Returns Default sim, 0 in the case of single standby.
1874 */
1875 public int getDefaultSim() {
1876 //TODO Need to get it from Telephony Devcontroller
1877 return 0;
1878 }
1879
Svet Ganovb320e182015-04-16 12:30:10 -07001880 public String[] getPcscfAddress(String apnType, String callingPackage) {
1881 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1882 return new String[0];
1883 }
1884
1885
ram87fca6f2014-07-18 18:58:44 +05301886 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001887 }
1888
1889 public void setImsRegistrationState(boolean registered) {
1890 enforceModifyPermission();
1891 mPhone.setImsRegistrationState(registered);
1892 }
1893
1894 /**
Stuart Scott54788802015-03-30 13:18:01 -07001895 * Set the network selection mode to automatic.
1896 *
1897 */
1898 @Override
1899 public void setNetworkSelectionModeAutomatic(int subId) {
1900 enforceModifyPermissionOrCarrierPrivilege();
1901 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1902 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1903 }
1904
1905 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001906 * Get the calculated preferred network type.
1907 * Used for debugging incorrect network type.
1908 *
1909 * @return the preferred network type, defined in RILConstants.java.
1910 */
1911 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001912 public int getCalculatedPreferredNetworkType(String callingPackage) {
1913 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1914 return RILConstants.PREFERRED_NETWORK_MODE;
1915 }
1916
Amit Mahajan43330e02014-11-18 11:54:45 -08001917 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001918 }
1919
1920 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001921 * Get the preferred network type.
1922 * Used for device configuration by some CDMA operators.
1923 *
1924 * @return the preferred network type, defined in RILConstants.java.
1925 */
1926 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001927 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001928 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001929 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001930 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001931 int networkType = (result != null ? result[0] : -1);
1932 if (DBG) log("getPreferredNetworkType: " + networkType);
1933 return networkType;
1934 }
1935
1936 /**
1937 * Set the preferred network type.
1938 * Used for device configuration by some CDMA operators.
1939 *
1940 * @param networkType the preferred network type, defined in RILConstants.java.
1941 * @return true on success; false on any failure.
1942 */
1943 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001944 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001945 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001946 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1947 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001948 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001949 if (success) {
1950 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001951 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001952 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001953 return success;
1954 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001955
1956 /**
Junda Liu475951f2014-11-07 16:45:03 -08001957 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1958 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1959 * tethering.
1960 *
1961 * @return 0: Not required. 1: required. 2: Not set.
1962 * @hide
1963 */
1964 @Override
1965 public int getTetherApnRequired() {
1966 enforceModifyPermissionOrCarrierPrivilege();
1967 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1968 Settings.Global.TETHER_DUN_REQUIRED, 2);
1969 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1970 // config_tether_apndata.
1971 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1972 dunRequired = 1;
1973 }
1974 return dunRequired;
1975 }
1976
1977 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001978 * Set mobile data enabled
1979 * Used by the user through settings etc to turn on/off mobile data
1980 *
1981 * @param enable {@code true} turn turn data on, else {@code false}
1982 */
1983 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001984 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001985 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001986 int phoneId = mSubscriptionController.getPhoneId(subId);
1987 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1988 Phone phone = PhoneFactory.getPhone(phoneId);
1989 if (phone != null) {
1990 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1991 phone.setDataEnabled(enable);
1992 } else {
1993 loge("setDataEnabled: no phone for subId=" + subId);
1994 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001995 }
1996
1997 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001998 * Get whether mobile data is enabled.
1999 *
2000 * Note that this used to be available from ConnectivityService, gated by
2001 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2002 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002003 *
2004 * @return {@code true} if data is enabled else {@code false}
2005 */
2006 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002007 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002008 try {
2009 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2010 null);
2011 } catch (Exception e) {
2012 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2013 null);
2014 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002015 int phoneId = mSubscriptionController.getPhoneId(subId);
2016 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2017 Phone phone = PhoneFactory.getPhone(phoneId);
2018 if (phone != null) {
2019 boolean retVal = phone.getDataEnabled();
2020 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2021 return retVal;
2022 } else {
2023 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2024 return false;
2025 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002026 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002027
2028 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002029 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002030 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002031 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002032 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002033 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2034 }
2035 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002036 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002037 }
Junda Liu29340342014-07-10 15:23:27 -07002038
2039 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002040 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002041 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002042 if (card == null) {
2043 loge("checkCarrierPrivilegesForPackage: No UICC");
2044 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2045 }
2046 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002047 }
Derek Tan89e89d42014-07-08 17:00:10 -07002048
2049 @Override
Junda Liue64de782015-04-16 17:19:16 -07002050 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2051 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2052 loge("phoneId " + phoneId + " is not valid.");
2053 return null;
2054 }
2055 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002056 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002057 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002058 return null ;
2059 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002060 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002061 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002062 }
2063
Wink Savilleb564aae2014-10-23 10:18:09 -07002064 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002065 UiccCard card = getPhone(subId).getUiccCard();
2066 if (card == null) {
2067 loge("getIccId: No UICC");
2068 return null;
2069 }
2070 String iccId = card.getIccId();
2071 if (TextUtils.isEmpty(iccId)) {
2072 loge("getIccId: ICC ID is null or empty.");
2073 return null;
2074 }
2075 return iccId;
2076 }
2077
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002078 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002079 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2080 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002081 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002082
Jeff Sharkey85190e62014-12-05 09:40:12 -08002083 final String iccId = getIccId(subId);
2084 final String subscriberId = getPhone(subId).getSubscriberId();
2085
2086 if (DBG_MERGE) {
2087 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2088 + subscriberId + " to " + number);
2089 }
2090
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002091 if (TextUtils.isEmpty(iccId)) {
2092 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002093 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002094
Jeff Sharkey85190e62014-12-05 09:40:12 -08002095 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2096
2097 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002098 if (alphaTag == null) {
2099 editor.remove(alphaTagPrefKey);
2100 } else {
2101 editor.putString(alphaTagPrefKey, alphaTag);
2102 }
2103
Jeff Sharkey85190e62014-12-05 09:40:12 -08002104 // Record both the line number and IMSI for this ICCID, since we need to
2105 // track all merged IMSIs based on line number
2106 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2107 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002108 if (number == null) {
2109 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002110 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002111 } else {
2112 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002113 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002114 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002115
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002116 editor.commit();
2117 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002118 }
2119
2120 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002121 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2122 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2123 return null;
2124 }
Derek Tan97ebb422014-09-05 16:55:38 -07002125
2126 String iccId = getIccId(subId);
2127 if (iccId != null) {
2128 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002129 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002130 }
Derek Tan97ebb422014-09-05 16:55:38 -07002131 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002132 }
2133
2134 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002135 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2136 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2137 return null;
2138 }
Derek Tan97ebb422014-09-05 16:55:38 -07002139
2140 String iccId = getIccId(subId);
2141 if (iccId != null) {
2142 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002143 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002144 }
Derek Tan97ebb422014-09-05 16:55:38 -07002145 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002146 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002147
2148 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002149 public String[] getMergedSubscriberIds() {
2150 final Context context = mPhone.getContext();
2151 final TelephonyManager tele = TelephonyManager.from(context);
2152 final SubscriptionManager sub = SubscriptionManager.from(context);
2153
2154 // Figure out what subscribers are currently active
2155 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2156 final int[] subIds = sub.getActiveSubscriptionIdList();
2157 for (int subId : subIds) {
2158 activeSubscriberIds.add(tele.getSubscriberId(subId));
2159 }
2160
2161 // First pass, find a number override for an active subscriber
2162 String mergeNumber = null;
2163 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2164 for (String key : prefs.keySet()) {
2165 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2166 final String subscriberId = (String) prefs.get(key);
2167 if (activeSubscriberIds.contains(subscriberId)) {
2168 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2169 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2170 mergeNumber = (String) prefs.get(numberKey);
2171 if (DBG_MERGE) {
2172 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2173 + " for active subscriber " + subscriberId);
2174 }
2175 if (!TextUtils.isEmpty(mergeNumber)) {
2176 break;
2177 }
2178 }
2179 }
2180 }
2181
2182 // Shortcut when no active merged subscribers
2183 if (TextUtils.isEmpty(mergeNumber)) {
2184 return null;
2185 }
2186
2187 // Second pass, find all subscribers under that line override
2188 final ArraySet<String> result = new ArraySet<>();
2189 for (String key : prefs.keySet()) {
2190 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2191 final String number = (String) prefs.get(key);
2192 if (mergeNumber.equals(number)) {
2193 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2194 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2195 final String subscriberId = (String) prefs.get(subscriberKey);
2196 if (!TextUtils.isEmpty(subscriberId)) {
2197 result.add(subscriberId);
2198 }
2199 }
2200 }
2201 }
2202
2203 final String[] resultArray = result.toArray(new String[result.size()]);
2204 Arrays.sort(resultArray);
2205 if (DBG_MERGE) {
2206 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2207 }
2208 return resultArray;
2209 }
2210
2211 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002212 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002213 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002214 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002215 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002216
2217 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002218 public boolean setRoamingOverride(List<String> gsmRoamingList,
2219 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2220 List<String> cdmaNonRoamingList) {
2221 enforceCarrierPrivilege();
2222 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2223 cdmaNonRoamingList);
2224 }
2225
2226 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002227 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2228 enforceModifyPermission();
2229
2230 int returnValue = 0;
2231 try {
2232 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2233 if(result.exception == null) {
2234 if (result.result != null) {
2235 byte[] responseData = (byte[])(result.result);
2236 if(responseData.length > oemResp.length) {
2237 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2238 responseData.length + "bytes. Buffer Size is " +
2239 oemResp.length + "bytes.");
2240 }
2241 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2242 returnValue = responseData.length;
2243 }
2244 } else {
2245 CommandException ex = (CommandException) result.exception;
2246 returnValue = ex.getCommandError().ordinal();
2247 if(returnValue > 0) returnValue *= -1;
2248 }
2249 } catch (RuntimeException e) {
2250 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2251 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2252 if(returnValue > 0) returnValue *= -1;
2253 }
2254
2255 return returnValue;
2256 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002257
2258 @Override
2259 public void setRadioCapability(RadioAccessFamily[] rafs) {
2260 try {
2261 ProxyController.getInstance().setRadioCapability(rafs);
2262 } catch (RuntimeException e) {
2263 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2264 }
2265 }
2266
2267 @Override
2268 public int getRadioAccessFamily(int phoneId) {
2269 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2270 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002271
2272 @Override
2273 public void enableVideoCalling(boolean enable) {
2274 enforceModifyPermission();
2275 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2276 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2277 editor.commit();
2278 }
2279
2280 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002281 public boolean isVideoCallingEnabled(String callingPackage) {
2282 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2283 return false;
2284 }
2285
Andrew Lee77527ac2014-10-21 16:57:39 -07002286 // Check the user preference and the system-level IMS setting. Even if the user has
2287 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2288 // In the long run, we may instead need to check if there exists a connection service
2289 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002290 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2291 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2292 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002293 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002294
Andrew Leea1239f22015-03-02 17:44:07 -08002295 @Override
2296 public boolean canChangeDtmfToneLength() {
2297 return mPhone.getContext().getResources().getBoolean(R.bool.dtmf_type_enabled);
2298 }
2299
2300 @Override
2301 public boolean isWorldPhone() {
2302 return mPhone.getContext().getResources().getBoolean(R.bool.world_phone);
2303 }
2304
Andrew Lee9431b832015-03-09 18:46:45 -07002305 @Override
2306 public boolean isTtyModeSupported() {
2307 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2308 TelephonyManager telephonyManager =
2309 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2310 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2311 }
2312
2313 @Override
2314 public boolean isHearingAidCompatibilitySupported() {
2315 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2316 }
2317
Sanket Padawe7310cc72015-01-14 09:53:20 -08002318 /**
2319 * Returns the unique device ID of phone, for example, the IMEI for
2320 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2321 *
2322 * <p>Requires Permission:
2323 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2324 */
2325 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002326 public String getDeviceId(String callingPackage) {
2327 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2328 return null;
2329 }
2330
Sanket Padawe7310cc72015-01-14 09:53:20 -08002331 final Phone phone = PhoneFactory.getPhone(0);
2332 if (phone != null) {
2333 return phone.getDeviceId();
2334 } else {
2335 return null;
2336 }
2337 }
2338
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002339 /*
2340 * {@hide}
2341 * Returns the IMS Registration Status
2342 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002343 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002344 public boolean isImsRegistered() {
2345 return mPhone.isImsRegistered();
2346 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002347
2348 @Override
2349 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2350 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2351 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002352
Nathan Haroldc55097a2015-03-11 18:14:50 -07002353 /*
2354 * {@hide}
2355 * Returns the IMS Registration Status
2356 */
2357 public boolean isWifiCallingEnabled() {
2358 return mPhone.isWifiCallingEnabled();
2359 }
2360
2361 /*
2362 * {@hide}
2363 * Returns the IMS Registration Status
2364 */
2365 public boolean isVolteEnabled() {
2366 return mPhone.isVolteEnabled();
2367 }
Svet Ganovb320e182015-04-16 12:30:10 -07002368
2369 private boolean canReadPhoneState(String callingPackage, String message) {
2370 mApp.enforceCallingOrSelfPermission(
2371 android.Manifest.permission.READ_PHONE_STATE, message);
2372
2373 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2374 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2375 return false;
2376 }
2377
2378 return true;
2379 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002380
2381 @Override
2382 public void factoryReset(int subId) {
2383 enforceConnectivityInternalPermission();
2384 if (SubscriptionManager.isUsableSubIdValue(subId)) {
2385 // Enable data
2386 setDataEnabled(subId, true);
2387 // Set network selection mode to automatic
2388 setNetworkSelectionModeAutomatic(subId);
2389 // Set preferred mobile network type to the best available
2390 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2391 // Turn off roaming
2392 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2393 }
2394 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002395}