blob: bde8e024abf292381913aa783361610ac103786a [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;
Junda Liua754ba12015-05-20 01:17:52 -0700467 if (ar.exception instanceof CommandException) {
468 CommandException.Error error =
469 ((CommandException) (ar.exception)).getCommandError();
470 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700471 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700472 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700473 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700474 }
475 }
476 openChannelResp = new IccOpenLogicalChannelResponse(
477 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700479 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700480 synchronized (request) {
481 request.notifyAll();
482 }
483 break;
484
485 case CMD_CLOSE_CHANNEL:
486 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700487 if (uiccCard == null) {
488 loge("iccCloseLogicalChannel: No UICC");
489 request.result = new IccIoResult(0x6F, 0, (byte[])null);
490 synchronized (request) {
491 request.notifyAll();
492 }
493 } else {
494 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
495 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
496 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700497 break;
498
499 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800500 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
501 break;
502
503 case CMD_NV_READ_ITEM:
504 request = (MainThreadRequest) msg.obj;
505 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
506 mPhone.nvReadItem((Integer) request.argument, onCompleted);
507 break;
508
509 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 ar = (AsyncResult) msg.obj;
511 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800512 if (ar.exception == null && ar.result != null) {
513 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800515 request.result = "";
516 if (ar.result == null) {
517 loge("nvReadItem: Empty response");
518 } else if (ar.exception instanceof CommandException) {
519 loge("nvReadItem: CommandException: " +
520 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700521 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800522 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700523 }
524 }
525 synchronized (request) {
526 request.notifyAll();
527 }
528 break;
529
Jake Hambye994d462014-02-03 13:10:13 -0800530 case CMD_NV_WRITE_ITEM:
531 request = (MainThreadRequest) msg.obj;
532 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
533 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
534 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
535 break;
536
537 case EVENT_NV_WRITE_ITEM_DONE:
538 handleNullReturnEvent(msg, "nvWriteItem");
539 break;
540
541 case CMD_NV_WRITE_CDMA_PRL:
542 request = (MainThreadRequest) msg.obj;
543 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
544 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
545 break;
546
547 case EVENT_NV_WRITE_CDMA_PRL_DONE:
548 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
549 break;
550
551 case CMD_NV_RESET_CONFIG:
552 request = (MainThreadRequest) msg.obj;
553 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
554 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
555 break;
556
557 case EVENT_NV_RESET_CONFIG_DONE:
558 handleNullReturnEvent(msg, "nvResetConfig");
559 break;
560
Jake Hamby7c27be32014-03-03 13:25:59 -0800561 case CMD_GET_PREFERRED_NETWORK_TYPE:
562 request = (MainThreadRequest) msg.obj;
563 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700564 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800565 break;
566
567 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
568 ar = (AsyncResult) msg.obj;
569 request = (MainThreadRequest) ar.userObj;
570 if (ar.exception == null && ar.result != null) {
571 request.result = ar.result; // Integer
572 } else {
573 request.result = -1;
574 if (ar.result == null) {
575 loge("getPreferredNetworkType: Empty response");
576 } else if (ar.exception instanceof CommandException) {
577 loge("getPreferredNetworkType: CommandException: " +
578 ar.exception);
579 } else {
580 loge("getPreferredNetworkType: Unknown exception");
581 }
582 }
583 synchronized (request) {
584 request.notifyAll();
585 }
586 break;
587
588 case CMD_SET_PREFERRED_NETWORK_TYPE:
589 request = (MainThreadRequest) msg.obj;
590 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
591 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700592 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800593 break;
594
595 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
596 handleNullReturnEvent(msg, "setPreferredNetworkType");
597 break;
598
Steven Liu4bf01bc2014-07-17 11:05:29 -0500599 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
600 request = (MainThreadRequest)msg.obj;
601 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
602 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
603 break;
604
605 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
606 ar = (AsyncResult)msg.obj;
607 request = (MainThreadRequest)ar.userObj;
608 request.result = ar;
609 synchronized (request) {
610 request.notifyAll();
611 }
612 break;
613
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800614 case CMD_SET_VOICEMAIL_NUMBER:
615 request = (MainThreadRequest) msg.obj;
616 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
617 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800618 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
619 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800620 break;
621
622 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
623 handleNullReturnEvent(msg, "setVoicemailNumber");
624 break;
625
Stuart Scott54788802015-03-30 13:18:01 -0700626 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
627 request = (MainThreadRequest) msg.obj;
628 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
629 request);
630 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
631 break;
632
633 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
634 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
635 break;
636
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700637 default:
638 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
639 break;
640 }
641 }
Jake Hambye994d462014-02-03 13:10:13 -0800642
643 private void handleNullReturnEvent(Message msg, String command) {
644 AsyncResult ar = (AsyncResult) msg.obj;
645 MainThreadRequest request = (MainThreadRequest) ar.userObj;
646 if (ar.exception == null) {
647 request.result = true;
648 } else {
649 request.result = false;
650 if (ar.exception instanceof CommandException) {
651 loge(command + ": CommandException: " + ar.exception);
652 } else {
653 loge(command + ": Unknown exception");
654 }
655 }
656 synchronized (request) {
657 request.notifyAll();
658 }
659 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660 }
661
662 /**
663 * Posts the specified command to be executed on the main thread,
664 * waits for the request to complete, and returns the result.
665 * @see #sendRequestAsync
666 */
667 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700668 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700669 }
670
671 /**
672 * Posts the specified command to be executed on the main thread,
673 * waits for the request to complete, and returns the result.
674 * @see #sendRequestAsync
675 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800676 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700677 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
678 throw new RuntimeException("This method will deadlock if called from the main thread.");
679 }
680
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800681 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 Message msg = mMainThreadHandler.obtainMessage(command, request);
683 msg.sendToTarget();
684
685 // Wait for the request to complete
686 synchronized (request) {
687 while (request.result == null) {
688 try {
689 request.wait();
690 } catch (InterruptedException e) {
691 // Do nothing, go back and wait until the request is complete
692 }
693 }
694 }
695 return request.result;
696 }
697
698 /**
699 * Asynchronous ("fire and forget") version of sendRequest():
700 * Posts the specified command to be executed on the main thread, and
701 * returns immediately.
702 * @see #sendRequest
703 */
704 private void sendRequestAsync(int command) {
705 mMainThreadHandler.sendEmptyMessage(command);
706 }
707
708 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700709 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
710 * @see {@link #sendRequest(int,Object)}
711 */
712 private void sendRequestAsync(int command, Object argument) {
713 MainThreadRequest request = new MainThreadRequest(argument);
714 Message msg = mMainThreadHandler.obtainMessage(command, request);
715 msg.sendToTarget();
716 }
717
718 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700719 * Initialize the singleton PhoneInterfaceManager instance.
720 * This is only done once, at startup, from PhoneApp.onCreate().
721 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700722 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 synchronized (PhoneInterfaceManager.class) {
724 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700725 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 } else {
727 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
728 }
729 return sInstance;
730 }
731 }
732
733 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700734 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700735 mApp = app;
736 mPhone = phone;
737 mCM = PhoneGlobals.getInstance().mCM;
738 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
739 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700740 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700741 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800742 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800743
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700744 publish();
745 }
746
747 private void publish() {
748 if (DBG) log("publish: " + this);
749
750 ServiceManager.addService("phone", this);
751 }
752
Stuart Scott584921c2015-01-15 17:10:34 -0800753 private Phone getPhoneFromRequest(MainThreadRequest request) {
754 return (request.subId == null) ? mPhone : getPhone(request.subId);
755 }
756
Wink Saville36469e72014-06-11 15:17:00 -0700757 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700758 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800759 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700760 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 //
762 // Implementation of the ITelephony interface.
763 //
764
765 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700766 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700767 }
768
Wink Savilleb564aae2014-10-23 10:18:09 -0700769 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 if (DBG) log("dial: " + number);
771 // No permission check needed here: This is just a wrapper around the
772 // ACTION_DIAL intent, which is available to any app since it puts up
773 // the UI before it does anything.
774
775 String url = createTelUrl(number);
776 if (url == null) {
777 return;
778 }
779
780 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700781 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
783 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
784 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
785 mApp.startActivity(intent);
786 }
787 }
788
789 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700790 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700791 }
792
Wink Savilleb564aae2014-10-23 10:18:09 -0700793 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 if (DBG) log("call: " + number);
795
796 // This is just a wrapper around the ACTION_CALL intent, but we still
797 // need to do a permission check since we're calling startActivity()
798 // from the context of the phone app.
799 enforceCallPermission();
800
801 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
802 != AppOpsManager.MODE_ALLOWED) {
803 return;
804 }
805
806 String url = createTelUrl(number);
807 if (url == null) {
808 return;
809 }
810
Wink Saville08874612014-08-31 19:19:58 -0700811 boolean isValid = false;
Svetoslav483aff72015-04-21 14:16:07 -0700812 List<SubscriptionInfo> slist;
813
814 final long identity = Binder.clearCallingIdentity();
815 try {
816 slist = mSubscriptionController.getActiveSubscriptionInfoList(
817 mPhone.getContext().getOpPackageName());
818 } finally {
819 Binder.restoreCallingIdentity(identity);
820 }
821
Wink Saville3ab207e2014-11-20 13:07:20 -0800822 if (slist != null) {
823 for (SubscriptionInfo subInfoRecord : slist) {
824 if (subInfoRecord.getSubscriptionId() == subId) {
825 isValid = true;
826 break;
827 }
Wink Saville08874612014-08-31 19:19:58 -0700828 }
829 }
830 if (isValid == false) {
831 return;
832 }
833
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700835 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
837 mApp.startActivity(intent);
838 }
839
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 /**
841 * End a call based on call state
842 * @return true is a call was ended
843 */
844 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700845 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700846 }
847
848 /**
849 * End a call based on the call state of the subId
850 * @return true is a call was ended
851 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700852 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800854 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 }
856
857 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700858 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700859 }
860
Wink Savilleb564aae2014-10-23 10:18:09 -0700861 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700862 if (DBG) log("answerRingingCall...");
863 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
864 // but that can probably wait till the big TelephonyManager API overhaul.
865 // For now, protect this call with the MODIFY_PHONE_STATE permission.
866 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800867 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700868 }
869
870 /**
871 * Make the actual telephony calls to implement answerRingingCall().
872 * This should only be called from the main thread of the Phone app.
873 * @see #answerRingingCall
874 *
875 * TODO: it would be nice to return true if we answered the call, or
876 * false if there wasn't actually a ringing incoming call, or some
877 * other error occurred. (In other words, pass back the return value
878 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
879 * But that would require calling this method via sendRequest() rather
880 * than sendRequestAsync(), and right now we don't actually *need* that
881 * return value, so let's just return void for now.
882 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700883 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700884 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700886 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
887 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 if (hasActiveCall && hasHoldingCall) {
889 // Both lines are in use!
890 // TODO: provide a flag to let the caller specify what
891 // policy to use if both lines are in use. (The current
892 // behavior is hardwired to "answer incoming, end ongoing",
893 // which is how the CALL button is specced to behave.)
894 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
895 return;
896 } else {
897 // answerCall() will automatically hold the current active
898 // call, if there is one.
899 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
900 return;
901 }
902 } else {
903 // No call was ringing.
904 return;
905 }
906 }
907
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700909 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700910 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700911 public void silenceRinger() {
912 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 }
914
915 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700916 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700917 }
918
Wink Savilleb564aae2014-10-23 10:18:09 -0700919 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700920 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 }
922
923 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700924 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700925 }
926
Wink Savilleb564aae2014-10-23 10:18:09 -0700927 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700928 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700929 }
930
931 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700932 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700933 }
934
Wink Savilleb564aae2014-10-23 10:18:09 -0700935 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700936 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 }
938
Svet Ganovb320e182015-04-16 12:30:10 -0700939 public boolean isSimPinEnabled(String callingPackage) {
940 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
941 return false;
942 }
943
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 return (PhoneGlobals.getInstance().isSimPinEnabled());
945 }
946
947 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700948 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700949 }
950
Wink Savilleb564aae2014-10-23 10:18:09 -0700951 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700952 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700953 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
954 }
955
956 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700957 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700958 }
959
Wink Savilleb564aae2014-10-23 10:18:09 -0700960 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700961 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700962 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
963 }
964
965 /** {@hide} */
966 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700967 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700968 }
969
Wink Savilleb564aae2014-10-23 10:18:09 -0700970 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700972 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 checkSimPin.start();
974 return checkSimPin.unlockSim(null, pin);
975 }
976
Wink Saville9de0f752013-10-22 19:04:03 -0700977 /** {@hide} */
978 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700979 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700980 }
981
Wink Savilleb564aae2014-10-23 10:18:09 -0700982 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700984 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 checkSimPuk.start();
986 return checkSimPuk.unlockSim(puk, pin);
987 }
988
989 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700990 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 * a synchronous one.
992 */
993 private static class UnlockSim extends Thread {
994
995 private final IccCard mSimCard;
996
997 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -0700998 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
999 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000
1001 // For replies from SimCard interface
1002 private Handler mHandler;
1003
1004 // For async handler to identify request type
1005 private static final int SUPPLY_PIN_COMPLETE = 100;
1006
1007 public UnlockSim(IccCard simCard) {
1008 mSimCard = simCard;
1009 }
1010
1011 @Override
1012 public void run() {
1013 Looper.prepare();
1014 synchronized (UnlockSim.this) {
1015 mHandler = new Handler() {
1016 @Override
1017 public void handleMessage(Message msg) {
1018 AsyncResult ar = (AsyncResult) msg.obj;
1019 switch (msg.what) {
1020 case SUPPLY_PIN_COMPLETE:
1021 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1022 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001023 mRetryCount = msg.arg1;
1024 if (ar.exception != null) {
1025 if (ar.exception instanceof CommandException &&
1026 ((CommandException)(ar.exception)).getCommandError()
1027 == CommandException.Error.PASSWORD_INCORRECT) {
1028 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1029 } else {
1030 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1031 }
1032 } else {
1033 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1034 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 mDone = true;
1036 UnlockSim.this.notifyAll();
1037 }
1038 break;
1039 }
1040 }
1041 };
1042 UnlockSim.this.notifyAll();
1043 }
1044 Looper.loop();
1045 }
1046
1047 /*
1048 * Use PIN or PUK to unlock SIM card
1049 *
1050 * If PUK is null, unlock SIM card with PIN
1051 *
1052 * If PUK is not null, unlock SIM card with PUK and set PIN code
1053 */
Wink Saville9de0f752013-10-22 19:04:03 -07001054 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055
1056 while (mHandler == null) {
1057 try {
1058 wait();
1059 } catch (InterruptedException e) {
1060 Thread.currentThread().interrupt();
1061 }
1062 }
1063 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1064
1065 if (puk == null) {
1066 mSimCard.supplyPin(pin, callback);
1067 } else {
1068 mSimCard.supplyPuk(puk, pin, callback);
1069 }
1070
1071 while (!mDone) {
1072 try {
1073 Log.d(LOG_TAG, "wait for done");
1074 wait();
1075 } catch (InterruptedException e) {
1076 // Restore the interrupted status
1077 Thread.currentThread().interrupt();
1078 }
1079 }
1080 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001081 int[] resultArray = new int[2];
1082 resultArray[0] = mResult;
1083 resultArray[1] = mRetryCount;
1084 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 }
1086 }
1087
1088 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001089 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001090
1091 }
1092
Wink Savilleb564aae2014-10-23 10:18:09 -07001093 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 // No permission check needed here: this call is harmless, and it's
1095 // needed for the ServiceState.requestStateUpdate() call (which is
1096 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001097 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001098 }
1099
1100 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001101 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001102 }
1103
Wink Savilleb564aae2014-10-23 10:18:09 -07001104 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001105 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 }
1107
1108 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001109 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001110
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 }
Wink Saville36469e72014-06-11 15:17:00 -07001112
Wink Savilleb564aae2014-10-23 10:18:09 -07001113 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001115 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001116 }
1117
1118 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001119 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001120 }
1121
Wink Savilleb564aae2014-10-23 10:18:09 -07001122 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001123 enforceModifyPermission();
1124 if ((getPhone(subId).getServiceState().getState() !=
1125 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001126 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 }
1128 return true;
1129 }
Wink Saville36469e72014-06-11 15:17:00 -07001130
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001131 public boolean needMobileRadioShutdown() {
1132 /*
1133 * If any of the Radios are available, it will need to be
1134 * shutdown. So return true if any Radio is available.
1135 */
1136 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1137 Phone phone = PhoneFactory.getPhone(i);
1138 if (phone != null && phone.isRadioAvailable()) return true;
1139 }
1140 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1141 return false;
1142 }
1143
1144 public void shutdownMobileRadios() {
1145 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1146 logv("Shutting down Phone " + i);
1147 shutdownRadioUsingPhoneId(i);
1148 }
1149 }
1150
1151 private void shutdownRadioUsingPhoneId(int phoneId) {
1152 enforceModifyPermission();
1153 Phone phone = PhoneFactory.getPhone(phoneId);
1154 if (phone != null && phone.isRadioAvailable()) {
1155 phone.shutdownRadio();
1156 }
1157 }
1158
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001159 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001160 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001161 }
1162
Wink Savilleb564aae2014-10-23 10:18:09 -07001163 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001165 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 return true;
1167 }
1168
Wink Saville36469e72014-06-11 15:17:00 -07001169 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 public boolean enableDataConnectivity() {
1171 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001172 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001173 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 return true;
1175 }
1176
Wink Saville36469e72014-06-11 15:17:00 -07001177 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 public boolean disableDataConnectivity() {
1179 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001180 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001181 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001182 return true;
1183 }
1184
Wink Saville36469e72014-06-11 15:17:00 -07001185 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001187 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001188 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001189 }
1190
1191 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001192 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001193 }
1194
Wink Savilleb564aae2014-10-23 10:18:09 -07001195 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001197 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 }
1199
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001201 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001202 }
1203
Wink Savilleb564aae2014-10-23 10:18:09 -07001204 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001205 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 }
1207
1208 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001209 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001210 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 }
1212
1213 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001214 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001215 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 }
1217
1218 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001219 public Bundle getCellLocation(String callingPackage) {
1220 enforceFineOrCoarseLocationPermission("getCellLocation");
1221
1222 // OP_COARSE_LOCATION controls both fine and coarse location.
1223 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1224 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1225 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001226 }
1227
Jake Hambye994d462014-02-03 13:10:13 -08001228 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 if (DBG_LOC) log("getCellLocation: is active user");
1230 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001231 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1232 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 return data;
1234 } else {
1235 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1236 return null;
1237 }
1238 }
1239
Svetoslav64fad262015-04-14 14:35:21 -07001240 private void enforceFineOrCoarseLocationPermission(String message) {
1241 try {
1242 mApp.enforceCallingOrSelfPermission(
1243 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1244 } catch (SecurityException e) {
1245 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1246 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1247 // is the weaker precondition
1248 mApp.enforceCallingOrSelfPermission(
1249 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1250 }
1251 }
1252
1253
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 @Override
1255 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001256 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001257 }
1258
Wink Savilleb564aae2014-10-23 10:18:09 -07001259 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 mApp.enforceCallingOrSelfPermission(
1261 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001262 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001263 }
1264
1265 @Override
1266 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001267 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001268 }
1269
Wink Savilleb564aae2014-10-23 10:18:09 -07001270 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001271 mApp.enforceCallingOrSelfPermission(
1272 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001273 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001274 }
1275
1276 @Override
1277 @SuppressWarnings("unchecked")
1278 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001279 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1280
1281 // OP_COARSE_LOCATION controls both fine and coarse location.
1282 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1283 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1284 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 }
1286
1287 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1288 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1289 return null;
1290 }
Svetoslav64fad262015-04-14 14:35:21 -07001291
Jake Hambye994d462014-02-03 13:10:13 -08001292 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1294
1295 ArrayList<NeighboringCellInfo> cells = null;
1296
1297 try {
1298 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001299 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001301 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001302 }
1303 return cells;
1304 } else {
1305 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1306 return null;
1307 }
1308 }
1309
1310
1311 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001312 public List<CellInfo> getAllCellInfo(String callingPackage) {
1313 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1314
1315 // OP_COARSE_LOCATION controls both fine and coarse location.
1316 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1317 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1318 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 }
1320
Jake Hambye994d462014-02-03 13:10:13 -08001321 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001323 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1324 for (Phone phone : PhoneFactory.getPhones()) {
1325 cellInfos.addAll(phone.getAllCellInfo());
1326 }
1327 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001328 } else {
1329 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1330 return null;
1331 }
1332 }
1333
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001334 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 public void setCellInfoListRate(int rateInMillis) {
1336 mPhone.setCellInfoListRate(rateInMillis);
1337 }
1338
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 //
1340 // Internal helper methods.
1341 //
1342
Jake Hambye994d462014-02-03 13:10:13 -08001343 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 boolean ok;
1345
1346 boolean self = Binder.getCallingUid() == Process.myUid();
1347 if (!self) {
1348 // Get the caller's user id then clear the calling identity
1349 // which will be restored in the finally clause.
1350 int callingUser = UserHandle.getCallingUserId();
1351 long ident = Binder.clearCallingIdentity();
1352
1353 try {
1354 // With calling identity cleared the current user is the foreground user.
1355 int foregroundUser = ActivityManager.getCurrentUser();
1356 ok = (foregroundUser == callingUser);
1357 if (DBG_LOC) {
1358 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1359 + " callingUser=" + callingUser + " ok=" + ok);
1360 }
1361 } catch (Exception ex) {
1362 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1363 ok = false;
1364 } finally {
1365 Binder.restoreCallingIdentity(ident);
1366 }
1367 } else {
1368 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1369 ok = true;
1370 }
1371 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1372 return ok;
1373 }
1374
1375 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1377 *
1378 * @throws SecurityException if the caller does not have the required permission
1379 */
1380 private void enforceModifyPermission() {
1381 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1382 }
1383
1384 /**
Junda Liua2e36012014-07-09 18:30:01 -07001385 * Make sure either system app or the caller has carrier privilege.
1386 *
1387 * @throws SecurityException if the caller does not have the required permission/privilege
1388 */
1389 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001390 int permission = mApp.checkCallingOrSelfPermission(
1391 android.Manifest.permission.MODIFY_PHONE_STATE);
1392 if (permission == PackageManager.PERMISSION_GRANTED) {
1393 return;
1394 }
1395
1396 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001397 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001398 loge("No Carrier Privilege.");
1399 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001400 }
1401 }
1402
1403 /**
1404 * Make sure the caller has carrier privilege.
1405 *
1406 * @throws SecurityException if the caller does not have the required permission
1407 */
1408 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001409 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001410 loge("No Carrier Privilege.");
1411 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001412 }
1413 }
1414
1415 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001416 * Make sure the caller has the CALL_PHONE permission.
1417 *
1418 * @throws SecurityException if the caller does not have the required permission
1419 */
1420 private void enforceCallPermission() {
1421 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1422 }
1423
Shishir Agrawal566b7612013-10-28 14:41:00 -07001424 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001425 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1426 *
1427 * @throws SecurityException if the caller does not have the required permission
1428 */
1429 private void enforcePrivilegedPhoneStatePermission() {
1430 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1431 null);
1432 }
1433
Stuart Scott8eef64f2015-04-08 15:13:54 -07001434 private void enforceConnectivityInternalPermission() {
1435 mApp.enforceCallingOrSelfPermission(
1436 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1437 "ConnectivityService");
1438 }
1439
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 private String createTelUrl(String number) {
1441 if (TextUtils.isEmpty(number)) {
1442 return null;
1443 }
1444
Jake Hambye994d462014-02-03 13:10:13 -08001445 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 }
1447
Ihab Awadf9e92732013-12-05 18:02:52 -08001448 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1450 }
1451
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001452 private static void logv(String msg) {
1453 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1454 }
1455
Ihab Awadf9e92732013-12-05 18:02:52 -08001456 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1458 }
1459
1460 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001461 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001462 }
1463
Wink Savilleb564aae2014-10-23 10:18:09 -07001464 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001465 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 }
1467
1468 /**
1469 * Returns the CDMA ERI icon index to display
1470 */
1471 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001472 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001473
1474 }
1475
Wink Savilleb564aae2014-10-23 10:18:09 -07001476 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001477 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 }
1479
1480 /**
1481 * Returns the CDMA ERI icon mode,
1482 * 0 - ON
1483 * 1 - FLASHING
1484 */
1485 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001486 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001487 }
1488
Wink Savilleb564aae2014-10-23 10:18:09 -07001489 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001490 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001491 }
1492
1493 /**
1494 * Returns the CDMA ERI text,
1495 */
1496 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001497 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001498 }
1499
Wink Savilleb564aae2014-10-23 10:18:09 -07001500 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001501 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001502 }
1503
1504 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001505 * Returns the CDMA MDN.
1506 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001507 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001508 enforceModifyPermissionOrCarrierPrivilege();
1509 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1510 return getPhone(subId).getLine1Number();
1511 } else {
1512 return null;
1513 }
1514 }
1515
1516 /**
1517 * Returns the CDMA MIN.
1518 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001519 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001520 enforceModifyPermissionOrCarrierPrivilege();
1521 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1522 return getPhone(subId).getCdmaMin();
1523 } else {
1524 return null;
1525 }
1526 }
1527
1528 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 * Returns true if CDMA provisioning needs to run.
1530 */
1531 public boolean needsOtaServiceProvisioning() {
1532 return mPhone.needsOtaServiceProvisioning();
1533 }
1534
1535 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001536 * Sets the voice mail number of a given subId.
1537 */
1538 @Override
1539 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001540 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001541 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1542 new Pair<String, String>(alphaTag, number), new Integer(subId));
1543 return success;
1544 }
1545
1546 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 * Returns the unread count of voicemails
1548 */
1549 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001550 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001551 }
1552
1553 /**
1554 * Returns the unread count of voicemails for a subId
1555 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001556 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001557 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001558 }
1559
1560 /**
1561 * Returns the data network type
1562 *
1563 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1564 */
1565 @Override
1566 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001567 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001568 }
1569
1570 /**
1571 * Returns the network type for a subId
1572 */
1573 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001574 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001575 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001576 }
1577
1578 /**
1579 * Returns the data network type
1580 */
1581 @Override
1582 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001583 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001584 }
1585
1586 /**
1587 * Returns the data network type for a subId
1588 */
1589 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001590 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001591 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 }
1593
1594 /**
1595 * Returns the data network type
1596 */
1597 @Override
1598 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001599 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001600 }
1601
1602 /**
1603 * Returns the Voice network type for a subId
1604 */
1605 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001606 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001607 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 }
1609
1610 /**
1611 * @return true if a ICC card is present
1612 */
1613 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001614 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001615 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001616 }
1617
1618 /**
1619 * @return true if a ICC card is present for a slotId
1620 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001621 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001622 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1623 if (subId != null) {
1624 return getPhone(subId[0]).getIccCard().hasIccCard();
1625 } else {
1626 return false;
1627 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 }
1629
1630 /**
1631 * Return if the current radio is LTE on CDMA. This
1632 * is a tri-state return value as for a period of time
1633 * the mode may be unknown.
1634 *
1635 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001636 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001637 */
1638 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001639 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001640 }
1641
Wink Savilleb564aae2014-10-23 10:18:09 -07001642 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001643 return getPhone(subId).getLteOnCdmaMode();
1644 }
1645
1646 public void setPhone(Phone phone) {
1647 mPhone = phone;
1648 }
1649
1650 /**
1651 * {@hide}
1652 * Returns Default subId, 0 in the case of single standby.
1653 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001654 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001655 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001656 }
1657
Wink Savilleb564aae2014-10-23 10:18:09 -07001658 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001659 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001660 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001661
1662 /**
1663 * @see android.telephony.TelephonyManager.WifiCallingChoices
1664 */
1665 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001666 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1667 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001668 }
1669
1670 /**
1671 * @see android.telephony.TelephonyManager.WifiCallingChoices
1672 */
1673 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001674 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1675 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1676 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001677 }
1678
Sailesh Nepald1e68152013-12-12 19:08:02 -08001679 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001680 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001681 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001682 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001683
Shishir Agrawal566b7612013-10-28 14:41:00 -07001684 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001685 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001686 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001687
1688 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001689 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1690 CMD_OPEN_CHANNEL, AID);
1691 if (DBG) log("iccOpenLogicalChannel: " + response);
1692 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001693 }
1694
1695 @Override
1696 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001697 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001698
1699 if (DBG) log("iccCloseLogicalChannel: " + channel);
1700 if (channel < 0) {
1701 return false;
1702 }
Jake Hambye994d462014-02-03 13:10:13 -08001703 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001704 if (DBG) log("iccCloseLogicalChannel: " + success);
1705 return success;
1706 }
1707
1708 @Override
1709 public String iccTransmitApduLogicalChannel(int channel, int cla,
1710 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001711 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001712
1713 if (DBG) {
1714 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1715 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1716 " data=" + data);
1717 }
1718
1719 if (channel < 0) {
1720 return "";
1721 }
1722
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001723 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001724 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1725 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1726
Shishir Agrawal566b7612013-10-28 14:41:00 -07001727 // Append the returned status code to the end of the response payload.
1728 String s = Integer.toHexString(
1729 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001730 if (response.payload != null) {
1731 s = IccUtils.bytesToHexString(response.payload) + s;
1732 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001733 return s;
1734 }
Jake Hambye994d462014-02-03 13:10:13 -08001735
Evan Charltonc66da362014-05-16 14:06:40 -07001736 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001737 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1738 int p3, String data) {
1739 enforceModifyPermissionOrCarrierPrivilege();
1740
1741 if (DBG) {
1742 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1743 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1744 }
1745
1746 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1747 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1748 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1749
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001750 // Append the returned status code to the end of the response payload.
1751 String s = Integer.toHexString(
1752 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001753 if (response.payload != null) {
1754 s = IccUtils.bytesToHexString(response.payload) + s;
1755 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001756 return s;
1757 }
1758
1759 @Override
1760 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1761 String filePath) {
1762 enforceModifyPermissionOrCarrierPrivilege();
1763
1764 if (DBG) {
1765 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1766 p1 + " " + p2 + " " + p3 + ":" + filePath);
1767 }
1768
1769 IccIoResult response =
1770 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001771 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001772
1773 if (DBG) {
1774 log("Exchange SIM_IO [R]" + response);
1775 }
1776
1777 byte[] result = null;
1778 int length = 2;
1779 if (response.payload != null) {
1780 length = 2 + response.payload.length;
1781 result = new byte[length];
1782 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1783 } else {
1784 result = new byte[length];
1785 }
1786
1787 result[length - 1] = (byte) response.sw2;
1788 result[length - 2] = (byte) response.sw1;
1789 return result;
1790 }
1791
1792 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001793 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001794 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001795
1796 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1797 if (response.payload == null) {
1798 return "";
1799 }
1800
1801 // Append the returned status code to the end of the response payload.
1802 String s = Integer.toHexString(
1803 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1804 s = IccUtils.bytesToHexString(response.payload) + s;
1805 return s;
1806 }
1807
Jake Hambye994d462014-02-03 13:10:13 -08001808 /**
1809 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1810 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1811 *
1812 * @param itemID the ID of the item to read
1813 * @return the NV item as a String, or null on error.
1814 */
1815 @Override
1816 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001817 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001818 if (DBG) log("nvReadItem: item " + itemID);
1819 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1820 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1821 return value;
1822 }
1823
1824 /**
1825 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1826 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1827 *
1828 * @param itemID the ID of the item to read
1829 * @param itemValue the value to write, as a String
1830 * @return true on success; false on any failure
1831 */
1832 @Override
1833 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001834 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001835 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1836 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1837 new Pair<Integer, String>(itemID, itemValue));
1838 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1839 return success;
1840 }
1841
1842 /**
1843 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1844 * Used for device configuration by some CDMA operators.
1845 *
1846 * @param preferredRoamingList byte array containing the new PRL
1847 * @return true on success; false on any failure
1848 */
1849 @Override
1850 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001851 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001852 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1853 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1854 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1855 return success;
1856 }
1857
1858 /**
1859 * Perform the specified type of NV config reset.
1860 * Used for device configuration by some CDMA operators.
1861 *
1862 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1863 * @return true on success; false on any failure
1864 */
1865 @Override
1866 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001867 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001868 if (DBG) log("nvResetConfig: type " + resetType);
1869 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1870 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1871 return success;
1872 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001873
1874 /**
Wink Saville36469e72014-06-11 15:17:00 -07001875 * {@hide}
1876 * Returns Default sim, 0 in the case of single standby.
1877 */
1878 public int getDefaultSim() {
1879 //TODO Need to get it from Telephony Devcontroller
1880 return 0;
1881 }
1882
Svet Ganovb320e182015-04-16 12:30:10 -07001883 public String[] getPcscfAddress(String apnType, String callingPackage) {
1884 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1885 return new String[0];
1886 }
1887
1888
ram87fca6f2014-07-18 18:58:44 +05301889 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001890 }
1891
1892 public void setImsRegistrationState(boolean registered) {
1893 enforceModifyPermission();
1894 mPhone.setImsRegistrationState(registered);
1895 }
1896
1897 /**
Stuart Scott54788802015-03-30 13:18:01 -07001898 * Set the network selection mode to automatic.
1899 *
1900 */
1901 @Override
1902 public void setNetworkSelectionModeAutomatic(int subId) {
1903 enforceModifyPermissionOrCarrierPrivilege();
1904 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1905 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1906 }
1907
1908 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001909 * Get the calculated preferred network type.
1910 * Used for debugging incorrect network type.
1911 *
1912 * @return the preferred network type, defined in RILConstants.java.
1913 */
1914 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001915 public int getCalculatedPreferredNetworkType(String callingPackage) {
1916 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1917 return RILConstants.PREFERRED_NETWORK_MODE;
1918 }
1919
Amit Mahajan43330e02014-11-18 11:54:45 -08001920 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001921 }
1922
1923 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001924 * Get the preferred network type.
1925 * Used for device configuration by some CDMA operators.
1926 *
1927 * @return the preferred network type, defined in RILConstants.java.
1928 */
1929 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001930 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001931 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001932 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001933 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001934 int networkType = (result != null ? result[0] : -1);
1935 if (DBG) log("getPreferredNetworkType: " + networkType);
1936 return networkType;
1937 }
1938
1939 /**
1940 * Set the preferred network type.
1941 * Used for device configuration by some CDMA operators.
1942 *
1943 * @param networkType the preferred network type, defined in RILConstants.java.
1944 * @return true on success; false on any failure.
1945 */
1946 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001947 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001948 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001949 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1950 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001951 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001952 if (success) {
1953 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001954 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001955 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001956 return success;
1957 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001958
1959 /**
Junda Liu475951f2014-11-07 16:45:03 -08001960 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1961 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1962 * tethering.
1963 *
1964 * @return 0: Not required. 1: required. 2: Not set.
1965 * @hide
1966 */
1967 @Override
1968 public int getTetherApnRequired() {
1969 enforceModifyPermissionOrCarrierPrivilege();
1970 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1971 Settings.Global.TETHER_DUN_REQUIRED, 2);
1972 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1973 // config_tether_apndata.
1974 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1975 dunRequired = 1;
1976 }
1977 return dunRequired;
1978 }
1979
1980 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001981 * Set mobile data enabled
1982 * Used by the user through settings etc to turn on/off mobile data
1983 *
1984 * @param enable {@code true} turn turn data on, else {@code false}
1985 */
1986 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001987 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001988 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001989 int phoneId = mSubscriptionController.getPhoneId(subId);
1990 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1991 Phone phone = PhoneFactory.getPhone(phoneId);
1992 if (phone != null) {
1993 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1994 phone.setDataEnabled(enable);
1995 } else {
1996 loge("setDataEnabled: no phone for subId=" + subId);
1997 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001998 }
1999
2000 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002001 * Get whether mobile data is enabled.
2002 *
2003 * Note that this used to be available from ConnectivityService, gated by
2004 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2005 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002006 *
2007 * @return {@code true} if data is enabled else {@code false}
2008 */
2009 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002010 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002011 try {
2012 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2013 null);
2014 } catch (Exception e) {
2015 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2016 null);
2017 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002018 int phoneId = mSubscriptionController.getPhoneId(subId);
2019 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2020 Phone phone = PhoneFactory.getPhone(phoneId);
2021 if (phone != null) {
2022 boolean retVal = phone.getDataEnabled();
2023 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2024 return retVal;
2025 } else {
2026 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2027 return false;
2028 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002029 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002030
2031 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002032 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002033 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002034 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002035 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002036 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2037 }
2038 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002039 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002040 }
Junda Liu29340342014-07-10 15:23:27 -07002041
2042 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002043 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002044 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002045 if (card == null) {
2046 loge("checkCarrierPrivilegesForPackage: No UICC");
2047 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2048 }
2049 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002050 }
Derek Tan89e89d42014-07-08 17:00:10 -07002051
2052 @Override
Junda Liue64de782015-04-16 17:19:16 -07002053 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2054 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2055 loge("phoneId " + phoneId + " is not valid.");
2056 return null;
2057 }
2058 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002059 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002060 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002061 return null ;
2062 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002063 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002064 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002065 }
2066
Wink Savilleb564aae2014-10-23 10:18:09 -07002067 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002068 UiccCard card = getPhone(subId).getUiccCard();
2069 if (card == null) {
2070 loge("getIccId: No UICC");
2071 return null;
2072 }
2073 String iccId = card.getIccId();
2074 if (TextUtils.isEmpty(iccId)) {
2075 loge("getIccId: ICC ID is null or empty.");
2076 return null;
2077 }
2078 return iccId;
2079 }
2080
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002081 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002082 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2083 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002084 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002085
Jeff Sharkey85190e62014-12-05 09:40:12 -08002086 final String iccId = getIccId(subId);
2087 final String subscriberId = getPhone(subId).getSubscriberId();
2088
2089 if (DBG_MERGE) {
2090 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2091 + subscriberId + " to " + number);
2092 }
2093
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002094 if (TextUtils.isEmpty(iccId)) {
2095 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002096 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002097
Jeff Sharkey85190e62014-12-05 09:40:12 -08002098 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2099
2100 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002101 if (alphaTag == null) {
2102 editor.remove(alphaTagPrefKey);
2103 } else {
2104 editor.putString(alphaTagPrefKey, alphaTag);
2105 }
2106
Jeff Sharkey85190e62014-12-05 09:40:12 -08002107 // Record both the line number and IMSI for this ICCID, since we need to
2108 // track all merged IMSIs based on line number
2109 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2110 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002111 if (number == null) {
2112 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002113 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002114 } else {
2115 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002116 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002117 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002118
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002119 editor.commit();
2120 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002121 }
2122
2123 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002124 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2125 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2126 return null;
2127 }
Derek Tan97ebb422014-09-05 16:55:38 -07002128
2129 String iccId = getIccId(subId);
2130 if (iccId != null) {
2131 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002132 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002133 }
Derek Tan97ebb422014-09-05 16:55:38 -07002134 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002135 }
2136
2137 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002138 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2139 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2140 return null;
2141 }
Derek Tan97ebb422014-09-05 16:55:38 -07002142
2143 String iccId = getIccId(subId);
2144 if (iccId != null) {
2145 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002146 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002147 }
Derek Tan97ebb422014-09-05 16:55:38 -07002148 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002149 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002150
2151 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002152 public String[] getMergedSubscriberIds() {
2153 final Context context = mPhone.getContext();
2154 final TelephonyManager tele = TelephonyManager.from(context);
2155 final SubscriptionManager sub = SubscriptionManager.from(context);
2156
2157 // Figure out what subscribers are currently active
2158 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2159 final int[] subIds = sub.getActiveSubscriptionIdList();
2160 for (int subId : subIds) {
2161 activeSubscriberIds.add(tele.getSubscriberId(subId));
2162 }
2163
2164 // First pass, find a number override for an active subscriber
2165 String mergeNumber = null;
2166 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2167 for (String key : prefs.keySet()) {
2168 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2169 final String subscriberId = (String) prefs.get(key);
2170 if (activeSubscriberIds.contains(subscriberId)) {
2171 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2172 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2173 mergeNumber = (String) prefs.get(numberKey);
2174 if (DBG_MERGE) {
2175 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2176 + " for active subscriber " + subscriberId);
2177 }
2178 if (!TextUtils.isEmpty(mergeNumber)) {
2179 break;
2180 }
2181 }
2182 }
2183 }
2184
2185 // Shortcut when no active merged subscribers
2186 if (TextUtils.isEmpty(mergeNumber)) {
2187 return null;
2188 }
2189
2190 // Second pass, find all subscribers under that line override
2191 final ArraySet<String> result = new ArraySet<>();
2192 for (String key : prefs.keySet()) {
2193 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2194 final String number = (String) prefs.get(key);
2195 if (mergeNumber.equals(number)) {
2196 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2197 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2198 final String subscriberId = (String) prefs.get(subscriberKey);
2199 if (!TextUtils.isEmpty(subscriberId)) {
2200 result.add(subscriberId);
2201 }
2202 }
2203 }
2204 }
2205
2206 final String[] resultArray = result.toArray(new String[result.size()]);
2207 Arrays.sort(resultArray);
2208 if (DBG_MERGE) {
2209 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2210 }
2211 return resultArray;
2212 }
2213
2214 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002215 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002216 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002217 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002218 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002219
2220 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002221 public boolean setRoamingOverride(List<String> gsmRoamingList,
2222 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2223 List<String> cdmaNonRoamingList) {
2224 enforceCarrierPrivilege();
2225 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2226 cdmaNonRoamingList);
2227 }
2228
2229 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002230 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2231 enforceModifyPermission();
2232
2233 int returnValue = 0;
2234 try {
2235 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2236 if(result.exception == null) {
2237 if (result.result != null) {
2238 byte[] responseData = (byte[])(result.result);
2239 if(responseData.length > oemResp.length) {
2240 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2241 responseData.length + "bytes. Buffer Size is " +
2242 oemResp.length + "bytes.");
2243 }
2244 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2245 returnValue = responseData.length;
2246 }
2247 } else {
2248 CommandException ex = (CommandException) result.exception;
2249 returnValue = ex.getCommandError().ordinal();
2250 if(returnValue > 0) returnValue *= -1;
2251 }
2252 } catch (RuntimeException e) {
2253 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2254 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2255 if(returnValue > 0) returnValue *= -1;
2256 }
2257
2258 return returnValue;
2259 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002260
2261 @Override
2262 public void setRadioCapability(RadioAccessFamily[] rafs) {
2263 try {
2264 ProxyController.getInstance().setRadioCapability(rafs);
2265 } catch (RuntimeException e) {
2266 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2267 }
2268 }
2269
2270 @Override
2271 public int getRadioAccessFamily(int phoneId) {
2272 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2273 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002274
2275 @Override
2276 public void enableVideoCalling(boolean enable) {
2277 enforceModifyPermission();
2278 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2279 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2280 editor.commit();
2281 }
2282
2283 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002284 public boolean isVideoCallingEnabled(String callingPackage) {
2285 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2286 return false;
2287 }
2288
Andrew Lee77527ac2014-10-21 16:57:39 -07002289 // Check the user preference and the system-level IMS setting. Even if the user has
2290 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2291 // In the long run, we may instead need to check if there exists a connection service
2292 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002293 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2294 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2295 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002296 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002297
Andrew Leea1239f22015-03-02 17:44:07 -08002298 @Override
2299 public boolean canChangeDtmfToneLength() {
Junda Liu12f7d802015-05-01 12:06:44 -07002300 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_DTMF_TYPE_ENABLED);
Andrew Leea1239f22015-03-02 17:44:07 -08002301 }
2302
2303 @Override
2304 public boolean isWorldPhone() {
Junda Liu12f7d802015-05-01 12:06:44 -07002305 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_WORLD_PHONE);
Andrew Leea1239f22015-03-02 17:44:07 -08002306 }
2307
Andrew Lee9431b832015-03-09 18:46:45 -07002308 @Override
2309 public boolean isTtyModeSupported() {
2310 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2311 TelephonyManager telephonyManager =
2312 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2313 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2314 }
2315
2316 @Override
2317 public boolean isHearingAidCompatibilitySupported() {
2318 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2319 }
2320
Sanket Padawe7310cc72015-01-14 09:53:20 -08002321 /**
2322 * Returns the unique device ID of phone, for example, the IMEI for
2323 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2324 *
2325 * <p>Requires Permission:
2326 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2327 */
2328 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002329 public String getDeviceId(String callingPackage) {
2330 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2331 return null;
2332 }
2333
Sanket Padawe7310cc72015-01-14 09:53:20 -08002334 final Phone phone = PhoneFactory.getPhone(0);
2335 if (phone != null) {
2336 return phone.getDeviceId();
2337 } else {
2338 return null;
2339 }
2340 }
2341
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002342 /*
2343 * {@hide}
2344 * Returns the IMS Registration Status
2345 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002346 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002347 public boolean isImsRegistered() {
2348 return mPhone.isImsRegistered();
2349 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002350
2351 @Override
2352 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2353 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2354 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002355
Nathan Haroldc55097a2015-03-11 18:14:50 -07002356 /*
2357 * {@hide}
2358 * Returns the IMS Registration Status
2359 */
2360 public boolean isWifiCallingEnabled() {
2361 return mPhone.isWifiCallingEnabled();
2362 }
2363
2364 /*
2365 * {@hide}
2366 * Returns the IMS Registration Status
2367 */
2368 public boolean isVolteEnabled() {
2369 return mPhone.isVolteEnabled();
2370 }
Svet Ganovb320e182015-04-16 12:30:10 -07002371
2372 private boolean canReadPhoneState(String callingPackage, String message) {
2373 mApp.enforceCallingOrSelfPermission(
2374 android.Manifest.permission.READ_PHONE_STATE, message);
2375
2376 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2377 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2378 return false;
2379 }
2380
2381 return true;
2382 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002383
2384 @Override
2385 public void factoryReset(int subId) {
2386 enforceConnectivityInternalPermission();
Svet Ganovcc087f82015-05-12 20:35:54 -07002387 final long identity = Binder.clearCallingIdentity();
2388 try {
2389 if (SubscriptionManager.isUsableSubIdValue(subId)) {
2390 // Enable data
2391 setDataEnabled(subId, true);
2392 // Set network selection mode to automatic
2393 setNetworkSelectionModeAutomatic(subId);
2394 // Set preferred mobile network type to the best available
2395 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2396 // Turn off roaming
2397 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2398 }
2399 } finally {
2400 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002401 }
2402 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002403}