blob: e4b8c35965a46293aa7e3b7637d1de239c3013c9 [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;
Junda Liu12f7d802015-05-01 12:06:44 -070040import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070042import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080043import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070044import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080046import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080047import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080050import android.util.ArrayMap;
51import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080054import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055
Andrew Lee312e8172014-10-23 17:01:36 -070056import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070057import com.android.internal.telephony.CallManager;
58import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080061import com.android.internal.telephony.IccCard;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070063import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070064import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070066import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080067import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070068import com.android.internal.telephony.uicc.IccIoResult;
69import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070070import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070071import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080072import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073
Wink Saville36469e72014-06-11 15:17:00 -070074import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
75
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080077import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080078import java.util.HashMap;
79import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080080import java.util.List;
Jeff Sharkey85190e62014-12-05 09:40:12 -080081import java.util.Map;
82import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083
84/**
85 * Implementation of the ITelephony interface.
86 */
Santos Cordon117fee72014-05-16 17:56:12 -070087public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088 private static final String LOG_TAG = "PhoneInterfaceManager";
89 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
90 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080091 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092
93 // Message codes used with mMainThreadHandler
94 private static final int CMD_HANDLE_PIN_MMI = 1;
95 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
96 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
97 private static final int CMD_ANSWER_RINGING_CALL = 4;
98 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070099 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
100 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700101 private static final int CMD_OPEN_CHANNEL = 9;
102 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
103 private static final int CMD_CLOSE_CHANNEL = 11;
104 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800105 private static final int CMD_NV_READ_ITEM = 13;
106 private static final int EVENT_NV_READ_ITEM_DONE = 14;
107 private static final int CMD_NV_WRITE_ITEM = 15;
108 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
109 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
110 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
111 private static final int CMD_NV_RESET_CONFIG = 19;
112 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800113 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
114 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
115 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
116 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800117 private static final int CMD_SEND_ENVELOPE = 25;
118 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700119 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
120 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
121 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
122 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
123 private static final int CMD_EXCHANGE_SIM_IO = 31;
124 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800125 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
126 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700127 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
128 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129
130 /** The singleton instance. */
131 private static PhoneInterfaceManager sInstance;
132
Wink Saville3ab207e2014-11-20 13:07:20 -0800133 private PhoneGlobals mApp;
134 private Phone mPhone;
135 private CallManager mCM;
136 private AppOpsManager mAppOps;
137 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800138 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800139 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140
Derek Tan97ebb422014-09-05 16:55:38 -0700141 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
142 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800143 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700144 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700145
146 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700147 * A request object to use for transmitting data to an ICC.
148 */
149 private static final class IccAPDUArgument {
150 public int channel, cla, command, p1, p2, p3;
151 public String data;
152
153 public IccAPDUArgument(int channel, int cla, int command,
154 int p1, int p2, int p3, String data) {
155 this.channel = channel;
156 this.cla = cla;
157 this.command = command;
158 this.p1 = p1;
159 this.p2 = p2;
160 this.p3 = p3;
161 this.data = data;
162 }
163 }
164
165 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
167 * request after sending. The main thread will notify the request when it is complete.
168 */
169 private static final class MainThreadRequest {
170 /** The argument to use for the request */
171 public Object argument;
172 /** The result of the request that is run on the main thread */
173 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800174 /** The subscriber id that this request applies to. Null if default. */
175 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176
177 public MainThreadRequest(Object argument) {
178 this.argument = argument;
179 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800180
181 public MainThreadRequest(Object argument, Integer subId) {
182 this.argument = argument;
183 this.subId = subId;
184 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 }
186
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800187 private static final class IncomingThirdPartyCallArgs {
188 public final ComponentName component;
189 public final String callId;
190 public final String callerDisplayName;
191
192 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
193 String callerDisplayName) {
194 this.component = component;
195 this.callId = callId;
196 this.callerDisplayName = callerDisplayName;
197 }
198 }
199
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 /**
201 * A handler that processes messages on the main thread in the phone process. Since many
202 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
203 * inbound binder threads to the main thread in the phone process. The Binder thread
204 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
205 * on, which will be notified when the operation completes and will contain the result of the
206 * request.
207 *
208 * <p>If a MainThreadRequest object is provided in the msg.obj field,
209 * note that request.result must be set to something non-null for the calling thread to
210 * unblock.
211 */
212 private final class MainThreadHandler extends Handler {
213 @Override
214 public void handleMessage(Message msg) {
215 MainThreadRequest request;
216 Message onCompleted;
217 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800218 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700219 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220
221 switch (msg.what) {
222 case CMD_HANDLE_PIN_MMI:
223 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800224 request.result = getPhoneFromRequest(request).handlePinMmi(
225 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 // Wake up the requesting thread
227 synchronized (request) {
228 request.notifyAll();
229 }
230 break;
231
232 case CMD_HANDLE_NEIGHBORING_CELL:
233 request = (MainThreadRequest) msg.obj;
234 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
235 request);
236 mPhone.getNeighboringCids(onCompleted);
237 break;
238
239 case EVENT_NEIGHBORING_CELL_DONE:
240 ar = (AsyncResult) msg.obj;
241 request = (MainThreadRequest) ar.userObj;
242 if (ar.exception == null && ar.result != null) {
243 request.result = ar.result;
244 } else {
245 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800246 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 }
248 // Wake up the requesting thread
249 synchronized (request) {
250 request.notifyAll();
251 }
252 break;
253
254 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700255 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800256 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700257 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 break;
259
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 case CMD_END_CALL:
261 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800262 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700263 final boolean hungUp;
264 int phoneType = getPhone(end_subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
266 // CDMA: If the user presses the Power button we treat it as
267 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700268 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
270 // GSM: End the call as per the Phone state
271 hungUp = PhoneUtils.hangup(mCM);
272 } else {
273 throw new IllegalStateException("Unexpected phone type: " + phoneType);
274 }
275 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
276 request.result = hungUp;
277 // Wake up the requesting thread
278 synchronized (request) {
279 request.notifyAll();
280 }
281 break;
282
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700283 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700284 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700285 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700286 if (uiccCard == null) {
287 loge("iccTransmitApduLogicalChannel: No UICC");
288 request.result = new IccIoResult(0x6F, 0, (byte[])null);
289 synchronized (request) {
290 request.notifyAll();
291 }
292 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700293 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
294 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700295 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700296 iccArgument.channel, iccArgument.cla, iccArgument.command,
297 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700298 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700299 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700300 break;
301
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700302 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700303 ar = (AsyncResult) msg.obj;
304 request = (MainThreadRequest) ar.userObj;
305 if (ar.exception == null && ar.result != null) {
306 request.result = ar.result;
307 } else {
308 request.result = new IccIoResult(0x6F, 0, (byte[])null);
309 if (ar.result == null) {
310 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800311 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700312 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800313 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700314 } else {
315 loge("iccTransmitApduLogicalChannel: Unknown exception");
316 }
317 }
318 synchronized (request) {
319 request.notifyAll();
320 }
321 break;
322
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700323 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
324 request = (MainThreadRequest) msg.obj;
325 iccArgument = (IccAPDUArgument) request.argument;
326 if (uiccCard == null) {
327 loge("iccTransmitApduBasicChannel: No UICC");
328 request.result = new IccIoResult(0x6F, 0, (byte[])null);
329 synchronized (request) {
330 request.notifyAll();
331 }
332 } else {
333 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
334 request);
335 uiccCard.iccTransmitApduBasicChannel(
336 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
337 iccArgument.p3, iccArgument.data, onCompleted);
338 }
339 break;
340
341 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
342 ar = (AsyncResult) msg.obj;
343 request = (MainThreadRequest) ar.userObj;
344 if (ar.exception == null && ar.result != null) {
345 request.result = ar.result;
346 } else {
347 request.result = new IccIoResult(0x6F, 0, (byte[])null);
348 if (ar.result == null) {
349 loge("iccTransmitApduBasicChannel: Empty response");
350 } else if (ar.exception instanceof CommandException) {
351 loge("iccTransmitApduBasicChannel: CommandException: " +
352 ar.exception);
353 } else {
354 loge("iccTransmitApduBasicChannel: Unknown exception");
355 }
356 }
357 synchronized (request) {
358 request.notifyAll();
359 }
360 break;
361
362 case CMD_EXCHANGE_SIM_IO:
363 request = (MainThreadRequest) msg.obj;
364 iccArgument = (IccAPDUArgument) request.argument;
365 if (uiccCard == null) {
366 loge("iccExchangeSimIO: No UICC");
367 request.result = new IccIoResult(0x6F, 0, (byte[])null);
368 synchronized (request) {
369 request.notifyAll();
370 }
371 } else {
372 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
373 request);
374 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
375 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
376 iccArgument.data, onCompleted);
377 }
378 break;
379
380 case EVENT_EXCHANGE_SIM_IO_DONE:
381 ar = (AsyncResult) msg.obj;
382 request = (MainThreadRequest) ar.userObj;
383 if (ar.exception == null && ar.result != null) {
384 request.result = ar.result;
385 } else {
386 request.result = new IccIoResult(0x6f, 0, (byte[])null);
387 }
388 synchronized (request) {
389 request.notifyAll();
390 }
391 break;
392
Derek Tan4d5e5c12014-02-04 11:54:58 -0800393 case CMD_SEND_ENVELOPE:
394 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700395 if (uiccCard == null) {
396 loge("sendEnvelopeWithStatus: No UICC");
397 request.result = new IccIoResult(0x6F, 0, (byte[])null);
398 synchronized (request) {
399 request.notifyAll();
400 }
401 } else {
402 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
403 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
404 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800405 break;
406
407 case EVENT_SEND_ENVELOPE_DONE:
408 ar = (AsyncResult) msg.obj;
409 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700410 if (ar.exception == null && ar.result != null) {
411 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800412 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700413 request.result = new IccIoResult(0x6F, 0, (byte[])null);
414 if (ar.result == null) {
415 loge("sendEnvelopeWithStatus: Empty response");
416 } else if (ar.exception instanceof CommandException) {
417 loge("sendEnvelopeWithStatus: CommandException: " +
418 ar.exception);
419 } else {
420 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
421 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800422 }
423 synchronized (request) {
424 request.notifyAll();
425 }
426 break;
427
Shishir Agrawal566b7612013-10-28 14:41:00 -0700428 case CMD_OPEN_CHANNEL:
429 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700430 if (uiccCard == null) {
431 loge("iccOpenLogicalChannel: No UICC");
432 request.result = new IccIoResult(0x6F, 0, (byte[])null);
433 synchronized (request) {
434 request.notifyAll();
435 }
436 } else {
437 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
438 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
439 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700440 break;
441
442 case EVENT_OPEN_CHANNEL_DONE:
443 ar = (AsyncResult) msg.obj;
444 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700445 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700446 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700447 int[] result = (int[]) ar.result;
448 int channelId = result[0];
449 byte[] selectResponse = null;
450 if (result.length > 1) {
451 selectResponse = new byte[result.length - 1];
452 for (int i = 1; i < result.length; ++i) {
453 selectResponse[i - 1] = (byte) result[i];
454 }
455 }
456 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700457 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700459 if (ar.result == null) {
460 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700461 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700462 if (ar.exception != null) {
463 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
464 }
465
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700466 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700467 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
468 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700469 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700470 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700471 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700472 }
473 }
474 openChannelResp = new IccOpenLogicalChannelResponse(
475 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700477 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 synchronized (request) {
479 request.notifyAll();
480 }
481 break;
482
483 case CMD_CLOSE_CHANNEL:
484 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700485 if (uiccCard == null) {
486 loge("iccCloseLogicalChannel: No UICC");
487 request.result = new IccIoResult(0x6F, 0, (byte[])null);
488 synchronized (request) {
489 request.notifyAll();
490 }
491 } else {
492 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
493 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
494 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 break;
496
497 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800498 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
499 break;
500
501 case CMD_NV_READ_ITEM:
502 request = (MainThreadRequest) msg.obj;
503 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
504 mPhone.nvReadItem((Integer) request.argument, onCompleted);
505 break;
506
507 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700508 ar = (AsyncResult) msg.obj;
509 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800510 if (ar.exception == null && ar.result != null) {
511 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800513 request.result = "";
514 if (ar.result == null) {
515 loge("nvReadItem: Empty response");
516 } else if (ar.exception instanceof CommandException) {
517 loge("nvReadItem: CommandException: " +
518 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800520 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 }
522 }
523 synchronized (request) {
524 request.notifyAll();
525 }
526 break;
527
Jake Hambye994d462014-02-03 13:10:13 -0800528 case CMD_NV_WRITE_ITEM:
529 request = (MainThreadRequest) msg.obj;
530 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
531 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
532 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
533 break;
534
535 case EVENT_NV_WRITE_ITEM_DONE:
536 handleNullReturnEvent(msg, "nvWriteItem");
537 break;
538
539 case CMD_NV_WRITE_CDMA_PRL:
540 request = (MainThreadRequest) msg.obj;
541 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
542 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
543 break;
544
545 case EVENT_NV_WRITE_CDMA_PRL_DONE:
546 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
547 break;
548
549 case CMD_NV_RESET_CONFIG:
550 request = (MainThreadRequest) msg.obj;
551 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
552 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
553 break;
554
555 case EVENT_NV_RESET_CONFIG_DONE:
556 handleNullReturnEvent(msg, "nvResetConfig");
557 break;
558
Jake Hamby7c27be32014-03-03 13:25:59 -0800559 case CMD_GET_PREFERRED_NETWORK_TYPE:
560 request = (MainThreadRequest) msg.obj;
561 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700562 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800563 break;
564
565 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
566 ar = (AsyncResult) msg.obj;
567 request = (MainThreadRequest) ar.userObj;
568 if (ar.exception == null && ar.result != null) {
569 request.result = ar.result; // Integer
570 } else {
571 request.result = -1;
572 if (ar.result == null) {
573 loge("getPreferredNetworkType: Empty response");
574 } else if (ar.exception instanceof CommandException) {
575 loge("getPreferredNetworkType: CommandException: " +
576 ar.exception);
577 } else {
578 loge("getPreferredNetworkType: Unknown exception");
579 }
580 }
581 synchronized (request) {
582 request.notifyAll();
583 }
584 break;
585
586 case CMD_SET_PREFERRED_NETWORK_TYPE:
587 request = (MainThreadRequest) msg.obj;
588 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
589 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700590 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800591 break;
592
593 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
594 handleNullReturnEvent(msg, "setPreferredNetworkType");
595 break;
596
Steven Liu4bf01bc2014-07-17 11:05:29 -0500597 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
598 request = (MainThreadRequest)msg.obj;
599 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
600 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
601 break;
602
603 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
604 ar = (AsyncResult)msg.obj;
605 request = (MainThreadRequest)ar.userObj;
606 request.result = ar;
607 synchronized (request) {
608 request.notifyAll();
609 }
610 break;
611
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800612 case CMD_SET_VOICEMAIL_NUMBER:
613 request = (MainThreadRequest) msg.obj;
614 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
615 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800616 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
617 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800618 break;
619
620 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
621 handleNullReturnEvent(msg, "setVoicemailNumber");
622 break;
623
Stuart Scott54788802015-03-30 13:18:01 -0700624 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
625 request = (MainThreadRequest) msg.obj;
626 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
627 request);
628 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
629 break;
630
631 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
632 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
633 break;
634
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700635 default:
636 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
637 break;
638 }
639 }
Jake Hambye994d462014-02-03 13:10:13 -0800640
641 private void handleNullReturnEvent(Message msg, String command) {
642 AsyncResult ar = (AsyncResult) msg.obj;
643 MainThreadRequest request = (MainThreadRequest) ar.userObj;
644 if (ar.exception == null) {
645 request.result = true;
646 } else {
647 request.result = false;
648 if (ar.exception instanceof CommandException) {
649 loge(command + ": CommandException: " + ar.exception);
650 } else {
651 loge(command + ": Unknown exception");
652 }
653 }
654 synchronized (request) {
655 request.notifyAll();
656 }
657 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658 }
659
660 /**
661 * Posts the specified command to be executed on the main thread,
662 * waits for the request to complete, and returns the result.
663 * @see #sendRequestAsync
664 */
665 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700666 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700667 }
668
669 /**
670 * Posts the specified command to be executed on the main thread,
671 * waits for the request to complete, and returns the result.
672 * @see #sendRequestAsync
673 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800674 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700675 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
676 throw new RuntimeException("This method will deadlock if called from the main thread.");
677 }
678
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800679 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 Message msg = mMainThreadHandler.obtainMessage(command, request);
681 msg.sendToTarget();
682
683 // Wait for the request to complete
684 synchronized (request) {
685 while (request.result == null) {
686 try {
687 request.wait();
688 } catch (InterruptedException e) {
689 // Do nothing, go back and wait until the request is complete
690 }
691 }
692 }
693 return request.result;
694 }
695
696 /**
697 * Asynchronous ("fire and forget") version of sendRequest():
698 * Posts the specified command to be executed on the main thread, and
699 * returns immediately.
700 * @see #sendRequest
701 */
702 private void sendRequestAsync(int command) {
703 mMainThreadHandler.sendEmptyMessage(command);
704 }
705
706 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700707 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
708 * @see {@link #sendRequest(int,Object)}
709 */
710 private void sendRequestAsync(int command, Object argument) {
711 MainThreadRequest request = new MainThreadRequest(argument);
712 Message msg = mMainThreadHandler.obtainMessage(command, request);
713 msg.sendToTarget();
714 }
715
716 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 * Initialize the singleton PhoneInterfaceManager instance.
718 * This is only done once, at startup, from PhoneApp.onCreate().
719 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700720 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 synchronized (PhoneInterfaceManager.class) {
722 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700723 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 } else {
725 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
726 }
727 return sInstance;
728 }
729 }
730
731 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700732 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 mApp = app;
734 mPhone = phone;
735 mCM = PhoneGlobals.getInstance().mCM;
736 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
737 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700738 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700739 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800740 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800741
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700742 publish();
743 }
744
745 private void publish() {
746 if (DBG) log("publish: " + this);
747
748 ServiceManager.addService("phone", this);
749 }
750
Stuart Scott584921c2015-01-15 17:10:34 -0800751 private Phone getPhoneFromRequest(MainThreadRequest request) {
752 return (request.subId == null) ? mPhone : getPhone(request.subId);
753 }
754
Wink Saville36469e72014-06-11 15:17:00 -0700755 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700756 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800757 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700758 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 //
760 // Implementation of the ITelephony interface.
761 //
762
763 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700764 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700765 }
766
Wink Savilleb564aae2014-10-23 10:18:09 -0700767 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700768 if (DBG) log("dial: " + number);
769 // No permission check needed here: This is just a wrapper around the
770 // ACTION_DIAL intent, which is available to any app since it puts up
771 // the UI before it does anything.
772
773 String url = createTelUrl(number);
774 if (url == null) {
775 return;
776 }
777
778 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700779 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
781 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
782 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
783 mApp.startActivity(intent);
784 }
785 }
786
787 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700788 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700789 }
790
Wink Savilleb564aae2014-10-23 10:18:09 -0700791 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 if (DBG) log("call: " + number);
793
794 // This is just a wrapper around the ACTION_CALL intent, but we still
795 // need to do a permission check since we're calling startActivity()
796 // from the context of the phone app.
797 enforceCallPermission();
798
799 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
800 != AppOpsManager.MODE_ALLOWED) {
801 return;
802 }
803
804 String url = createTelUrl(number);
805 if (url == null) {
806 return;
807 }
808
Wink Saville08874612014-08-31 19:19:58 -0700809 boolean isValid = false;
Svetoslav483aff72015-04-21 14:16:07 -0700810 List<SubscriptionInfo> slist;
811
812 final long identity = Binder.clearCallingIdentity();
813 try {
814 slist = mSubscriptionController.getActiveSubscriptionInfoList(
815 mPhone.getContext().getOpPackageName());
816 } finally {
817 Binder.restoreCallingIdentity(identity);
818 }
819
Wink Saville3ab207e2014-11-20 13:07:20 -0800820 if (slist != null) {
821 for (SubscriptionInfo subInfoRecord : slist) {
822 if (subInfoRecord.getSubscriptionId() == subId) {
823 isValid = true;
824 break;
825 }
Wink Saville08874612014-08-31 19:19:58 -0700826 }
827 }
828 if (isValid == false) {
829 return;
830 }
831
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700832 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700833 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
835 mApp.startActivity(intent);
836 }
837
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 /**
839 * End a call based on call state
840 * @return true is a call was ended
841 */
842 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700843 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700844 }
845
846 /**
847 * End a call based on the call state of the subId
848 * @return true is a call was ended
849 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700850 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700851 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800852 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 }
854
855 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700856 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700857 }
858
Wink Savilleb564aae2014-10-23 10:18:09 -0700859 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 if (DBG) log("answerRingingCall...");
861 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
862 // but that can probably wait till the big TelephonyManager API overhaul.
863 // For now, protect this call with the MODIFY_PHONE_STATE permission.
864 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800865 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700866 }
867
868 /**
869 * Make the actual telephony calls to implement answerRingingCall().
870 * This should only be called from the main thread of the Phone app.
871 * @see #answerRingingCall
872 *
873 * TODO: it would be nice to return true if we answered the call, or
874 * false if there wasn't actually a ringing incoming call, or some
875 * other error occurred. (In other words, pass back the return value
876 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
877 * But that would require calling this method via sendRequest() rather
878 * than sendRequestAsync(), and right now we don't actually *need* that
879 * return value, so let's just return void for now.
880 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700881 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700882 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700883 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700884 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
885 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700886 if (hasActiveCall && hasHoldingCall) {
887 // Both lines are in use!
888 // TODO: provide a flag to let the caller specify what
889 // policy to use if both lines are in use. (The current
890 // behavior is hardwired to "answer incoming, end ongoing",
891 // which is how the CALL button is specced to behave.)
892 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
893 return;
894 } else {
895 // answerCall() will automatically hold the current active
896 // call, if there is one.
897 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
898 return;
899 }
900 } else {
901 // No call was ringing.
902 return;
903 }
904 }
905
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700907 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700909 public void silenceRinger() {
910 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 }
912
913 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700914 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700915 }
916
Wink Savilleb564aae2014-10-23 10:18:09 -0700917 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700918 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 }
920
921 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700922 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700923 }
924
Wink Savilleb564aae2014-10-23 10:18:09 -0700925 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700926 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 }
928
929 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700930 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700931 }
932
Wink Savilleb564aae2014-10-23 10:18:09 -0700933 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700934 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 }
936
Svet Ganovb320e182015-04-16 12:30:10 -0700937 public boolean isSimPinEnabled(String callingPackage) {
938 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
939 return false;
940 }
941
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 return (PhoneGlobals.getInstance().isSimPinEnabled());
943 }
944
945 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700946 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700947 }
948
Wink Savilleb564aae2014-10-23 10:18:09 -0700949 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700950 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700951 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
952 }
953
954 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700955 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700956 }
957
Wink Savilleb564aae2014-10-23 10:18:09 -0700958 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700959 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700960 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
961 }
962
963 /** {@hide} */
964 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700965 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700966 }
967
Wink Savilleb564aae2014-10-23 10:18:09 -0700968 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700970 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 checkSimPin.start();
972 return checkSimPin.unlockSim(null, pin);
973 }
974
Wink Saville9de0f752013-10-22 19:04:03 -0700975 /** {@hide} */
976 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700977 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700978 }
979
Wink Savilleb564aae2014-10-23 10:18:09 -0700980 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700982 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 checkSimPuk.start();
984 return checkSimPuk.unlockSim(puk, pin);
985 }
986
987 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700988 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700989 * a synchronous one.
990 */
991 private static class UnlockSim extends Thread {
992
993 private final IccCard mSimCard;
994
995 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700996 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
997 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998
999 // For replies from SimCard interface
1000 private Handler mHandler;
1001
1002 // For async handler to identify request type
1003 private static final int SUPPLY_PIN_COMPLETE = 100;
1004
1005 public UnlockSim(IccCard simCard) {
1006 mSimCard = simCard;
1007 }
1008
1009 @Override
1010 public void run() {
1011 Looper.prepare();
1012 synchronized (UnlockSim.this) {
1013 mHandler = new Handler() {
1014 @Override
1015 public void handleMessage(Message msg) {
1016 AsyncResult ar = (AsyncResult) msg.obj;
1017 switch (msg.what) {
1018 case SUPPLY_PIN_COMPLETE:
1019 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1020 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001021 mRetryCount = msg.arg1;
1022 if (ar.exception != null) {
1023 if (ar.exception instanceof CommandException &&
1024 ((CommandException)(ar.exception)).getCommandError()
1025 == CommandException.Error.PASSWORD_INCORRECT) {
1026 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1027 } else {
1028 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1029 }
1030 } else {
1031 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1032 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 mDone = true;
1034 UnlockSim.this.notifyAll();
1035 }
1036 break;
1037 }
1038 }
1039 };
1040 UnlockSim.this.notifyAll();
1041 }
1042 Looper.loop();
1043 }
1044
1045 /*
1046 * Use PIN or PUK to unlock SIM card
1047 *
1048 * If PUK is null, unlock SIM card with PIN
1049 *
1050 * If PUK is not null, unlock SIM card with PUK and set PIN code
1051 */
Wink Saville9de0f752013-10-22 19:04:03 -07001052 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053
1054 while (mHandler == null) {
1055 try {
1056 wait();
1057 } catch (InterruptedException e) {
1058 Thread.currentThread().interrupt();
1059 }
1060 }
1061 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1062
1063 if (puk == null) {
1064 mSimCard.supplyPin(pin, callback);
1065 } else {
1066 mSimCard.supplyPuk(puk, pin, callback);
1067 }
1068
1069 while (!mDone) {
1070 try {
1071 Log.d(LOG_TAG, "wait for done");
1072 wait();
1073 } catch (InterruptedException e) {
1074 // Restore the interrupted status
1075 Thread.currentThread().interrupt();
1076 }
1077 }
1078 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001079 int[] resultArray = new int[2];
1080 resultArray[0] = mResult;
1081 resultArray[1] = mRetryCount;
1082 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 }
1084 }
1085
1086 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001087 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001088
1089 }
1090
Wink Savilleb564aae2014-10-23 10:18:09 -07001091 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 // No permission check needed here: this call is harmless, and it's
1093 // needed for the ServiceState.requestStateUpdate() call (which is
1094 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001095 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 }
1097
1098 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001099 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001100 }
1101
Wink Savilleb564aae2014-10-23 10:18:09 -07001102 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001103 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 }
1105
1106 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001107 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001108
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 }
Wink Saville36469e72014-06-11 15:17:00 -07001110
Wink Savilleb564aae2014-10-23 10:18:09 -07001111 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001113 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001114 }
1115
1116 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001117 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001118 }
1119
Wink Savilleb564aae2014-10-23 10:18:09 -07001120 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001121 enforceModifyPermission();
1122 if ((getPhone(subId).getServiceState().getState() !=
1123 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001124 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 }
1126 return true;
1127 }
Wink Saville36469e72014-06-11 15:17:00 -07001128
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001129 public boolean needMobileRadioShutdown() {
1130 /*
1131 * If any of the Radios are available, it will need to be
1132 * shutdown. So return true if any Radio is available.
1133 */
1134 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1135 Phone phone = PhoneFactory.getPhone(i);
1136 if (phone != null && phone.isRadioAvailable()) return true;
1137 }
1138 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1139 return false;
1140 }
1141
1142 public void shutdownMobileRadios() {
1143 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1144 logv("Shutting down Phone " + i);
1145 shutdownRadioUsingPhoneId(i);
1146 }
1147 }
1148
1149 private void shutdownRadioUsingPhoneId(int phoneId) {
1150 enforceModifyPermission();
1151 Phone phone = PhoneFactory.getPhone(phoneId);
1152 if (phone != null && phone.isRadioAvailable()) {
1153 phone.shutdownRadio();
1154 }
1155 }
1156
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001157 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001158 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001159 }
1160
Wink Savilleb564aae2014-10-23 10:18:09 -07001161 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001163 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 return true;
1165 }
1166
Wink Saville36469e72014-06-11 15:17:00 -07001167 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 public boolean enableDataConnectivity() {
1169 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001170 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001171 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 return true;
1173 }
1174
Wink Saville36469e72014-06-11 15:17:00 -07001175 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 public boolean disableDataConnectivity() {
1177 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001178 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001179 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001180 return true;
1181 }
1182
Wink Saville36469e72014-06-11 15:17:00 -07001183 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001185 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001186 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001187 }
1188
1189 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001190 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001191 }
1192
Wink Savilleb564aae2014-10-23 10:18:09 -07001193 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001195 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 }
1197
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001199 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001200 }
1201
Wink Savilleb564aae2014-10-23 10:18:09 -07001202 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001203 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204 }
1205
1206 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001207 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001208 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 }
1210
1211 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001212 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001213 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215
1216 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001217 public Bundle getCellLocation(String callingPackage) {
1218 enforceFineOrCoarseLocationPermission("getCellLocation");
1219
1220 // OP_COARSE_LOCATION controls both fine and coarse location.
1221 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1222 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1223 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Jake Hambye994d462014-02-03 13:10:13 -08001226 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001227 if (DBG_LOC) log("getCellLocation: is active user");
1228 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001229 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1230 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 return data;
1232 } else {
1233 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1234 return null;
1235 }
1236 }
1237
Svetoslav64fad262015-04-14 14:35:21 -07001238 private void enforceFineOrCoarseLocationPermission(String message) {
1239 try {
1240 mApp.enforceCallingOrSelfPermission(
1241 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1242 } catch (SecurityException e) {
1243 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1244 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1245 // is the weaker precondition
1246 mApp.enforceCallingOrSelfPermission(
1247 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1248 }
1249 }
1250
1251
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 @Override
1253 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001254 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001255 }
1256
Wink Savilleb564aae2014-10-23 10:18:09 -07001257 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 mApp.enforceCallingOrSelfPermission(
1259 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001260 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 }
1262
1263 @Override
1264 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001265 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001266 }
1267
Wink Savilleb564aae2014-10-23 10:18:09 -07001268 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 mApp.enforceCallingOrSelfPermission(
1270 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001271 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 }
1273
1274 @Override
1275 @SuppressWarnings("unchecked")
1276 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001277 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1278
1279 // OP_COARSE_LOCATION controls both fine and coarse location.
1280 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1281 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1282 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 }
1284
1285 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1286 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1287 return null;
1288 }
Svetoslav64fad262015-04-14 14:35:21 -07001289
Jake Hambye994d462014-02-03 13:10:13 -08001290 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1292
1293 ArrayList<NeighboringCellInfo> cells = null;
1294
1295 try {
1296 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001297 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001299 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 }
1301 return cells;
1302 } else {
1303 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1304 return null;
1305 }
1306 }
1307
1308
1309 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001310 public List<CellInfo> getAllCellInfo(String callingPackage) {
1311 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1312
1313 // OP_COARSE_LOCATION controls both fine and coarse location.
1314 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1315 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1316 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 }
1318
Jake Hambye994d462014-02-03 13:10:13 -08001319 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001320 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001321 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1322 for (Phone phone : PhoneFactory.getPhones()) {
1323 cellInfos.addAll(phone.getAllCellInfo());
1324 }
1325 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 } else {
1327 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1328 return null;
1329 }
1330 }
1331
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001332 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 public void setCellInfoListRate(int rateInMillis) {
1334 mPhone.setCellInfoListRate(rateInMillis);
1335 }
1336
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001337 //
1338 // Internal helper methods.
1339 //
1340
Jake Hambye994d462014-02-03 13:10:13 -08001341 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 boolean ok;
1343
1344 boolean self = Binder.getCallingUid() == Process.myUid();
1345 if (!self) {
1346 // Get the caller's user id then clear the calling identity
1347 // which will be restored in the finally clause.
1348 int callingUser = UserHandle.getCallingUserId();
1349 long ident = Binder.clearCallingIdentity();
1350
1351 try {
1352 // With calling identity cleared the current user is the foreground user.
1353 int foregroundUser = ActivityManager.getCurrentUser();
1354 ok = (foregroundUser == callingUser);
1355 if (DBG_LOC) {
1356 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1357 + " callingUser=" + callingUser + " ok=" + ok);
1358 }
1359 } catch (Exception ex) {
1360 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1361 ok = false;
1362 } finally {
1363 Binder.restoreCallingIdentity(ident);
1364 }
1365 } else {
1366 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1367 ok = true;
1368 }
1369 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1370 return ok;
1371 }
1372
1373 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001374 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1375 *
1376 * @throws SecurityException if the caller does not have the required permission
1377 */
1378 private void enforceModifyPermission() {
1379 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1380 }
1381
1382 /**
Junda Liua2e36012014-07-09 18:30:01 -07001383 * Make sure either system app or the caller has carrier privilege.
1384 *
1385 * @throws SecurityException if the caller does not have the required permission/privilege
1386 */
1387 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001388 int permission = mApp.checkCallingOrSelfPermission(
1389 android.Manifest.permission.MODIFY_PHONE_STATE);
1390 if (permission == PackageManager.PERMISSION_GRANTED) {
1391 return;
1392 }
1393
1394 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001395 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001396 loge("No Carrier Privilege.");
1397 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001398 }
1399 }
1400
1401 /**
1402 * Make sure the caller has carrier privilege.
1403 *
1404 * @throws SecurityException if the caller does not have the required permission
1405 */
1406 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001407 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001408 loge("No Carrier Privilege.");
1409 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001410 }
1411 }
1412
1413 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 * Make sure the caller has the CALL_PHONE permission.
1415 *
1416 * @throws SecurityException if the caller does not have the required permission
1417 */
1418 private void enforceCallPermission() {
1419 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1420 }
1421
Shishir Agrawal566b7612013-10-28 14:41:00 -07001422 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001423 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1424 *
1425 * @throws SecurityException if the caller does not have the required permission
1426 */
1427 private void enforcePrivilegedPhoneStatePermission() {
1428 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1429 null);
1430 }
1431
Stuart Scott8eef64f2015-04-08 15:13:54 -07001432 private void enforceConnectivityInternalPermission() {
1433 mApp.enforceCallingOrSelfPermission(
1434 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1435 "ConnectivityService");
1436 }
1437
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 private String createTelUrl(String number) {
1439 if (TextUtils.isEmpty(number)) {
1440 return null;
1441 }
1442
Jake Hambye994d462014-02-03 13:10:13 -08001443 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 }
1445
Ihab Awadf9e92732013-12-05 18:02:52 -08001446 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1448 }
1449
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001450 private static void logv(String msg) {
1451 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1452 }
1453
Ihab Awadf9e92732013-12-05 18:02:52 -08001454 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1456 }
1457
1458 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001459 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001460 }
1461
Wink Savilleb564aae2014-10-23 10:18:09 -07001462 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001463 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 }
1465
1466 /**
1467 * Returns the CDMA ERI icon index to display
1468 */
1469 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001470 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001471
1472 }
1473
Wink Savilleb564aae2014-10-23 10:18:09 -07001474 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001475 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476 }
1477
1478 /**
1479 * Returns the CDMA ERI icon mode,
1480 * 0 - ON
1481 * 1 - FLASHING
1482 */
1483 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001484 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001485 }
1486
Wink Savilleb564aae2014-10-23 10:18:09 -07001487 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001488 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 }
1490
1491 /**
1492 * Returns the CDMA ERI text,
1493 */
1494 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001495 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001496 }
1497
Wink Savilleb564aae2014-10-23 10:18:09 -07001498 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001499 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 }
1501
1502 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001503 * Returns the CDMA MDN.
1504 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001505 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001506 enforceModifyPermissionOrCarrierPrivilege();
1507 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1508 return getPhone(subId).getLine1Number();
1509 } else {
1510 return null;
1511 }
1512 }
1513
1514 /**
1515 * Returns the CDMA MIN.
1516 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001517 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001518 enforceModifyPermissionOrCarrierPrivilege();
1519 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1520 return getPhone(subId).getCdmaMin();
1521 } else {
1522 return null;
1523 }
1524 }
1525
1526 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001527 * Returns true if CDMA provisioning needs to run.
1528 */
1529 public boolean needsOtaServiceProvisioning() {
1530 return mPhone.needsOtaServiceProvisioning();
1531 }
1532
1533 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001534 * Sets the voice mail number of a given subId.
1535 */
1536 @Override
1537 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001538 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001539 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1540 new Pair<String, String>(alphaTag, number), new Integer(subId));
1541 return success;
1542 }
1543
1544 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 * Returns the unread count of voicemails
1546 */
1547 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001548 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001549 }
1550
1551 /**
1552 * Returns the unread count of voicemails for a subId
1553 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001554 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001555 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 }
1557
1558 /**
1559 * Returns the data network type
1560 *
1561 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1562 */
1563 @Override
1564 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001565 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001566 }
1567
1568 /**
1569 * Returns the network type for a subId
1570 */
1571 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001572 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001573 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 }
1575
1576 /**
1577 * Returns the data network type
1578 */
1579 @Override
1580 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001581 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001582 }
1583
1584 /**
1585 * Returns the data network type for a subId
1586 */
1587 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001588 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001589 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
1592 /**
1593 * Returns the data network type
1594 */
1595 @Override
1596 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001597 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001598 }
1599
1600 /**
1601 * Returns the Voice network type for a subId
1602 */
1603 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001604 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001605 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001606 }
1607
1608 /**
1609 * @return true if a ICC card is present
1610 */
1611 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001612 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001613 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001614 }
1615
1616 /**
1617 * @return true if a ICC card is present for a slotId
1618 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001619 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001620 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1621 if (subId != null) {
1622 return getPhone(subId[0]).getIccCard().hasIccCard();
1623 } else {
1624 return false;
1625 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 }
1627
1628 /**
1629 * Return if the current radio is LTE on CDMA. This
1630 * is a tri-state return value as for a period of time
1631 * the mode may be unknown.
1632 *
1633 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001634 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 */
1636 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001637 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001638 }
1639
Wink Savilleb564aae2014-10-23 10:18:09 -07001640 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001641 return getPhone(subId).getLteOnCdmaMode();
1642 }
1643
1644 public void setPhone(Phone phone) {
1645 mPhone = phone;
1646 }
1647
1648 /**
1649 * {@hide}
1650 * Returns Default subId, 0 in the case of single standby.
1651 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001652 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001653 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001654 }
1655
Wink Savilleb564aae2014-10-23 10:18:09 -07001656 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001657 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001659
1660 /**
1661 * @see android.telephony.TelephonyManager.WifiCallingChoices
1662 */
1663 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001664 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1665 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001666 }
1667
1668 /**
1669 * @see android.telephony.TelephonyManager.WifiCallingChoices
1670 */
1671 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001672 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1673 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1674 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001675 }
1676
Sailesh Nepald1e68152013-12-12 19:08:02 -08001677 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001678 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001679 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001680 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001681
Shishir Agrawal566b7612013-10-28 14:41:00 -07001682 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001683 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001684 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001685
1686 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001687 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1688 CMD_OPEN_CHANNEL, AID);
1689 if (DBG) log("iccOpenLogicalChannel: " + response);
1690 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001691 }
1692
1693 @Override
1694 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001695 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001696
1697 if (DBG) log("iccCloseLogicalChannel: " + channel);
1698 if (channel < 0) {
1699 return false;
1700 }
Jake Hambye994d462014-02-03 13:10:13 -08001701 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001702 if (DBG) log("iccCloseLogicalChannel: " + success);
1703 return success;
1704 }
1705
1706 @Override
1707 public String iccTransmitApduLogicalChannel(int channel, int cla,
1708 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001709 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001710
1711 if (DBG) {
1712 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1713 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1714 " data=" + data);
1715 }
1716
1717 if (channel < 0) {
1718 return "";
1719 }
1720
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001721 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001722 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1723 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1724
Shishir Agrawal566b7612013-10-28 14:41:00 -07001725 // Append the returned status code to the end of the response payload.
1726 String s = Integer.toHexString(
1727 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001728 if (response.payload != null) {
1729 s = IccUtils.bytesToHexString(response.payload) + s;
1730 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001731 return s;
1732 }
Jake Hambye994d462014-02-03 13:10:13 -08001733
Evan Charltonc66da362014-05-16 14:06:40 -07001734 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001735 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1736 int p3, String data) {
1737 enforceModifyPermissionOrCarrierPrivilege();
1738
1739 if (DBG) {
1740 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1741 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1742 }
1743
1744 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1745 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1746 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1747
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001748 // Append the returned status code to the end of the response payload.
1749 String s = Integer.toHexString(
1750 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001751 if (response.payload != null) {
1752 s = IccUtils.bytesToHexString(response.payload) + s;
1753 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001754 return s;
1755 }
1756
1757 @Override
1758 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1759 String filePath) {
1760 enforceModifyPermissionOrCarrierPrivilege();
1761
1762 if (DBG) {
1763 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1764 p1 + " " + p2 + " " + p3 + ":" + filePath);
1765 }
1766
1767 IccIoResult response =
1768 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001769 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001770
1771 if (DBG) {
1772 log("Exchange SIM_IO [R]" + response);
1773 }
1774
1775 byte[] result = null;
1776 int length = 2;
1777 if (response.payload != null) {
1778 length = 2 + response.payload.length;
1779 result = new byte[length];
1780 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1781 } else {
1782 result = new byte[length];
1783 }
1784
1785 result[length - 1] = (byte) response.sw2;
1786 result[length - 2] = (byte) response.sw1;
1787 return result;
1788 }
1789
1790 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001791 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001792 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001793
1794 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1795 if (response.payload == null) {
1796 return "";
1797 }
1798
1799 // Append the returned status code to the end of the response payload.
1800 String s = Integer.toHexString(
1801 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1802 s = IccUtils.bytesToHexString(response.payload) + s;
1803 return s;
1804 }
1805
Jake Hambye994d462014-02-03 13:10:13 -08001806 /**
1807 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1808 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1809 *
1810 * @param itemID the ID of the item to read
1811 * @return the NV item as a String, or null on error.
1812 */
1813 @Override
1814 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001815 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001816 if (DBG) log("nvReadItem: item " + itemID);
1817 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1818 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1819 return value;
1820 }
1821
1822 /**
1823 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1824 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1825 *
1826 * @param itemID the ID of the item to read
1827 * @param itemValue the value to write, as a String
1828 * @return true on success; false on any failure
1829 */
1830 @Override
1831 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001832 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001833 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1834 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1835 new Pair<Integer, String>(itemID, itemValue));
1836 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1837 return success;
1838 }
1839
1840 /**
1841 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1842 * Used for device configuration by some CDMA operators.
1843 *
1844 * @param preferredRoamingList byte array containing the new PRL
1845 * @return true on success; false on any failure
1846 */
1847 @Override
1848 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001849 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001850 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1851 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1852 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1853 return success;
1854 }
1855
1856 /**
1857 * Perform the specified type of NV config reset.
1858 * Used for device configuration by some CDMA operators.
1859 *
1860 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1861 * @return true on success; false on any failure
1862 */
1863 @Override
1864 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001865 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001866 if (DBG) log("nvResetConfig: type " + resetType);
1867 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1868 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1869 return success;
1870 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001871
1872 /**
Wink Saville36469e72014-06-11 15:17:00 -07001873 * {@hide}
1874 * Returns Default sim, 0 in the case of single standby.
1875 */
1876 public int getDefaultSim() {
1877 //TODO Need to get it from Telephony Devcontroller
1878 return 0;
1879 }
1880
Svet Ganovb320e182015-04-16 12:30:10 -07001881 public String[] getPcscfAddress(String apnType, String callingPackage) {
1882 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1883 return new String[0];
1884 }
1885
1886
ram87fca6f2014-07-18 18:58:44 +05301887 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001888 }
1889
1890 public void setImsRegistrationState(boolean registered) {
1891 enforceModifyPermission();
1892 mPhone.setImsRegistrationState(registered);
1893 }
1894
1895 /**
Stuart Scott54788802015-03-30 13:18:01 -07001896 * Set the network selection mode to automatic.
1897 *
1898 */
1899 @Override
1900 public void setNetworkSelectionModeAutomatic(int subId) {
1901 enforceModifyPermissionOrCarrierPrivilege();
1902 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1903 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1904 }
1905
1906 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001907 * Get the calculated preferred network type.
1908 * Used for debugging incorrect network type.
1909 *
1910 * @return the preferred network type, defined in RILConstants.java.
1911 */
1912 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001913 public int getCalculatedPreferredNetworkType(String callingPackage) {
1914 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1915 return RILConstants.PREFERRED_NETWORK_MODE;
1916 }
1917
Amit Mahajan43330e02014-11-18 11:54:45 -08001918 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001919 }
1920
1921 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001922 * Get the preferred network type.
1923 * Used for device configuration by some CDMA operators.
1924 *
1925 * @return the preferred network type, defined in RILConstants.java.
1926 */
1927 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001928 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001929 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001930 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001931 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001932 int networkType = (result != null ? result[0] : -1);
1933 if (DBG) log("getPreferredNetworkType: " + networkType);
1934 return networkType;
1935 }
1936
1937 /**
1938 * Set the preferred network type.
1939 * Used for device configuration by some CDMA operators.
1940 *
1941 * @param networkType the preferred network type, defined in RILConstants.java.
1942 * @return true on success; false on any failure.
1943 */
1944 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001945 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001946 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001947 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1948 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001949 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001950 if (success) {
1951 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001952 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001953 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001954 return success;
1955 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001956
1957 /**
Junda Liu475951f2014-11-07 16:45:03 -08001958 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1959 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1960 * tethering.
1961 *
1962 * @return 0: Not required. 1: required. 2: Not set.
1963 * @hide
1964 */
1965 @Override
1966 public int getTetherApnRequired() {
1967 enforceModifyPermissionOrCarrierPrivilege();
1968 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1969 Settings.Global.TETHER_DUN_REQUIRED, 2);
1970 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1971 // config_tether_apndata.
1972 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1973 dunRequired = 1;
1974 }
1975 return dunRequired;
1976 }
1977
1978 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001979 * Set mobile data enabled
1980 * Used by the user through settings etc to turn on/off mobile data
1981 *
1982 * @param enable {@code true} turn turn data on, else {@code false}
1983 */
1984 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001985 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001986 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001987 int phoneId = mSubscriptionController.getPhoneId(subId);
1988 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1989 Phone phone = PhoneFactory.getPhone(phoneId);
1990 if (phone != null) {
1991 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1992 phone.setDataEnabled(enable);
1993 } else {
1994 loge("setDataEnabled: no phone for subId=" + subId);
1995 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001996 }
1997
1998 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07001999 * Get whether mobile data is enabled.
2000 *
2001 * Note that this used to be available from ConnectivityService, gated by
2002 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2003 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002004 *
2005 * @return {@code true} if data is enabled else {@code false}
2006 */
2007 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002008 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002009 try {
2010 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2011 null);
2012 } catch (Exception e) {
2013 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2014 null);
2015 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002016 int phoneId = mSubscriptionController.getPhoneId(subId);
2017 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2018 Phone phone = PhoneFactory.getPhone(phoneId);
2019 if (phone != null) {
2020 boolean retVal = phone.getDataEnabled();
2021 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2022 return retVal;
2023 } else {
2024 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2025 return false;
2026 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002027 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002028
2029 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002030 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002031 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002032 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002033 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002034 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2035 }
2036 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002037 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002038 }
Junda Liu29340342014-07-10 15:23:27 -07002039
2040 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002041 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002042 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002043 if (card == null) {
2044 loge("checkCarrierPrivilegesForPackage: No UICC");
2045 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2046 }
2047 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002048 }
Derek Tan89e89d42014-07-08 17:00:10 -07002049
2050 @Override
Junda Liue64de782015-04-16 17:19:16 -07002051 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2052 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2053 loge("phoneId " + phoneId + " is not valid.");
2054 return null;
2055 }
2056 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002057 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002058 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002059 return null ;
2060 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002061 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002062 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002063 }
2064
Wink Savilleb564aae2014-10-23 10:18:09 -07002065 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002066 UiccCard card = getPhone(subId).getUiccCard();
2067 if (card == null) {
2068 loge("getIccId: No UICC");
2069 return null;
2070 }
2071 String iccId = card.getIccId();
2072 if (TextUtils.isEmpty(iccId)) {
2073 loge("getIccId: ICC ID is null or empty.");
2074 return null;
2075 }
2076 return iccId;
2077 }
2078
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002079 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002080 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2081 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002082 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002083
Jeff Sharkey85190e62014-12-05 09:40:12 -08002084 final String iccId = getIccId(subId);
2085 final String subscriberId = getPhone(subId).getSubscriberId();
2086
2087 if (DBG_MERGE) {
2088 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2089 + subscriberId + " to " + number);
2090 }
2091
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002092 if (TextUtils.isEmpty(iccId)) {
2093 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002094 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002095
Jeff Sharkey85190e62014-12-05 09:40:12 -08002096 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2097
2098 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002099 if (alphaTag == null) {
2100 editor.remove(alphaTagPrefKey);
2101 } else {
2102 editor.putString(alphaTagPrefKey, alphaTag);
2103 }
2104
Jeff Sharkey85190e62014-12-05 09:40:12 -08002105 // Record both the line number and IMSI for this ICCID, since we need to
2106 // track all merged IMSIs based on line number
2107 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2108 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002109 if (number == null) {
2110 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002111 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002112 } else {
2113 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002114 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002115 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002116
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002117 editor.commit();
2118 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002119 }
2120
2121 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002122 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2123 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2124 return null;
2125 }
Derek Tan97ebb422014-09-05 16:55:38 -07002126
2127 String iccId = getIccId(subId);
2128 if (iccId != null) {
2129 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002130 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002131 }
Derek Tan97ebb422014-09-05 16:55:38 -07002132 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002133 }
2134
2135 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002136 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2137 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2138 return null;
2139 }
Derek Tan97ebb422014-09-05 16:55:38 -07002140
2141 String iccId = getIccId(subId);
2142 if (iccId != null) {
2143 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002144 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002145 }
Derek Tan97ebb422014-09-05 16:55:38 -07002146 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002147 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002148
2149 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002150 public String[] getMergedSubscriberIds() {
2151 final Context context = mPhone.getContext();
2152 final TelephonyManager tele = TelephonyManager.from(context);
2153 final SubscriptionManager sub = SubscriptionManager.from(context);
2154
2155 // Figure out what subscribers are currently active
2156 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2157 final int[] subIds = sub.getActiveSubscriptionIdList();
2158 for (int subId : subIds) {
2159 activeSubscriberIds.add(tele.getSubscriberId(subId));
2160 }
2161
2162 // First pass, find a number override for an active subscriber
2163 String mergeNumber = null;
2164 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2165 for (String key : prefs.keySet()) {
2166 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2167 final String subscriberId = (String) prefs.get(key);
2168 if (activeSubscriberIds.contains(subscriberId)) {
2169 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2170 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2171 mergeNumber = (String) prefs.get(numberKey);
2172 if (DBG_MERGE) {
2173 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2174 + " for active subscriber " + subscriberId);
2175 }
2176 if (!TextUtils.isEmpty(mergeNumber)) {
2177 break;
2178 }
2179 }
2180 }
2181 }
2182
2183 // Shortcut when no active merged subscribers
2184 if (TextUtils.isEmpty(mergeNumber)) {
2185 return null;
2186 }
2187
2188 // Second pass, find all subscribers under that line override
2189 final ArraySet<String> result = new ArraySet<>();
2190 for (String key : prefs.keySet()) {
2191 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2192 final String number = (String) prefs.get(key);
2193 if (mergeNumber.equals(number)) {
2194 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2195 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2196 final String subscriberId = (String) prefs.get(subscriberKey);
2197 if (!TextUtils.isEmpty(subscriberId)) {
2198 result.add(subscriberId);
2199 }
2200 }
2201 }
2202 }
2203
2204 final String[] resultArray = result.toArray(new String[result.size()]);
2205 Arrays.sort(resultArray);
2206 if (DBG_MERGE) {
2207 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2208 }
2209 return resultArray;
2210 }
2211
2212 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002213 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002214 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002215 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002216 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002217
2218 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002219 public boolean setRoamingOverride(List<String> gsmRoamingList,
2220 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2221 List<String> cdmaNonRoamingList) {
2222 enforceCarrierPrivilege();
2223 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2224 cdmaNonRoamingList);
2225 }
2226
2227 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002228 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2229 enforceModifyPermission();
2230
2231 int returnValue = 0;
2232 try {
2233 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2234 if(result.exception == null) {
2235 if (result.result != null) {
2236 byte[] responseData = (byte[])(result.result);
2237 if(responseData.length > oemResp.length) {
2238 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2239 responseData.length + "bytes. Buffer Size is " +
2240 oemResp.length + "bytes.");
2241 }
2242 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2243 returnValue = responseData.length;
2244 }
2245 } else {
2246 CommandException ex = (CommandException) result.exception;
2247 returnValue = ex.getCommandError().ordinal();
2248 if(returnValue > 0) returnValue *= -1;
2249 }
2250 } catch (RuntimeException e) {
2251 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2252 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2253 if(returnValue > 0) returnValue *= -1;
2254 }
2255
2256 return returnValue;
2257 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002258
2259 @Override
2260 public void setRadioCapability(RadioAccessFamily[] rafs) {
2261 try {
2262 ProxyController.getInstance().setRadioCapability(rafs);
2263 } catch (RuntimeException e) {
2264 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2265 }
2266 }
2267
2268 @Override
2269 public int getRadioAccessFamily(int phoneId) {
2270 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2271 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002272
2273 @Override
2274 public void enableVideoCalling(boolean enable) {
2275 enforceModifyPermission();
2276 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2277 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2278 editor.commit();
2279 }
2280
2281 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002282 public boolean isVideoCallingEnabled(String callingPackage) {
2283 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2284 return false;
2285 }
2286
Andrew Lee77527ac2014-10-21 16:57:39 -07002287 // Check the user preference and the system-level IMS setting. Even if the user has
2288 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2289 // In the long run, we may instead need to check if there exists a connection service
2290 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002291 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2292 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2293 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002294 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002295
Andrew Leea1239f22015-03-02 17:44:07 -08002296 @Override
2297 public boolean canChangeDtmfToneLength() {
Junda Liu12f7d802015-05-01 12:06:44 -07002298 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_DTMF_TYPE_ENABLED);
Andrew Leea1239f22015-03-02 17:44:07 -08002299 }
2300
2301 @Override
2302 public boolean isWorldPhone() {
Junda Liu12f7d802015-05-01 12:06:44 -07002303 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_WORLD_PHONE);
Andrew Leea1239f22015-03-02 17:44:07 -08002304 }
2305
Andrew Lee9431b832015-03-09 18:46:45 -07002306 @Override
2307 public boolean isTtyModeSupported() {
2308 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2309 TelephonyManager telephonyManager =
2310 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2311 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2312 }
2313
2314 @Override
2315 public boolean isHearingAidCompatibilitySupported() {
2316 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2317 }
2318
Sanket Padawe7310cc72015-01-14 09:53:20 -08002319 /**
2320 * Returns the unique device ID of phone, for example, the IMEI for
2321 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2322 *
2323 * <p>Requires Permission:
2324 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2325 */
2326 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002327 public String getDeviceId(String callingPackage) {
2328 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2329 return null;
2330 }
2331
Sanket Padawe7310cc72015-01-14 09:53:20 -08002332 final Phone phone = PhoneFactory.getPhone(0);
2333 if (phone != null) {
2334 return phone.getDeviceId();
2335 } else {
2336 return null;
2337 }
2338 }
2339
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002340 /*
2341 * {@hide}
2342 * Returns the IMS Registration Status
2343 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002344 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002345 public boolean isImsRegistered() {
2346 return mPhone.isImsRegistered();
2347 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002348
2349 @Override
2350 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2351 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2352 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002353
Nathan Haroldc55097a2015-03-11 18:14:50 -07002354 /*
2355 * {@hide}
2356 * Returns the IMS Registration Status
2357 */
2358 public boolean isWifiCallingEnabled() {
2359 return mPhone.isWifiCallingEnabled();
2360 }
2361
2362 /*
2363 * {@hide}
2364 * Returns the IMS Registration Status
2365 */
2366 public boolean isVolteEnabled() {
2367 return mPhone.isVolteEnabled();
2368 }
Svet Ganovb320e182015-04-16 12:30:10 -07002369
2370 private boolean canReadPhoneState(String callingPackage, String message) {
2371 mApp.enforceCallingOrSelfPermission(
2372 android.Manifest.permission.READ_PHONE_STATE, message);
2373
2374 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2375 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2376 return false;
2377 }
2378
2379 return true;
2380 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002381
2382 @Override
2383 public void factoryReset(int subId) {
2384 enforceConnectivityInternalPermission();
Svet Ganovcc087f82015-05-12 20:35:54 -07002385 final long identity = Binder.clearCallingIdentity();
2386 try {
2387 if (SubscriptionManager.isUsableSubIdValue(subId)) {
2388 // Enable data
2389 setDataEnabled(subId, true);
2390 // Set network selection mode to automatic
2391 setNetworkSelectionModeAutomatic(subId);
2392 // Set preferred mobile network type to the best available
2393 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2394 // Turn off roaming
2395 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2396 }
2397 } finally {
2398 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002399 }
2400 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002401}