blob: 8c2fe2fea1d153005389407e67c3bec1fa6c55d9 [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;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700472 if ((ar.exception != null) && (ar.exception instanceof CommandException)) {
473 if (ar.exception.getMessage().compareTo("MISSING_RESOURCE") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700474 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700475 } else if (ar.exception.getMessage().compareTo("NO_SUCH_ELEMENT") == 0) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700476 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700477 }
478 }
479 openChannelResp = new IccOpenLogicalChannelResponse(
480 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700481 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700482 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700483 synchronized (request) {
484 request.notifyAll();
485 }
486 break;
487
488 case CMD_CLOSE_CHANNEL:
489 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700490 if (uiccCard == null) {
491 loge("iccCloseLogicalChannel: No UICC");
492 request.result = new IccIoResult(0x6F, 0, (byte[])null);
493 synchronized (request) {
494 request.notifyAll();
495 }
496 } else {
497 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
498 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
499 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700500 break;
501
502 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800503 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
504 break;
505
506 case CMD_NV_READ_ITEM:
507 request = (MainThreadRequest) msg.obj;
508 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
509 mPhone.nvReadItem((Integer) request.argument, onCompleted);
510 break;
511
512 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700513 ar = (AsyncResult) msg.obj;
514 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800515 if (ar.exception == null && ar.result != null) {
516 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800518 request.result = "";
519 if (ar.result == null) {
520 loge("nvReadItem: Empty response");
521 } else if (ar.exception instanceof CommandException) {
522 loge("nvReadItem: CommandException: " +
523 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700524 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800525 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 }
527 }
528 synchronized (request) {
529 request.notifyAll();
530 }
531 break;
532
Jake Hambye994d462014-02-03 13:10:13 -0800533 case CMD_NV_WRITE_ITEM:
534 request = (MainThreadRequest) msg.obj;
535 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
536 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
537 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
538 break;
539
540 case EVENT_NV_WRITE_ITEM_DONE:
541 handleNullReturnEvent(msg, "nvWriteItem");
542 break;
543
544 case CMD_NV_WRITE_CDMA_PRL:
545 request = (MainThreadRequest) msg.obj;
546 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
547 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
548 break;
549
550 case EVENT_NV_WRITE_CDMA_PRL_DONE:
551 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
552 break;
553
554 case CMD_NV_RESET_CONFIG:
555 request = (MainThreadRequest) msg.obj;
556 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
557 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
558 break;
559
560 case EVENT_NV_RESET_CONFIG_DONE:
561 handleNullReturnEvent(msg, "nvResetConfig");
562 break;
563
Jake Hamby7c27be32014-03-03 13:25:59 -0800564 case CMD_GET_PREFERRED_NETWORK_TYPE:
565 request = (MainThreadRequest) msg.obj;
566 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700567 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800568 break;
569
570 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
571 ar = (AsyncResult) msg.obj;
572 request = (MainThreadRequest) ar.userObj;
573 if (ar.exception == null && ar.result != null) {
574 request.result = ar.result; // Integer
575 } else {
576 request.result = -1;
577 if (ar.result == null) {
578 loge("getPreferredNetworkType: Empty response");
579 } else if (ar.exception instanceof CommandException) {
580 loge("getPreferredNetworkType: CommandException: " +
581 ar.exception);
582 } else {
583 loge("getPreferredNetworkType: Unknown exception");
584 }
585 }
586 synchronized (request) {
587 request.notifyAll();
588 }
589 break;
590
591 case CMD_SET_PREFERRED_NETWORK_TYPE:
592 request = (MainThreadRequest) msg.obj;
593 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
594 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700595 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800596 break;
597
598 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
599 handleNullReturnEvent(msg, "setPreferredNetworkType");
600 break;
601
Steven Liu4bf01bc2014-07-17 11:05:29 -0500602 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
603 request = (MainThreadRequest)msg.obj;
604 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
605 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
606 break;
607
608 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
609 ar = (AsyncResult)msg.obj;
610 request = (MainThreadRequest)ar.userObj;
611 request.result = ar;
612 synchronized (request) {
613 request.notifyAll();
614 }
615 break;
616
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800617 case CMD_SET_VOICEMAIL_NUMBER:
618 request = (MainThreadRequest) msg.obj;
619 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
620 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800621 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
622 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800623 break;
624
625 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
626 handleNullReturnEvent(msg, "setVoicemailNumber");
627 break;
628
Stuart Scott54788802015-03-30 13:18:01 -0700629 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
630 request = (MainThreadRequest) msg.obj;
631 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
632 request);
633 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
634 break;
635
636 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
637 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
638 break;
639
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 default:
641 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
642 break;
643 }
644 }
Jake Hambye994d462014-02-03 13:10:13 -0800645
646 private void handleNullReturnEvent(Message msg, String command) {
647 AsyncResult ar = (AsyncResult) msg.obj;
648 MainThreadRequest request = (MainThreadRequest) ar.userObj;
649 if (ar.exception == null) {
650 request.result = true;
651 } else {
652 request.result = false;
653 if (ar.exception instanceof CommandException) {
654 loge(command + ": CommandException: " + ar.exception);
655 } else {
656 loge(command + ": Unknown exception");
657 }
658 }
659 synchronized (request) {
660 request.notifyAll();
661 }
662 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 }
664
665 /**
666 * Posts the specified command to be executed on the main thread,
667 * waits for the request to complete, and returns the result.
668 * @see #sendRequestAsync
669 */
670 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700671 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700672 }
673
674 /**
675 * Posts the specified command to be executed on the main thread,
676 * waits for the request to complete, and returns the result.
677 * @see #sendRequestAsync
678 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800679 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
681 throw new RuntimeException("This method will deadlock if called from the main thread.");
682 }
683
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800684 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 Message msg = mMainThreadHandler.obtainMessage(command, request);
686 msg.sendToTarget();
687
688 // Wait for the request to complete
689 synchronized (request) {
690 while (request.result == null) {
691 try {
692 request.wait();
693 } catch (InterruptedException e) {
694 // Do nothing, go back and wait until the request is complete
695 }
696 }
697 }
698 return request.result;
699 }
700
701 /**
702 * Asynchronous ("fire and forget") version of sendRequest():
703 * Posts the specified command to be executed on the main thread, and
704 * returns immediately.
705 * @see #sendRequest
706 */
707 private void sendRequestAsync(int command) {
708 mMainThreadHandler.sendEmptyMessage(command);
709 }
710
711 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700712 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
713 * @see {@link #sendRequest(int,Object)}
714 */
715 private void sendRequestAsync(int command, Object argument) {
716 MainThreadRequest request = new MainThreadRequest(argument);
717 Message msg = mMainThreadHandler.obtainMessage(command, request);
718 msg.sendToTarget();
719 }
720
721 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700722 * Initialize the singleton PhoneInterfaceManager instance.
723 * This is only done once, at startup, from PhoneApp.onCreate().
724 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700725 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 synchronized (PhoneInterfaceManager.class) {
727 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700728 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 } else {
730 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
731 }
732 return sInstance;
733 }
734 }
735
736 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700737 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700738 mApp = app;
739 mPhone = phone;
740 mCM = PhoneGlobals.getInstance().mCM;
741 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
742 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700743 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700744 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800745 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800746
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 publish();
748 }
749
750 private void publish() {
751 if (DBG) log("publish: " + this);
752
753 ServiceManager.addService("phone", this);
754 }
755
Stuart Scott584921c2015-01-15 17:10:34 -0800756 private Phone getPhoneFromRequest(MainThreadRequest request) {
757 return (request.subId == null) ? mPhone : getPhone(request.subId);
758 }
759
Wink Saville36469e72014-06-11 15:17:00 -0700760 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700761 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800762 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700763 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700764 //
765 // Implementation of the ITelephony interface.
766 //
767
768 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700769 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700770 }
771
Wink Savilleb564aae2014-10-23 10:18:09 -0700772 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 if (DBG) log("dial: " + number);
774 // No permission check needed here: This is just a wrapper around the
775 // ACTION_DIAL intent, which is available to any app since it puts up
776 // the UI before it does anything.
777
778 String url = createTelUrl(number);
779 if (url == null) {
780 return;
781 }
782
783 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700784 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
786 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
787 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
788 mApp.startActivity(intent);
789 }
790 }
791
792 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700793 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700794 }
795
Wink Savilleb564aae2014-10-23 10:18:09 -0700796 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700797 if (DBG) log("call: " + number);
798
799 // This is just a wrapper around the ACTION_CALL intent, but we still
800 // need to do a permission check since we're calling startActivity()
801 // from the context of the phone app.
802 enforceCallPermission();
803
804 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
805 != AppOpsManager.MODE_ALLOWED) {
806 return;
807 }
808
809 String url = createTelUrl(number);
810 if (url == null) {
811 return;
812 }
813
Wink Saville08874612014-08-31 19:19:58 -0700814 boolean isValid = false;
Svetoslav483aff72015-04-21 14:16:07 -0700815 List<SubscriptionInfo> slist;
816
817 final long identity = Binder.clearCallingIdentity();
818 try {
819 slist = mSubscriptionController.getActiveSubscriptionInfoList(
820 mPhone.getContext().getOpPackageName());
821 } finally {
822 Binder.restoreCallingIdentity(identity);
823 }
824
Wink Saville3ab207e2014-11-20 13:07:20 -0800825 if (slist != null) {
826 for (SubscriptionInfo subInfoRecord : slist) {
827 if (subInfoRecord.getSubscriptionId() == subId) {
828 isValid = true;
829 break;
830 }
Wink Saville08874612014-08-31 19:19:58 -0700831 }
832 }
833 if (isValid == false) {
834 return;
835 }
836
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700838 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
840 mApp.startActivity(intent);
841 }
842
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 /**
844 * End a call based on call state
845 * @return true is a call was ended
846 */
847 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700848 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700849 }
850
851 /**
852 * End a call based on the call state of the subId
853 * @return true is a call was ended
854 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700855 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800857 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 }
859
860 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700861 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700862 }
863
Wink Savilleb564aae2014-10-23 10:18:09 -0700864 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 if (DBG) log("answerRingingCall...");
866 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
867 // but that can probably wait till the big TelephonyManager API overhaul.
868 // For now, protect this call with the MODIFY_PHONE_STATE permission.
869 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800870 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700871 }
872
873 /**
874 * Make the actual telephony calls to implement answerRingingCall().
875 * This should only be called from the main thread of the Phone app.
876 * @see #answerRingingCall
877 *
878 * TODO: it would be nice to return true if we answered the call, or
879 * false if there wasn't actually a ringing incoming call, or some
880 * other error occurred. (In other words, pass back the return value
881 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
882 * But that would require calling this method via sendRequest() rather
883 * than sendRequestAsync(), and right now we don't actually *need* that
884 * return value, so let's just return void for now.
885 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700886 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700887 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700888 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700889 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
890 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700891 if (hasActiveCall && hasHoldingCall) {
892 // Both lines are in use!
893 // TODO: provide a flag to let the caller specify what
894 // policy to use if both lines are in use. (The current
895 // behavior is hardwired to "answer incoming, end ongoing",
896 // which is how the CALL button is specced to behave.)
897 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
898 return;
899 } else {
900 // answerCall() will automatically hold the current active
901 // call, if there is one.
902 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
903 return;
904 }
905 } else {
906 // No call was ringing.
907 return;
908 }
909 }
910
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700912 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700914 public void silenceRinger() {
915 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 }
917
918 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700919 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700920 }
921
Wink Savilleb564aae2014-10-23 10:18:09 -0700922 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700923 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 }
925
926 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700927 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700928 }
929
Wink Savilleb564aae2014-10-23 10:18:09 -0700930 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700931 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 }
933
934 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700935 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700936 }
937
Wink Savilleb564aae2014-10-23 10:18:09 -0700938 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700939 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 }
941
Svet Ganovb320e182015-04-16 12:30:10 -0700942 public boolean isSimPinEnabled(String callingPackage) {
943 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
944 return false;
945 }
946
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 return (PhoneGlobals.getInstance().isSimPinEnabled());
948 }
949
950 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700951 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700952 }
953
Wink Savilleb564aae2014-10-23 10:18:09 -0700954 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700955 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700956 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
957 }
958
959 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700960 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700961 }
962
Wink Savilleb564aae2014-10-23 10:18:09 -0700963 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700964 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700965 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
966 }
967
968 /** {@hide} */
969 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700970 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700971 }
972
Wink Savilleb564aae2014-10-23 10:18:09 -0700973 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700975 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 checkSimPin.start();
977 return checkSimPin.unlockSim(null, pin);
978 }
979
Wink Saville9de0f752013-10-22 19:04:03 -0700980 /** {@hide} */
981 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700982 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700983 }
984
Wink Savilleb564aae2014-10-23 10:18:09 -0700985 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -0700987 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 checkSimPuk.start();
989 return checkSimPuk.unlockSim(puk, pin);
990 }
991
992 /**
Wink Saville9de0f752013-10-22 19:04:03 -0700993 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700994 * a synchronous one.
995 */
996 private static class UnlockSim extends Thread {
997
998 private final IccCard mSimCard;
999
1000 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001001 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1002 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003
1004 // For replies from SimCard interface
1005 private Handler mHandler;
1006
1007 // For async handler to identify request type
1008 private static final int SUPPLY_PIN_COMPLETE = 100;
1009
1010 public UnlockSim(IccCard simCard) {
1011 mSimCard = simCard;
1012 }
1013
1014 @Override
1015 public void run() {
1016 Looper.prepare();
1017 synchronized (UnlockSim.this) {
1018 mHandler = new Handler() {
1019 @Override
1020 public void handleMessage(Message msg) {
1021 AsyncResult ar = (AsyncResult) msg.obj;
1022 switch (msg.what) {
1023 case SUPPLY_PIN_COMPLETE:
1024 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1025 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001026 mRetryCount = msg.arg1;
1027 if (ar.exception != null) {
1028 if (ar.exception instanceof CommandException &&
1029 ((CommandException)(ar.exception)).getCommandError()
1030 == CommandException.Error.PASSWORD_INCORRECT) {
1031 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1032 } else {
1033 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1034 }
1035 } else {
1036 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1037 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 mDone = true;
1039 UnlockSim.this.notifyAll();
1040 }
1041 break;
1042 }
1043 }
1044 };
1045 UnlockSim.this.notifyAll();
1046 }
1047 Looper.loop();
1048 }
1049
1050 /*
1051 * Use PIN or PUK to unlock SIM card
1052 *
1053 * If PUK is null, unlock SIM card with PIN
1054 *
1055 * If PUK is not null, unlock SIM card with PUK and set PIN code
1056 */
Wink Saville9de0f752013-10-22 19:04:03 -07001057 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001058
1059 while (mHandler == null) {
1060 try {
1061 wait();
1062 } catch (InterruptedException e) {
1063 Thread.currentThread().interrupt();
1064 }
1065 }
1066 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1067
1068 if (puk == null) {
1069 mSimCard.supplyPin(pin, callback);
1070 } else {
1071 mSimCard.supplyPuk(puk, pin, callback);
1072 }
1073
1074 while (!mDone) {
1075 try {
1076 Log.d(LOG_TAG, "wait for done");
1077 wait();
1078 } catch (InterruptedException e) {
1079 // Restore the interrupted status
1080 Thread.currentThread().interrupt();
1081 }
1082 }
1083 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001084 int[] resultArray = new int[2];
1085 resultArray[0] = mResult;
1086 resultArray[1] = mRetryCount;
1087 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 }
1089 }
1090
1091 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001092 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001093
1094 }
1095
Wink Savilleb564aae2014-10-23 10:18:09 -07001096 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001097 // No permission check needed here: this call is harmless, and it's
1098 // needed for the ServiceState.requestStateUpdate() call (which is
1099 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001100 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 }
1102
1103 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001104 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001105 }
1106
Wink Savilleb564aae2014-10-23 10:18:09 -07001107 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001108 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 }
1110
1111 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001112 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001113
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 }
Wink Saville36469e72014-06-11 15:17:00 -07001115
Wink Savilleb564aae2014-10-23 10:18:09 -07001116 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001118 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001119 }
1120
1121 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001122 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001123 }
1124
Wink Savilleb564aae2014-10-23 10:18:09 -07001125 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001126 enforceModifyPermission();
1127 if ((getPhone(subId).getServiceState().getState() !=
1128 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001129 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001130 }
1131 return true;
1132 }
Wink Saville36469e72014-06-11 15:17:00 -07001133
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001134 public boolean needMobileRadioShutdown() {
1135 /*
1136 * If any of the Radios are available, it will need to be
1137 * shutdown. So return true if any Radio is available.
1138 */
1139 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1140 Phone phone = PhoneFactory.getPhone(i);
1141 if (phone != null && phone.isRadioAvailable()) return true;
1142 }
1143 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1144 return false;
1145 }
1146
1147 public void shutdownMobileRadios() {
1148 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1149 logv("Shutting down Phone " + i);
1150 shutdownRadioUsingPhoneId(i);
1151 }
1152 }
1153
1154 private void shutdownRadioUsingPhoneId(int phoneId) {
1155 enforceModifyPermission();
1156 Phone phone = PhoneFactory.getPhone(phoneId);
1157 if (phone != null && phone.isRadioAvailable()) {
1158 phone.shutdownRadio();
1159 }
1160 }
1161
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001163 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001164 }
1165
Wink Savilleb564aae2014-10-23 10:18:09 -07001166 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001168 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 return true;
1170 }
1171
Wink Saville36469e72014-06-11 15:17:00 -07001172 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 public boolean enableDataConnectivity() {
1174 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001175 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001176 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001177 return true;
1178 }
1179
Wink Saville36469e72014-06-11 15:17:00 -07001180 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 public boolean disableDataConnectivity() {
1182 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001183 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001184 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001185 return true;
1186 }
1187
Wink Saville36469e72014-06-11 15:17:00 -07001188 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001189 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001190 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001191 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192 }
1193
1194 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001195 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001196 }
1197
Wink Savilleb564aae2014-10-23 10:18:09 -07001198 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001200 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 }
1202
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001204 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001205 }
1206
Wink Savilleb564aae2014-10-23 10:18:09 -07001207 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001208 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 }
1210
1211 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001212 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001213 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215
1216 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001217 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001218 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001219 }
1220
1221 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001222 public Bundle getCellLocation(String callingPackage) {
1223 enforceFineOrCoarseLocationPermission("getCellLocation");
1224
1225 // OP_COARSE_LOCATION controls both fine and coarse location.
1226 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1227 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1228 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 }
1230
Jake Hambye994d462014-02-03 13:10:13 -08001231 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001232 if (DBG_LOC) log("getCellLocation: is active user");
1233 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001234 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1235 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 return data;
1237 } else {
1238 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1239 return null;
1240 }
1241 }
1242
Svetoslav64fad262015-04-14 14:35:21 -07001243 private void enforceFineOrCoarseLocationPermission(String message) {
1244 try {
1245 mApp.enforceCallingOrSelfPermission(
1246 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1247 } catch (SecurityException e) {
1248 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1249 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1250 // is the weaker precondition
1251 mApp.enforceCallingOrSelfPermission(
1252 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1253 }
1254 }
1255
1256
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 @Override
1258 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001259 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001260 }
1261
Wink Savilleb564aae2014-10-23 10:18:09 -07001262 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001263 mApp.enforceCallingOrSelfPermission(
1264 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001265 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 }
1267
1268 @Override
1269 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001270 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001271 }
1272
Wink Savilleb564aae2014-10-23 10:18:09 -07001273 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001274 mApp.enforceCallingOrSelfPermission(
1275 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001276 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 }
1278
1279 @Override
1280 @SuppressWarnings("unchecked")
1281 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001282 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1283
1284 // OP_COARSE_LOCATION controls both fine and coarse location.
1285 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1286 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1287 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 }
1289
1290 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1291 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1292 return null;
1293 }
Svetoslav64fad262015-04-14 14:35:21 -07001294
Jake Hambye994d462014-02-03 13:10:13 -08001295 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1297
1298 ArrayList<NeighboringCellInfo> cells = null;
1299
1300 try {
1301 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001302 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001304 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 }
1306 return cells;
1307 } else {
1308 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1309 return null;
1310 }
1311 }
1312
1313
1314 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001315 public List<CellInfo> getAllCellInfo(String callingPackage) {
1316 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1317
1318 // OP_COARSE_LOCATION controls both fine and coarse location.
1319 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1320 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1321 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322 }
1323
Jake Hambye994d462014-02-03 13:10:13 -08001324 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001326 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1327 for (Phone phone : PhoneFactory.getPhones()) {
1328 cellInfos.addAll(phone.getAllCellInfo());
1329 }
1330 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 } else {
1332 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1333 return null;
1334 }
1335 }
1336
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001337 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001338 public void setCellInfoListRate(int rateInMillis) {
1339 mPhone.setCellInfoListRate(rateInMillis);
1340 }
1341
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 //
1343 // Internal helper methods.
1344 //
1345
Jake Hambye994d462014-02-03 13:10:13 -08001346 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001347 boolean ok;
1348
1349 boolean self = Binder.getCallingUid() == Process.myUid();
1350 if (!self) {
1351 // Get the caller's user id then clear the calling identity
1352 // which will be restored in the finally clause.
1353 int callingUser = UserHandle.getCallingUserId();
1354 long ident = Binder.clearCallingIdentity();
1355
1356 try {
1357 // With calling identity cleared the current user is the foreground user.
1358 int foregroundUser = ActivityManager.getCurrentUser();
1359 ok = (foregroundUser == callingUser);
1360 if (DBG_LOC) {
1361 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1362 + " callingUser=" + callingUser + " ok=" + ok);
1363 }
1364 } catch (Exception ex) {
1365 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1366 ok = false;
1367 } finally {
1368 Binder.restoreCallingIdentity(ident);
1369 }
1370 } else {
1371 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1372 ok = true;
1373 }
1374 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1375 return ok;
1376 }
1377
1378 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1380 *
1381 * @throws SecurityException if the caller does not have the required permission
1382 */
1383 private void enforceModifyPermission() {
1384 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1385 }
1386
1387 /**
Junda Liua2e36012014-07-09 18:30:01 -07001388 * Make sure either system app or the caller has carrier privilege.
1389 *
1390 * @throws SecurityException if the caller does not have the required permission/privilege
1391 */
1392 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001393 int permission = mApp.checkCallingOrSelfPermission(
1394 android.Manifest.permission.MODIFY_PHONE_STATE);
1395 if (permission == PackageManager.PERMISSION_GRANTED) {
1396 return;
1397 }
1398
1399 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001400 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001401 loge("No Carrier Privilege.");
1402 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001403 }
1404 }
1405
1406 /**
1407 * Make sure the caller has carrier privilege.
1408 *
1409 * @throws SecurityException if the caller does not have the required permission
1410 */
1411 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001412 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001413 loge("No Carrier Privilege.");
1414 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001415 }
1416 }
1417
1418 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001419 * Make sure the caller has the CALL_PHONE permission.
1420 *
1421 * @throws SecurityException if the caller does not have the required permission
1422 */
1423 private void enforceCallPermission() {
1424 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1425 }
1426
Shishir Agrawal566b7612013-10-28 14:41:00 -07001427 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001428 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1429 *
1430 * @throws SecurityException if the caller does not have the required permission
1431 */
1432 private void enforcePrivilegedPhoneStatePermission() {
1433 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1434 null);
1435 }
1436
Stuart Scott8eef64f2015-04-08 15:13:54 -07001437 private void enforceConnectivityInternalPermission() {
1438 mApp.enforceCallingOrSelfPermission(
1439 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1440 "ConnectivityService");
1441 }
1442
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 private String createTelUrl(String number) {
1444 if (TextUtils.isEmpty(number)) {
1445 return null;
1446 }
1447
Jake Hambye994d462014-02-03 13:10:13 -08001448 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 }
1450
Ihab Awadf9e92732013-12-05 18:02:52 -08001451 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001452 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1453 }
1454
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001455 private static void logv(String msg) {
1456 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1457 }
1458
Ihab Awadf9e92732013-12-05 18:02:52 -08001459 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1461 }
1462
1463 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001464 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001465 }
1466
Wink Savilleb564aae2014-10-23 10:18:09 -07001467 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001468 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 }
1470
1471 /**
1472 * Returns the CDMA ERI icon index to display
1473 */
1474 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001475 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001476
1477 }
1478
Wink Savilleb564aae2014-10-23 10:18:09 -07001479 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001480 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 }
1482
1483 /**
1484 * Returns the CDMA ERI icon mode,
1485 * 0 - ON
1486 * 1 - FLASHING
1487 */
1488 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001489 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001490 }
1491
Wink Savilleb564aae2014-10-23 10:18:09 -07001492 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001493 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001494 }
1495
1496 /**
1497 * Returns the CDMA ERI text,
1498 */
1499 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001500 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001501 }
1502
Wink Savilleb564aae2014-10-23 10:18:09 -07001503 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001504 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 }
1506
1507 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001508 * Returns the CDMA MDN.
1509 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001510 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001511 enforceModifyPermissionOrCarrierPrivilege();
1512 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1513 return getPhone(subId).getLine1Number();
1514 } else {
1515 return null;
1516 }
1517 }
1518
1519 /**
1520 * Returns the CDMA MIN.
1521 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001522 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001523 enforceModifyPermissionOrCarrierPrivilege();
1524 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1525 return getPhone(subId).getCdmaMin();
1526 } else {
1527 return null;
1528 }
1529 }
1530
1531 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 * Returns true if CDMA provisioning needs to run.
1533 */
1534 public boolean needsOtaServiceProvisioning() {
1535 return mPhone.needsOtaServiceProvisioning();
1536 }
1537
1538 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001539 * Sets the voice mail number of a given subId.
1540 */
1541 @Override
1542 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001543 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001544 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1545 new Pair<String, String>(alphaTag, number), new Integer(subId));
1546 return success;
1547 }
1548
1549 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 * Returns the unread count of voicemails
1551 */
1552 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001553 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001554 }
1555
1556 /**
1557 * Returns the unread count of voicemails for a subId
1558 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001559 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001560 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001561 }
1562
1563 /**
1564 * Returns the data network type
1565 *
1566 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1567 */
1568 @Override
1569 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001570 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001571 }
1572
1573 /**
1574 * Returns the network type for a subId
1575 */
1576 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001577 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001578 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 }
1580
1581 /**
1582 * Returns the data network type
1583 */
1584 @Override
1585 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001586 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001587 }
1588
1589 /**
1590 * Returns the data network type for a subId
1591 */
1592 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001593 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001594 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001595 }
1596
1597 /**
1598 * Returns the data network type
1599 */
1600 @Override
1601 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001602 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001603 }
1604
1605 /**
1606 * Returns the Voice network type for a subId
1607 */
1608 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001609 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001610 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001611 }
1612
1613 /**
1614 * @return true if a ICC card is present
1615 */
1616 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001617 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001618 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001619 }
1620
1621 /**
1622 * @return true if a ICC card is present for a slotId
1623 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001624 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001625 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1626 if (subId != null) {
1627 return getPhone(subId[0]).getIccCard().hasIccCard();
1628 } else {
1629 return false;
1630 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631 }
1632
1633 /**
1634 * Return if the current radio is LTE on CDMA. This
1635 * is a tri-state return value as for a period of time
1636 * the mode may be unknown.
1637 *
1638 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001639 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001640 */
1641 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001642 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001643 }
1644
Wink Savilleb564aae2014-10-23 10:18:09 -07001645 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001646 return getPhone(subId).getLteOnCdmaMode();
1647 }
1648
1649 public void setPhone(Phone phone) {
1650 mPhone = phone;
1651 }
1652
1653 /**
1654 * {@hide}
1655 * Returns Default subId, 0 in the case of single standby.
1656 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001657 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001658 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001659 }
1660
Wink Savilleb564aae2014-10-23 10:18:09 -07001661 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001662 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001664
1665 /**
1666 * @see android.telephony.TelephonyManager.WifiCallingChoices
1667 */
1668 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001669 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1670 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001671 }
1672
1673 /**
1674 * @see android.telephony.TelephonyManager.WifiCallingChoices
1675 */
1676 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001677 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1678 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1679 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001680 }
1681
Sailesh Nepald1e68152013-12-12 19:08:02 -08001682 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001683 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001684 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001685 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001686
Shishir Agrawal566b7612013-10-28 14:41:00 -07001687 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001688 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001689 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001690
1691 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001692 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1693 CMD_OPEN_CHANNEL, AID);
1694 if (DBG) log("iccOpenLogicalChannel: " + response);
1695 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001696 }
1697
1698 @Override
1699 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001700 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001701
1702 if (DBG) log("iccCloseLogicalChannel: " + channel);
1703 if (channel < 0) {
1704 return false;
1705 }
Jake Hambye994d462014-02-03 13:10:13 -08001706 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001707 if (DBG) log("iccCloseLogicalChannel: " + success);
1708 return success;
1709 }
1710
1711 @Override
1712 public String iccTransmitApduLogicalChannel(int channel, int cla,
1713 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001714 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001715
1716 if (DBG) {
1717 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1718 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1719 " data=" + data);
1720 }
1721
1722 if (channel < 0) {
1723 return "";
1724 }
1725
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001726 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001727 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1728 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1729
Shishir Agrawal566b7612013-10-28 14:41:00 -07001730 // Append the returned status code to the end of the response payload.
1731 String s = Integer.toHexString(
1732 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001733 if (response.payload != null) {
1734 s = IccUtils.bytesToHexString(response.payload) + s;
1735 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001736 return s;
1737 }
Jake Hambye994d462014-02-03 13:10:13 -08001738
Evan Charltonc66da362014-05-16 14:06:40 -07001739 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001740 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1741 int p3, String data) {
1742 enforceModifyPermissionOrCarrierPrivilege();
1743
1744 if (DBG) {
1745 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1746 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1747 }
1748
1749 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1750 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1751 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1752
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001753 // Append the returned status code to the end of the response payload.
1754 String s = Integer.toHexString(
1755 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001756 if (response.payload != null) {
1757 s = IccUtils.bytesToHexString(response.payload) + s;
1758 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001759 return s;
1760 }
1761
1762 @Override
1763 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1764 String filePath) {
1765 enforceModifyPermissionOrCarrierPrivilege();
1766
1767 if (DBG) {
1768 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1769 p1 + " " + p2 + " " + p3 + ":" + filePath);
1770 }
1771
1772 IccIoResult response =
1773 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001774 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001775
1776 if (DBG) {
1777 log("Exchange SIM_IO [R]" + response);
1778 }
1779
1780 byte[] result = null;
1781 int length = 2;
1782 if (response.payload != null) {
1783 length = 2 + response.payload.length;
1784 result = new byte[length];
1785 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1786 } else {
1787 result = new byte[length];
1788 }
1789
1790 result[length - 1] = (byte) response.sw2;
1791 result[length - 2] = (byte) response.sw1;
1792 return result;
1793 }
1794
1795 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001796 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001797 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001798
1799 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1800 if (response.payload == null) {
1801 return "";
1802 }
1803
1804 // Append the returned status code to the end of the response payload.
1805 String s = Integer.toHexString(
1806 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1807 s = IccUtils.bytesToHexString(response.payload) + s;
1808 return s;
1809 }
1810
Jake Hambye994d462014-02-03 13:10:13 -08001811 /**
1812 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1813 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1814 *
1815 * @param itemID the ID of the item to read
1816 * @return the NV item as a String, or null on error.
1817 */
1818 @Override
1819 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001820 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001821 if (DBG) log("nvReadItem: item " + itemID);
1822 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1823 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1824 return value;
1825 }
1826
1827 /**
1828 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1829 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1830 *
1831 * @param itemID the ID of the item to read
1832 * @param itemValue the value to write, as a String
1833 * @return true on success; false on any failure
1834 */
1835 @Override
1836 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001837 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001838 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1839 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1840 new Pair<Integer, String>(itemID, itemValue));
1841 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1842 return success;
1843 }
1844
1845 /**
1846 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1847 * Used for device configuration by some CDMA operators.
1848 *
1849 * @param preferredRoamingList byte array containing the new PRL
1850 * @return true on success; false on any failure
1851 */
1852 @Override
1853 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001854 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001855 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1856 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1857 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1858 return success;
1859 }
1860
1861 /**
1862 * Perform the specified type of NV config reset.
1863 * Used for device configuration by some CDMA operators.
1864 *
1865 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1866 * @return true on success; false on any failure
1867 */
1868 @Override
1869 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001870 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001871 if (DBG) log("nvResetConfig: type " + resetType);
1872 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1873 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1874 return success;
1875 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001876
1877 /**
Wink Saville36469e72014-06-11 15:17:00 -07001878 * {@hide}
1879 * Returns Default sim, 0 in the case of single standby.
1880 */
1881 public int getDefaultSim() {
1882 //TODO Need to get it from Telephony Devcontroller
1883 return 0;
1884 }
1885
Svet Ganovb320e182015-04-16 12:30:10 -07001886 public String[] getPcscfAddress(String apnType, String callingPackage) {
1887 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1888 return new String[0];
1889 }
1890
1891
ram87fca6f2014-07-18 18:58:44 +05301892 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001893 }
1894
1895 public void setImsRegistrationState(boolean registered) {
1896 enforceModifyPermission();
1897 mPhone.setImsRegistrationState(registered);
1898 }
1899
1900 /**
Stuart Scott54788802015-03-30 13:18:01 -07001901 * Set the network selection mode to automatic.
1902 *
1903 */
1904 @Override
1905 public void setNetworkSelectionModeAutomatic(int subId) {
1906 enforceModifyPermissionOrCarrierPrivilege();
1907 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1908 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1909 }
1910
1911 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001912 * Get the calculated preferred network type.
1913 * Used for debugging incorrect network type.
1914 *
1915 * @return the preferred network type, defined in RILConstants.java.
1916 */
1917 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001918 public int getCalculatedPreferredNetworkType(String callingPackage) {
1919 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1920 return RILConstants.PREFERRED_NETWORK_MODE;
1921 }
1922
Amit Mahajan43330e02014-11-18 11:54:45 -08001923 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001924 }
1925
1926 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001927 * Get the preferred network type.
1928 * Used for device configuration by some CDMA operators.
1929 *
1930 * @return the preferred network type, defined in RILConstants.java.
1931 */
1932 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001933 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001934 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001935 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001936 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001937 int networkType = (result != null ? result[0] : -1);
1938 if (DBG) log("getPreferredNetworkType: " + networkType);
1939 return networkType;
1940 }
1941
1942 /**
1943 * Set the preferred network type.
1944 * Used for device configuration by some CDMA operators.
1945 *
1946 * @param networkType the preferred network type, defined in RILConstants.java.
1947 * @return true on success; false on any failure.
1948 */
1949 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001950 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001951 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001952 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1953 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001954 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001955 if (success) {
1956 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001957 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001958 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001959 return success;
1960 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001961
1962 /**
Junda Liu475951f2014-11-07 16:45:03 -08001963 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1964 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1965 * tethering.
1966 *
1967 * @return 0: Not required. 1: required. 2: Not set.
1968 * @hide
1969 */
1970 @Override
1971 public int getTetherApnRequired() {
1972 enforceModifyPermissionOrCarrierPrivilege();
1973 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
1974 Settings.Global.TETHER_DUN_REQUIRED, 2);
1975 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
1976 // config_tether_apndata.
1977 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
1978 dunRequired = 1;
1979 }
1980 return dunRequired;
1981 }
1982
1983 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07001984 * Set mobile data enabled
1985 * Used by the user through settings etc to turn on/off mobile data
1986 *
1987 * @param enable {@code true} turn turn data on, else {@code false}
1988 */
1989 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08001990 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07001991 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08001992 int phoneId = mSubscriptionController.getPhoneId(subId);
1993 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
1994 Phone phone = PhoneFactory.getPhone(phoneId);
1995 if (phone != null) {
1996 log("setDataEnabled: subId=" + subId + " enable=" + enable);
1997 phone.setDataEnabled(enable);
1998 } else {
1999 loge("setDataEnabled: no phone for subId=" + subId);
2000 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002001 }
2002
2003 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002004 * Get whether mobile data is enabled.
2005 *
2006 * Note that this used to be available from ConnectivityService, gated by
2007 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2008 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002009 *
2010 * @return {@code true} if data is enabled else {@code false}
2011 */
2012 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002013 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002014 try {
2015 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2016 null);
2017 } catch (Exception e) {
2018 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2019 null);
2020 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002021 int phoneId = mSubscriptionController.getPhoneId(subId);
2022 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2023 Phone phone = PhoneFactory.getPhone(phoneId);
2024 if (phone != null) {
2025 boolean retVal = phone.getDataEnabled();
2026 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2027 return retVal;
2028 } else {
2029 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2030 return false;
2031 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002032 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002033
2034 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002035 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002036 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002037 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002038 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002039 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2040 }
2041 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002042 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002043 }
Junda Liu29340342014-07-10 15:23:27 -07002044
2045 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002046 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002047 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002048 if (card == null) {
2049 loge("checkCarrierPrivilegesForPackage: No UICC");
2050 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2051 }
2052 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002053 }
Derek Tan89e89d42014-07-08 17:00:10 -07002054
2055 @Override
Junda Liue64de782015-04-16 17:19:16 -07002056 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2057 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2058 loge("phoneId " + phoneId + " is not valid.");
2059 return null;
2060 }
2061 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002062 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002063 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002064 return null ;
2065 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002066 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002067 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002068 }
2069
Wink Savilleb564aae2014-10-23 10:18:09 -07002070 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002071 UiccCard card = getPhone(subId).getUiccCard();
2072 if (card == null) {
2073 loge("getIccId: No UICC");
2074 return null;
2075 }
2076 String iccId = card.getIccId();
2077 if (TextUtils.isEmpty(iccId)) {
2078 loge("getIccId: ICC ID is null or empty.");
2079 return null;
2080 }
2081 return iccId;
2082 }
2083
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002084 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002085 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2086 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002087 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002088
Jeff Sharkey85190e62014-12-05 09:40:12 -08002089 final String iccId = getIccId(subId);
2090 final String subscriberId = getPhone(subId).getSubscriberId();
2091
2092 if (DBG_MERGE) {
2093 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2094 + subscriberId + " to " + number);
2095 }
2096
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002097 if (TextUtils.isEmpty(iccId)) {
2098 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002099 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002100
Jeff Sharkey85190e62014-12-05 09:40:12 -08002101 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2102
2103 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002104 if (alphaTag == null) {
2105 editor.remove(alphaTagPrefKey);
2106 } else {
2107 editor.putString(alphaTagPrefKey, alphaTag);
2108 }
2109
Jeff Sharkey85190e62014-12-05 09:40:12 -08002110 // Record both the line number and IMSI for this ICCID, since we need to
2111 // track all merged IMSIs based on line number
2112 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2113 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002114 if (number == null) {
2115 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002116 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002117 } else {
2118 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002119 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002120 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002121
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002122 editor.commit();
2123 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002124 }
2125
2126 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002127 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2128 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2129 return null;
2130 }
Derek Tan97ebb422014-09-05 16:55:38 -07002131
2132 String iccId = getIccId(subId);
2133 if (iccId != null) {
2134 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002135 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002136 }
Derek Tan97ebb422014-09-05 16:55:38 -07002137 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002138 }
2139
2140 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002141 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2142 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2143 return null;
2144 }
Derek Tan97ebb422014-09-05 16:55:38 -07002145
2146 String iccId = getIccId(subId);
2147 if (iccId != null) {
2148 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002149 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002150 }
Derek Tan97ebb422014-09-05 16:55:38 -07002151 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002152 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002153
2154 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002155 public String[] getMergedSubscriberIds() {
2156 final Context context = mPhone.getContext();
2157 final TelephonyManager tele = TelephonyManager.from(context);
2158 final SubscriptionManager sub = SubscriptionManager.from(context);
2159
2160 // Figure out what subscribers are currently active
2161 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2162 final int[] subIds = sub.getActiveSubscriptionIdList();
2163 for (int subId : subIds) {
2164 activeSubscriberIds.add(tele.getSubscriberId(subId));
2165 }
2166
2167 // First pass, find a number override for an active subscriber
2168 String mergeNumber = null;
2169 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2170 for (String key : prefs.keySet()) {
2171 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2172 final String subscriberId = (String) prefs.get(key);
2173 if (activeSubscriberIds.contains(subscriberId)) {
2174 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2175 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2176 mergeNumber = (String) prefs.get(numberKey);
2177 if (DBG_MERGE) {
2178 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2179 + " for active subscriber " + subscriberId);
2180 }
2181 if (!TextUtils.isEmpty(mergeNumber)) {
2182 break;
2183 }
2184 }
2185 }
2186 }
2187
2188 // Shortcut when no active merged subscribers
2189 if (TextUtils.isEmpty(mergeNumber)) {
2190 return null;
2191 }
2192
2193 // Second pass, find all subscribers under that line override
2194 final ArraySet<String> result = new ArraySet<>();
2195 for (String key : prefs.keySet()) {
2196 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2197 final String number = (String) prefs.get(key);
2198 if (mergeNumber.equals(number)) {
2199 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2200 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2201 final String subscriberId = (String) prefs.get(subscriberKey);
2202 if (!TextUtils.isEmpty(subscriberId)) {
2203 result.add(subscriberId);
2204 }
2205 }
2206 }
2207 }
2208
2209 final String[] resultArray = result.toArray(new String[result.size()]);
2210 Arrays.sort(resultArray);
2211 if (DBG_MERGE) {
2212 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2213 }
2214 return resultArray;
2215 }
2216
2217 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002218 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002219 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002220 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002221 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002222
2223 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002224 public boolean setRoamingOverride(List<String> gsmRoamingList,
2225 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2226 List<String> cdmaNonRoamingList) {
2227 enforceCarrierPrivilege();
2228 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2229 cdmaNonRoamingList);
2230 }
2231
2232 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002233 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2234 enforceModifyPermission();
2235
2236 int returnValue = 0;
2237 try {
2238 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2239 if(result.exception == null) {
2240 if (result.result != null) {
2241 byte[] responseData = (byte[])(result.result);
2242 if(responseData.length > oemResp.length) {
2243 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2244 responseData.length + "bytes. Buffer Size is " +
2245 oemResp.length + "bytes.");
2246 }
2247 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2248 returnValue = responseData.length;
2249 }
2250 } else {
2251 CommandException ex = (CommandException) result.exception;
2252 returnValue = ex.getCommandError().ordinal();
2253 if(returnValue > 0) returnValue *= -1;
2254 }
2255 } catch (RuntimeException e) {
2256 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2257 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2258 if(returnValue > 0) returnValue *= -1;
2259 }
2260
2261 return returnValue;
2262 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002263
2264 @Override
2265 public void setRadioCapability(RadioAccessFamily[] rafs) {
2266 try {
2267 ProxyController.getInstance().setRadioCapability(rafs);
2268 } catch (RuntimeException e) {
2269 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2270 }
2271 }
2272
2273 @Override
2274 public int getRadioAccessFamily(int phoneId) {
2275 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2276 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002277
2278 @Override
2279 public void enableVideoCalling(boolean enable) {
2280 enforceModifyPermission();
2281 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2282 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2283 editor.commit();
2284 }
2285
2286 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002287 public boolean isVideoCallingEnabled(String callingPackage) {
2288 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2289 return false;
2290 }
2291
Andrew Lee77527ac2014-10-21 16:57:39 -07002292 // Check the user preference and the system-level IMS setting. Even if the user has
2293 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2294 // In the long run, we may instead need to check if there exists a connection service
2295 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002296 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2297 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2298 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002299 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002300
Andrew Leea1239f22015-03-02 17:44:07 -08002301 @Override
2302 public boolean canChangeDtmfToneLength() {
Junda Liu12f7d802015-05-01 12:06:44 -07002303 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_DTMF_TYPE_ENABLED);
Andrew Leea1239f22015-03-02 17:44:07 -08002304 }
2305
2306 @Override
2307 public boolean isWorldPhone() {
Junda Liu12f7d802015-05-01 12:06:44 -07002308 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_WORLD_PHONE);
Andrew Leea1239f22015-03-02 17:44:07 -08002309 }
2310
Andrew Lee9431b832015-03-09 18:46:45 -07002311 @Override
2312 public boolean isTtyModeSupported() {
2313 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2314 TelephonyManager telephonyManager =
2315 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2316 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2317 }
2318
2319 @Override
2320 public boolean isHearingAidCompatibilitySupported() {
2321 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2322 }
2323
Sanket Padawe7310cc72015-01-14 09:53:20 -08002324 /**
2325 * Returns the unique device ID of phone, for example, the IMEI for
2326 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2327 *
2328 * <p>Requires Permission:
2329 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2330 */
2331 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002332 public String getDeviceId(String callingPackage) {
2333 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2334 return null;
2335 }
2336
Sanket Padawe7310cc72015-01-14 09:53:20 -08002337 final Phone phone = PhoneFactory.getPhone(0);
2338 if (phone != null) {
2339 return phone.getDeviceId();
2340 } else {
2341 return null;
2342 }
2343 }
2344
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002345 /*
2346 * {@hide}
2347 * Returns the IMS Registration Status
2348 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002349 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002350 public boolean isImsRegistered() {
2351 return mPhone.isImsRegistered();
2352 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002353
2354 @Override
2355 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2356 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2357 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002358
Nathan Haroldc55097a2015-03-11 18:14:50 -07002359 /*
2360 * {@hide}
2361 * Returns the IMS Registration Status
2362 */
2363 public boolean isWifiCallingEnabled() {
2364 return mPhone.isWifiCallingEnabled();
2365 }
2366
2367 /*
2368 * {@hide}
2369 * Returns the IMS Registration Status
2370 */
2371 public boolean isVolteEnabled() {
2372 return mPhone.isVolteEnabled();
2373 }
Svet Ganovb320e182015-04-16 12:30:10 -07002374
2375 private boolean canReadPhoneState(String callingPackage, String message) {
2376 mApp.enforceCallingOrSelfPermission(
2377 android.Manifest.permission.READ_PHONE_STATE, message);
2378
2379 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2380 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2381 return false;
2382 }
2383
2384 return true;
2385 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002386
2387 @Override
2388 public void factoryReset(int subId) {
2389 enforceConnectivityInternalPermission();
Svet Ganovcc087f82015-05-12 20:35:54 -07002390 final long identity = Binder.clearCallingIdentity();
2391 try {
2392 if (SubscriptionManager.isUsableSubIdValue(subId)) {
2393 // Enable data
2394 setDataEnabled(subId, true);
2395 // Set network selection mode to automatic
2396 setNetworkSelectionModeAutomatic(subId);
2397 // Set preferred mobile network type to the best available
2398 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2399 // Turn off roaming
2400 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2401 }
2402 } finally {
2403 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002404 }
2405 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002406}