blob: 747d82a222c2024a20a6099238bd0461b4f4eafd [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);
653 mPhone.queryModemActivityInfo(onCompleted);
654 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) {
1256 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 }
1258
Jake Hambye994d462014-02-03 13:10:13 -08001259 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 if (DBG_LOC) log("getCellLocation: is active user");
1261 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001262 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1263 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 return data;
1265 } else {
1266 if (DBG_LOC) log("getCellLocation: suppress non-active user");
1267 return null;
1268 }
1269 }
1270
Svetoslav64fad262015-04-14 14:35:21 -07001271 private void enforceFineOrCoarseLocationPermission(String message) {
1272 try {
1273 mApp.enforceCallingOrSelfPermission(
1274 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1275 } catch (SecurityException e) {
1276 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1277 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1278 // is the weaker precondition
1279 mApp.enforceCallingOrSelfPermission(
1280 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1281 }
1282 }
1283
1284
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 @Override
1286 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001287 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001288 }
1289
Wink Savilleb564aae2014-10-23 10:18:09 -07001290 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 mApp.enforceCallingOrSelfPermission(
1292 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001293 getPhone(subId).enableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 }
1295
1296 @Override
1297 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001298 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001299 }
1300
Wink Savilleb564aae2014-10-23 10:18:09 -07001301 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001302 mApp.enforceCallingOrSelfPermission(
1303 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Wink Saville36469e72014-06-11 15:17:00 -07001304 getPhone(subId).disableLocationUpdates();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 }
1306
1307 @Override
1308 @SuppressWarnings("unchecked")
1309 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001310 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1311
1312 // OP_COARSE_LOCATION controls both fine and coarse location.
1313 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1314 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1315 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 }
1317
1318 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1319 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1320 return null;
1321 }
Svetoslav64fad262015-04-14 14:35:21 -07001322
Jake Hambye994d462014-02-03 13:10:13 -08001323 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1325
1326 ArrayList<NeighboringCellInfo> cells = null;
1327
1328 try {
1329 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001330 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001332 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 }
1334 return cells;
1335 } else {
1336 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1337 return null;
1338 }
1339 }
1340
1341
1342 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001343 public List<CellInfo> getAllCellInfo(String callingPackage) {
1344 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1345
1346 // OP_COARSE_LOCATION controls both fine and coarse location.
1347 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1348 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1349 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 }
1351
Jake Hambye994d462014-02-03 13:10:13 -08001352 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001354 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1355 for (Phone phone : PhoneFactory.getPhones()) {
1356 cellInfos.addAll(phone.getAllCellInfo());
1357 }
1358 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359 } else {
1360 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1361 return null;
1362 }
1363 }
1364
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001365 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 public void setCellInfoListRate(int rateInMillis) {
1367 mPhone.setCellInfoListRate(rateInMillis);
1368 }
1369
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001370 //
1371 // Internal helper methods.
1372 //
1373
Jake Hambye994d462014-02-03 13:10:13 -08001374 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375 boolean ok;
1376
1377 boolean self = Binder.getCallingUid() == Process.myUid();
1378 if (!self) {
1379 // Get the caller's user id then clear the calling identity
1380 // which will be restored in the finally clause.
1381 int callingUser = UserHandle.getCallingUserId();
1382 long ident = Binder.clearCallingIdentity();
1383
1384 try {
1385 // With calling identity cleared the current user is the foreground user.
1386 int foregroundUser = ActivityManager.getCurrentUser();
1387 ok = (foregroundUser == callingUser);
1388 if (DBG_LOC) {
1389 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1390 + " callingUser=" + callingUser + " ok=" + ok);
1391 }
1392 } catch (Exception ex) {
1393 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1394 ok = false;
1395 } finally {
1396 Binder.restoreCallingIdentity(ident);
1397 }
1398 } else {
1399 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1400 ok = true;
1401 }
1402 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1403 return ok;
1404 }
1405
1406 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1408 *
1409 * @throws SecurityException if the caller does not have the required permission
1410 */
1411 private void enforceModifyPermission() {
1412 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1413 }
1414
1415 /**
Junda Liua2e36012014-07-09 18:30:01 -07001416 * Make sure either system app or the caller has carrier privilege.
1417 *
1418 * @throws SecurityException if the caller does not have the required permission/privilege
1419 */
1420 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001421 int permission = mApp.checkCallingOrSelfPermission(
1422 android.Manifest.permission.MODIFY_PHONE_STATE);
1423 if (permission == PackageManager.PERMISSION_GRANTED) {
1424 return;
1425 }
1426
1427 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001428 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001429 loge("No Carrier Privilege.");
1430 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001431 }
1432 }
1433
1434 /**
1435 * Make sure the caller has carrier privilege.
1436 *
1437 * @throws SecurityException if the caller does not have the required permission
1438 */
1439 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001440 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001441 loge("No Carrier Privilege.");
1442 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001443 }
1444 }
1445
1446 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 * Make sure the caller has the CALL_PHONE permission.
1448 *
1449 * @throws SecurityException if the caller does not have the required permission
1450 */
1451 private void enforceCallPermission() {
1452 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1453 }
1454
Shishir Agrawal566b7612013-10-28 14:41:00 -07001455 /**
Gabriel Peal36ebb0d2014-03-20 09:20:43 -07001456 * Make sure the caller has the READ_PRIVILEGED_PHONE_STATE permission.
1457 *
1458 * @throws SecurityException if the caller does not have the required permission
1459 */
1460 private void enforcePrivilegedPhoneStatePermission() {
1461 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1462 null);
1463 }
1464
Stuart Scott8eef64f2015-04-08 15:13:54 -07001465 private void enforceConnectivityInternalPermission() {
1466 mApp.enforceCallingOrSelfPermission(
1467 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1468 "ConnectivityService");
1469 }
1470
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 private String createTelUrl(String number) {
1472 if (TextUtils.isEmpty(number)) {
1473 return null;
1474 }
1475
Jake Hambye994d462014-02-03 13:10:13 -08001476 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001477 }
1478
Ihab Awadf9e92732013-12-05 18:02:52 -08001479 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1481 }
1482
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001483 private static void logv(String msg) {
1484 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1485 }
1486
Ihab Awadf9e92732013-12-05 18:02:52 -08001487 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1489 }
1490
1491 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001492 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001493 }
1494
Wink Savilleb564aae2014-10-23 10:18:09 -07001495 public int getActivePhoneTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001496 return getPhone(subId).getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 }
1498
1499 /**
1500 * Returns the CDMA ERI icon index to display
1501 */
1502 public int getCdmaEriIconIndex() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001503 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001504
1505 }
1506
Wink Savilleb564aae2014-10-23 10:18:09 -07001507 public int getCdmaEriIconIndexForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001508 return getPhone(subId).getCdmaEriIconIndex();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 }
1510
1511 /**
1512 * Returns the CDMA ERI icon mode,
1513 * 0 - ON
1514 * 1 - FLASHING
1515 */
1516 public int getCdmaEriIconMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001517 return getCdmaEriIconModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001518 }
1519
Wink Savilleb564aae2014-10-23 10:18:09 -07001520 public int getCdmaEriIconModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001521 return getPhone(subId).getCdmaEriIconMode();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 }
1523
1524 /**
1525 * Returns the CDMA ERI text,
1526 */
1527 public String getCdmaEriText() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001528 return getCdmaEriTextForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001529 }
1530
Wink Savilleb564aae2014-10-23 10:18:09 -07001531 public String getCdmaEriTextForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001532 return getPhone(subId).getCdmaEriText();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 }
1534
1535 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001536 * Returns the CDMA MDN.
1537 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001538 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001539 enforceModifyPermissionOrCarrierPrivilege();
1540 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1541 return getPhone(subId).getLine1Number();
1542 } else {
1543 return null;
1544 }
1545 }
1546
1547 /**
1548 * Returns the CDMA MIN.
1549 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001550 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001551 enforceModifyPermissionOrCarrierPrivilege();
1552 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1553 return getPhone(subId).getCdmaMin();
1554 } else {
1555 return null;
1556 }
1557 }
1558
1559 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 * Returns true if CDMA provisioning needs to run.
1561 */
1562 public boolean needsOtaServiceProvisioning() {
1563 return mPhone.needsOtaServiceProvisioning();
1564 }
1565
1566 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001567 * Sets the voice mail number of a given subId.
1568 */
1569 @Override
1570 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001571 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001572 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1573 new Pair<String, String>(alphaTag, number), new Integer(subId));
1574 return success;
1575 }
1576
1577 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001578 * Returns the unread count of voicemails
1579 */
1580 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001581 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001582 }
1583
1584 /**
1585 * Returns the unread count of voicemails for a subId
1586 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001587 public int getVoiceMessageCountForSubscriber( int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001588 return getPhone(subId).getVoiceMessageCount();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 }
1590
1591 /**
1592 * Returns the data network type
1593 *
1594 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1595 */
1596 @Override
1597 public int getNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001598 return getNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001599 }
1600
1601 /**
1602 * Returns the network type for a subId
1603 */
1604 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001605 public int getNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001606 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001607 }
1608
1609 /**
1610 * Returns the data network type
1611 */
1612 @Override
1613 public int getDataNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001614 return getDataNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001615 }
1616
1617 /**
1618 * Returns the data network type for a subId
1619 */
1620 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001621 public int getDataNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001622 return getPhone(subId).getServiceState().getDataNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001623 }
1624
1625 /**
1626 * Returns the data network type
1627 */
1628 @Override
1629 public int getVoiceNetworkType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001630 return getVoiceNetworkTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001631 }
1632
1633 /**
1634 * Returns the Voice network type for a subId
1635 */
1636 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001637 public int getVoiceNetworkTypeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001638 return getPhone(subId).getServiceState().getVoiceNetworkType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001639 }
1640
1641 /**
1642 * @return true if a ICC card is present
1643 */
1644 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001645 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001646 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001647 }
1648
1649 /**
1650 * @return true if a ICC card is present for a slotId
1651 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001652 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001653 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
1654 if (subId != null) {
1655 return getPhone(subId[0]).getIccCard().hasIccCard();
1656 } else {
1657 return false;
1658 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001659 }
1660
1661 /**
1662 * Return if the current radio is LTE on CDMA. This
1663 * is a tri-state return value as for a period of time
1664 * the mode may be unknown.
1665 *
1666 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001667 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 */
1669 public int getLteOnCdmaMode() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001670 return getLteOnCdmaModeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001671 }
1672
Wink Savilleb564aae2014-10-23 10:18:09 -07001673 public int getLteOnCdmaModeForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001674 return getPhone(subId).getLteOnCdmaMode();
1675 }
1676
1677 public void setPhone(Phone phone) {
1678 mPhone = phone;
1679 }
1680
1681 /**
1682 * {@hide}
1683 * Returns Default subId, 0 in the case of single standby.
1684 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001685 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001686 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001687 }
1688
Wink Savilleb564aae2014-10-23 10:18:09 -07001689 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001690 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001691 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001692
1693 /**
1694 * @see android.telephony.TelephonyManager.WifiCallingChoices
1695 */
1696 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001697 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1698 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001699 }
1700
1701 /**
1702 * @see android.telephony.TelephonyManager.WifiCallingChoices
1703 */
1704 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001705 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1706 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1707 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001708 }
1709
Sailesh Nepald1e68152013-12-12 19:08:02 -08001710 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001711 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001712 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001713 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001714
Shishir Agrawal566b7612013-10-28 14:41:00 -07001715 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001716 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001717 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001718
1719 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001720 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1721 CMD_OPEN_CHANNEL, AID);
1722 if (DBG) log("iccOpenLogicalChannel: " + response);
1723 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001724 }
1725
1726 @Override
1727 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001728 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001729
1730 if (DBG) log("iccCloseLogicalChannel: " + channel);
1731 if (channel < 0) {
1732 return false;
1733 }
Jake Hambye994d462014-02-03 13:10:13 -08001734 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001735 if (DBG) log("iccCloseLogicalChannel: " + success);
1736 return success;
1737 }
1738
1739 @Override
1740 public String iccTransmitApduLogicalChannel(int channel, int cla,
1741 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001742 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001743
1744 if (DBG) {
1745 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1746 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1747 " data=" + data);
1748 }
1749
1750 if (channel < 0) {
1751 return "";
1752 }
1753
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001754 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07001755 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
1756 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
1757
Shishir Agrawal566b7612013-10-28 14:41:00 -07001758 // Append the returned status code to the end of the response payload.
1759 String s = Integer.toHexString(
1760 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001761 if (response.payload != null) {
1762 s = IccUtils.bytesToHexString(response.payload) + s;
1763 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07001764 return s;
1765 }
Jake Hambye994d462014-02-03 13:10:13 -08001766
Evan Charltonc66da362014-05-16 14:06:40 -07001767 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001768 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
1769 int p3, String data) {
1770 enforceModifyPermissionOrCarrierPrivilege();
1771
1772 if (DBG) {
1773 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
1774 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
1775 }
1776
1777 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
1778 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
1779 if (DBG) log("iccTransmitApduBasicChannel: " + response);
1780
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001781 // Append the returned status code to the end of the response payload.
1782 String s = Integer.toHexString(
1783 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07001784 if (response.payload != null) {
1785 s = IccUtils.bytesToHexString(response.payload) + s;
1786 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001787 return s;
1788 }
1789
1790 @Override
1791 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
1792 String filePath) {
1793 enforceModifyPermissionOrCarrierPrivilege();
1794
1795 if (DBG) {
1796 log("Exchange SIM_IO " + fileID + ":" + command + " " +
1797 p1 + " " + p2 + " " + p3 + ":" + filePath);
1798 }
1799
1800 IccIoResult response =
1801 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05001802 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001803
1804 if (DBG) {
1805 log("Exchange SIM_IO [R]" + response);
1806 }
1807
1808 byte[] result = null;
1809 int length = 2;
1810 if (response.payload != null) {
1811 length = 2 + response.payload.length;
1812 result = new byte[length];
1813 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
1814 } else {
1815 result = new byte[length];
1816 }
1817
1818 result[length - 1] = (byte) response.sw2;
1819 result[length - 2] = (byte) response.sw1;
1820 return result;
1821 }
1822
1823 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07001824 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07001825 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07001826
1827 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
1828 if (response.payload == null) {
1829 return "";
1830 }
1831
1832 // Append the returned status code to the end of the response payload.
1833 String s = Integer.toHexString(
1834 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
1835 s = IccUtils.bytesToHexString(response.payload) + s;
1836 return s;
1837 }
1838
Jake Hambye994d462014-02-03 13:10:13 -08001839 /**
1840 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1841 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1842 *
1843 * @param itemID the ID of the item to read
1844 * @return the NV item as a String, or null on error.
1845 */
1846 @Override
1847 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07001848 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001849 if (DBG) log("nvReadItem: item " + itemID);
1850 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
1851 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
1852 return value;
1853 }
1854
1855 /**
1856 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
1857 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
1858 *
1859 * @param itemID the ID of the item to read
1860 * @param itemValue the value to write, as a String
1861 * @return true on success; false on any failure
1862 */
1863 @Override
1864 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07001865 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001866 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
1867 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
1868 new Pair<Integer, String>(itemID, itemValue));
1869 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
1870 return success;
1871 }
1872
1873 /**
1874 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
1875 * Used for device configuration by some CDMA operators.
1876 *
1877 * @param preferredRoamingList byte array containing the new PRL
1878 * @return true on success; false on any failure
1879 */
1880 @Override
1881 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07001882 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001883 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
1884 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
1885 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
1886 return success;
1887 }
1888
1889 /**
1890 * Perform the specified type of NV config reset.
1891 * Used for device configuration by some CDMA operators.
1892 *
1893 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
1894 * @return true on success; false on any failure
1895 */
1896 @Override
1897 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07001898 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08001899 if (DBG) log("nvResetConfig: type " + resetType);
1900 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
1901 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
1902 return success;
1903 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001904
1905 /**
Wink Saville36469e72014-06-11 15:17:00 -07001906 * {@hide}
1907 * Returns Default sim, 0 in the case of single standby.
1908 */
1909 public int getDefaultSim() {
1910 //TODO Need to get it from Telephony Devcontroller
1911 return 0;
1912 }
1913
Svet Ganovb320e182015-04-16 12:30:10 -07001914 public String[] getPcscfAddress(String apnType, String callingPackage) {
1915 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
1916 return new String[0];
1917 }
1918
1919
ram87fca6f2014-07-18 18:58:44 +05301920 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07001921 }
1922
1923 public void setImsRegistrationState(boolean registered) {
1924 enforceModifyPermission();
1925 mPhone.setImsRegistrationState(registered);
1926 }
1927
1928 /**
Stuart Scott54788802015-03-30 13:18:01 -07001929 * Set the network selection mode to automatic.
1930 *
1931 */
1932 @Override
1933 public void setNetworkSelectionModeAutomatic(int subId) {
1934 enforceModifyPermissionOrCarrierPrivilege();
1935 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
1936 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
1937 }
1938
1939 /**
Junda Liu84d15a22014-07-02 11:21:04 -07001940 * Get the calculated preferred network type.
1941 * Used for debugging incorrect network type.
1942 *
1943 * @return the preferred network type, defined in RILConstants.java.
1944 */
1945 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07001946 public int getCalculatedPreferredNetworkType(String callingPackage) {
1947 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
1948 return RILConstants.PREFERRED_NETWORK_MODE;
1949 }
1950
Amit Mahajan43330e02014-11-18 11:54:45 -08001951 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07001952 }
1953
1954 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08001955 * Get the preferred network type.
1956 * Used for device configuration by some CDMA operators.
1957 *
1958 * @return the preferred network type, defined in RILConstants.java.
1959 */
1960 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001961 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07001962 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08001963 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07001964 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001965 int networkType = (result != null ? result[0] : -1);
1966 if (DBG) log("getPreferredNetworkType: " + networkType);
1967 return networkType;
1968 }
1969
1970 /**
1971 * Set the preferred network type.
1972 * Used for device configuration by some CDMA operators.
1973 *
1974 * @param networkType the preferred network type, defined in RILConstants.java.
1975 * @return true on success; false on any failure.
1976 */
1977 @Override
Stuart Scott54788802015-03-30 13:18:01 -07001978 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07001979 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07001980 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
1981 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08001982 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07001983 if (success) {
1984 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07001985 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07001986 }
Jake Hamby7c27be32014-03-03 13:25:59 -08001987 return success;
1988 }
Robert Greenwalted86e582014-05-21 20:03:20 -07001989
1990 /**
Junda Liu475951f2014-11-07 16:45:03 -08001991 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
1992 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
1993 * tethering.
1994 *
1995 * @return 0: Not required. 1: required. 2: Not set.
1996 * @hide
1997 */
1998 @Override
1999 public int getTetherApnRequired() {
2000 enforceModifyPermissionOrCarrierPrivilege();
2001 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2002 Settings.Global.TETHER_DUN_REQUIRED, 2);
2003 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2004 // config_tether_apndata.
2005 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2006 dunRequired = 1;
2007 }
2008 return dunRequired;
2009 }
2010
2011 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002012 * Set mobile data enabled
2013 * Used by the user through settings etc to turn on/off mobile data
2014 *
2015 * @param enable {@code true} turn turn data on, else {@code false}
2016 */
2017 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002018 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002019 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002020 int phoneId = mSubscriptionController.getPhoneId(subId);
2021 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2022 Phone phone = PhoneFactory.getPhone(phoneId);
2023 if (phone != null) {
2024 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2025 phone.setDataEnabled(enable);
2026 } else {
2027 loge("setDataEnabled: no phone for subId=" + subId);
2028 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002029 }
2030
2031 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002032 * Get whether mobile data is enabled.
2033 *
2034 * Note that this used to be available from ConnectivityService, gated by
2035 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2036 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002037 *
2038 * @return {@code true} if data is enabled else {@code false}
2039 */
2040 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002041 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002042 try {
2043 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2044 null);
2045 } catch (Exception e) {
2046 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2047 null);
2048 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002049 int phoneId = mSubscriptionController.getPhoneId(subId);
2050 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2051 Phone phone = PhoneFactory.getPhone(phoneId);
2052 if (phone != null) {
2053 boolean retVal = phone.getDataEnabled();
2054 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2055 return retVal;
2056 } else {
2057 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2058 return false;
2059 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002060 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002061
2062 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002063 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002064 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002065 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002066 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002067 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2068 }
2069 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002070 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002071 }
Junda Liu29340342014-07-10 15:23:27 -07002072
2073 @Override
Shishir Agrawal6d5a2852014-07-11 16:32:57 -07002074 public int checkCarrierPrivilegesForPackage(String pkgname) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002075 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002076 if (card == null) {
2077 loge("checkCarrierPrivilegesForPackage: No UICC");
2078 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2079 }
2080 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgname);
Junda Liu29340342014-07-10 15:23:27 -07002081 }
Derek Tan89e89d42014-07-08 17:00:10 -07002082
2083 @Override
Junda Liue64de782015-04-16 17:19:16 -07002084 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2085 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2086 loge("phoneId " + phoneId + " is not valid.");
2087 return null;
2088 }
2089 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002090 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002091 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002092 return null ;
2093 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002094 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002095 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002096 }
2097
Wink Savilleb564aae2014-10-23 10:18:09 -07002098 private String getIccId(int subId) {
Derek Tan97ebb422014-09-05 16:55:38 -07002099 UiccCard card = getPhone(subId).getUiccCard();
2100 if (card == null) {
2101 loge("getIccId: No UICC");
2102 return null;
2103 }
2104 String iccId = card.getIccId();
2105 if (TextUtils.isEmpty(iccId)) {
2106 loge("getIccId: ICC ID is null or empty.");
2107 return null;
2108 }
2109 return iccId;
2110 }
2111
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002112 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002113 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2114 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002115 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002116
Jeff Sharkey85190e62014-12-05 09:40:12 -08002117 final String iccId = getIccId(subId);
2118 final String subscriberId = getPhone(subId).getSubscriberId();
2119
2120 if (DBG_MERGE) {
2121 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2122 + subscriberId + " to " + number);
2123 }
2124
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002125 if (TextUtils.isEmpty(iccId)) {
2126 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002127 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002128
Jeff Sharkey85190e62014-12-05 09:40:12 -08002129 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2130
2131 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002132 if (alphaTag == null) {
2133 editor.remove(alphaTagPrefKey);
2134 } else {
2135 editor.putString(alphaTagPrefKey, alphaTag);
2136 }
2137
Jeff Sharkey85190e62014-12-05 09:40:12 -08002138 // Record both the line number and IMSI for this ICCID, since we need to
2139 // track all merged IMSIs based on line number
2140 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2141 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002142 if (number == null) {
2143 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002144 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002145 } else {
2146 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002147 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002148 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002149
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002150 editor.commit();
2151 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002152 }
2153
2154 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002155 public String getLine1NumberForDisplay(int subId, String callingPackage) {
2156 if (!canReadPhoneState(callingPackage, "getLine1NumberForDisplay")) {
2157 return null;
2158 }
Derek Tan97ebb422014-09-05 16:55:38 -07002159
2160 String iccId = getIccId(subId);
2161 if (iccId != null) {
2162 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002163 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002164 }
Derek Tan97ebb422014-09-05 16:55:38 -07002165 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002166 }
2167
2168 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002169 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2170 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2171 return null;
2172 }
Derek Tan97ebb422014-09-05 16:55:38 -07002173
2174 String iccId = getIccId(subId);
2175 if (iccId != null) {
2176 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002177 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002178 }
Derek Tan97ebb422014-09-05 16:55:38 -07002179 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002180 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002181
2182 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002183 public String[] getMergedSubscriberIds() {
2184 final Context context = mPhone.getContext();
2185 final TelephonyManager tele = TelephonyManager.from(context);
2186 final SubscriptionManager sub = SubscriptionManager.from(context);
2187
2188 // Figure out what subscribers are currently active
2189 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
2190 final int[] subIds = sub.getActiveSubscriptionIdList();
2191 for (int subId : subIds) {
2192 activeSubscriberIds.add(tele.getSubscriberId(subId));
2193 }
2194
2195 // First pass, find a number override for an active subscriber
2196 String mergeNumber = null;
2197 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2198 for (String key : prefs.keySet()) {
2199 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2200 final String subscriberId = (String) prefs.get(key);
2201 if (activeSubscriberIds.contains(subscriberId)) {
2202 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2203 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2204 mergeNumber = (String) prefs.get(numberKey);
2205 if (DBG_MERGE) {
2206 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2207 + " for active subscriber " + subscriberId);
2208 }
2209 if (!TextUtils.isEmpty(mergeNumber)) {
2210 break;
2211 }
2212 }
2213 }
2214 }
2215
2216 // Shortcut when no active merged subscribers
2217 if (TextUtils.isEmpty(mergeNumber)) {
2218 return null;
2219 }
2220
2221 // Second pass, find all subscribers under that line override
2222 final ArraySet<String> result = new ArraySet<>();
2223 for (String key : prefs.keySet()) {
2224 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2225 final String number = (String) prefs.get(key);
2226 if (mergeNumber.equals(number)) {
2227 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2228 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2229 final String subscriberId = (String) prefs.get(subscriberKey);
2230 if (!TextUtils.isEmpty(subscriberId)) {
2231 result.add(subscriberId);
2232 }
2233 }
2234 }
2235 }
2236
2237 final String[] resultArray = result.toArray(new String[result.size()]);
2238 Arrays.sort(resultArray);
2239 if (DBG_MERGE) {
2240 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2241 }
2242 return resultArray;
2243 }
2244
2245 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002246 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002247 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002248 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002249 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002250
2251 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002252 public boolean setRoamingOverride(List<String> gsmRoamingList,
2253 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2254 List<String> cdmaNonRoamingList) {
2255 enforceCarrierPrivilege();
2256 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2257 cdmaNonRoamingList);
2258 }
2259
2260 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002261 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2262 enforceModifyPermission();
2263
2264 int returnValue = 0;
2265 try {
2266 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2267 if(result.exception == null) {
2268 if (result.result != null) {
2269 byte[] responseData = (byte[])(result.result);
2270 if(responseData.length > oemResp.length) {
2271 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2272 responseData.length + "bytes. Buffer Size is " +
2273 oemResp.length + "bytes.");
2274 }
2275 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2276 returnValue = responseData.length;
2277 }
2278 } else {
2279 CommandException ex = (CommandException) result.exception;
2280 returnValue = ex.getCommandError().ordinal();
2281 if(returnValue > 0) returnValue *= -1;
2282 }
2283 } catch (RuntimeException e) {
2284 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2285 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2286 if(returnValue > 0) returnValue *= -1;
2287 }
2288
2289 return returnValue;
2290 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002291
2292 @Override
2293 public void setRadioCapability(RadioAccessFamily[] rafs) {
2294 try {
2295 ProxyController.getInstance().setRadioCapability(rafs);
2296 } catch (RuntimeException e) {
2297 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2298 }
2299 }
2300
2301 @Override
2302 public int getRadioAccessFamily(int phoneId) {
2303 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2304 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002305
2306 @Override
2307 public void enableVideoCalling(boolean enable) {
2308 enforceModifyPermission();
2309 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2310 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2311 editor.commit();
2312 }
2313
2314 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002315 public boolean isVideoCallingEnabled(String callingPackage) {
2316 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2317 return false;
2318 }
2319
Andrew Lee77527ac2014-10-21 16:57:39 -07002320 // Check the user preference and the system-level IMS setting. Even if the user has
2321 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2322 // In the long run, we may instead need to check if there exists a connection service
2323 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002324 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2325 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2326 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002327 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002328
Andrew Leea1239f22015-03-02 17:44:07 -08002329 @Override
2330 public boolean canChangeDtmfToneLength() {
Junda Liu12f7d802015-05-01 12:06:44 -07002331 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_DTMF_TYPE_ENABLED);
Andrew Leea1239f22015-03-02 17:44:07 -08002332 }
2333
2334 @Override
2335 public boolean isWorldPhone() {
Junda Liu12f7d802015-05-01 12:06:44 -07002336 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.BOOL_WORLD_PHONE);
Andrew Leea1239f22015-03-02 17:44:07 -08002337 }
2338
Andrew Lee9431b832015-03-09 18:46:45 -07002339 @Override
2340 public boolean isTtyModeSupported() {
2341 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2342 TelephonyManager telephonyManager =
2343 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2344 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2345 }
2346
2347 @Override
2348 public boolean isHearingAidCompatibilitySupported() {
2349 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2350 }
2351
Sanket Padawe7310cc72015-01-14 09:53:20 -08002352 /**
2353 * Returns the unique device ID of phone, for example, the IMEI for
2354 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2355 *
2356 * <p>Requires Permission:
2357 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2358 */
2359 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002360 public String getDeviceId(String callingPackage) {
2361 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2362 return null;
2363 }
2364
Sanket Padawe7310cc72015-01-14 09:53:20 -08002365 final Phone phone = PhoneFactory.getPhone(0);
2366 if (phone != null) {
2367 return phone.getDeviceId();
2368 } else {
2369 return null;
2370 }
2371 }
2372
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002373 /*
2374 * {@hide}
2375 * Returns the IMS Registration Status
2376 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002377 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002378 public boolean isImsRegistered() {
2379 return mPhone.isImsRegistered();
2380 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002381
2382 @Override
2383 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2384 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2385 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002386
Nathan Haroldc55097a2015-03-11 18:14:50 -07002387 /*
2388 * {@hide}
2389 * Returns the IMS Registration Status
2390 */
2391 public boolean isWifiCallingEnabled() {
2392 return mPhone.isWifiCallingEnabled();
2393 }
2394
2395 /*
2396 * {@hide}
2397 * Returns the IMS Registration Status
2398 */
2399 public boolean isVolteEnabled() {
2400 return mPhone.isVolteEnabled();
2401 }
Svet Ganovb320e182015-04-16 12:30:10 -07002402
2403 private boolean canReadPhoneState(String callingPackage, String message) {
2404 mApp.enforceCallingOrSelfPermission(
2405 android.Manifest.permission.READ_PHONE_STATE, message);
2406
2407 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2408 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2409 return false;
2410 }
2411
2412 return true;
2413 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002414
2415 @Override
2416 public void factoryReset(int subId) {
2417 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002418 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2419 return;
2420 }
2421
Svet Ganovcc087f82015-05-12 20:35:54 -07002422 final long identity = Binder.clearCallingIdentity();
2423 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002424 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2425 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002426 // Enable data
2427 setDataEnabled(subId, true);
2428 // Set network selection mode to automatic
2429 setNetworkSelectionModeAutomatic(subId);
2430 // Set preferred mobile network type to the best available
2431 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2432 // Turn off roaming
2433 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2434 }
2435 } finally {
2436 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002437 }
2438 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002439
2440 @Override
2441 public String getLocaleFromDefaultSim() {
2442 // We query all subscriptions instead of just the active ones, because
2443 // this might be called early on in the provisioning flow when the
2444 // subscriptions potentially aren't active yet.
2445 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2446 if (slist == null || slist.isEmpty()) {
2447 return null;
2448 }
2449
2450 // This function may be called very early, say, from the setup wizard, at
2451 // which point we won't have a default subscription set. If that's the case
2452 // we just choose the first, which will be valid in "most cases".
2453 final int defaultSubId = getDefaultSubscription();
2454 SubscriptionInfo info = null;
2455 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2456 info = slist.get(0);
2457 } else {
2458 for (SubscriptionInfo item : slist) {
2459 if (item.getSubscriptionId() == defaultSubId) {
2460 info = item;
2461 break;
2462 }
2463 }
2464
2465 if (info == null) {
2466 return null;
2467 }
2468 }
2469
2470 // Try and fetch the locale from the carrier properties or from the SIM language
2471 // preferences (EF-PL and EF-LI)...
2472 final Phone defaultPhone = getPhone(info.getSubscriptionId());
2473 if (defaultPhone != null) {
2474 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2475 if (localeFromDefaultSim != null) {
2476 return localeFromDefaultSim.toLanguageTag();
2477 }
2478 }
2479
2480 // .. if that doesn't work, try and guess the language from the sim MCC.
2481 final int mcc = info.getMcc();
2482 final Locale locale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc);
2483 if (locale != null) {
2484 return locale.toLanguageTag();
2485 }
2486
2487 return null;
2488 }
2489
2490 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2491 final long identity = Binder.clearCallingIdentity();
2492 try {
2493 return mSubscriptionController.getAllSubInfoList(
2494 mPhone.getContext().getOpPackageName());
2495 } finally {
2496 Binder.restoreCallingIdentity(identity);
2497 }
2498 }
2499
2500 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2501 final long identity = Binder.clearCallingIdentity();
2502 try {
2503 return mSubscriptionController.getActiveSubscriptionInfoList(
2504 mPhone.getContext().getOpPackageName());
2505 } finally {
2506 Binder.restoreCallingIdentity(identity);
2507 }
2508 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002509
2510 /**
2511 * {@hide}
2512 * Returns the modem stats
2513 */
2514 @Override
2515 public ModemActivityInfo getModemActivityInfo() {
2516 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2517 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002518}