blob: 25e25bebc710379949678307278602de44271b35 [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;
Stuart Scott981d8582015-04-21 14:09:50 -070036import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070037import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080038import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080039import android.telecom.PhoneAccount;
Andrew Lee9431b832015-03-09 18:46:45 -070040import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070041import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070043import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080044import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070045import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080047import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080048import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080049import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070050import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080052import android.util.ArrayMap;
53import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080056import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
Andrew Lee312e8172014-10-23 17:01:36 -070058import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070059import com.android.internal.telephony.CallManager;
60import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080063import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010064import com.android.internal.telephony.MccTable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070066import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070067import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070069import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080070import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070071import com.android.internal.telephony.uicc.IccIoResult;
72import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070073import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070074import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080075import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Wink Saville36469e72014-06-11 15:17:00 -070077import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
78
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080080import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080081import java.util.HashMap;
82import java.util.Iterator;
Jake Hambye994d462014-02-03 13:10:13 -080083import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010084import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080085import java.util.Map;
86import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
88/**
89 * Implementation of the ITelephony interface.
90 */
Santos Cordon117fee72014-05-16 17:56:12 -070091public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070092 private static final String LOG_TAG = "PhoneInterfaceManager";
93 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
94 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080095 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096
97 // Message codes used with mMainThreadHandler
98 private static final int CMD_HANDLE_PIN_MMI = 1;
99 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
100 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
101 private static final int CMD_ANSWER_RINGING_CALL = 4;
102 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700103 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
104 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700105 private static final int CMD_OPEN_CHANNEL = 9;
106 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
107 private static final int CMD_CLOSE_CHANNEL = 11;
108 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800109 private static final int CMD_NV_READ_ITEM = 13;
110 private static final int EVENT_NV_READ_ITEM_DONE = 14;
111 private static final int CMD_NV_WRITE_ITEM = 15;
112 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
113 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
114 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
115 private static final int CMD_NV_RESET_CONFIG = 19;
116 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800117 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
118 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
119 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
120 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800121 private static final int CMD_SEND_ENVELOPE = 25;
122 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700123 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
124 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
125 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
126 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
127 private static final int CMD_EXCHANGE_SIM_IO = 31;
128 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800129 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
130 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700131 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
132 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700133 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
134 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
135
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136
137 /** The singleton instance. */
138 private static PhoneInterfaceManager sInstance;
139
Wink Saville3ab207e2014-11-20 13:07:20 -0800140 private PhoneGlobals mApp;
141 private Phone mPhone;
142 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700143 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800144 private AppOpsManager mAppOps;
145 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800146 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800147 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148
Derek Tan97ebb422014-09-05 16:55:38 -0700149 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
150 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800151 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700152 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700153
154 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155 * A request object to use for transmitting data to an ICC.
156 */
157 private static final class IccAPDUArgument {
158 public int channel, cla, command, p1, p2, p3;
159 public String data;
160
161 public IccAPDUArgument(int channel, int cla, int command,
162 int p1, int p2, int p3, String data) {
163 this.channel = channel;
164 this.cla = cla;
165 this.command = command;
166 this.p1 = p1;
167 this.p2 = p2;
168 this.p3 = p3;
169 this.data = data;
170 }
171 }
172
173 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
175 * request after sending. The main thread will notify the request when it is complete.
176 */
177 private static final class MainThreadRequest {
178 /** The argument to use for the request */
179 public Object argument;
180 /** The result of the request that is run on the main thread */
181 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800182 /** The subscriber id that this request applies to. Null if default. */
183 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184
185 public MainThreadRequest(Object argument) {
186 this.argument = argument;
187 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800188
189 public MainThreadRequest(Object argument, Integer subId) {
190 this.argument = argument;
191 this.subId = subId;
192 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193 }
194
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800195 private static final class IncomingThirdPartyCallArgs {
196 public final ComponentName component;
197 public final String callId;
198 public final String callerDisplayName;
199
200 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
201 String callerDisplayName) {
202 this.component = component;
203 this.callId = callId;
204 this.callerDisplayName = callerDisplayName;
205 }
206 }
207
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 /**
209 * A handler that processes messages on the main thread in the phone process. Since many
210 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
211 * inbound binder threads to the main thread in the phone process. The Binder thread
212 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
213 * on, which will be notified when the operation completes and will contain the result of the
214 * request.
215 *
216 * <p>If a MainThreadRequest object is provided in the msg.obj field,
217 * note that request.result must be set to something non-null for the calling thread to
218 * unblock.
219 */
220 private final class MainThreadHandler extends Handler {
221 @Override
222 public void handleMessage(Message msg) {
223 MainThreadRequest request;
224 Message onCompleted;
225 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800226 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700227 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228
229 switch (msg.what) {
230 case CMD_HANDLE_PIN_MMI:
231 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800232 request.result = getPhoneFromRequest(request).handlePinMmi(
233 (String) request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700234 // Wake up the requesting thread
235 synchronized (request) {
236 request.notifyAll();
237 }
238 break;
239
240 case CMD_HANDLE_NEIGHBORING_CELL:
241 request = (MainThreadRequest) msg.obj;
242 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
243 request);
244 mPhone.getNeighboringCids(onCompleted);
245 break;
246
247 case EVENT_NEIGHBORING_CELL_DONE:
248 ar = (AsyncResult) msg.obj;
249 request = (MainThreadRequest) ar.userObj;
250 if (ar.exception == null && ar.result != null) {
251 request.result = ar.result;
252 } else {
253 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800254 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 }
256 // Wake up the requesting thread
257 synchronized (request) {
258 request.notifyAll();
259 }
260 break;
261
262 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700263 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800264 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700265 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 break;
267
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700268 case CMD_END_CALL:
269 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800270 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700271 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700272 Phone phone = getPhone(end_subId);
273 if (phone == null) {
274 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
275 break;
276 }
277 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
279 // CDMA: If the user presses the Power button we treat it as
280 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700281 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
283 // GSM: End the call as per the Phone state
284 hungUp = PhoneUtils.hangup(mCM);
285 } else {
286 throw new IllegalStateException("Unexpected phone type: " + phoneType);
287 }
288 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
289 request.result = hungUp;
290 // Wake up the requesting thread
291 synchronized (request) {
292 request.notifyAll();
293 }
294 break;
295
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700296 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700297 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700298 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700299 if (uiccCard == null) {
300 loge("iccTransmitApduLogicalChannel: No UICC");
301 request.result = new IccIoResult(0x6F, 0, (byte[])null);
302 synchronized (request) {
303 request.notifyAll();
304 }
305 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700306 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
307 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700308 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700309 iccArgument.channel, iccArgument.cla, iccArgument.command,
310 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700311 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700312 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700313 break;
314
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700315 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700316 ar = (AsyncResult) msg.obj;
317 request = (MainThreadRequest) ar.userObj;
318 if (ar.exception == null && ar.result != null) {
319 request.result = ar.result;
320 } else {
321 request.result = new IccIoResult(0x6F, 0, (byte[])null);
322 if (ar.result == null) {
323 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800324 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700325 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800326 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700327 } else {
328 loge("iccTransmitApduLogicalChannel: Unknown exception");
329 }
330 }
331 synchronized (request) {
332 request.notifyAll();
333 }
334 break;
335
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700336 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
337 request = (MainThreadRequest) msg.obj;
338 iccArgument = (IccAPDUArgument) request.argument;
339 if (uiccCard == null) {
340 loge("iccTransmitApduBasicChannel: No UICC");
341 request.result = new IccIoResult(0x6F, 0, (byte[])null);
342 synchronized (request) {
343 request.notifyAll();
344 }
345 } else {
346 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
347 request);
348 uiccCard.iccTransmitApduBasicChannel(
349 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
350 iccArgument.p3, iccArgument.data, onCompleted);
351 }
352 break;
353
354 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
355 ar = (AsyncResult) msg.obj;
356 request = (MainThreadRequest) ar.userObj;
357 if (ar.exception == null && ar.result != null) {
358 request.result = ar.result;
359 } else {
360 request.result = new IccIoResult(0x6F, 0, (byte[])null);
361 if (ar.result == null) {
362 loge("iccTransmitApduBasicChannel: Empty response");
363 } else if (ar.exception instanceof CommandException) {
364 loge("iccTransmitApduBasicChannel: CommandException: " +
365 ar.exception);
366 } else {
367 loge("iccTransmitApduBasicChannel: Unknown exception");
368 }
369 }
370 synchronized (request) {
371 request.notifyAll();
372 }
373 break;
374
375 case CMD_EXCHANGE_SIM_IO:
376 request = (MainThreadRequest) msg.obj;
377 iccArgument = (IccAPDUArgument) request.argument;
378 if (uiccCard == null) {
379 loge("iccExchangeSimIO: No UICC");
380 request.result = new IccIoResult(0x6F, 0, (byte[])null);
381 synchronized (request) {
382 request.notifyAll();
383 }
384 } else {
385 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
386 request);
387 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
388 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
389 iccArgument.data, onCompleted);
390 }
391 break;
392
393 case EVENT_EXCHANGE_SIM_IO_DONE:
394 ar = (AsyncResult) msg.obj;
395 request = (MainThreadRequest) ar.userObj;
396 if (ar.exception == null && ar.result != null) {
397 request.result = ar.result;
398 } else {
399 request.result = new IccIoResult(0x6f, 0, (byte[])null);
400 }
401 synchronized (request) {
402 request.notifyAll();
403 }
404 break;
405
Derek Tan4d5e5c12014-02-04 11:54:58 -0800406 case CMD_SEND_ENVELOPE:
407 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700408 if (uiccCard == null) {
409 loge("sendEnvelopeWithStatus: No UICC");
410 request.result = new IccIoResult(0x6F, 0, (byte[])null);
411 synchronized (request) {
412 request.notifyAll();
413 }
414 } else {
415 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
416 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
417 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800418 break;
419
420 case EVENT_SEND_ENVELOPE_DONE:
421 ar = (AsyncResult) msg.obj;
422 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700423 if (ar.exception == null && ar.result != null) {
424 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800425 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700426 request.result = new IccIoResult(0x6F, 0, (byte[])null);
427 if (ar.result == null) {
428 loge("sendEnvelopeWithStatus: Empty response");
429 } else if (ar.exception instanceof CommandException) {
430 loge("sendEnvelopeWithStatus: CommandException: " +
431 ar.exception);
432 } else {
433 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
434 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800435 }
436 synchronized (request) {
437 request.notifyAll();
438 }
439 break;
440
Shishir Agrawal566b7612013-10-28 14:41:00 -0700441 case CMD_OPEN_CHANNEL:
442 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700443 if (uiccCard == null) {
444 loge("iccOpenLogicalChannel: No UICC");
445 request.result = new IccIoResult(0x6F, 0, (byte[])null);
446 synchronized (request) {
447 request.notifyAll();
448 }
449 } else {
450 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
451 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
452 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700453 break;
454
455 case EVENT_OPEN_CHANNEL_DONE:
456 ar = (AsyncResult) msg.obj;
457 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700458 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700459 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700460 int[] result = (int[]) ar.result;
461 int channelId = result[0];
462 byte[] selectResponse = null;
463 if (result.length > 1) {
464 selectResponse = new byte[result.length - 1];
465 for (int i = 1; i < result.length; ++i) {
466 selectResponse[i - 1] = (byte) result[i];
467 }
468 }
469 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700470 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700472 if (ar.result == null) {
473 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700475 if (ar.exception != null) {
476 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
477 }
478
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700479 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700480 if (ar.exception instanceof CommandException) {
481 CommandException.Error error =
482 ((CommandException) (ar.exception)).getCommandError();
483 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700484 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700485 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700486 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700487 }
488 }
489 openChannelResp = new IccOpenLogicalChannelResponse(
490 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700491 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700492 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 synchronized (request) {
494 request.notifyAll();
495 }
496 break;
497
498 case CMD_CLOSE_CHANNEL:
499 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700500 if (uiccCard == null) {
501 loge("iccCloseLogicalChannel: No UICC");
502 request.result = new IccIoResult(0x6F, 0, (byte[])null);
503 synchronized (request) {
504 request.notifyAll();
505 }
506 } else {
507 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
508 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
509 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 break;
511
512 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800513 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
514 break;
515
516 case CMD_NV_READ_ITEM:
517 request = (MainThreadRequest) msg.obj;
518 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
519 mPhone.nvReadItem((Integer) request.argument, onCompleted);
520 break;
521
522 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700523 ar = (AsyncResult) msg.obj;
524 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800525 if (ar.exception == null && ar.result != null) {
526 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700527 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800528 request.result = "";
529 if (ar.result == null) {
530 loge("nvReadItem: Empty response");
531 } else if (ar.exception instanceof CommandException) {
532 loge("nvReadItem: CommandException: " +
533 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700534 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800535 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700536 }
537 }
538 synchronized (request) {
539 request.notifyAll();
540 }
541 break;
542
Jake Hambye994d462014-02-03 13:10:13 -0800543 case CMD_NV_WRITE_ITEM:
544 request = (MainThreadRequest) msg.obj;
545 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
546 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
547 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
548 break;
549
550 case EVENT_NV_WRITE_ITEM_DONE:
551 handleNullReturnEvent(msg, "nvWriteItem");
552 break;
553
554 case CMD_NV_WRITE_CDMA_PRL:
555 request = (MainThreadRequest) msg.obj;
556 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
557 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
558 break;
559
560 case EVENT_NV_WRITE_CDMA_PRL_DONE:
561 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
562 break;
563
564 case CMD_NV_RESET_CONFIG:
565 request = (MainThreadRequest) msg.obj;
566 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
567 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
568 break;
569
570 case EVENT_NV_RESET_CONFIG_DONE:
571 handleNullReturnEvent(msg, "nvResetConfig");
572 break;
573
Jake Hamby7c27be32014-03-03 13:25:59 -0800574 case CMD_GET_PREFERRED_NETWORK_TYPE:
575 request = (MainThreadRequest) msg.obj;
576 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700577 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800578 break;
579
580 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
581 ar = (AsyncResult) msg.obj;
582 request = (MainThreadRequest) ar.userObj;
583 if (ar.exception == null && ar.result != null) {
584 request.result = ar.result; // Integer
585 } else {
586 request.result = -1;
587 if (ar.result == null) {
588 loge("getPreferredNetworkType: Empty response");
589 } else if (ar.exception instanceof CommandException) {
590 loge("getPreferredNetworkType: CommandException: " +
591 ar.exception);
592 } else {
593 loge("getPreferredNetworkType: Unknown exception");
594 }
595 }
596 synchronized (request) {
597 request.notifyAll();
598 }
599 break;
600
601 case CMD_SET_PREFERRED_NETWORK_TYPE:
602 request = (MainThreadRequest) msg.obj;
603 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
604 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700605 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800606 break;
607
608 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
609 handleNullReturnEvent(msg, "setPreferredNetworkType");
610 break;
611
Steven Liu4bf01bc2014-07-17 11:05:29 -0500612 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
613 request = (MainThreadRequest)msg.obj;
614 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
615 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
616 break;
617
618 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
619 ar = (AsyncResult)msg.obj;
620 request = (MainThreadRequest)ar.userObj;
621 request.result = ar;
622 synchronized (request) {
623 request.notifyAll();
624 }
625 break;
626
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800627 case CMD_SET_VOICEMAIL_NUMBER:
628 request = (MainThreadRequest) msg.obj;
629 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
630 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800631 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
632 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800633 break;
634
635 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
636 handleNullReturnEvent(msg, "setVoicemailNumber");
637 break;
638
Stuart Scott54788802015-03-30 13:18:01 -0700639 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
640 request = (MainThreadRequest) msg.obj;
641 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
642 request);
643 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
644 break;
645
646 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
647 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
648 break;
649
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700650 case CMD_GET_MODEM_ACTIVITY_INFO:
651 request = (MainThreadRequest) msg.obj;
652 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700653 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700654 break;
655
656 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
657 ar = (AsyncResult) msg.obj;
658 request = (MainThreadRequest) ar.userObj;
659 if (ar.exception == null && ar.result != null) {
660 request.result = ar.result;
661 } else {
662 if (ar.result == null) {
663 loge("queryModemActivityInfo: Empty response");
664 } else if (ar.exception instanceof CommandException) {
665 loge("queryModemActivityInfo: CommandException: " +
666 ar.exception);
667 } else {
668 loge("queryModemActivityInfo: Unknown exception");
669 }
670 }
671 synchronized (request) {
672 request.notifyAll();
673 }
674 break;
675
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700676 default:
677 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
678 break;
679 }
680 }
Jake Hambye994d462014-02-03 13:10:13 -0800681
682 private void handleNullReturnEvent(Message msg, String command) {
683 AsyncResult ar = (AsyncResult) msg.obj;
684 MainThreadRequest request = (MainThreadRequest) ar.userObj;
685 if (ar.exception == null) {
686 request.result = true;
687 } else {
688 request.result = false;
689 if (ar.exception instanceof CommandException) {
690 loge(command + ": CommandException: " + ar.exception);
691 } else {
692 loge(command + ": Unknown exception");
693 }
694 }
695 synchronized (request) {
696 request.notifyAll();
697 }
698 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700699 }
700
701 /**
702 * Posts the specified command to be executed on the main thread,
703 * waits for the request to complete, and returns the result.
704 * @see #sendRequestAsync
705 */
706 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700707 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700708 }
709
710 /**
711 * Posts the specified command to be executed on the main thread,
712 * waits for the request to complete, and returns the result.
713 * @see #sendRequestAsync
714 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800715 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700716 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
717 throw new RuntimeException("This method will deadlock if called from the main thread.");
718 }
719
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800720 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700721 Message msg = mMainThreadHandler.obtainMessage(command, request);
722 msg.sendToTarget();
723
724 // Wait for the request to complete
725 synchronized (request) {
726 while (request.result == null) {
727 try {
728 request.wait();
729 } catch (InterruptedException e) {
730 // Do nothing, go back and wait until the request is complete
731 }
732 }
733 }
734 return request.result;
735 }
736
737 /**
738 * Asynchronous ("fire and forget") version of sendRequest():
739 * Posts the specified command to be executed on the main thread, and
740 * returns immediately.
741 * @see #sendRequest
742 */
743 private void sendRequestAsync(int command) {
744 mMainThreadHandler.sendEmptyMessage(command);
745 }
746
747 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700748 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
749 * @see {@link #sendRequest(int,Object)}
750 */
751 private void sendRequestAsync(int command, Object argument) {
752 MainThreadRequest request = new MainThreadRequest(argument);
753 Message msg = mMainThreadHandler.obtainMessage(command, request);
754 msg.sendToTarget();
755 }
756
757 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700758 * Initialize the singleton PhoneInterfaceManager instance.
759 * This is only done once, at startup, from PhoneApp.onCreate().
760 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700761 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700762 synchronized (PhoneInterfaceManager.class) {
763 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700764 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700765 } else {
766 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
767 }
768 return sInstance;
769 }
770 }
771
772 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700773 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774 mApp = app;
775 mPhone = phone;
776 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700777 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
779 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700780 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700781 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800782 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800783
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 publish();
785 }
786
787 private void publish() {
788 if (DBG) log("publish: " + this);
789
790 ServiceManager.addService("phone", this);
791 }
792
Stuart Scott584921c2015-01-15 17:10:34 -0800793 private Phone getPhoneFromRequest(MainThreadRequest request) {
794 return (request.subId == null) ? mPhone : getPhone(request.subId);
795 }
796
Wink Saville36469e72014-06-11 15:17:00 -0700797 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700798 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800799 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700800 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 //
802 // Implementation of the ITelephony interface.
803 //
804
805 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700806 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700807 }
808
Wink Savilleb564aae2014-10-23 10:18:09 -0700809 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 if (DBG) log("dial: " + number);
811 // No permission check needed here: This is just a wrapper around the
812 // ACTION_DIAL intent, which is available to any app since it puts up
813 // the UI before it does anything.
814
815 String url = createTelUrl(number);
816 if (url == null) {
817 return;
818 }
819
820 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700821 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700822 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
823 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
824 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
825 mApp.startActivity(intent);
826 }
827 }
828
829 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700830 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700831 }
832
Wink Savilleb564aae2014-10-23 10:18:09 -0700833 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 if (DBG) log("call: " + number);
835
836 // This is just a wrapper around the ACTION_CALL intent, but we still
837 // need to do a permission check since we're calling startActivity()
838 // from the context of the phone app.
839 enforceCallPermission();
840
841 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
842 != AppOpsManager.MODE_ALLOWED) {
843 return;
844 }
845
846 String url = createTelUrl(number);
847 if (url == null) {
848 return;
849 }
850
Wink Saville08874612014-08-31 19:19:58 -0700851 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100852 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800853 if (slist != null) {
854 for (SubscriptionInfo subInfoRecord : slist) {
855 if (subInfoRecord.getSubscriptionId() == subId) {
856 isValid = true;
857 break;
858 }
Wink Saville08874612014-08-31 19:19:58 -0700859 }
860 }
861 if (isValid == false) {
862 return;
863 }
864
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700866 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700867 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
868 mApp.startActivity(intent);
869 }
870
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700871 /**
872 * End a call based on call state
873 * @return true is a call was ended
874 */
875 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700876 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700877 }
878
879 /**
880 * End a call based on the call state of the subId
881 * @return true is a call was ended
882 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700883 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700884 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800885 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700886 }
887
888 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700889 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700890 }
891
Wink Savilleb564aae2014-10-23 10:18:09 -0700892 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 if (DBG) log("answerRingingCall...");
894 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
895 // but that can probably wait till the big TelephonyManager API overhaul.
896 // For now, protect this call with the MODIFY_PHONE_STATE permission.
897 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800898 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 }
900
901 /**
902 * Make the actual telephony calls to implement answerRingingCall().
903 * This should only be called from the main thread of the Phone app.
904 * @see #answerRingingCall
905 *
906 * TODO: it would be nice to return true if we answered the call, or
907 * false if there wasn't actually a ringing incoming call, or some
908 * other error occurred. (In other words, pass back the return value
909 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
910 * But that would require calling this method via sendRequest() rather
911 * than sendRequestAsync(), and right now we don't actually *need* that
912 * return value, so let's just return void for now.
913 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700914 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700915 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700917 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
918 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 if (hasActiveCall && hasHoldingCall) {
920 // Both lines are in use!
921 // TODO: provide a flag to let the caller specify what
922 // policy to use if both lines are in use. (The current
923 // behavior is hardwired to "answer incoming, end ongoing",
924 // which is how the CALL button is specced to behave.)
925 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
926 return;
927 } else {
928 // answerCall() will automatically hold the current active
929 // call, if there is one.
930 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
931 return;
932 }
933 } else {
934 // No call was ringing.
935 return;
936 }
937 }
938
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -0700940 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 */
Santos Cordon5422a8d2014-09-12 04:20:56 -0700942 public void silenceRinger() {
943 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 }
945
946 public boolean isOffhook() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700947 return isOffhookForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700948 }
949
Wink Savilleb564aae2014-10-23 10:18:09 -0700950 public boolean isOffhookForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700951 return (getPhone(subId).getState() == PhoneConstants.State.OFFHOOK);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 }
953
954 public boolean isRinging() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700955 return (isRingingForSubscriber(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -0700956 }
957
Wink Savilleb564aae2014-10-23 10:18:09 -0700958 public boolean isRingingForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700959 return (getPhone(subId).getState() == PhoneConstants.State.RINGING);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 }
961
962 public boolean isIdle() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700963 return isIdleForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700964 }
965
Wink Savilleb564aae2014-10-23 10:18:09 -0700966 public boolean isIdleForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -0700967 return (getPhone(subId).getState() == PhoneConstants.State.IDLE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 }
969
Svet Ganovb320e182015-04-16 12:30:10 -0700970 public boolean isSimPinEnabled(String callingPackage) {
971 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
972 return false;
973 }
974
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 return (PhoneGlobals.getInstance().isSimPinEnabled());
976 }
977
978 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700979 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700980 }
981
Wink Savilleb564aae2014-10-23 10:18:09 -0700982 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700983 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700984 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
985 }
986
987 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700988 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -0700989 }
990
Wink Savilleb564aae2014-10-23 10:18:09 -0700991 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700992 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -0700993 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
994 }
995
996 /** {@hide} */
997 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700998 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -0700999 }
1000
Wink Savilleb564aae2014-10-23 10:18:09 -07001001 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001003 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 checkSimPin.start();
1005 return checkSimPin.unlockSim(null, pin);
1006 }
1007
Wink Saville9de0f752013-10-22 19:04:03 -07001008 /** {@hide} */
1009 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001010 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001011 }
1012
Wink Savilleb564aae2014-10-23 10:18:09 -07001013 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001014 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001015 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 checkSimPuk.start();
1017 return checkSimPuk.unlockSim(puk, pin);
1018 }
1019
1020 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001021 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 * a synchronous one.
1023 */
1024 private static class UnlockSim extends Thread {
1025
1026 private final IccCard mSimCard;
1027
1028 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001029 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1030 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031
1032 // For replies from SimCard interface
1033 private Handler mHandler;
1034
1035 // For async handler to identify request type
1036 private static final int SUPPLY_PIN_COMPLETE = 100;
1037
1038 public UnlockSim(IccCard simCard) {
1039 mSimCard = simCard;
1040 }
1041
1042 @Override
1043 public void run() {
1044 Looper.prepare();
1045 synchronized (UnlockSim.this) {
1046 mHandler = new Handler() {
1047 @Override
1048 public void handleMessage(Message msg) {
1049 AsyncResult ar = (AsyncResult) msg.obj;
1050 switch (msg.what) {
1051 case SUPPLY_PIN_COMPLETE:
1052 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1053 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001054 mRetryCount = msg.arg1;
1055 if (ar.exception != null) {
1056 if (ar.exception instanceof CommandException &&
1057 ((CommandException)(ar.exception)).getCommandError()
1058 == CommandException.Error.PASSWORD_INCORRECT) {
1059 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1060 } else {
1061 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1062 }
1063 } else {
1064 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1065 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001066 mDone = true;
1067 UnlockSim.this.notifyAll();
1068 }
1069 break;
1070 }
1071 }
1072 };
1073 UnlockSim.this.notifyAll();
1074 }
1075 Looper.loop();
1076 }
1077
1078 /*
1079 * Use PIN or PUK to unlock SIM card
1080 *
1081 * If PUK is null, unlock SIM card with PIN
1082 *
1083 * If PUK is not null, unlock SIM card with PUK and set PIN code
1084 */
Wink Saville9de0f752013-10-22 19:04:03 -07001085 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001086
1087 while (mHandler == null) {
1088 try {
1089 wait();
1090 } catch (InterruptedException e) {
1091 Thread.currentThread().interrupt();
1092 }
1093 }
1094 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1095
1096 if (puk == null) {
1097 mSimCard.supplyPin(pin, callback);
1098 } else {
1099 mSimCard.supplyPuk(puk, pin, callback);
1100 }
1101
1102 while (!mDone) {
1103 try {
1104 Log.d(LOG_TAG, "wait for done");
1105 wait();
1106 } catch (InterruptedException e) {
1107 // Restore the interrupted status
1108 Thread.currentThread().interrupt();
1109 }
1110 }
1111 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001112 int[] resultArray = new int[2];
1113 resultArray[0] = mResult;
1114 resultArray[1] = mRetryCount;
1115 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 }
1117 }
1118
1119 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001120 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001121
1122 }
1123
Wink Savilleb564aae2014-10-23 10:18:09 -07001124 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 // No permission check needed here: this call is harmless, and it's
1126 // needed for the ServiceState.requestStateUpdate() call (which is
1127 // already intentionally exposed to 3rd parties.)
Wink Saville36469e72014-06-11 15:17:00 -07001128 getPhone(subId).updateServiceLocation();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 }
1130
1131 public boolean isRadioOn() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001132 return isRadioOnForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001133 }
1134
Wink Savilleb564aae2014-10-23 10:18:09 -07001135 public boolean isRadioOnForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001136 return getPhone(subId).getServiceState().getState() != ServiceState.STATE_POWER_OFF;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 }
1138
1139 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001140 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001141
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 }
Wink Saville36469e72014-06-11 15:17:00 -07001143
Wink Savilleb564aae2014-10-23 10:18:09 -07001144 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 enforceModifyPermission();
Wink Savilleadd7cc52014-09-08 14:23:09 -07001146 getPhone(subId).setRadioPower(!isRadioOnForSubscriber(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001147 }
1148
1149 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001150 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001151 }
1152
Wink Savilleb564aae2014-10-23 10:18:09 -07001153 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001154 enforceModifyPermission();
1155 if ((getPhone(subId).getServiceState().getState() !=
1156 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001157 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001158 }
1159 return true;
1160 }
Wink Saville36469e72014-06-11 15:17:00 -07001161
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001162 public boolean needMobileRadioShutdown() {
1163 /*
1164 * If any of the Radios are available, it will need to be
1165 * shutdown. So return true if any Radio is available.
1166 */
1167 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1168 Phone phone = PhoneFactory.getPhone(i);
1169 if (phone != null && phone.isRadioAvailable()) return true;
1170 }
1171 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1172 return false;
1173 }
1174
1175 public void shutdownMobileRadios() {
1176 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1177 logv("Shutting down Phone " + i);
1178 shutdownRadioUsingPhoneId(i);
1179 }
1180 }
1181
1182 private void shutdownRadioUsingPhoneId(int phoneId) {
1183 enforceModifyPermission();
1184 Phone phone = PhoneFactory.getPhone(phoneId);
1185 if (phone != null && phone.isRadioAvailable()) {
1186 phone.shutdownRadio();
1187 }
1188 }
1189
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001191 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001192 }
1193
Wink Savilleb564aae2014-10-23 10:18:09 -07001194 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001196 getPhone(subId).setRadioPower(turnOn);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 return true;
1198 }
1199
Wink Saville36469e72014-06-11 15:17:00 -07001200 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001201 public boolean enableDataConnectivity() {
1202 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001203 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001204 getPhone(subId).setDataEnabled(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 return true;
1206 }
1207
Wink Saville36469e72014-06-11 15:17:00 -07001208 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209 public boolean disableDataConnectivity() {
1210 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001211 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001212 getPhone(subId).setDataEnabled(false);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 return true;
1214 }
1215
Wink Saville36469e72014-06-11 15:17:00 -07001216 // FIXME: subId version needed
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001217 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001218 int subId = mSubscriptionController.getDefaultDataSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001219 return getPhone(subId).isDataConnectivityPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 }
1221
1222 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001223 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001224 }
1225
Wink Savilleb564aae2014-10-23 10:18:09 -07001226 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001227 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001228 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 }
1230
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001232 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001233 }
1234
Wink Savilleb564aae2014-10-23 10:18:09 -07001235 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001236 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 }
1238
1239 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001240 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001241 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001242 }
1243
1244 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001245 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Wink Saville36469e72014-06-11 15:17:00 -07001246 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001247 }
1248
1249 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001250 public Bundle getCellLocation(String callingPackage) {
1251 enforceFineOrCoarseLocationPermission("getCellLocation");
1252
1253 // OP_COARSE_LOCATION controls both fine and coarse location.
1254 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1255 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001256 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001257 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 }
1259
Jake Hambye994d462014-02-03 13:10:13 -08001260 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 if (DBG_LOC) log("getCellLocation: is active user");
1262 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001263 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1264 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 return data;
1266 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001267 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 return null;
1269 }
1270 }
1271
Svetoslav64fad262015-04-14 14:35:21 -07001272 private void enforceFineOrCoarseLocationPermission(String message) {
1273 try {
1274 mApp.enforceCallingOrSelfPermission(
1275 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1276 } catch (SecurityException e) {
1277 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1278 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1279 // is the weaker precondition
1280 mApp.enforceCallingOrSelfPermission(
1281 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1282 }
1283 }
1284
1285
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 @Override
1287 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001289 }
1290
Wink Savilleb564aae2014-10-23 10:18:09 -07001291 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 mApp.enforceCallingOrSelfPermission(
1293 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001294 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295 }
1296
1297 @Override
1298 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001299 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001300 }
1301
Wink Savilleb564aae2014-10-23 10:18:09 -07001302 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001303 mApp.enforceCallingOrSelfPermission(
1304 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001305 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 }
1307
1308 @Override
1309 @SuppressWarnings("unchecked")
1310 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001311 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1312
1313 // OP_COARSE_LOCATION controls both fine and coarse location.
1314 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1315 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1316 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001317 }
1318
1319 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1320 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1321 return null;
1322 }
Svetoslav64fad262015-04-14 14:35:21 -07001323
Jake Hambye994d462014-02-03 13:10:13 -08001324 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1326
1327 ArrayList<NeighboringCellInfo> cells = null;
1328
1329 try {
1330 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001331 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001333 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 }
1335 return cells;
1336 } else {
1337 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1338 return null;
1339 }
1340 }
1341
1342
1343 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001344 public List<CellInfo> getAllCellInfo(String callingPackage) {
1345 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1346
1347 // OP_COARSE_LOCATION controls both fine and coarse location.
1348 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1349 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1350 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 }
1352
Jake Hambye994d462014-02-03 13:10:13 -08001353 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001355 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1356 for (Phone phone : PhoneFactory.getPhones()) {
1357 cellInfos.addAll(phone.getAllCellInfo());
1358 }
1359 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360 } else {
1361 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1362 return null;
1363 }
1364 }
1365
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001366 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001367 public void setCellInfoListRate(int rateInMillis) {
1368 mPhone.setCellInfoListRate(rateInMillis);
1369 }
1370
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 //
1372 // Internal helper methods.
1373 //
1374
Jake Hambye994d462014-02-03 13:10:13 -08001375 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001376 boolean ok;
1377
1378 boolean self = Binder.getCallingUid() == Process.myUid();
1379 if (!self) {
1380 // Get the caller's user id then clear the calling identity
1381 // which will be restored in the finally clause.
1382 int callingUser = UserHandle.getCallingUserId();
1383 long ident = Binder.clearCallingIdentity();
1384
1385 try {
1386 // With calling identity cleared the current user is the foreground user.
1387 int foregroundUser = ActivityManager.getCurrentUser();
1388 ok = (foregroundUser == callingUser);
1389 if (DBG_LOC) {
1390 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1391 + " callingUser=" + callingUser + " ok=" + ok);
1392 }
1393 } catch (Exception ex) {
1394 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1395 ok = false;
1396 } finally {
1397 Binder.restoreCallingIdentity(ident);
1398 }
1399 } else {
1400 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1401 ok = true;
1402 }
1403 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1404 return ok;
1405 }
1406
1407 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1409 *
1410 * @throws SecurityException if the caller does not have the required permission
1411 */
1412 private void enforceModifyPermission() {
1413 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1414 }
1415
1416 /**
Junda Liua2e36012014-07-09 18:30:01 -07001417 * Make sure either system app or the caller has carrier privilege.
1418 *
1419 * @throws SecurityException if the caller does not have the required permission/privilege
1420 */
1421 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001422 int permission = mApp.checkCallingOrSelfPermission(
1423 android.Manifest.permission.MODIFY_PHONE_STATE);
1424 if (permission == PackageManager.PERMISSION_GRANTED) {
1425 return;
1426 }
1427
1428 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001429 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001430 loge("No Carrier Privilege.");
1431 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001432 }
1433 }
1434
1435 /**
1436 * Make sure the caller has carrier privilege.
1437 *
1438 * @throws SecurityException if the caller does not have the required permission
1439 */
1440 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001441 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001442 loge("No Carrier Privilege.");
1443 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001444 }
1445 }
1446
1447 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001448 * Make sure the caller has the CALL_PHONE permission.
1449 *
1450 * @throws SecurityException if the caller does not have the required permission
1451 */
1452 private void enforceCallPermission() {
1453 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1454 }
1455
Shishir Agrawal566b7612013-10-28 14:41:00 -07001456 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001457 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1458 *
1459 * @throws SecurityException if the caller does not have the required permission
1460 */
1461 private void enforcePrivilegedPhoneStatePermission() {
1462 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1463 null);
1464 }
1465
Stuart Scott8eef64f2015-04-08 15:13:54 -07001466 private void enforceConnectivityInternalPermission() {
1467 mApp.enforceCallingOrSelfPermission(
1468 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1469 "ConnectivityService");
1470 }
1471
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001472 private String createTelUrl(String number) {
1473 if (TextUtils.isEmpty(number)) {
1474 return null;
1475 }
1476
Jake Hambye994d462014-02-03 13:10:13 -08001477 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 }
1479
Ihab Awadf9e92732013-12-05 18:02:52 -08001480 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1482 }
1483
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001484 private static void logv(String msg) {
1485 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1486 }
1487
Ihab Awadf9e92732013-12-05 18:02:52 -08001488 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1490 }
1491
1492 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001493 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001494 }
1495
Wink Savilleb564aae2014-10-23 10:18:09 -07001496 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001497 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001498 }
1499
1500 /**
1501 * Returns the CDMA ERI icon index to display
1502 */
1503 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001504 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001505
1506 }
1507
Wink Savilleb564aae2014-10-23 10:18:09 -07001508 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001509 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 }
1511
1512 /**
1513 * Returns the CDMA ERI icon mode,
1514 * 0 - ON
1515 * 1 - FLASHING
1516 */
1517 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001518 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001519 }
1520
Wink Savilleb564aae2014-10-23 10:18:09 -07001521 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001522 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 }
1524
1525 /**
1526 * Returns the CDMA ERI text,
1527 */
1528 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001529 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001530 }
1531
Wink Savilleb564aae2014-10-23 10:18:09 -07001532 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001533 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 }
1535
1536 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001537 * Returns the CDMA MDN.
1538 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001539 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001540 enforceModifyPermissionOrCarrierPrivilege();
1541 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1542 return getPhone(subId).getLine1Number();
1543 } else {
1544 return null;
1545 }
1546 }
1547
1548 /**
1549 * Returns the CDMA MIN.
1550 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001551 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001552 enforceModifyPermissionOrCarrierPrivilege();
1553 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1554 return getPhone(subId).getCdmaMin();
1555 } else {
1556 return null;
1557 }
1558 }
1559
1560 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001561 * Returns true if CDMA provisioning needs to run.
1562 */
1563 public boolean needsOtaServiceProvisioning() {
1564 return mPhone.needsOtaServiceProvisioning();
1565 }
1566
1567 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001568 * Sets the voice mail number of a given subId.
1569 */
1570 @Override
1571 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001572 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001573 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1574 new Pair<String, String>(alphaTag, number), new Integer(subId));
1575 return success;
1576 }
1577
1578 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 * Returns the unread count of voicemails
1580 */
1581 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001582 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001583 }
1584
1585 /**
1586 * Returns the unread count of voicemails for a subId
1587 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001588 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001589 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
1592 /**
1593 * Returns the data network type
1594 *
1595 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1596 */
1597 @Override
1598 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001599 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001600 }
1601
1602 /**
1603 * Returns the network type for a subId
1604 */
1605 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001606 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001607 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 }
1609
1610 /**
1611 * Returns the data network type
1612 */
1613 @Override
1614 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001615 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001616 }
1617
1618 /**
1619 * Returns the data network type for a subId
1620 */
1621 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001622 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001623 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001624 }
1625
1626 /**
1627 * Returns the data network type
1628 */
1629 @Override
1630 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001631 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001632 }
1633
1634 /**
1635 * Returns the Voice network type for a subId
1636 */
1637 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001638 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001639 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001640 }
1641
1642 /**
1643 * @return true if a ICC card is present
1644 */
1645 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001646 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001647 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001648 }
1649
1650 /**
1651 * @return true if a ICC card is present for a slotId
1652 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001653 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001654 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1655 if (subId != null) {
1656 return getPhone(subId[0]).getIccCard().hasIccCard();
1657 } else {
1658 return false;
1659 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001660 }
1661
1662 /**
1663 * Return if the current radio is LTE on CDMA. This
1664 * is a tri-state return value as for a period of time
1665 * the mode may be unknown.
1666 *
1667 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001668 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001669 */
1670 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001671 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001672 }
1673
Wink Savilleb564aae2014-10-23 10:18:09 -07001674 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001675 return getPhone(subId).getLteOnCdmaMode();
1676 }
1677
1678 public void setPhone(Phone phone) {
1679 mPhone = phone;
1680 }
1681
1682 /**
1683 * {@hide}
1684 * Returns Default subId, 0 in the case of single standby.
1685 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001686 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001687 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001688 }
1689
Wink Savilleb564aae2014-10-23 10:18:09 -07001690 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001691 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001693
1694 /**
1695 * @see android.telephony.TelephonyManager.WifiCallingChoices
1696 */
1697 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001698 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1699 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001700 }
1701
1702 /**
1703 * @see android.telephony.TelephonyManager.WifiCallingChoices
1704 */
1705 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001706 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1707 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1708 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001709 }
1710
Sailesh Nepald1e68152013-12-12 19:08:02 -08001711 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001712 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001713 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001714 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001715
Shishir Agrawal566b7612013-10-28 14:41:00 -07001716 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001717 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001718 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001719
1720 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001721 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1722 CMD_OPEN_CHANNEL, AID);
1723 if (DBG) log("iccOpenLogicalChannel: " + response);
1724 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001725 }
1726
1727 @Override
1728 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001729 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001730
1731 if (DBG) log("iccCloseLogicalChannel: " + channel);
1732 if (channel < 0) {
1733 return false;
1734 }
Jake Hambye994d462014-02-03 13:10:13 -08001735 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001736 if (DBG) log("iccCloseLogicalChannel: " + success);
1737 return success;
1738 }
1739
1740 @Override
1741 public String iccTransmitApduLogicalChannel(int channel, int cla,
1742 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001743 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001744
1745 if (DBG) {
1746 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1747 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1748 " data=" + data);
1749 }
1750
1751 if (channel < 0) {
1752 return "";
1753 }
1754
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001755 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001756 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1757 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1758
Shishir Agrawal566b7612013-10-28 14:41:00 -07001759 // Append the returned status code to the end of the response payload.
1760 String s = Integer.toHexString(
1761 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001762 if (response.payload != null) {
1763 s = IccUtils.bytesToHexString(response.payload) + s;
1764 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001765 return s;
1766 }
Jake Hambye994d462014-02-03 13:10:13 -08001767
Evan Charltonc66da362014-05-16 14:06:40 -07001768 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001769 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1770 int p3, String data) {
1771 enforceModifyPermissionOrCarrierPrivilege();
1772
1773 if (DBG) {
1774 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1775 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1776 }
1777
1778 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1779 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1780 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1781
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001782 // Append the returned status code to the end of the response payload.
1783 String s = Integer.toHexString(
1784 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001785 if (response.payload != null) {
1786 s = IccUtils.bytesToHexString(response.payload) + s;
1787 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001788 return s;
1789 }
1790
1791 @Override
1792 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1793 String filePath) {
1794 enforceModifyPermissionOrCarrierPrivilege();
1795
1796 if (DBG) {
1797 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1798 p1 + " " + p2 + " " + p3 + ":" + filePath);
1799 }
1800
1801 IccIoResult response =
1802 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001803 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001804
1805 if (DBG) {
1806 log("Exchange SIM_IO [R]" + response);
1807 }
1808
1809 byte[] result = null;
1810 int length = 2;
1811 if (response.payload != null) {
1812 length = 2 + response.payload.length;
1813 result = new byte[length];
1814 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1815 } else {
1816 result = new byte[length];
1817 }
1818
1819 result[length - 1] = (byte) response.sw2;
1820 result[length - 2] = (byte) response.sw1;
1821 return result;
1822 }
1823
1824 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001825 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001826 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001827
1828 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1829 if (response.payload == null) {
1830 return "";
1831 }
1832
1833 // Append the returned status code to the end of the response payload.
1834 String s = Integer.toHexString(
1835 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1836 s = IccUtils.bytesToHexString(response.payload) + s;
1837 return s;
1838 }
1839
Jake Hambye994d462014-02-03 13:10:13 -08001840 /**
1841 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1842 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1843 *
1844 * @param itemID the ID of the item to read
1845 * @return the NV item as a String, or null on error.
1846 */
1847 @Override
1848 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001849 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001850 if (DBG) log("nvReadItem: item " + itemID);
1851 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1852 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1853 return value;
1854 }
1855
1856 /**
1857 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1858 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1859 *
1860 * @param itemID the ID of the item to read
1861 * @param itemValue the value to write, as a String
1862 * @return true on success; false on any failure
1863 */
1864 @Override
1865 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001866 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001867 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1868 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1869 new Pair<Integer, String>(itemID, itemValue));
1870 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1871 return success;
1872 }
1873
1874 /**
1875 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1876 * Used for device configuration by some CDMA operators.
1877 *
1878 * @param preferredRoamingList byte array containing the new PRL
1879 * @return true on success; false on any failure
1880 */
1881 @Override
1882 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001883 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001884 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1885 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1886 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1887 return success;
1888 }
1889
1890 /**
1891 * Perform the specified type of NV config reset.
1892 * Used for device configuration by some CDMA operators.
1893 *
1894 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1895 * @return true on success; false on any failure
1896 */
1897 @Override
1898 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001899 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001900 if (DBG) log("nvResetConfig: type " + resetType);
1901 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1902 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1903 return success;
1904 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001905
1906 /**
Wink Saville36469e72014-06-11 15:17:00 -07001907 * {@hide}
1908 * Returns Default sim, 0 in the case of single standby.
1909 */
1910 public int getDefaultSim() {
1911 //TODO Need to get it from Telephony Devcontroller
1912 return 0;
1913 }
1914
Svet Ganovb320e182015-04-16 12:30:10 -07001915 public String[] getPcscfAddress(String apnType, String callingPackage) {
1916 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1917 return new String[0];
1918 }
1919
1920
ram87fca6f2014-07-18 18:58:44 +05301921 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001922 }
1923
1924 public void setImsRegistrationState(boolean registered) {
1925 enforceModifyPermission();
1926 mPhone.setImsRegistrationState(registered);
1927 }
1928
1929 /**
Stuart Scott54788802015-03-30 13:18:01 -07001930 * Set the network selection mode to automatic.
1931 *
1932 */
1933 @Override
1934 public void setNetworkSelectionModeAutomatic(int subId) {
1935 enforceModifyPermissionOrCarrierPrivilege();
1936 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1937 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1938 }
1939
1940 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001941 * Get the calculated preferred network type.
1942 * Used for debugging incorrect network type.
1943 *
1944 * @return the preferred network type, defined in RILConstants.java.
1945 */
1946 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001947 public int getCalculatedPreferredNetworkType(String callingPackage) {
1948 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1949 return RILConstants.PREFERRED_NETWORK_MODE;
1950 }
1951
Amit Mahajan43330e02014-11-18 11:54:45 -08001952 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001953 }
1954
1955 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001956 * Get the preferred network type.
1957 * Used for device configuration by some CDMA operators.
1958 *
1959 * @return the preferred network type, defined in RILConstants.java.
1960 */
1961 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001962 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001963 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001964 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001965 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001966 int networkType = (result != null ? result[0] : -1);
1967 if (DBG) log("getPreferredNetworkType: " + networkType);
1968 return networkType;
1969 }
1970
1971 /**
1972 * Set the preferred network type.
1973 * Used for device configuration by some CDMA operators.
1974 *
1975 * @param networkType the preferred network type, defined in RILConstants.java.
1976 * @return true on success; false on any failure.
1977 */
1978 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001979 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001980 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001981 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1982 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001983 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001984 if (success) {
1985 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001986 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001987 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001988 return success;
1989 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001990
1991 /**
Junda Liu475951f2014-11-07 16:45:03 -08001992 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1993 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1994 * tethering.
1995 *
1996 * @return 0: Not required. 1: required. 2: Not set.
1997 * @hide
1998 */
1999 @Override
2000 public int getTetherApnRequired() {
2001 enforceModifyPermissionOrCarrierPrivilege();
2002 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2003 Settings.Global.TETHER_DUN_REQUIRED, 2);
2004 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2005 // config_tether_apndata.
2006 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2007 dunRequired = 1;
2008 }
2009 return dunRequired;
2010 }
2011
2012 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002013 * Set mobile data enabled
2014 * Used by the user through settings etc to turn on/off mobile data
2015 *
2016 * @param enable {@code true} turn turn data on, else {@code false}
2017 */
2018 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002019 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002020 enforceModifyPermission();
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 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2026 phone.setDataEnabled(enable);
2027 } else {
2028 loge("setDataEnabled: no phone for subId=" + subId);
2029 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002030 }
2031
2032 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002033 * Get whether mobile data is enabled.
2034 *
2035 * Note that this used to be available from ConnectivityService, gated by
2036 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2037 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002038 *
2039 * @return {@code true} if data is enabled else {@code false}
2040 */
2041 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002042 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002043 try {
2044 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2045 null);
2046 } catch (Exception e) {
2047 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2048 null);
2049 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002050 int phoneId = mSubscriptionController.getPhoneId(subId);
2051 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2052 Phone phone = PhoneFactory.getPhone(phoneId);
2053 if (phone != null) {
2054 boolean retVal = phone.getDataEnabled();
2055 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2056 return retVal;
2057 } else {
2058 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2059 return false;
2060 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002061 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002062
2063 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002064 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002065 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002066 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002067 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002068 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2069 }
2070 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002071 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002072 }
Junda Liu29340342014-07-10 15:23:27 -07002073
2074 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002075 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002076 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002077 if (card == null) {
2078 loge("checkCarrierPrivilegesForPackage: No UICC");
2079 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2080 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002081 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2082 }
2083
2084 @Override
2085 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2086 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2087 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2088 UiccCard card = UiccController.getInstance().getUiccCard(i);
2089 if (card == null) {
2090 loge("checkCarrierPrivilegesForPackageAnyPhones: No UICC");
2091 continue;
2092 }
2093
2094 result = card.getCarrierPrivilegeStatus(
2095 mPhone.getContext().getPackageManager(), pkgName);
2096 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2097 break;
2098 }
2099 }
2100
2101 return result;
Junda Liu29340342014-07-10 15:23:27 -07002102 }
Derek Tan89e89d42014-07-08 17:00:10 -07002103
2104 @Override
Junda Liue64de782015-04-16 17:19:16 -07002105 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2106 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2107 loge("phoneId " + phoneId + " is not valid.");
2108 return null;
2109 }
2110 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002111 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002112 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002113 return null ;
2114 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002115 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002116 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002117 }
2118
Wink Savilleb564aae2014-10-23 10:18:09 -07002119 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002120 UiccCard card = getPhone(subId).getUiccCard();
2121 if (card == null) {
2122 loge("getIccId: No UICC");
2123 return null;
2124 }
2125 String iccId = card.getIccId();
2126 if (TextUtils.isEmpty(iccId)) {
2127 loge("getIccId: ICC ID is null or empty.");
2128 return null;
2129 }
2130 return iccId;
2131 }
2132
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002133 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002134 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2135 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002136 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002137
Jeff Sharkey85190e62014-12-05 09:40:12 -08002138 final String iccId = getIccId(subId);
2139 final String subscriberId = getPhone(subId).getSubscriberId();
2140
2141 if (DBG_MERGE) {
2142 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2143 + subscriberId + " to " + number);
2144 }
2145
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002146 if (TextUtils.isEmpty(iccId)) {
2147 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002148 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002149
Jeff Sharkey85190e62014-12-05 09:40:12 -08002150 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2151
2152 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002153 if (alphaTag == null) {
2154 editor.remove(alphaTagPrefKey);
2155 } else {
2156 editor.putString(alphaTagPrefKey, alphaTag);
2157 }
2158
Jeff Sharkey85190e62014-12-05 09:40:12 -08002159 // Record both the line number and IMSI for this ICCID, since we need to
2160 // track all merged IMSIs based on line number
2161 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2162 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002163 if (number == null) {
2164 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002165 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002166 } else {
2167 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002168 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002169 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002170
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002171 editor.commit();
2172 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002173 }
2174
2175 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002176 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2177 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2178 return null;
2179 }
Derek Tan97ebb422014-09-05 16:55:38 -07002180
2181 String iccId = getIccId(subId);
2182 if (iccId != null) {
2183 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002184 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002185 }
Derek Tan97ebb422014-09-05 16:55:38 -07002186 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002187 }
2188
2189 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002190 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2191 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2192 return null;
2193 }
Derek Tan97ebb422014-09-05 16:55:38 -07002194
2195 String iccId = getIccId(subId);
2196 if (iccId != null) {
2197 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002198 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002199 }
Derek Tan97ebb422014-09-05 16:55:38 -07002200 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002201 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002202
2203 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002204 public String[] getMergedSubscriberIds() {
2205 final Context context = mPhone.getContext();
2206 final TelephonyManager tele = TelephonyManager.from(context);
2207 final SubscriptionManager sub = SubscriptionManager.from(context);
2208
2209 // Figure out what subscribers are currently active
2210 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2211 final int[] subIds = sub.getActiveSubscriptionIdList();
2212 for (int subId : subIds) {
2213 activeSubscriberIds.add(tele.getSubscriberId(subId));
2214 }
2215
2216 // First pass, find a number override for an active subscriber
2217 String mergeNumber = null;
2218 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2219 for (String key : prefs.keySet()) {
2220 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2221 final String subscriberId = (String) prefs.get(key);
2222 if (activeSubscriberIds.contains(subscriberId)) {
2223 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2224 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2225 mergeNumber = (String) prefs.get(numberKey);
2226 if (DBG_MERGE) {
2227 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2228 + " for active subscriber " + subscriberId);
2229 }
2230 if (!TextUtils.isEmpty(mergeNumber)) {
2231 break;
2232 }
2233 }
2234 }
2235 }
2236
2237 // Shortcut when no active merged subscribers
2238 if (TextUtils.isEmpty(mergeNumber)) {
2239 return null;
2240 }
2241
2242 // Second pass, find all subscribers under that line override
2243 final ArraySet<String> result = new ArraySet<>();
2244 for (String key : prefs.keySet()) {
2245 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2246 final String number = (String) prefs.get(key);
2247 if (mergeNumber.equals(number)) {
2248 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2249 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2250 final String subscriberId = (String) prefs.get(subscriberKey);
2251 if (!TextUtils.isEmpty(subscriberId)) {
2252 result.add(subscriberId);
2253 }
2254 }
2255 }
2256 }
2257
2258 final String[] resultArray = result.toArray(new String[result.size()]);
2259 Arrays.sort(resultArray);
2260 if (DBG_MERGE) {
2261 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2262 }
2263 return resultArray;
2264 }
2265
2266 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002267 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002268 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002269 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002270 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002271
2272 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002273 public boolean setRoamingOverride(List<String> gsmRoamingList,
2274 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2275 List<String> cdmaNonRoamingList) {
2276 enforceCarrierPrivilege();
2277 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2278 cdmaNonRoamingList);
2279 }
2280
2281 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002282 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2283 enforceModifyPermission();
2284
2285 int returnValue = 0;
2286 try {
2287 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2288 if(result.exception == null) {
2289 if (result.result != null) {
2290 byte[] responseData = (byte[])(result.result);
2291 if(responseData.length > oemResp.length) {
2292 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2293 responseData.length + "bytes. Buffer Size is " +
2294 oemResp.length + "bytes.");
2295 }
2296 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2297 returnValue = responseData.length;
2298 }
2299 } else {
2300 CommandException ex = (CommandException) result.exception;
2301 returnValue = ex.getCommandError().ordinal();
2302 if(returnValue > 0) returnValue *= -1;
2303 }
2304 } catch (RuntimeException e) {
2305 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2306 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2307 if(returnValue > 0) returnValue *= -1;
2308 }
2309
2310 return returnValue;
2311 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002312
2313 @Override
2314 public void setRadioCapability(RadioAccessFamily[] rafs) {
2315 try {
2316 ProxyController.getInstance().setRadioCapability(rafs);
2317 } catch (RuntimeException e) {
2318 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2319 }
2320 }
2321
2322 @Override
2323 public int getRadioAccessFamily(int phoneId) {
2324 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2325 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002326
2327 @Override
2328 public void enableVideoCalling(boolean enable) {
2329 enforceModifyPermission();
2330 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2331 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2332 editor.commit();
2333 }
2334
2335 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002336 public boolean isVideoCallingEnabled(String callingPackage) {
2337 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2338 return false;
2339 }
2340
Andrew Lee77527ac2014-10-21 16:57:39 -07002341 // Check the user preference and the system-level IMS setting. Even if the user has
2342 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2343 // In the long run, we may instead need to check if there exists a connection service
2344 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002345 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2346 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2347 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002348 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002349
Andrew Leea1239f22015-03-02 17:44:07 -08002350 @Override
2351 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002352 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002353 }
2354
2355 @Override
2356 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002357 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002358 }
2359
Andrew Lee9431b832015-03-09 18:46:45 -07002360 @Override
2361 public boolean isTtyModeSupported() {
2362 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2363 TelephonyManager telephonyManager =
2364 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2365 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2366 }
2367
2368 @Override
2369 public boolean isHearingAidCompatibilitySupported() {
2370 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2371 }
2372
Sanket Padawe7310cc72015-01-14 09:53:20 -08002373 /**
2374 * Returns the unique device ID of phone, for example, the IMEI for
2375 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2376 *
2377 * <p>Requires Permission:
2378 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2379 */
2380 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002381 public String getDeviceId(String callingPackage) {
2382 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2383 return null;
2384 }
2385
Sanket Padawe7310cc72015-01-14 09:53:20 -08002386 final Phone phone = PhoneFactory.getPhone(0);
2387 if (phone != null) {
2388 return phone.getDeviceId();
2389 } else {
2390 return null;
2391 }
2392 }
2393
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002394 /*
2395 * {@hide}
2396 * Returns the IMS Registration Status
2397 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002398 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002399 public boolean isImsRegistered() {
2400 return mPhone.isImsRegistered();
2401 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002402
2403 @Override
2404 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2405 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2406 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002407
Nathan Haroldc55097a2015-03-11 18:14:50 -07002408 /*
2409 * {@hide}
2410 * Returns the IMS Registration Status
2411 */
2412 public boolean isWifiCallingEnabled() {
2413 return mPhone.isWifiCallingEnabled();
2414 }
2415
2416 /*
2417 * {@hide}
2418 * Returns the IMS Registration Status
2419 */
2420 public boolean isVolteEnabled() {
2421 return mPhone.isVolteEnabled();
2422 }
Svet Ganovb320e182015-04-16 12:30:10 -07002423
2424 private boolean canReadPhoneState(String callingPackage, String message) {
2425 mApp.enforceCallingOrSelfPermission(
2426 android.Manifest.permission.READ_PHONE_STATE, message);
2427
2428 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2429 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2430 return false;
2431 }
2432
2433 return true;
2434 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002435
2436 @Override
2437 public void factoryReset(int subId) {
2438 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002439 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2440 return;
2441 }
2442
Svet Ganovcc087f82015-05-12 20:35:54 -07002443 final long identity = Binder.clearCallingIdentity();
2444 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002445 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2446 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002447 // Enable data
2448 setDataEnabled(subId, true);
2449 // Set network selection mode to automatic
2450 setNetworkSelectionModeAutomatic(subId);
2451 // Set preferred mobile network type to the best available
2452 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2453 // Turn off roaming
2454 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2455 }
2456 } finally {
2457 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002458 }
2459 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002460
2461 @Override
2462 public String getLocaleFromDefaultSim() {
2463 // We query all subscriptions instead of just the active ones, because
2464 // this might be called early on in the provisioning flow when the
2465 // subscriptions potentially aren't active yet.
2466 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2467 if (slist == null || slist.isEmpty()) {
2468 return null;
2469 }
2470
2471 // This function may be called very early, say, from the setup wizard, at
2472 // which point we won't have a default subscription set. If that's the case
2473 // we just choose the first, which will be valid in "most cases".
2474 final int defaultSubId = getDefaultSubscription();
2475 SubscriptionInfo info = null;
2476 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2477 info = slist.get(0);
2478 } else {
2479 for (SubscriptionInfo item : slist) {
2480 if (item.getSubscriptionId() == defaultSubId) {
2481 info = item;
2482 break;
2483 }
2484 }
2485
2486 if (info == null) {
2487 return null;
2488 }
2489 }
2490
2491 // Try and fetch the locale from the carrier properties or from the SIM language
2492 // preferences (EF-PL and EF-LI)...
2493 final Phone defaultPhone = getPhone(info.getSubscriptionId());
2494 if (defaultPhone != null) {
2495 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2496 if (localeFromDefaultSim != null) {
2497 return localeFromDefaultSim.toLanguageTag();
2498 }
2499 }
2500
2501 // .. if that doesn't work, try and guess the language from the sim MCC.
2502 final int mcc = info.getMcc();
2503 final Locale locale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc);
2504 if (locale != null) {
2505 return locale.toLanguageTag();
2506 }
2507
2508 return null;
2509 }
2510
2511 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2512 final long identity = Binder.clearCallingIdentity();
2513 try {
2514 return mSubscriptionController.getAllSubInfoList(
2515 mPhone.getContext().getOpPackageName());
2516 } finally {
2517 Binder.restoreCallingIdentity(identity);
2518 }
2519 }
2520
2521 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2522 final long identity = Binder.clearCallingIdentity();
2523 try {
2524 return mSubscriptionController.getActiveSubscriptionInfoList(
2525 mPhone.getContext().getOpPackageName());
2526 } finally {
2527 Binder.restoreCallingIdentity(identity);
2528 }
2529 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002530
2531 /**
2532 * {@hide}
2533 * Returns the modem stats
2534 */
2535 @Override
2536 public ModemActivityInfo getModemActivityInfo() {
2537 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2538 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002539}