blob: 7d1602fb3fd6d27b0a23c9dbe589082cb68fd53a [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;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700264 Phone phone = getPhone(end_subId);
265 if (phone == null) {
266 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
267 break;
268 }
269 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
271 // CDMA: If the user presses the Power button we treat it as
272 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700273 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
275 // GSM: End the call as per the Phone state
276 hungUp = PhoneUtils.hangup(mCM);
277 } else {
278 throw new IllegalStateException("Unexpected phone type: " + phoneType);
279 }
280 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
281 request.result = hungUp;
282 // Wake up the requesting thread
283 synchronized (request) {
284 request.notifyAll();
285 }
286 break;
287
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700288 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700289 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700290 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700291 if (uiccCard == null) {
292 loge("iccTransmitApduLogicalChannel: No UICC");
293 request.result = new IccIoResult(0x6F, 0, (byte[])null);
294 synchronized (request) {
295 request.notifyAll();
296 }
297 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700298 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
299 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700300 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700301 iccArgument.channel, iccArgument.cla, iccArgument.command,
302 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700303 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700304 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700305 break;
306
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700307 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700308 ar = (AsyncResult) msg.obj;
309 request = (MainThreadRequest) ar.userObj;
310 if (ar.exception == null && ar.result != null) {
311 request.result = ar.result;
312 } else {
313 request.result = new IccIoResult(0x6F, 0, (byte[])null);
314 if (ar.result == null) {
315 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800316 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700317 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800318 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700319 } else {
320 loge("iccTransmitApduLogicalChannel: Unknown exception");
321 }
322 }
323 synchronized (request) {
324 request.notifyAll();
325 }
326 break;
327
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700328 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
329 request = (MainThreadRequest) msg.obj;
330 iccArgument = (IccAPDUArgument) request.argument;
331 if (uiccCard == null) {
332 loge("iccTransmitApduBasicChannel: No UICC");
333 request.result = new IccIoResult(0x6F, 0, (byte[])null);
334 synchronized (request) {
335 request.notifyAll();
336 }
337 } else {
338 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
339 request);
340 uiccCard.iccTransmitApduBasicChannel(
341 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
342 iccArgument.p3, iccArgument.data, onCompleted);
343 }
344 break;
345
346 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
347 ar = (AsyncResult) msg.obj;
348 request = (MainThreadRequest) ar.userObj;
349 if (ar.exception == null && ar.result != null) {
350 request.result = ar.result;
351 } else {
352 request.result = new IccIoResult(0x6F, 0, (byte[])null);
353 if (ar.result == null) {
354 loge("iccTransmitApduBasicChannel: Empty response");
355 } else if (ar.exception instanceof CommandException) {
356 loge("iccTransmitApduBasicChannel: CommandException: " +
357 ar.exception);
358 } else {
359 loge("iccTransmitApduBasicChannel: Unknown exception");
360 }
361 }
362 synchronized (request) {
363 request.notifyAll();
364 }
365 break;
366
367 case CMD_EXCHANGE_SIM_IO:
368 request = (MainThreadRequest) msg.obj;
369 iccArgument = (IccAPDUArgument) request.argument;
370 if (uiccCard == null) {
371 loge("iccExchangeSimIO: No UICC");
372 request.result = new IccIoResult(0x6F, 0, (byte[])null);
373 synchronized (request) {
374 request.notifyAll();
375 }
376 } else {
377 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
378 request);
379 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
380 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
381 iccArgument.data, onCompleted);
382 }
383 break;
384
385 case EVENT_EXCHANGE_SIM_IO_DONE:
386 ar = (AsyncResult) msg.obj;
387 request = (MainThreadRequest) ar.userObj;
388 if (ar.exception == null && ar.result != null) {
389 request.result = ar.result;
390 } else {
391 request.result = new IccIoResult(0x6f, 0, (byte[])null);
392 }
393 synchronized (request) {
394 request.notifyAll();
395 }
396 break;
397
Derek Tan4d5e5c12014-02-04 11:54:58 -0800398 case CMD_SEND_ENVELOPE:
399 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700400 if (uiccCard == null) {
401 loge("sendEnvelopeWithStatus: No UICC");
402 request.result = new IccIoResult(0x6F, 0, (byte[])null);
403 synchronized (request) {
404 request.notifyAll();
405 }
406 } else {
407 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
408 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
409 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800410 break;
411
412 case EVENT_SEND_ENVELOPE_DONE:
413 ar = (AsyncResult) msg.obj;
414 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700415 if (ar.exception == null && ar.result != null) {
416 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800417 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700418 request.result = new IccIoResult(0x6F, 0, (byte[])null);
419 if (ar.result == null) {
420 loge("sendEnvelopeWithStatus: Empty response");
421 } else if (ar.exception instanceof CommandException) {
422 loge("sendEnvelopeWithStatus: CommandException: " +
423 ar.exception);
424 } else {
425 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
426 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800427 }
428 synchronized (request) {
429 request.notifyAll();
430 }
431 break;
432
Shishir Agrawal566b7612013-10-28 14:41:00 -0700433 case CMD_OPEN_CHANNEL:
434 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700435 if (uiccCard == null) {
436 loge("iccOpenLogicalChannel: No UICC");
437 request.result = new IccIoResult(0x6F, 0, (byte[])null);
438 synchronized (request) {
439 request.notifyAll();
440 }
441 } else {
442 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
443 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
444 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700445 break;
446
447 case EVENT_OPEN_CHANNEL_DONE:
448 ar = (AsyncResult) msg.obj;
449 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700450 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700451 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700452 int[] result = (int[]) ar.result;
453 int channelId = result[0];
454 byte[] selectResponse = null;
455 if (result.length > 1) {
456 selectResponse = new byte[result.length - 1];
457 for (int i = 1; i < result.length; ++i) {
458 selectResponse[i - 1] = (byte) result[i];
459 }
460 }
461 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700462 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700463 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700464 if (ar.result == null) {
465 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700466 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700467 if (ar.exception != null) {
468 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
469 }
470
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700471 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700472 if (ar.exception instanceof CommandException) {
473 CommandException.Error error =
474 ((CommandException) (ar.exception)).getCommandError();
475 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700476 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700477 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700478 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700479 }
480 }
481 openChannelResp = new IccOpenLogicalChannelResponse(
482 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700483 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700484 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 synchronized (request) {
486 request.notifyAll();
487 }
488 break;
489
490 case CMD_CLOSE_CHANNEL:
491 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700492 if (uiccCard == null) {
493 loge("iccCloseLogicalChannel: No UICC");
494 request.result = new IccIoResult(0x6F, 0, (byte[])null);
495 synchronized (request) {
496 request.notifyAll();
497 }
498 } else {
499 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
500 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
501 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700502 break;
503
504 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800505 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
506 break;
507
508 case CMD_NV_READ_ITEM:
509 request = (MainThreadRequest) msg.obj;
510 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
511 mPhone.nvReadItem((Integer) request.argument, onCompleted);
512 break;
513
514 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700515 ar = (AsyncResult) msg.obj;
516 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800517 if (ar.exception == null && ar.result != null) {
518 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800520 request.result = "";
521 if (ar.result == null) {
522 loge("nvReadItem: Empty response");
523 } else if (ar.exception instanceof CommandException) {
524 loge("nvReadItem: CommandException: " +
525 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800527 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 }
529 }
530 synchronized (request) {
531 request.notifyAll();
532 }
533 break;
534
Jake Hambye994d462014-02-03 13:10:13 -0800535 case CMD_NV_WRITE_ITEM:
536 request = (MainThreadRequest) msg.obj;
537 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
538 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
539 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
540 break;
541
542 case EVENT_NV_WRITE_ITEM_DONE:
543 handleNullReturnEvent(msg, "nvWriteItem");
544 break;
545
546 case CMD_NV_WRITE_CDMA_PRL:
547 request = (MainThreadRequest) msg.obj;
548 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
549 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
550 break;
551
552 case EVENT_NV_WRITE_CDMA_PRL_DONE:
553 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
554 break;
555
556 case CMD_NV_RESET_CONFIG:
557 request = (MainThreadRequest) msg.obj;
558 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
559 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
560 break;
561
562 case EVENT_NV_RESET_CONFIG_DONE:
563 handleNullReturnEvent(msg, "nvResetConfig");
564 break;
565
Jake Hamby7c27be32014-03-03 13:25:59 -0800566 case CMD_GET_PREFERRED_NETWORK_TYPE:
567 request = (MainThreadRequest) msg.obj;
568 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700569 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800570 break;
571
572 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
573 ar = (AsyncResult) msg.obj;
574 request = (MainThreadRequest) ar.userObj;
575 if (ar.exception == null && ar.result != null) {
576 request.result = ar.result; // Integer
577 } else {
578 request.result = -1;
579 if (ar.result == null) {
580 loge("getPreferredNetworkType: Empty response");
581 } else if (ar.exception instanceof CommandException) {
582 loge("getPreferredNetworkType: CommandException: " +
583 ar.exception);
584 } else {
585 loge("getPreferredNetworkType: Unknown exception");
586 }
587 }
588 synchronized (request) {
589 request.notifyAll();
590 }
591 break;
592
593 case CMD_SET_PREFERRED_NETWORK_TYPE:
594 request = (MainThreadRequest) msg.obj;
595 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
596 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700597 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800598 break;
599
600 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
601 handleNullReturnEvent(msg, "setPreferredNetworkType");
602 break;
603
Steven Liu4bf01bc2014-07-17 11:05:29 -0500604 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
605 request = (MainThreadRequest)msg.obj;
606 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
607 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
608 break;
609
610 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
611 ar = (AsyncResult)msg.obj;
612 request = (MainThreadRequest)ar.userObj;
613 request.result = ar;
614 synchronized (request) {
615 request.notifyAll();
616 }
617 break;
618
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800619 case CMD_SET_VOICEMAIL_NUMBER:
620 request = (MainThreadRequest) msg.obj;
621 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
622 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800623 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
624 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800625 break;
626
627 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
628 handleNullReturnEvent(msg, "setVoicemailNumber");
629 break;
630
Stuart Scott54788802015-03-30 13:18:01 -0700631 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
632 request = (MainThreadRequest) msg.obj;
633 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
634 request);
635 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
636 break;
637
638 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
639 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
640 break;
641
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700642 default:
643 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
644 break;
645 }
646 }
Jake Hambye994d462014-02-03 13:10:13 -0800647
648 private void handleNullReturnEvent(Message msg, String command) {
649 AsyncResult ar = (AsyncResult) msg.obj;
650 MainThreadRequest request = (MainThreadRequest) ar.userObj;
651 if (ar.exception == null) {
652 request.result = true;
653 } else {
654 request.result = false;
655 if (ar.exception instanceof CommandException) {
656 loge(command + ": CommandException: " + ar.exception);
657 } else {
658 loge(command + ": Unknown exception");
659 }
660 }
661 synchronized (request) {
662 request.notifyAll();
663 }
664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665 }
666
667 /**
668 * Posts the specified command to be executed on the main thread,
669 * waits for the request to complete, and returns the result.
670 * @see #sendRequestAsync
671 */
672 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700673 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700674 }
675
676 /**
677 * Posts the specified command to be executed on the main thread,
678 * waits for the request to complete, and returns the result.
679 * @see #sendRequestAsync
680 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800681 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700682 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
683 throw new RuntimeException("This method will deadlock if called from the main thread.");
684 }
685
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800686 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700687 Message msg = mMainThreadHandler.obtainMessage(command, request);
688 msg.sendToTarget();
689
690 // Wait for the request to complete
691 synchronized (request) {
692 while (request.result == null) {
693 try {
694 request.wait();
695 } catch (InterruptedException e) {
696 // Do nothing, go back and wait until the request is complete
697 }
698 }
699 }
700 return request.result;
701 }
702
703 /**
704 * Asynchronous ("fire and forget") version of sendRequest():
705 * Posts the specified command to be executed on the main thread, and
706 * returns immediately.
707 * @see #sendRequest
708 */
709 private void sendRequestAsync(int command) {
710 mMainThreadHandler.sendEmptyMessage(command);
711 }
712
713 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700714 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
715 * @see {@link #sendRequest(int,Object)}
716 */
717 private void sendRequestAsync(int command, Object argument) {
718 MainThreadRequest request = new MainThreadRequest(argument);
719 Message msg = mMainThreadHandler.obtainMessage(command, request);
720 msg.sendToTarget();
721 }
722
723 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 * Initialize the singleton PhoneInterfaceManager instance.
725 * This is only done once, at startup, from PhoneApp.onCreate().
726 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700727 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 synchronized (PhoneInterfaceManager.class) {
729 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700730 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 } else {
732 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
733 }
734 return sInstance;
735 }
736 }
737
738 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700739 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 mApp = app;
741 mPhone = phone;
742 mCM = PhoneGlobals.getInstance().mCM;
743 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
744 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700745 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700746 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800747 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800748
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700749 publish();
750 }
751
752 private void publish() {
753 if (DBG) log("publish: " + this);
754
755 ServiceManager.addService("phone", this);
756 }
757
Stuart Scott584921c2015-01-15 17:10:34 -0800758 private Phone getPhoneFromRequest(MainThreadRequest request) {
759 return (request.subId == null) ? mPhone : getPhone(request.subId);
760 }
761
Wink Saville36469e72014-06-11 15:17:00 -0700762 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700763 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800764 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700765 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 //
767 // Implementation of the ITelephony interface.
768 //
769
770 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700771 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700772 }
773
Wink Savilleb564aae2014-10-23 10:18:09 -0700774 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775 if (DBG) log("dial: " + number);
776 // No permission check needed here: This is just a wrapper around the
777 // ACTION_DIAL intent, which is available to any app since it puts up
778 // the UI before it does anything.
779
780 String url = createTelUrl(number);
781 if (url == null) {
782 return;
783 }
784
785 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700786 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
788 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
789 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
790 mApp.startActivity(intent);
791 }
792 }
793
794 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700795 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700796 }
797
Wink Savilleb564aae2014-10-23 10:18:09 -0700798 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 if (DBG) log("call: " + number);
800
801 // This is just a wrapper around the ACTION_CALL intent, but we still
802 // need to do a permission check since we're calling startActivity()
803 // from the context of the phone app.
804 enforceCallPermission();
805
806 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
807 != AppOpsManager.MODE_ALLOWED) {
808 return;
809 }
810
811 String url = createTelUrl(number);
812 if (url == null) {
813 return;
814 }
815
Wink Saville08874612014-08-31 19:19:58 -0700816 boolean isValid = false;
Svetoslav483aff72015-04-21 14:16:07 -0700817 List<SubscriptionInfo> slist;
818
819 final long identity = Binder.clearCallingIdentity();
820 try {
821 slist = mSubscriptionController.getActiveSubscriptionInfoList(
822 mPhone.getContext().getOpPackageName());
823 } finally {
824 Binder.restoreCallingIdentity(identity);
825 }
826
Wink Saville3ab207e2014-11-20 13:07:20 -0800827 if (slist != null) {
828 for (SubscriptionInfo subInfoRecord : slist) {
829 if (subInfoRecord.getSubscriptionId() == subId) {
830 isValid = true;
831 break;
832 }
Wink Saville08874612014-08-31 19:19:58 -0700833 }
834 }
835 if (isValid == false) {
836 return;
837 }
838
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700840 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
842 mApp.startActivity(intent);
843 }
844
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 /**
846 * End a call based on call state
847 * @return true is a call was ended
848 */
849 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700850 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700851 }
852
853 /**
854 * End a call based on the call state of the subId
855 * @return true is a call was ended
856 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700857 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800859 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700860 }
861
862 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700863 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700864 }
865
Wink Savilleb564aae2014-10-23 10:18:09 -0700866 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700867 if (DBG) log("answerRingingCall...");
868 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
869 // but that can probably wait till the big TelephonyManager API overhaul.
870 // For now, protect this call with the MODIFY_PHONE_STATE permission.
871 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800872 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 }
874
875 /**
876 * Make the actual telephony calls to implement answerRingingCall().
877 * This should only be called from the main thread of the Phone app.
878 * @see #answerRingingCall
879 *
880 * TODO: it would be nice to return true if we answered the call, or
881 * false if there wasn't actually a ringing incoming call, or some
882 * other error occurred. (In other words, pass back the return value
883 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
884 * But that would require calling this method via sendRequest() rather
885 * than sendRequestAsync(), and right now we don't actually *need* that
886 * return value, so let's just return void for now.
887 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700888 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700889 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700890 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700891 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
892 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 if (hasActiveCall && hasHoldingCall) {
894 // Both lines are in use!
895 // TODO: provide a flag to let the caller specify what
896 // policy to use if both lines are in use. (The current
897 // behavior is hardwired to "answer incoming, end ongoing",
898 // which is how the CALL button is specced to behave.)
899 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
900 return;
901 } else {
902 // answerCall() will automatically hold the current active
903 // call, if there is one.
904 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
905 return;
906 }
907 } else {
908 // No call was ringing.
909 return;
910 }
911 }
912
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700914 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700916 public void silenceRinger() {
917 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 }
919
920 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700921 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700922 }
923
Wink Savilleb564aae2014-10-23 10:18:09 -0700924 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700925 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 }
927
928 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700929 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700930 }
931
Wink Savilleb564aae2014-10-23 10:18:09 -0700932 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700933 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 }
935
936 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700937 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700938 }
939
Wink Savilleb564aae2014-10-23 10:18:09 -0700940 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700941 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 }
943
Svet Ganovb320e182015-04-16 12:30:10 -0700944 public boolean isSimPinEnabled(String callingPackage) {
945 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
946 return false;
947 }
948
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 return (PhoneGlobals.getInstance().isSimPinEnabled());
950 }
951
952 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700953 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700954 }
955
Wink Savilleb564aae2014-10-23 10:18:09 -0700956 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700957 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700958 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
959 }
960
961 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700962 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700963 }
964
Wink Savilleb564aae2014-10-23 10:18:09 -0700965 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700966 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700967 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
968 }
969
970 /** {@hide} */
971 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700972 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700973 }
974
Wink Savilleb564aae2014-10-23 10:18:09 -0700975 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700977 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 checkSimPin.start();
979 return checkSimPin.unlockSim(null, pin);
980 }
981
Wink Saville9de0f752013-10-22 19:04:03 -0700982 /** {@hide} */
983 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700984 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700985 }
986
Wink Savilleb564aae2014-10-23 10:18:09 -0700987 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700989 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 checkSimPuk.start();
991 return checkSimPuk.unlockSim(puk, pin);
992 }
993
994 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700995 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 * a synchronous one.
997 */
998 private static class UnlockSim extends Thread {
999
1000 private final IccCard mSimCard;
1001
1002 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001003 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1004 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001005
1006 // For replies from SimCard interface
1007 private Handler mHandler;
1008
1009 // For async handler to identify request type
1010 private static final int SUPPLY_PIN_COMPLETE = 100;
1011
1012 public UnlockSim(IccCard simCard) {
1013 mSimCard = simCard;
1014 }
1015
1016 @Override
1017 public void run() {
1018 Looper.prepare();
1019 synchronized (UnlockSim.this) {
1020 mHandler = new Handler() {
1021 @Override
1022 public void handleMessage(Message msg) {
1023 AsyncResult ar = (AsyncResult) msg.obj;
1024 switch (msg.what) {
1025 case SUPPLY_PIN_COMPLETE:
1026 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1027 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001028 mRetryCount = msg.arg1;
1029 if (ar.exception != null) {
1030 if (ar.exception instanceof CommandException &&
1031 ((CommandException)(ar.exception)).getCommandError()
1032 == CommandException.Error.PASSWORD_INCORRECT) {
1033 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1034 } else {
1035 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1036 }
1037 } else {
1038 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1039 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001040 mDone = true;
1041 UnlockSim.this.notifyAll();
1042 }
1043 break;
1044 }
1045 }
1046 };
1047 UnlockSim.this.notifyAll();
1048 }
1049 Looper.loop();
1050 }
1051
1052 /*
1053 * Use PIN or PUK to unlock SIM card
1054 *
1055 * If PUK is null, unlock SIM card with PIN
1056 *
1057 * If PUK is not null, unlock SIM card with PUK and set PIN code
1058 */
Wink Saville9de0f752013-10-22 19:04:03 -07001059 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060
1061 while (mHandler == null) {
1062 try {
1063 wait();
1064 } catch (InterruptedException e) {
1065 Thread.currentThread().interrupt();
1066 }
1067 }
1068 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1069
1070 if (puk == null) {
1071 mSimCard.supplyPin(pin, callback);
1072 } else {
1073 mSimCard.supplyPuk(puk, pin, callback);
1074 }
1075
1076 while (!mDone) {
1077 try {
1078 Log.d(LOG_TAG, "wait for done");
1079 wait();
1080 } catch (InterruptedException e) {
1081 // Restore the interrupted status
1082 Thread.currentThread().interrupt();
1083 }
1084 }
1085 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001086 int[] resultArray = new int[2];
1087 resultArray[0] = mResult;
1088 resultArray[1] = mRetryCount;
1089 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001090 }
1091 }
1092
1093 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001094 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001095
1096 }
1097
Wink Savilleb564aae2014-10-23 10:18:09 -07001098 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 // No permission check needed here: this call is harmless, and it's
1100 // needed for the ServiceState.requestStateUpdate() call (which is
1101 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001102 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001103 }
1104
1105 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001106 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001107 }
1108
Wink Savilleb564aae2014-10-23 10:18:09 -07001109 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001110 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 }
1112
1113 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001114 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001115
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 }
Wink Saville36469e72014-06-11 15:17:00 -07001117
Wink Savilleb564aae2014-10-23 10:18:09 -07001118 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001119 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001120 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001121 }
1122
1123 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001124 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001125 }
1126
Wink Savilleb564aae2014-10-23 10:18:09 -07001127 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001128 enforceModifyPermission();
1129 if ((getPhone(subId).getServiceState().getState() !=
1130 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001131 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001132 }
1133 return true;
1134 }
Wink Saville36469e72014-06-11 15:17:00 -07001135
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001136 public boolean needMobileRadioShutdown() {
1137 /*
1138 * If any of the Radios are available, it will need to be
1139 * shutdown. So return true if any Radio is available.
1140 */
1141 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1142 Phone phone = PhoneFactory.getPhone(i);
1143 if (phone != null && phone.isRadioAvailable()) return true;
1144 }
1145 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1146 return false;
1147 }
1148
1149 public void shutdownMobileRadios() {
1150 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1151 logv("Shutting down Phone " + i);
1152 shutdownRadioUsingPhoneId(i);
1153 }
1154 }
1155
1156 private void shutdownRadioUsingPhoneId(int phoneId) {
1157 enforceModifyPermission();
1158 Phone phone = PhoneFactory.getPhone(phoneId);
1159 if (phone != null && phone.isRadioAvailable()) {
1160 phone.shutdownRadio();
1161 }
1162 }
1163
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001165 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001166 }
1167
Wink Savilleb564aae2014-10-23 10:18:09 -07001168 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001170 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 return true;
1172 }
1173
Wink Saville36469e72014-06-11 15:17:00 -07001174 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001175 public boolean enableDataConnectivity() {
1176 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001177 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001178 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001179 return true;
1180 }
1181
Wink Saville36469e72014-06-11 15:17:00 -07001182 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183 public boolean disableDataConnectivity() {
1184 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001185 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001186 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001187 return true;
1188 }
1189
Wink Saville36469e72014-06-11 15:17:00 -07001190 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001192 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001193 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 }
1195
1196 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001197 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001198 }
1199
Wink Savilleb564aae2014-10-23 10:18:09 -07001200 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001202 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 }
1204
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001206 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001207 }
1208
Wink Savilleb564aae2014-10-23 10:18:09 -07001209 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001210 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 }
1212
1213 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001214 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001215 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 }
1217
1218 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001219 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001220 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 }
1222
1223 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001224 public Bundle getCellLocation(String callingPackage) {
1225 enforceFineOrCoarseLocationPermission("getCellLocation");
1226
1227 // OP_COARSE_LOCATION controls both fine and coarse location.
1228 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1229 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1230 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 }
1232
Jake Hambye994d462014-02-03 13:10:13 -08001233 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001234 if (DBG_LOC) log("getCellLocation: is active user");
1235 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001236 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1237 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238 return data;
1239 } else {
1240 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1241 return null;
1242 }
1243 }
1244
Svetoslav64fad262015-04-14 14:35:21 -07001245 private void enforceFineOrCoarseLocationPermission(String message) {
1246 try {
1247 mApp.enforceCallingOrSelfPermission(
1248 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1249 } catch (SecurityException e) {
1250 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1251 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1252 // is the weaker precondition
1253 mApp.enforceCallingOrSelfPermission(
1254 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1255 }
1256 }
1257
1258
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259 @Override
1260 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001261 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001262 }
1263
Wink Savilleb564aae2014-10-23 10:18:09 -07001264 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 mApp.enforceCallingOrSelfPermission(
1266 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001267 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 }
1269
1270 @Override
1271 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001272 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001273 }
1274
Wink Savilleb564aae2014-10-23 10:18:09 -07001275 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 mApp.enforceCallingOrSelfPermission(
1277 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001278 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 }
1280
1281 @Override
1282 @SuppressWarnings("unchecked")
1283 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001284 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1285
1286 // OP_COARSE_LOCATION controls both fine and coarse location.
1287 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1288 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1289 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 }
1291
1292 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1293 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1294 return null;
1295 }
Svetoslav64fad262015-04-14 14:35:21 -07001296
Jake Hambye994d462014-02-03 13:10:13 -08001297 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1299
1300 ArrayList<NeighboringCellInfo> cells = null;
1301
1302 try {
1303 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001304 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001306 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307 }
1308 return cells;
1309 } else {
1310 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1311 return null;
1312 }
1313 }
1314
1315
1316 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001317 public List<CellInfo> getAllCellInfo(String callingPackage) {
1318 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1319
1320 // OP_COARSE_LOCATION controls both fine and coarse location.
1321 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1322 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1323 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 }
1325
Jake Hambye994d462014-02-03 13:10:13 -08001326 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001327 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001328 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1329 for (Phone phone : PhoneFactory.getPhones()) {
1330 cellInfos.addAll(phone.getAllCellInfo());
1331 }
1332 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 } else {
1334 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1335 return null;
1336 }
1337 }
1338
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001339 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340 public void setCellInfoListRate(int rateInMillis) {
1341 mPhone.setCellInfoListRate(rateInMillis);
1342 }
1343
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 //
1345 // Internal helper methods.
1346 //
1347
Jake Hambye994d462014-02-03 13:10:13 -08001348 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 boolean ok;
1350
1351 boolean self = Binder.getCallingUid() == Process.myUid();
1352 if (!self) {
1353 // Get the caller's user id then clear the calling identity
1354 // which will be restored in the finally clause.
1355 int callingUser = UserHandle.getCallingUserId();
1356 long ident = Binder.clearCallingIdentity();
1357
1358 try {
1359 // With calling identity cleared the current user is the foreground user.
1360 int foregroundUser = ActivityManager.getCurrentUser();
1361 ok = (foregroundUser == callingUser);
1362 if (DBG_LOC) {
1363 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1364 + " callingUser=" + callingUser + " ok=" + ok);
1365 }
1366 } catch (Exception ex) {
1367 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1368 ok = false;
1369 } finally {
1370 Binder.restoreCallingIdentity(ident);
1371 }
1372 } else {
1373 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1374 ok = true;
1375 }
1376 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1377 return ok;
1378 }
1379
1380 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1382 *
1383 * @throws SecurityException if the caller does not have the required permission
1384 */
1385 private void enforceModifyPermission() {
1386 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1387 }
1388
1389 /**
Junda Liua2e36012014-07-09 18:30:01 -07001390 * Make sure either system app or the caller has carrier privilege.
1391 *
1392 * @throws SecurityException if the caller does not have the required permission/privilege
1393 */
1394 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001395 int permission = mApp.checkCallingOrSelfPermission(
1396 android.Manifest.permission.MODIFY_PHONE_STATE);
1397 if (permission == PackageManager.PERMISSION_GRANTED) {
1398 return;
1399 }
1400
1401 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001402 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001403 loge("No Carrier Privilege.");
1404 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001405 }
1406 }
1407
1408 /**
1409 * Make sure the caller has carrier privilege.
1410 *
1411 * @throws SecurityException if the caller does not have the required permission
1412 */
1413 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001414 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001415 loge("No Carrier Privilege.");
1416 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001417 }
1418 }
1419
1420 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001421 * Make sure the caller has the CALL_PHONE permission.
1422 *
1423 * @throws SecurityException if the caller does not have the required permission
1424 */
1425 private void enforceCallPermission() {
1426 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1427 }
1428
Shishir Agrawal566b7612013-10-28 14:41:00 -07001429 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001430 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1431 *
1432 * @throws SecurityException if the caller does not have the required permission
1433 */
1434 private void enforcePrivilegedPhoneStatePermission() {
1435 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1436 null);
1437 }
1438
Stuart Scott8eef64f2015-04-08 15:13:54 -07001439 private void enforceConnectivityInternalPermission() {
1440 mApp.enforceCallingOrSelfPermission(
1441 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1442 "ConnectivityService");
1443 }
1444
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 private String createTelUrl(String number) {
1446 if (TextUtils.isEmpty(number)) {
1447 return null;
1448 }
1449
Jake Hambye994d462014-02-03 13:10:13 -08001450 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001451 }
1452
Ihab Awadf9e92732013-12-05 18:02:52 -08001453 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1455 }
1456
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001457 private static void logv(String msg) {
1458 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1459 }
1460
Ihab Awadf9e92732013-12-05 18:02:52 -08001461 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1463 }
1464
1465 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001466 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001467 }
1468
Wink Savilleb564aae2014-10-23 10:18:09 -07001469 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001470 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 }
1472
1473 /**
1474 * Returns the CDMA ERI icon index to display
1475 */
1476 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001477 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001478
1479 }
1480
Wink Savilleb564aae2014-10-23 10:18:09 -07001481 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001482 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 }
1484
1485 /**
1486 * Returns the CDMA ERI icon mode,
1487 * 0 - ON
1488 * 1 - FLASHING
1489 */
1490 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001491 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001492 }
1493
Wink Savilleb564aae2014-10-23 10:18:09 -07001494 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001495 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001496 }
1497
1498 /**
1499 * Returns the CDMA ERI text,
1500 */
1501 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001502 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001503 }
1504
Wink Savilleb564aae2014-10-23 10:18:09 -07001505 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001506 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 }
1508
1509 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001510 * Returns the CDMA MDN.
1511 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001512 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001513 enforceModifyPermissionOrCarrierPrivilege();
1514 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1515 return getPhone(subId).getLine1Number();
1516 } else {
1517 return null;
1518 }
1519 }
1520
1521 /**
1522 * Returns the CDMA MIN.
1523 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001524 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001525 enforceModifyPermissionOrCarrierPrivilege();
1526 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1527 return getPhone(subId).getCdmaMin();
1528 } else {
1529 return null;
1530 }
1531 }
1532
1533 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 * Returns true if CDMA provisioning needs to run.
1535 */
1536 public boolean needsOtaServiceProvisioning() {
1537 return mPhone.needsOtaServiceProvisioning();
1538 }
1539
1540 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001541 * Sets the voice mail number of a given subId.
1542 */
1543 @Override
1544 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001545 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001546 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1547 new Pair<String, String>(alphaTag, number), new Integer(subId));
1548 return success;
1549 }
1550
1551 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001552 * Returns the unread count of voicemails
1553 */
1554 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001555 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001556 }
1557
1558 /**
1559 * Returns the unread count of voicemails for a subId
1560 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001561 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001562 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563 }
1564
1565 /**
1566 * Returns the data network type
1567 *
1568 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1569 */
1570 @Override
1571 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001572 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001573 }
1574
1575 /**
1576 * Returns the network type for a subId
1577 */
1578 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001579 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001580 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 }
1582
1583 /**
1584 * Returns the data network type
1585 */
1586 @Override
1587 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001588 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001589 }
1590
1591 /**
1592 * Returns the data network type for a subId
1593 */
1594 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001595 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001596 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 }
1598
1599 /**
1600 * Returns the data network type
1601 */
1602 @Override
1603 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001604 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001605 }
1606
1607 /**
1608 * Returns the Voice network type for a subId
1609 */
1610 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001611 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001612 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001613 }
1614
1615 /**
1616 * @return true if a ICC card is present
1617 */
1618 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001619 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001620 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001621 }
1622
1623 /**
1624 * @return true if a ICC card is present for a slotId
1625 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001626 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001627 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1628 if (subId != null) {
1629 return getPhone(subId[0]).getIccCard().hasIccCard();
1630 } else {
1631 return false;
1632 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 }
1634
1635 /**
1636 * Return if the current radio is LTE on CDMA. This
1637 * is a tri-state return value as for a period of time
1638 * the mode may be unknown.
1639 *
1640 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001641 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642 */
1643 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001644 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001645 }
1646
Wink Savilleb564aae2014-10-23 10:18:09 -07001647 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001648 return getPhone(subId).getLteOnCdmaMode();
1649 }
1650
1651 public void setPhone(Phone phone) {
1652 mPhone = phone;
1653 }
1654
1655 /**
1656 * {@hide}
1657 * Returns Default subId, 0 in the case of single standby.
1658 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001659 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001660 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001661 }
1662
Wink Savilleb564aae2014-10-23 10:18:09 -07001663 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001664 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001665 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001666
1667 /**
1668 * @see android.telephony.TelephonyManager.WifiCallingChoices
1669 */
1670 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001671 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1672 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001673 }
1674
1675 /**
1676 * @see android.telephony.TelephonyManager.WifiCallingChoices
1677 */
1678 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001679 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1680 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1681 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001682 }
1683
Sailesh Nepald1e68152013-12-12 19:08:02 -08001684 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001685 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001686 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001687 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001688
Shishir Agrawal566b7612013-10-28 14:41:00 -07001689 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001690 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001691 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001692
1693 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001694 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1695 CMD_OPEN_CHANNEL, AID);
1696 if (DBG) log("iccOpenLogicalChannel: " + response);
1697 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001698 }
1699
1700 @Override
1701 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001702 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001703
1704 if (DBG) log("iccCloseLogicalChannel: " + channel);
1705 if (channel < 0) {
1706 return false;
1707 }
Jake Hambye994d462014-02-03 13:10:13 -08001708 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001709 if (DBG) log("iccCloseLogicalChannel: " + success);
1710 return success;
1711 }
1712
1713 @Override
1714 public String iccTransmitApduLogicalChannel(int channel, int cla,
1715 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001716 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001717
1718 if (DBG) {
1719 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1720 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1721 " data=" + data);
1722 }
1723
1724 if (channel < 0) {
1725 return "";
1726 }
1727
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001728 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001729 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1730 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1731
Shishir Agrawal566b7612013-10-28 14:41:00 -07001732 // Append the returned status code to the end of the response payload.
1733 String s = Integer.toHexString(
1734 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001735 if (response.payload != null) {
1736 s = IccUtils.bytesToHexString(response.payload) + s;
1737 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001738 return s;
1739 }
Jake Hambye994d462014-02-03 13:10:13 -08001740
Evan Charltonc66da362014-05-16 14:06:40 -07001741 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001742 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1743 int p3, String data) {
1744 enforceModifyPermissionOrCarrierPrivilege();
1745
1746 if (DBG) {
1747 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1748 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1749 }
1750
1751 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1752 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1753 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1754
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001755 // Append the returned status code to the end of the response payload.
1756 String s = Integer.toHexString(
1757 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001758 if (response.payload != null) {
1759 s = IccUtils.bytesToHexString(response.payload) + s;
1760 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001761 return s;
1762 }
1763
1764 @Override
1765 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1766 String filePath) {
1767 enforceModifyPermissionOrCarrierPrivilege();
1768
1769 if (DBG) {
1770 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1771 p1 + " " + p2 + " " + p3 + ":" + filePath);
1772 }
1773
1774 IccIoResult response =
1775 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001776 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001777
1778 if (DBG) {
1779 log("Exchange SIM_IO [R]" + response);
1780 }
1781
1782 byte[] result = null;
1783 int length = 2;
1784 if (response.payload != null) {
1785 length = 2 + response.payload.length;
1786 result = new byte[length];
1787 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1788 } else {
1789 result = new byte[length];
1790 }
1791
1792 result[length - 1] = (byte) response.sw2;
1793 result[length - 2] = (byte) response.sw1;
1794 return result;
1795 }
1796
1797 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001798 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001799 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001800
1801 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1802 if (response.payload == null) {
1803 return "";
1804 }
1805
1806 // Append the returned status code to the end of the response payload.
1807 String s = Integer.toHexString(
1808 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1809 s = IccUtils.bytesToHexString(response.payload) + s;
1810 return s;
1811 }
1812
Jake Hambye994d462014-02-03 13:10:13 -08001813 /**
1814 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1815 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1816 *
1817 * @param itemID the ID of the item to read
1818 * @return the NV item as a String, or null on error.
1819 */
1820 @Override
1821 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001822 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001823 if (DBG) log("nvReadItem: item " + itemID);
1824 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1825 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1826 return value;
1827 }
1828
1829 /**
1830 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1831 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1832 *
1833 * @param itemID the ID of the item to read
1834 * @param itemValue the value to write, as a String
1835 * @return true on success; false on any failure
1836 */
1837 @Override
1838 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001839 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001840 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1841 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1842 new Pair<Integer, String>(itemID, itemValue));
1843 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1844 return success;
1845 }
1846
1847 /**
1848 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1849 * Used for device configuration by some CDMA operators.
1850 *
1851 * @param preferredRoamingList byte array containing the new PRL
1852 * @return true on success; false on any failure
1853 */
1854 @Override
1855 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001856 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001857 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1858 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1859 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1860 return success;
1861 }
1862
1863 /**
1864 * Perform the specified type of NV config reset.
1865 * Used for device configuration by some CDMA operators.
1866 *
1867 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1868 * @return true on success; false on any failure
1869 */
1870 @Override
1871 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001872 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001873 if (DBG) log("nvResetConfig: type " + resetType);
1874 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1875 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1876 return success;
1877 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001878
1879 /**
Wink Saville36469e72014-06-11 15:17:00 -07001880 * {@hide}
1881 * Returns Default sim, 0 in the case of single standby.
1882 */
1883 public int getDefaultSim() {
1884 //TODO Need to get it from Telephony Devcontroller
1885 return 0;
1886 }
1887
Svet Ganovb320e182015-04-16 12:30:10 -07001888 public String[] getPcscfAddress(String apnType, String callingPackage) {
1889 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1890 return new String[0];
1891 }
1892
1893
ram87fca6f2014-07-18 18:58:44 +05301894 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001895 }
1896
1897 public void setImsRegistrationState(boolean registered) {
1898 enforceModifyPermission();
1899 mPhone.setImsRegistrationState(registered);
1900 }
1901
1902 /**
Stuart Scott54788802015-03-30 13:18:01 -07001903 * Set the network selection mode to automatic.
1904 *
1905 */
1906 @Override
1907 public void setNetworkSelectionModeAutomatic(int subId) {
1908 enforceModifyPermissionOrCarrierPrivilege();
1909 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1910 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1911 }
1912
1913 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001914 * Get the calculated preferred network type.
1915 * Used for debugging incorrect network type.
1916 *
1917 * @return the preferred network type, defined in RILConstants.java.
1918 */
1919 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001920 public int getCalculatedPreferredNetworkType(String callingPackage) {
1921 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1922 return RILConstants.PREFERRED_NETWORK_MODE;
1923 }
1924
Amit Mahajan43330e02014-11-18 11:54:45 -08001925 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001926 }
1927
1928 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001929 * Get the preferred network type.
1930 * Used for device configuration by some CDMA operators.
1931 *
1932 * @return the preferred network type, defined in RILConstants.java.
1933 */
1934 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001935 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001936 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001937 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001938 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001939 int networkType = (result != null ? result[0] : -1);
1940 if (DBG) log("getPreferredNetworkType: " + networkType);
1941 return networkType;
1942 }
1943
1944 /**
1945 * Set the preferred network type.
1946 * Used for device configuration by some CDMA operators.
1947 *
1948 * @param networkType the preferred network type, defined in RILConstants.java.
1949 * @return true on success; false on any failure.
1950 */
1951 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001952 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001953 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001954 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1955 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001956 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001957 if (success) {
1958 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001959 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001960 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001961 return success;
1962 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001963
1964 /**
Junda Liu475951f2014-11-07 16:45:03 -08001965 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1966 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1967 * tethering.
1968 *
1969 * @return 0: Not required. 1: required. 2: Not set.
1970 * @hide
1971 */
1972 @Override
1973 public int getTetherApnRequired() {
1974 enforceModifyPermissionOrCarrierPrivilege();
1975 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1976 Settings.Global.TETHER_DUN_REQUIRED, 2);
1977 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1978 // config_tether_apndata.
1979 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1980 dunRequired = 1;
1981 }
1982 return dunRequired;
1983 }
1984
1985 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001986 * Set mobile data enabled
1987 * Used by the user through settings etc to turn on/off mobile data
1988 *
1989 * @param enable {@code true} turn turn data on, else {@code false}
1990 */
1991 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001992 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001993 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001994 int phoneId = mSubscriptionController.getPhoneId(subId);
1995 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1996 Phone phone = PhoneFactory.getPhone(phoneId);
1997 if (phone != null) {
1998 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1999 phone.setDataEnabled(enable);
2000 } else {
2001 loge("setDataEnabled: no phone for subId=" + subId);
2002 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002003 }
2004
2005 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002006 * Get whether mobile data is enabled.
2007 *
2008 * Note that this used to be available from ConnectivityService, gated by
2009 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2010 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002011 *
2012 * @return {@code true} if data is enabled else {@code false}
2013 */
2014 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002015 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002016 try {
2017 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2018 null);
2019 } catch (Exception e) {
2020 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2021 null);
2022 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002023 int phoneId = mSubscriptionController.getPhoneId(subId);
2024 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2025 Phone phone = PhoneFactory.getPhone(phoneId);
2026 if (phone != null) {
2027 boolean retVal = phone.getDataEnabled();
2028 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2029 return retVal;
2030 } else {
2031 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2032 return false;
2033 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002034 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002035
2036 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002037 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002038 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002039 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002040 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002041 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2042 }
2043 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002044 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002045 }
Junda Liu29340342014-07-10 15:23:27 -07002046
2047 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002048 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002049 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002050 if (card == null) {
2051 loge("checkCarrierPrivilegesForPackage: No UICC");
2052 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2053 }
2054 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002055 }
Derek Tan89e89d42014-07-08 17:00:10 -07002056
2057 @Override
Junda Liue64de782015-04-16 17:19:16 -07002058 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2059 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2060 loge("phoneId " + phoneId + " is not valid.");
2061 return null;
2062 }
2063 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002064 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002065 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002066 return null ;
2067 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002068 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002069 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002070 }
2071
Wink Savilleb564aae2014-10-23 10:18:09 -07002072 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002073 UiccCard card = getPhone(subId).getUiccCard();
2074 if (card == null) {
2075 loge("getIccId: No UICC");
2076 return null;
2077 }
2078 String iccId = card.getIccId();
2079 if (TextUtils.isEmpty(iccId)) {
2080 loge("getIccId: ICC ID is null or empty.");
2081 return null;
2082 }
2083 return iccId;
2084 }
2085
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002086 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002087 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2088 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002089 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002090
Jeff Sharkey85190e62014-12-05 09:40:12 -08002091 final String iccId = getIccId(subId);
2092 final String subscriberId = getPhone(subId).getSubscriberId();
2093
2094 if (DBG_MERGE) {
2095 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2096 + subscriberId + " to " + number);
2097 }
2098
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002099 if (TextUtils.isEmpty(iccId)) {
2100 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002101 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002102
Jeff Sharkey85190e62014-12-05 09:40:12 -08002103 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2104
2105 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002106 if (alphaTag == null) {
2107 editor.remove(alphaTagPrefKey);
2108 } else {
2109 editor.putString(alphaTagPrefKey, alphaTag);
2110 }
2111
Jeff Sharkey85190e62014-12-05 09:40:12 -08002112 // Record both the line number and IMSI for this ICCID, since we need to
2113 // track all merged IMSIs based on line number
2114 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2115 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002116 if (number == null) {
2117 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002118 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002119 } else {
2120 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002121 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002122 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002123
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002124 editor.commit();
2125 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002126 }
2127
2128 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002129 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2130 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2131 return null;
2132 }
Derek Tan97ebb422014-09-05 16:55:38 -07002133
2134 String iccId = getIccId(subId);
2135 if (iccId != null) {
2136 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002137 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002138 }
Derek Tan97ebb422014-09-05 16:55:38 -07002139 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002140 }
2141
2142 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002143 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2144 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2145 return null;
2146 }
Derek Tan97ebb422014-09-05 16:55:38 -07002147
2148 String iccId = getIccId(subId);
2149 if (iccId != null) {
2150 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002151 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002152 }
Derek Tan97ebb422014-09-05 16:55:38 -07002153 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002154 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002155
2156 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002157 public String[] getMergedSubscriberIds() {
2158 final Context context = mPhone.getContext();
2159 final TelephonyManager tele = TelephonyManager.from(context);
2160 final SubscriptionManager sub = SubscriptionManager.from(context);
2161
2162 // Figure out what subscribers are currently active
2163 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2164 final int[] subIds = sub.getActiveSubscriptionIdList();
2165 for (int subId : subIds) {
2166 activeSubscriberIds.add(tele.getSubscriberId(subId));
2167 }
2168
2169 // First pass, find a number override for an active subscriber
2170 String mergeNumber = null;
2171 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2172 for (String key : prefs.keySet()) {
2173 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2174 final String subscriberId = (String) prefs.get(key);
2175 if (activeSubscriberIds.contains(subscriberId)) {
2176 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2177 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2178 mergeNumber = (String) prefs.get(numberKey);
2179 if (DBG_MERGE) {
2180 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2181 + " for active subscriber " + subscriberId);
2182 }
2183 if (!TextUtils.isEmpty(mergeNumber)) {
2184 break;
2185 }
2186 }
2187 }
2188 }
2189
2190 // Shortcut when no active merged subscribers
2191 if (TextUtils.isEmpty(mergeNumber)) {
2192 return null;
2193 }
2194
2195 // Second pass, find all subscribers under that line override
2196 final ArraySet<String> result = new ArraySet<>();
2197 for (String key : prefs.keySet()) {
2198 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2199 final String number = (String) prefs.get(key);
2200 if (mergeNumber.equals(number)) {
2201 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2202 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2203 final String subscriberId = (String) prefs.get(subscriberKey);
2204 if (!TextUtils.isEmpty(subscriberId)) {
2205 result.add(subscriberId);
2206 }
2207 }
2208 }
2209 }
2210
2211 final String[] resultArray = result.toArray(new String[result.size()]);
2212 Arrays.sort(resultArray);
2213 if (DBG_MERGE) {
2214 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2215 }
2216 return resultArray;
2217 }
2218
2219 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002220 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002221 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002222 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002223 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002224
2225 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002226 public boolean setRoamingOverride(List<String> gsmRoamingList,
2227 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2228 List<String> cdmaNonRoamingList) {
2229 enforceCarrierPrivilege();
2230 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2231 cdmaNonRoamingList);
2232 }
2233
2234 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002235 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2236 enforceModifyPermission();
2237
2238 int returnValue = 0;
2239 try {
2240 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2241 if(result.exception == null) {
2242 if (result.result != null) {
2243 byte[] responseData = (byte[])(result.result);
2244 if(responseData.length > oemResp.length) {
2245 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2246 responseData.length + "bytes. Buffer Size is " +
2247 oemResp.length + "bytes.");
2248 }
2249 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2250 returnValue = responseData.length;
2251 }
2252 } else {
2253 CommandException ex = (CommandException) result.exception;
2254 returnValue = ex.getCommandError().ordinal();
2255 if(returnValue > 0) returnValue *= -1;
2256 }
2257 } catch (RuntimeException e) {
2258 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2259 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2260 if(returnValue > 0) returnValue *= -1;
2261 }
2262
2263 return returnValue;
2264 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002265
2266 @Override
2267 public void setRadioCapability(RadioAccessFamily[] rafs) {
2268 try {
2269 ProxyController.getInstance().setRadioCapability(rafs);
2270 } catch (RuntimeException e) {
2271 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2272 }
2273 }
2274
2275 @Override
2276 public int getRadioAccessFamily(int phoneId) {
2277 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2278 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002279
2280 @Override
2281 public void enableVideoCalling(boolean enable) {
2282 enforceModifyPermission();
2283 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2284 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2285 editor.commit();
2286 }
2287
2288 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002289 public boolean isVideoCallingEnabled(String callingPackage) {
2290 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2291 return false;
2292 }
2293
Andrew Lee77527ac2014-10-21 16:57:39 -07002294 // Check the user preference and the system-level IMS setting. Even if the user has
2295 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2296 // In the long run, we may instead need to check if there exists a connection service
2297 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002298 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2299 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2300 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002301 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002302
Andrew Leea1239f22015-03-02 17:44:07 -08002303 @Override
2304 public boolean canChangeDtmfToneLength() {
Junda Liu12f7d802015-05-01 12:06:44 -07002305 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_DTMF_TYPE_ENABLED);
Andrew Leea1239f22015-03-02 17:44:07 -08002306 }
2307
2308 @Override
2309 public boolean isWorldPhone() {
Junda Liu12f7d802015-05-01 12:06:44 -07002310 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_WORLD_PHONE);
Andrew Leea1239f22015-03-02 17:44:07 -08002311 }
2312
Andrew Lee9431b832015-03-09 18:46:45 -07002313 @Override
2314 public boolean isTtyModeSupported() {
2315 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2316 TelephonyManager telephonyManager =
2317 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2318 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2319 }
2320
2321 @Override
2322 public boolean isHearingAidCompatibilitySupported() {
2323 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2324 }
2325
Sanket Padawe7310cc72015-01-14 09:53:20 -08002326 /**
2327 * Returns the unique device ID of phone, for example, the IMEI for
2328 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2329 *
2330 * <p>Requires Permission:
2331 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2332 */
2333 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002334 public String getDeviceId(String callingPackage) {
2335 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2336 return null;
2337 }
2338
Sanket Padawe7310cc72015-01-14 09:53:20 -08002339 final Phone phone = PhoneFactory.getPhone(0);
2340 if (phone != null) {
2341 return phone.getDeviceId();
2342 } else {
2343 return null;
2344 }
2345 }
2346
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002347 /*
2348 * {@hide}
2349 * Returns the IMS Registration Status
2350 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002351 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002352 public boolean isImsRegistered() {
2353 return mPhone.isImsRegistered();
2354 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002355
2356 @Override
2357 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2358 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2359 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002360
Nathan Haroldc55097a2015-03-11 18:14:50 -07002361 /*
2362 * {@hide}
2363 * Returns the IMS Registration Status
2364 */
2365 public boolean isWifiCallingEnabled() {
2366 return mPhone.isWifiCallingEnabled();
2367 }
2368
2369 /*
2370 * {@hide}
2371 * Returns the IMS Registration Status
2372 */
2373 public boolean isVolteEnabled() {
2374 return mPhone.isVolteEnabled();
2375 }
Svet Ganovb320e182015-04-16 12:30:10 -07002376
2377 private boolean canReadPhoneState(String callingPackage, String message) {
2378 mApp.enforceCallingOrSelfPermission(
2379 android.Manifest.permission.READ_PHONE_STATE, message);
2380
2381 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2382 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2383 return false;
2384 }
2385
2386 return true;
2387 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002388
2389 @Override
2390 public void factoryReset(int subId) {
2391 enforceConnectivityInternalPermission();
Svet Ganovcc087f82015-05-12 20:35:54 -07002392 final long identity = Binder.clearCallingIdentity();
2393 try {
2394 if (SubscriptionManager.isUsableSubIdValue(subId)) {
2395 // Enable data
2396 setDataEnabled(subId, true);
2397 // Set network selection mode to automatic
2398 setNetworkSelectionModeAutomatic(subId);
2399 // Set preferred mobile network type to the best available
2400 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2401 // Turn off roaming
2402 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2403 }
2404 } finally {
2405 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002406 }
2407 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002408}