blob: 2cbafe85a50c5309125bac7c3c1cba10d873a9c6 [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;
Shishir Agrawal302c8692015-06-19 13:49:39 -070060import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070061import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080064import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010065import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070068import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070069import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070071import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080072import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.IccIoResult;
74import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070075import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080077import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078
Wink Saville36469e72014-06-11 15:17:00 -070079import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
80
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080082import java.util.Arrays;
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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086
87/**
88 * Implementation of the ITelephony interface.
89 */
Santos Cordon117fee72014-05-16 17:56:12 -070090public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091 private static final String LOG_TAG = "PhoneInterfaceManager";
92 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
93 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080094 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095
96 // Message codes used with mMainThreadHandler
97 private static final int CMD_HANDLE_PIN_MMI = 1;
98 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
99 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
100 private static final int CMD_ANSWER_RINGING_CALL = 4;
101 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700102 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
103 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700104 private static final int CMD_OPEN_CHANNEL = 9;
105 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
106 private static final int CMD_CLOSE_CHANNEL = 11;
107 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800108 private static final int CMD_NV_READ_ITEM = 13;
109 private static final int EVENT_NV_READ_ITEM_DONE = 14;
110 private static final int CMD_NV_WRITE_ITEM = 15;
111 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
112 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
113 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
114 private static final int CMD_NV_RESET_CONFIG = 19;
115 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800116 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
117 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
118 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
119 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800120 private static final int CMD_SEND_ENVELOPE = 25;
121 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700122 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
123 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
124 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
125 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
126 private static final int CMD_EXCHANGE_SIM_IO = 31;
127 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800128 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
129 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700130 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
131 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700132 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
133 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700134 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
135 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
136 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
137 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138
139 /** The singleton instance. */
140 private static PhoneInterfaceManager sInstance;
141
Wink Saville3ab207e2014-11-20 13:07:20 -0800142 private PhoneGlobals mApp;
143 private Phone mPhone;
144 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700145 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800146 private AppOpsManager mAppOps;
147 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800148 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800149 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150
Derek Tan97ebb422014-09-05 16:55:38 -0700151 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
152 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800153 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700154 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700155
156 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700157 * A request object to use for transmitting data to an ICC.
158 */
159 private static final class IccAPDUArgument {
160 public int channel, cla, command, p1, p2, p3;
161 public String data;
162
163 public IccAPDUArgument(int channel, int cla, int command,
164 int p1, int p2, int p3, String data) {
165 this.channel = channel;
166 this.cla = cla;
167 this.command = command;
168 this.p1 = p1;
169 this.p2 = p2;
170 this.p3 = p3;
171 this.data = data;
172 }
173 }
174
175 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700176 * A request object to use for transmitting data to an ICC.
177 */
178 private static final class ManualNetworkSelectionArgument {
179 public OperatorInfo operatorInfo;
180 public boolean persistSelection;
181
182 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
183 this.operatorInfo = operatorInfo;
184 this.persistSelection = persistSelection;
185 }
186 }
187
188 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
190 * request after sending. The main thread will notify the request when it is complete.
191 */
192 private static final class MainThreadRequest {
193 /** The argument to use for the request */
194 public Object argument;
195 /** The result of the request that is run on the main thread */
196 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800197 // The subscriber id that this request applies to. Defaults to
198 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
199 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
201 public MainThreadRequest(Object argument) {
202 this.argument = argument;
203 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800204
205 public MainThreadRequest(Object argument, Integer subId) {
206 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800207 if (subId != null) {
208 this.subId = subId;
209 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 }
212
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800213 private static final class IncomingThirdPartyCallArgs {
214 public final ComponentName component;
215 public final String callId;
216 public final String callerDisplayName;
217
218 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
219 String callerDisplayName) {
220 this.component = component;
221 this.callId = callId;
222 this.callerDisplayName = callerDisplayName;
223 }
224 }
225
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 /**
227 * A handler that processes messages on the main thread in the phone process. Since many
228 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
229 * inbound binder threads to the main thread in the phone process. The Binder thread
230 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
231 * on, which will be notified when the operation completes and will contain the result of the
232 * request.
233 *
234 * <p>If a MainThreadRequest object is provided in the msg.obj field,
235 * note that request.result must be set to something non-null for the calling thread to
236 * unblock.
237 */
238 private final class MainThreadHandler extends Handler {
239 @Override
240 public void handleMessage(Message msg) {
241 MainThreadRequest request;
242 Message onCompleted;
243 AsyncResult ar;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800244 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700245 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246
247 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700248 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700250 final Phone phone = getPhoneFromRequest(request);
251 request.result = phone != null ?
252 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
253 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 // Wake up the requesting thread
255 synchronized (request) {
256 request.notifyAll();
257 }
258 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260
261 case CMD_HANDLE_NEIGHBORING_CELL:
262 request = (MainThreadRequest) msg.obj;
263 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
264 request);
265 mPhone.getNeighboringCids(onCompleted);
266 break;
267
268 case EVENT_NEIGHBORING_CELL_DONE:
269 ar = (AsyncResult) msg.obj;
270 request = (MainThreadRequest) ar.userObj;
271 if (ar.exception == null && ar.result != null) {
272 request.result = ar.result;
273 } else {
274 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800275 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 }
277 // Wake up the requesting thread
278 synchronized (request) {
279 request.notifyAll();
280 }
281 break;
282
283 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700284 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800285 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700286 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 break;
288
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 case CMD_END_CALL:
290 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800291 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700292 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700293 Phone phone = getPhone(end_subId);
294 if (phone == null) {
295 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
296 break;
297 }
298 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
300 // CDMA: If the user presses the Power button we treat it as
301 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700302 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
304 // GSM: End the call as per the Phone state
305 hungUp = PhoneUtils.hangup(mCM);
306 } else {
307 throw new IllegalStateException("Unexpected phone type: " + phoneType);
308 }
309 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
310 request.result = hungUp;
311 // Wake up the requesting thread
312 synchronized (request) {
313 request.notifyAll();
314 }
315 break;
316
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700317 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700318 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700319 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800320 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700321 if (uiccCard == null) {
322 loge("iccTransmitApduLogicalChannel: No UICC");
323 request.result = new IccIoResult(0x6F, 0, (byte[])null);
324 synchronized (request) {
325 request.notifyAll();
326 }
327 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700328 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
329 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700330 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700331 iccArgument.channel, iccArgument.cla, iccArgument.command,
332 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700333 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700334 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700335 break;
336
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700337 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700338 ar = (AsyncResult) msg.obj;
339 request = (MainThreadRequest) ar.userObj;
340 if (ar.exception == null && ar.result != null) {
341 request.result = ar.result;
342 } else {
343 request.result = new IccIoResult(0x6F, 0, (byte[])null);
344 if (ar.result == null) {
345 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800346 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700347 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800348 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700349 } else {
350 loge("iccTransmitApduLogicalChannel: Unknown exception");
351 }
352 }
353 synchronized (request) {
354 request.notifyAll();
355 }
356 break;
357
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700358 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
359 request = (MainThreadRequest) msg.obj;
360 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800361 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700362 if (uiccCard == null) {
363 loge("iccTransmitApduBasicChannel: No UICC");
364 request.result = new IccIoResult(0x6F, 0, (byte[])null);
365 synchronized (request) {
366 request.notifyAll();
367 }
368 } else {
369 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
370 request);
371 uiccCard.iccTransmitApduBasicChannel(
372 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
373 iccArgument.p3, iccArgument.data, onCompleted);
374 }
375 break;
376
377 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
378 ar = (AsyncResult) msg.obj;
379 request = (MainThreadRequest) ar.userObj;
380 if (ar.exception == null && ar.result != null) {
381 request.result = ar.result;
382 } else {
383 request.result = new IccIoResult(0x6F, 0, (byte[])null);
384 if (ar.result == null) {
385 loge("iccTransmitApduBasicChannel: Empty response");
386 } else if (ar.exception instanceof CommandException) {
387 loge("iccTransmitApduBasicChannel: CommandException: " +
388 ar.exception);
389 } else {
390 loge("iccTransmitApduBasicChannel: Unknown exception");
391 }
392 }
393 synchronized (request) {
394 request.notifyAll();
395 }
396 break;
397
398 case CMD_EXCHANGE_SIM_IO:
399 request = (MainThreadRequest) msg.obj;
400 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800401 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700402 if (uiccCard == null) {
403 loge("iccExchangeSimIO: No UICC");
404 request.result = new IccIoResult(0x6F, 0, (byte[])null);
405 synchronized (request) {
406 request.notifyAll();
407 }
408 } else {
409 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
410 request);
411 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
412 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
413 iccArgument.data, onCompleted);
414 }
415 break;
416
417 case EVENT_EXCHANGE_SIM_IO_DONE:
418 ar = (AsyncResult) msg.obj;
419 request = (MainThreadRequest) ar.userObj;
420 if (ar.exception == null && ar.result != null) {
421 request.result = ar.result;
422 } else {
423 request.result = new IccIoResult(0x6f, 0, (byte[])null);
424 }
425 synchronized (request) {
426 request.notifyAll();
427 }
428 break;
429
Derek Tan4d5e5c12014-02-04 11:54:58 -0800430 case CMD_SEND_ENVELOPE:
431 request = (MainThreadRequest) msg.obj;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800432 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700433 if (uiccCard == null) {
434 loge("sendEnvelopeWithStatus: No UICC");
435 request.result = new IccIoResult(0x6F, 0, (byte[])null);
436 synchronized (request) {
437 request.notifyAll();
438 }
439 } else {
440 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
441 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
442 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800443 break;
444
445 case EVENT_SEND_ENVELOPE_DONE:
446 ar = (AsyncResult) msg.obj;
447 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700448 if (ar.exception == null && ar.result != null) {
449 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800450 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700451 request.result = new IccIoResult(0x6F, 0, (byte[])null);
452 if (ar.result == null) {
453 loge("sendEnvelopeWithStatus: Empty response");
454 } else if (ar.exception instanceof CommandException) {
455 loge("sendEnvelopeWithStatus: CommandException: " +
456 ar.exception);
457 } else {
458 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
459 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800460 }
461 synchronized (request) {
462 request.notifyAll();
463 }
464 break;
465
Shishir Agrawal566b7612013-10-28 14:41:00 -0700466 case CMD_OPEN_CHANNEL:
467 request = (MainThreadRequest) msg.obj;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800468 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700469 if (uiccCard == null) {
470 loge("iccOpenLogicalChannel: No UICC");
471 request.result = new IccIoResult(0x6F, 0, (byte[])null);
472 synchronized (request) {
473 request.notifyAll();
474 }
475 } else {
476 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
477 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
478 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700479 break;
480
481 case EVENT_OPEN_CHANNEL_DONE:
482 ar = (AsyncResult) msg.obj;
483 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700484 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700486 int[] result = (int[]) ar.result;
487 int channelId = result[0];
488 byte[] selectResponse = null;
489 if (result.length > 1) {
490 selectResponse = new byte[result.length - 1];
491 for (int i = 1; i < result.length; ++i) {
492 selectResponse[i - 1] = (byte) result[i];
493 }
494 }
495 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700496 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700497 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700498 if (ar.result == null) {
499 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700500 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700501 if (ar.exception != null) {
502 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
503 }
504
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700505 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700506 if (ar.exception instanceof CommandException) {
507 CommandException.Error error =
508 ((CommandException) (ar.exception)).getCommandError();
509 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700510 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700511 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700512 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700513 }
514 }
515 openChannelResp = new IccOpenLogicalChannelResponse(
516 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700518 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 synchronized (request) {
520 request.notifyAll();
521 }
522 break;
523
524 case CMD_CLOSE_CHANNEL:
525 request = (MainThreadRequest) msg.obj;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800526 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700527 if (uiccCard == null) {
528 loge("iccCloseLogicalChannel: No UICC");
529 request.result = new IccIoResult(0x6F, 0, (byte[])null);
530 synchronized (request) {
531 request.notifyAll();
532 }
533 } else {
534 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
535 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
536 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 break;
538
539 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800540 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
541 break;
542
543 case CMD_NV_READ_ITEM:
544 request = (MainThreadRequest) msg.obj;
545 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
546 mPhone.nvReadItem((Integer) request.argument, onCompleted);
547 break;
548
549 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 ar = (AsyncResult) msg.obj;
551 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800552 if (ar.exception == null && ar.result != null) {
553 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700554 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800555 request.result = "";
556 if (ar.result == null) {
557 loge("nvReadItem: Empty response");
558 } else if (ar.exception instanceof CommandException) {
559 loge("nvReadItem: CommandException: " +
560 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700561 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800562 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700563 }
564 }
565 synchronized (request) {
566 request.notifyAll();
567 }
568 break;
569
Jake Hambye994d462014-02-03 13:10:13 -0800570 case CMD_NV_WRITE_ITEM:
571 request = (MainThreadRequest) msg.obj;
572 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
573 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
574 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
575 break;
576
577 case EVENT_NV_WRITE_ITEM_DONE:
578 handleNullReturnEvent(msg, "nvWriteItem");
579 break;
580
581 case CMD_NV_WRITE_CDMA_PRL:
582 request = (MainThreadRequest) msg.obj;
583 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
584 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
585 break;
586
587 case EVENT_NV_WRITE_CDMA_PRL_DONE:
588 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
589 break;
590
591 case CMD_NV_RESET_CONFIG:
592 request = (MainThreadRequest) msg.obj;
593 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
594 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
595 break;
596
597 case EVENT_NV_RESET_CONFIG_DONE:
598 handleNullReturnEvent(msg, "nvResetConfig");
599 break;
600
Jake Hamby7c27be32014-03-03 13:25:59 -0800601 case CMD_GET_PREFERRED_NETWORK_TYPE:
602 request = (MainThreadRequest) msg.obj;
603 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700604 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800605 break;
606
607 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
608 ar = (AsyncResult) msg.obj;
609 request = (MainThreadRequest) ar.userObj;
610 if (ar.exception == null && ar.result != null) {
611 request.result = ar.result; // Integer
612 } else {
613 request.result = -1;
614 if (ar.result == null) {
615 loge("getPreferredNetworkType: Empty response");
616 } else if (ar.exception instanceof CommandException) {
617 loge("getPreferredNetworkType: CommandException: " +
618 ar.exception);
619 } else {
620 loge("getPreferredNetworkType: Unknown exception");
621 }
622 }
623 synchronized (request) {
624 request.notifyAll();
625 }
626 break;
627
628 case CMD_SET_PREFERRED_NETWORK_TYPE:
629 request = (MainThreadRequest) msg.obj;
630 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
631 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700632 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800633 break;
634
635 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
636 handleNullReturnEvent(msg, "setPreferredNetworkType");
637 break;
638
Steven Liu4bf01bc2014-07-17 11:05:29 -0500639 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
640 request = (MainThreadRequest)msg.obj;
641 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
642 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
643 break;
644
645 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
646 ar = (AsyncResult)msg.obj;
647 request = (MainThreadRequest)ar.userObj;
648 request.result = ar;
649 synchronized (request) {
650 request.notifyAll();
651 }
652 break;
653
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800654 case CMD_SET_VOICEMAIL_NUMBER:
655 request = (MainThreadRequest) msg.obj;
656 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
657 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800658 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
659 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800660 break;
661
662 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
663 handleNullReturnEvent(msg, "setVoicemailNumber");
664 break;
665
Stuart Scott54788802015-03-30 13:18:01 -0700666 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
667 request = (MainThreadRequest) msg.obj;
668 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
669 request);
670 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
671 break;
672
673 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
674 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
675 break;
676
Shishir Agrawal302c8692015-06-19 13:49:39 -0700677 case CMD_PERFORM_NETWORK_SCAN:
678 request = (MainThreadRequest) msg.obj;
679 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
680 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
681 break;
682
683 case EVENT_PERFORM_NETWORK_SCAN_DONE:
684 ar = (AsyncResult) msg.obj;
685 request = (MainThreadRequest) ar.userObj;
686 CellNetworkScanResult cellScanResult;
687 if (ar.exception == null && ar.result != null) {
688 cellScanResult = new CellNetworkScanResult(
689 CellNetworkScanResult.STATUS_SUCCESS,
690 (List<OperatorInfo>) ar.result);
691 } else {
692 if (ar.result == null) {
693 loge("getCellNetworkScanResults: Empty response");
694 }
695 if (ar.exception != null) {
696 loge("getCellNetworkScanResults: Exception: " + ar.exception);
697 }
698 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
699 if (ar.exception instanceof CommandException) {
700 CommandException.Error error =
701 ((CommandException) (ar.exception)).getCommandError();
702 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
703 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
704 } else if (error == CommandException.Error.GENERIC_FAILURE) {
705 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
706 }
707 }
708 cellScanResult = new CellNetworkScanResult(errorCode, null);
709 }
710 request.result = cellScanResult;
711 synchronized (request) {
712 request.notifyAll();
713 }
714 break;
715
716 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
717 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700718 ManualNetworkSelectionArgument selArg =
719 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700720 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
721 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700722 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
723 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700724 break;
725
726 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
727 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
728 break;
729
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700730 case CMD_GET_MODEM_ACTIVITY_INFO:
731 request = (MainThreadRequest) msg.obj;
732 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700733 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700734 break;
735
736 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
737 ar = (AsyncResult) msg.obj;
738 request = (MainThreadRequest) ar.userObj;
739 if (ar.exception == null && ar.result != null) {
740 request.result = ar.result;
741 } else {
742 if (ar.result == null) {
743 loge("queryModemActivityInfo: Empty response");
744 } else if (ar.exception instanceof CommandException) {
745 loge("queryModemActivityInfo: CommandException: " +
746 ar.exception);
747 } else {
748 loge("queryModemActivityInfo: Unknown exception");
749 }
750 }
Amit Mahajand4766222016-01-28 15:28:28 -0800751 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
752 if (request.result == null) {
753 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
754 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700755 synchronized (request) {
756 request.notifyAll();
757 }
758 break;
759
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700760 default:
761 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
762 break;
763 }
764 }
Jake Hambye994d462014-02-03 13:10:13 -0800765
766 private void handleNullReturnEvent(Message msg, String command) {
767 AsyncResult ar = (AsyncResult) msg.obj;
768 MainThreadRequest request = (MainThreadRequest) ar.userObj;
769 if (ar.exception == null) {
770 request.result = true;
771 } else {
772 request.result = false;
773 if (ar.exception instanceof CommandException) {
774 loge(command + ": CommandException: " + ar.exception);
775 } else {
776 loge(command + ": Unknown exception");
777 }
778 }
779 synchronized (request) {
780 request.notifyAll();
781 }
782 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700783 }
784
785 /**
786 * Posts the specified command to be executed on the main thread,
787 * waits for the request to complete, and returns the result.
788 * @see #sendRequestAsync
789 */
790 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800791 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700792 }
793
794 /**
795 * Posts the specified command to be executed on the main thread,
796 * waits for the request to complete, and returns the result.
797 * @see #sendRequestAsync
798 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800799 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
801 throw new RuntimeException("This method will deadlock if called from the main thread.");
802 }
803
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800804 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 Message msg = mMainThreadHandler.obtainMessage(command, request);
806 msg.sendToTarget();
807
808 // Wait for the request to complete
809 synchronized (request) {
810 while (request.result == null) {
811 try {
812 request.wait();
813 } catch (InterruptedException e) {
814 // Do nothing, go back and wait until the request is complete
815 }
816 }
817 }
818 return request.result;
819 }
820
821 /**
822 * Asynchronous ("fire and forget") version of sendRequest():
823 * Posts the specified command to be executed on the main thread, and
824 * returns immediately.
825 * @see #sendRequest
826 */
827 private void sendRequestAsync(int command) {
828 mMainThreadHandler.sendEmptyMessage(command);
829 }
830
831 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700832 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
833 * @see {@link #sendRequest(int,Object)}
834 */
835 private void sendRequestAsync(int command, Object argument) {
836 MainThreadRequest request = new MainThreadRequest(argument);
837 Message msg = mMainThreadHandler.obtainMessage(command, request);
838 msg.sendToTarget();
839 }
840
841 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 * Initialize the singleton PhoneInterfaceManager instance.
843 * This is only done once, at startup, from PhoneApp.onCreate().
844 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700845 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 synchronized (PhoneInterfaceManager.class) {
847 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700848 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 } else {
850 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
851 }
852 return sInstance;
853 }
854 }
855
856 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700857 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 mApp = app;
859 mPhone = phone;
860 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700861 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700862 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
863 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700864 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700865 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800866 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800867
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700868 publish();
869 }
870
871 private void publish() {
872 if (DBG) log("publish: " + this);
873
874 ServiceManager.addService("phone", this);
875 }
876
Stuart Scott584921c2015-01-15 17:10:34 -0800877 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800878 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
879 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800880 }
881
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800882 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
883 Phone phone = getPhoneFromRequest(request);
884 return phone == null ? null :
885 UiccController.getInstance().getUiccCard(phone.getPhoneId());
886 }
887
Wink Saville36469e72014-06-11 15:17:00 -0700888 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700889 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800890 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700891 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 //
893 // Implementation of the ITelephony interface.
894 //
895
896 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700897 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700898 }
899
Wink Savilleb564aae2014-10-23 10:18:09 -0700900 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 if (DBG) log("dial: " + number);
902 // No permission check needed here: This is just a wrapper around the
903 // ACTION_DIAL intent, which is available to any app since it puts up
904 // the UI before it does anything.
905
906 String url = createTelUrl(number);
907 if (url == null) {
908 return;
909 }
910
911 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700912 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
914 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
915 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
916 mApp.startActivity(intent);
917 }
918 }
919
920 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700921 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700922 }
923
Wink Savilleb564aae2014-10-23 10:18:09 -0700924 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700925 if (DBG) log("call: " + number);
926
927 // This is just a wrapper around the ACTION_CALL intent, but we still
928 // need to do a permission check since we're calling startActivity()
929 // from the context of the phone app.
930 enforceCallPermission();
931
932 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
933 != AppOpsManager.MODE_ALLOWED) {
934 return;
935 }
936
937 String url = createTelUrl(number);
938 if (url == null) {
939 return;
940 }
941
Wink Saville08874612014-08-31 19:19:58 -0700942 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100943 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800944 if (slist != null) {
945 for (SubscriptionInfo subInfoRecord : slist) {
946 if (subInfoRecord.getSubscriptionId() == subId) {
947 isValid = true;
948 break;
949 }
Wink Saville08874612014-08-31 19:19:58 -0700950 }
951 }
952 if (isValid == false) {
953 return;
954 }
955
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700957 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
959 mApp.startActivity(intent);
960 }
961
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 /**
963 * End a call based on call state
964 * @return true is a call was ended
965 */
966 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700967 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700968 }
969
970 /**
971 * End a call based on the call state of the subId
972 * @return true is a call was ended
973 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700974 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800976 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700977 }
978
979 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700980 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700981 }
982
Wink Savilleb564aae2014-10-23 10:18:09 -0700983 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984 if (DBG) log("answerRingingCall...");
985 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
986 // but that can probably wait till the big TelephonyManager API overhaul.
987 // For now, protect this call with the MODIFY_PHONE_STATE permission.
988 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800989 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 }
991
992 /**
993 * Make the actual telephony calls to implement answerRingingCall().
994 * This should only be called from the main thread of the Phone app.
995 * @see #answerRingingCall
996 *
997 * TODO: it would be nice to return true if we answered the call, or
998 * false if there wasn't actually a ringing incoming call, or some
999 * other error occurred. (In other words, pass back the return value
1000 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1001 * But that would require calling this method via sendRequest() rather
1002 * than sendRequestAsync(), and right now we don't actually *need* that
1003 * return value, so let's just return void for now.
1004 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001005 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001006 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001008 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1009 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 if (hasActiveCall && hasHoldingCall) {
1011 // Both lines are in use!
1012 // TODO: provide a flag to let the caller specify what
1013 // policy to use if both lines are in use. (The current
1014 // behavior is hardwired to "answer incoming, end ongoing",
1015 // which is how the CALL button is specced to behave.)
1016 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1017 return;
1018 } else {
1019 // answerCall() will automatically hold the current active
1020 // call, if there is one.
1021 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1022 return;
1023 }
1024 } else {
1025 // No call was ringing.
1026 return;
1027 }
1028 }
1029
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001031 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001033 public void silenceRinger() {
1034 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 }
1036
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001037 @Override
1038 public boolean isOffhook(String callingPackage) {
1039 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001040 }
1041
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001042 @Override
1043 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1044 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1045 return false;
1046 }
1047
Sanket Padawe356d7632015-06-22 14:03:32 -07001048 final Phone phone = getPhone(subId);
1049 if (phone != null) {
1050 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1051 } else {
1052 return false;
1053 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 }
1055
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001056 @Override
1057 public boolean isRinging(String callingPackage) {
1058 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001059 }
1060
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001061 @Override
1062 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1063 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1064 return false;
1065 }
1066
Sanket Padawe356d7632015-06-22 14:03:32 -07001067 final Phone phone = getPhone(subId);
1068 if (phone != null) {
1069 return (phone.getState() == PhoneConstants.State.RINGING);
1070 } else {
1071 return false;
1072 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 }
1074
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001075 @Override
1076 public boolean isIdle(String callingPackage) {
1077 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001078 }
1079
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001080 @Override
1081 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1082 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1083 return false;
1084 }
1085
Sanket Padawe356d7632015-06-22 14:03:32 -07001086 final Phone phone = getPhone(subId);
1087 if (phone != null) {
1088 return (phone.getState() == PhoneConstants.State.IDLE);
1089 } else {
1090 return false;
1091 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 }
1093
Svet Ganovb320e182015-04-16 12:30:10 -07001094 public boolean isSimPinEnabled(String callingPackage) {
1095 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1096 return false;
1097 }
1098
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 return (PhoneGlobals.getInstance().isSimPinEnabled());
1100 }
1101
1102 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001103 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001104 }
1105
Wink Savilleb564aae2014-10-23 10:18:09 -07001106 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001107 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001108 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1109 }
1110
1111 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001112 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001113 }
1114
Wink Savilleb564aae2014-10-23 10:18:09 -07001115 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001116 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001117 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1118 }
1119
1120 /** {@hide} */
1121 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001122 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001123 }
1124
Wink Savilleb564aae2014-10-23 10:18:09 -07001125 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001127 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 checkSimPin.start();
1129 return checkSimPin.unlockSim(null, pin);
1130 }
1131
Wink Saville9de0f752013-10-22 19:04:03 -07001132 /** {@hide} */
1133 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001134 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001135 }
1136
Wink Savilleb564aae2014-10-23 10:18:09 -07001137 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001138 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001139 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140 checkSimPuk.start();
1141 return checkSimPuk.unlockSim(puk, pin);
1142 }
1143
1144 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001145 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001146 * a synchronous one.
1147 */
1148 private static class UnlockSim extends Thread {
1149
1150 private final IccCard mSimCard;
1151
1152 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001153 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1154 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001155
1156 // For replies from SimCard interface
1157 private Handler mHandler;
1158
1159 // For async handler to identify request type
1160 private static final int SUPPLY_PIN_COMPLETE = 100;
1161
1162 public UnlockSim(IccCard simCard) {
1163 mSimCard = simCard;
1164 }
1165
1166 @Override
1167 public void run() {
1168 Looper.prepare();
1169 synchronized (UnlockSim.this) {
1170 mHandler = new Handler() {
1171 @Override
1172 public void handleMessage(Message msg) {
1173 AsyncResult ar = (AsyncResult) msg.obj;
1174 switch (msg.what) {
1175 case SUPPLY_PIN_COMPLETE:
1176 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1177 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001178 mRetryCount = msg.arg1;
1179 if (ar.exception != null) {
1180 if (ar.exception instanceof CommandException &&
1181 ((CommandException)(ar.exception)).getCommandError()
1182 == CommandException.Error.PASSWORD_INCORRECT) {
1183 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1184 } else {
1185 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1186 }
1187 } else {
1188 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1189 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 mDone = true;
1191 UnlockSim.this.notifyAll();
1192 }
1193 break;
1194 }
1195 }
1196 };
1197 UnlockSim.this.notifyAll();
1198 }
1199 Looper.loop();
1200 }
1201
1202 /*
1203 * Use PIN or PUK to unlock SIM card
1204 *
1205 * If PUK is null, unlock SIM card with PIN
1206 *
1207 * If PUK is not null, unlock SIM card with PUK and set PIN code
1208 */
Wink Saville9de0f752013-10-22 19:04:03 -07001209 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001210
1211 while (mHandler == null) {
1212 try {
1213 wait();
1214 } catch (InterruptedException e) {
1215 Thread.currentThread().interrupt();
1216 }
1217 }
1218 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1219
1220 if (puk == null) {
1221 mSimCard.supplyPin(pin, callback);
1222 } else {
1223 mSimCard.supplyPuk(puk, pin, callback);
1224 }
1225
1226 while (!mDone) {
1227 try {
1228 Log.d(LOG_TAG, "wait for done");
1229 wait();
1230 } catch (InterruptedException e) {
1231 // Restore the interrupted status
1232 Thread.currentThread().interrupt();
1233 }
1234 }
1235 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001236 int[] resultArray = new int[2];
1237 resultArray[0] = mResult;
1238 resultArray[1] = mRetryCount;
1239 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 }
1241 }
1242
1243 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001244 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001245
1246 }
1247
Wink Savilleb564aae2014-10-23 10:18:09 -07001248 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001249 // No permission check needed here: this call is harmless, and it's
1250 // needed for the ServiceState.requestStateUpdate() call (which is
1251 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001252 final Phone phone = getPhone(subId);
1253 if (phone != null) {
1254 phone.updateServiceLocation();
1255 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 }
1257
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001258 @Override
1259 public boolean isRadioOn(String callingPackage) {
1260 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001261 }
1262
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001263 @Override
1264 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1265 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1266 return false;
1267 }
1268 return isRadioOnForSubscriber(subId);
1269 }
1270
1271 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001272 final Phone phone = getPhone(subId);
1273 if (phone != null) {
1274 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1275 } else {
1276 return false;
1277 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 }
1279
1280 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001281 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001282
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 }
Wink Saville36469e72014-06-11 15:17:00 -07001284
Wink Savilleb564aae2014-10-23 10:18:09 -07001285 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001287 final Phone phone = getPhone(subId);
1288 if (phone != null) {
1289 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1290 }
Wink Saville36469e72014-06-11 15:17:00 -07001291 }
1292
1293 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001294 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001295 }
1296
Wink Savilleb564aae2014-10-23 10:18:09 -07001297 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001298 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001299 final Phone phone = getPhone(subId);
1300 if (phone == null) {
1301 return false;
1302 }
1303 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001304 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001305 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 }
1307 return true;
1308 }
Wink Saville36469e72014-06-11 15:17:00 -07001309
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001310 public boolean needMobileRadioShutdown() {
1311 /*
1312 * If any of the Radios are available, it will need to be
1313 * shutdown. So return true if any Radio is available.
1314 */
1315 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1316 Phone phone = PhoneFactory.getPhone(i);
1317 if (phone != null && phone.isRadioAvailable()) return true;
1318 }
1319 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1320 return false;
1321 }
1322
1323 public void shutdownMobileRadios() {
1324 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1325 logv("Shutting down Phone " + i);
1326 shutdownRadioUsingPhoneId(i);
1327 }
1328 }
1329
1330 private void shutdownRadioUsingPhoneId(int phoneId) {
1331 enforceModifyPermission();
1332 Phone phone = PhoneFactory.getPhone(phoneId);
1333 if (phone != null && phone.isRadioAvailable()) {
1334 phone.shutdownRadio();
1335 }
1336 }
1337
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001338 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001339 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001340 }
1341
Wink Savilleb564aae2014-10-23 10:18:09 -07001342 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001344 final Phone phone = getPhone(subId);
1345 if (phone != null) {
1346 phone.setRadioPower(turnOn);
1347 return true;
1348 } else {
1349 return false;
1350 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 }
1352
Wink Saville36469e72014-06-11 15:17:00 -07001353 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001354 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 public boolean enableDataConnectivity() {
1356 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001357 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001358 final Phone phone = getPhone(subId);
1359 if (phone != null) {
1360 phone.setDataEnabled(true);
1361 return true;
1362 } else {
1363 return false;
1364 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365 }
1366
Wink Saville36469e72014-06-11 15:17:00 -07001367 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001368 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001369 public boolean disableDataConnectivity() {
1370 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001371 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001372 final Phone phone = getPhone(subId);
1373 if (phone != null) {
1374 phone.setDataEnabled(false);
1375 return true;
1376 } else {
1377 return false;
1378 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 }
1380
Wink Saville36469e72014-06-11 15:17:00 -07001381 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001382 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001383 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001384 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001385 final Phone phone = getPhone(subId);
1386 if (phone != null) {
1387 return phone.isDataConnectivityPossible();
1388 } else {
1389 return false;
1390 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 }
1392
1393 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001394 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001395 }
1396
Wink Savilleb564aae2014-10-23 10:18:09 -07001397 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001399 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1400 return false;
1401 }
Wink Saville36469e72014-06-11 15:17:00 -07001402 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 }
1404
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001406 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001407 }
1408
Wink Savilleb564aae2014-10-23 10:18:09 -07001409 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001410 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 }
1412
Sanket Padawe356d7632015-06-22 14:03:32 -07001413 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001415 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001416 if (phone != null) {
1417 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1418 } else {
1419 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1420 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001421 }
1422
Sanket Padawe356d7632015-06-22 14:03:32 -07001423 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001425 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001426 if (phone != null) {
1427 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1428 } else {
1429 return TelephonyManager.DATA_ACTIVITY_NONE;
1430 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431 }
1432
1433 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001434 public Bundle getCellLocation(String callingPackage) {
1435 enforceFineOrCoarseLocationPermission("getCellLocation");
1436
1437 // OP_COARSE_LOCATION controls both fine and coarse location.
1438 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1439 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001440 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001441 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 }
1443
Jake Hambye994d462014-02-03 13:10:13 -08001444 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 if (DBG_LOC) log("getCellLocation: is active user");
1446 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001447 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001448 if (phone == null) {
1449 return null;
1450 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001451 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001452 return data;
1453 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001454 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 return null;
1456 }
1457 }
1458
Svetoslav64fad262015-04-14 14:35:21 -07001459 private void enforceFineOrCoarseLocationPermission(String message) {
1460 try {
1461 mApp.enforceCallingOrSelfPermission(
1462 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1463 } catch (SecurityException e) {
1464 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1465 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1466 // is the weaker precondition
1467 mApp.enforceCallingOrSelfPermission(
1468 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1469 }
1470 }
1471
1472
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001473 @Override
1474 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001475 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001476 }
1477
Sanket Padawe356d7632015-06-22 14:03:32 -07001478 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001479 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 mApp.enforceCallingOrSelfPermission(
1481 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001482 final Phone phone = getPhone(subId);
1483 if (phone != null) {
1484 phone.enableLocationUpdates();
1485 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001486 }
1487
1488 @Override
1489 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001490 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001491 }
1492
Sanket Padawe356d7632015-06-22 14:03:32 -07001493 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001494 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001495 mApp.enforceCallingOrSelfPermission(
1496 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001497 final Phone phone = getPhone(subId);
1498 if (phone != null) {
1499 phone.disableLocationUpdates();
1500 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 }
1502
1503 @Override
1504 @SuppressWarnings("unchecked")
1505 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001506 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1507
1508 // OP_COARSE_LOCATION controls both fine and coarse location.
1509 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1510 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1511 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001512 }
1513
1514 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1515 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1516 return null;
1517 }
Svetoslav64fad262015-04-14 14:35:21 -07001518
Jake Hambye994d462014-02-03 13:10:13 -08001519 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001520 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1521
1522 ArrayList<NeighboringCellInfo> cells = null;
1523
1524 try {
1525 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001526 CMD_HANDLE_NEIGHBORING_CELL, null,
1527 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001529 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 }
1531 return cells;
1532 } else {
1533 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1534 return null;
1535 }
1536 }
1537
1538
1539 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001540 public List<CellInfo> getAllCellInfo(String callingPackage) {
1541 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1542
1543 // OP_COARSE_LOCATION controls both fine and coarse location.
1544 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1545 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1546 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 }
1548
Jake Hambye994d462014-02-03 13:10:13 -08001549 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001551 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1552 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001553 final List<CellInfo> info = phone.getAllCellInfo();
1554 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001555 }
1556 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 } else {
1558 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1559 return null;
1560 }
1561 }
1562
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001563 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 public void setCellInfoListRate(int rateInMillis) {
1565 mPhone.setCellInfoListRate(rateInMillis);
1566 }
1567
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 //
1569 // Internal helper methods.
1570 //
1571
Jake Hambye994d462014-02-03 13:10:13 -08001572 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 boolean ok;
1574
1575 boolean self = Binder.getCallingUid() == Process.myUid();
1576 if (!self) {
1577 // Get the caller's user id then clear the calling identity
1578 // which will be restored in the finally clause.
1579 int callingUser = UserHandle.getCallingUserId();
1580 long ident = Binder.clearCallingIdentity();
1581
1582 try {
1583 // With calling identity cleared the current user is the foreground user.
1584 int foregroundUser = ActivityManager.getCurrentUser();
1585 ok = (foregroundUser == callingUser);
1586 if (DBG_LOC) {
1587 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1588 + " callingUser=" + callingUser + " ok=" + ok);
1589 }
1590 } catch (Exception ex) {
1591 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1592 ok = false;
1593 } finally {
1594 Binder.restoreCallingIdentity(ident);
1595 }
1596 } else {
1597 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1598 ok = true;
1599 }
1600 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1601 return ok;
1602 }
1603
1604 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1606 *
1607 * @throws SecurityException if the caller does not have the required permission
1608 */
1609 private void enforceModifyPermission() {
1610 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1611 }
1612
1613 /**
Junda Liua2e36012014-07-09 18:30:01 -07001614 * Make sure either system app or the caller has carrier privilege.
1615 *
1616 * @throws SecurityException if the caller does not have the required permission/privilege
1617 */
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001618 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001619 int permission = mApp.checkCallingOrSelfPermission(
1620 android.Manifest.permission.MODIFY_PHONE_STATE);
1621 if (permission == PackageManager.PERMISSION_GRANTED) {
1622 return;
1623 }
1624
1625 log("No modify permission, check carrier privilege next.");
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001626 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001627 }
1628
1629 /**
1630 * Make sure the caller has carrier privilege.
1631 *
1632 * @throws SecurityException if the caller does not have the required permission
1633 */
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001634 private void enforceCarrierPrivilege(int subId) {
1635 if (getCarrierPrivilegeStatus(subId) !=
1636 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001637 loge("No Carrier Privilege.");
1638 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001639 }
1640 }
1641
1642 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001643 * Make sure the caller has the CALL_PHONE permission.
1644 *
1645 * @throws SecurityException if the caller does not have the required permission
1646 */
1647 private void enforceCallPermission() {
1648 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1649 }
1650
Stuart Scott8eef64f2015-04-08 15:13:54 -07001651 private void enforceConnectivityInternalPermission() {
1652 mApp.enforceCallingOrSelfPermission(
1653 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1654 "ConnectivityService");
1655 }
1656
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 private String createTelUrl(String number) {
1658 if (TextUtils.isEmpty(number)) {
1659 return null;
1660 }
1661
Jake Hambye994d462014-02-03 13:10:13 -08001662 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 }
1664
Ihab Awadf9e92732013-12-05 18:02:52 -08001665 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1667 }
1668
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001669 private static void logv(String msg) {
1670 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1671 }
1672
Ihab Awadf9e92732013-12-05 18:02:52 -08001673 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1675 }
1676
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001677 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001679 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001680 }
1681
Sanket Padawe356d7632015-06-22 14:03:32 -07001682 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001683 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001684 final Phone phone = getPhone(subId);
1685 if (phone == null) {
1686 return PhoneConstants.PHONE_TYPE_NONE;
1687 } else {
1688 return getPhone(subId).getPhoneType();
1689 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001690 }
1691
1692 /**
1693 * Returns the CDMA ERI icon index to display
1694 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001695 @Override
1696 public int getCdmaEriIconIndex(String callingPackage) {
1697 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001698 }
1699
Sanket Padawe356d7632015-06-22 14:03:32 -07001700 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001701 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1702 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1703 return -1;
1704 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001705 final Phone phone = getPhone(subId);
1706 if (phone != null) {
1707 return phone.getCdmaEriIconIndex();
1708 } else {
1709 return -1;
1710 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001711 }
1712
1713 /**
1714 * Returns the CDMA ERI icon mode,
1715 * 0 - ON
1716 * 1 - FLASHING
1717 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001718 @Override
1719 public int getCdmaEriIconMode(String callingPackage) {
1720 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001721 }
1722
Sanket Padawe356d7632015-06-22 14:03:32 -07001723 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001724 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1725 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1726 return -1;
1727 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001728 final Phone phone = getPhone(subId);
1729 if (phone != null) {
1730 return phone.getCdmaEriIconMode();
1731 } else {
1732 return -1;
1733 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001734 }
1735
1736 /**
1737 * Returns the CDMA ERI text,
1738 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001739 @Override
1740 public String getCdmaEriText(String callingPackage) {
1741 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001742 }
1743
Sanket Padawe356d7632015-06-22 14:03:32 -07001744 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001745 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1746 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1747 return null;
1748 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001749 final Phone phone = getPhone(subId);
1750 if (phone != null) {
1751 return phone.getCdmaEriText();
1752 } else {
1753 return null;
1754 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001755 }
1756
1757 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001758 * Returns the CDMA MDN.
1759 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001760 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001761 public String getCdmaMdn(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001762 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001763 final Phone phone = getPhone(subId);
1764 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1765 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001766 } else {
1767 return null;
1768 }
1769 }
1770
1771 /**
1772 * Returns the CDMA MIN.
1773 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001774 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001775 public String getCdmaMin(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001776 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001777 final Phone phone = getPhone(subId);
1778 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1779 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001780 } else {
1781 return null;
1782 }
1783 }
1784
1785 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001786 * Returns true if CDMA provisioning needs to run.
1787 */
1788 public boolean needsOtaServiceProvisioning() {
1789 return mPhone.needsOtaServiceProvisioning();
1790 }
1791
1792 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001793 * Sets the voice mail number of a given subId.
1794 */
1795 @Override
1796 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001797 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001798 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1799 new Pair<String, String>(alphaTag, number), new Integer(subId));
1800 return success;
1801 }
1802
1803 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001804 * Returns the unread count of voicemails
1805 */
1806 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001807 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001808 }
1809
1810 /**
1811 * Returns the unread count of voicemails for a subId
1812 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001813 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001814 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001815 final Phone phone = getPhone(subId);
1816 if (phone != null) {
1817 return phone.getVoiceMessageCount();
1818 } else {
1819 return 0;
1820 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001821 }
1822
1823 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001824 * Returns the data network type.
1825 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001826 *
1827 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1828 */
1829 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001830 public int getNetworkType() {
1831 final Phone phone = getPhone(getDefaultSubscription());
1832 if (phone != null) {
1833 return phone.getServiceState().getDataNetworkType();
1834 } else {
1835 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1836 }
Wink Saville36469e72014-06-11 15:17:00 -07001837 }
1838
1839 /**
1840 * Returns the network type for a subId
1841 */
1842 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001843 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1844 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1845 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1846 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001847
Sanket Padawe356d7632015-06-22 14:03:32 -07001848 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001849 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001850 return phone.getServiceState().getDataNetworkType();
1851 } else {
1852 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1853 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001854 }
1855
1856 /**
1857 * Returns the data network type
1858 */
1859 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001860 public int getDataNetworkType(String callingPackage) {
1861 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001862 }
1863
1864 /**
1865 * Returns the data network type for a subId
1866 */
1867 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001868 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1869 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1870 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1871 }
1872
Sanket Padawe356d7632015-06-22 14:03:32 -07001873 final Phone phone = getPhone(subId);
1874 if (phone != null) {
1875 return phone.getServiceState().getDataNetworkType();
1876 } else {
1877 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1878 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001879 }
1880
1881 /**
Wink Saville36469e72014-06-11 15:17:00 -07001882 * Returns the Voice network type for a subId
1883 */
1884 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001885 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1886 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1887 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1888 }
1889
Sanket Padawe356d7632015-06-22 14:03:32 -07001890 final Phone phone = getPhone(subId);
1891 if (phone != null) {
1892 return phone.getServiceState().getVoiceNetworkType();
1893 } else {
1894 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1895 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001896 }
1897
1898 /**
1899 * @return true if a ICC card is present
1900 */
1901 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001902 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001903 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001904 }
1905
1906 /**
1907 * @return true if a ICC card is present for a slotId
1908 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001909 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001910 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001911 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001912 final Phone phone = getPhone(subId[0]);
1913 if (subId != null && phone != null) {
1914 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001915 } else {
1916 return false;
1917 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001918 }
1919
1920 /**
1921 * Return if the current radio is LTE on CDMA. This
1922 * is a tri-state return value as for a period of time
1923 * the mode may be unknown.
1924 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001925 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001926 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001927 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001928 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001929 @Override
1930 public int getLteOnCdmaMode(String callingPackage) {
1931 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001932 }
1933
Sanket Padawe356d7632015-06-22 14:03:32 -07001934 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001935 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1936 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1937 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1938 }
1939
Sanket Padawe356d7632015-06-22 14:03:32 -07001940 final Phone phone = getPhone(subId);
1941 if (phone == null) {
1942 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1943 } else {
1944 return phone.getLteOnCdmaMode();
1945 }
Wink Saville36469e72014-06-11 15:17:00 -07001946 }
1947
1948 public void setPhone(Phone phone) {
1949 mPhone = phone;
1950 }
1951
1952 /**
1953 * {@hide}
1954 * Returns Default subId, 0 in the case of single standby.
1955 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001956 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001957 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001958 }
1959
Wink Savilleb564aae2014-10-23 10:18:09 -07001960 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001961 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001962 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001963
1964 /**
1965 * @see android.telephony.TelephonyManager.WifiCallingChoices
1966 */
1967 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001968 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1969 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001970 }
1971
1972 /**
1973 * @see android.telephony.TelephonyManager.WifiCallingChoices
1974 */
1975 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001976 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1977 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1978 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001979 }
1980
Sailesh Nepald1e68152013-12-12 19:08:02 -08001981 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001982 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001983 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001984 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001985
Shishir Agrawal566b7612013-10-28 14:41:00 -07001986 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001987 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
1988 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001989
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001990 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001991 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001992 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001993 if (DBG) log("iccOpenLogicalChannel: " + response);
1994 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001995 }
1996
1997 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001998 public boolean iccCloseLogicalChannel(int subId, int channel) {
1999 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002000
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002001 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002002 if (channel < 0) {
2003 return false;
2004 }
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002005 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002006 if (DBG) log("iccCloseLogicalChannel: " + success);
2007 return success;
2008 }
2009
2010 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002011 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002012 int command, int p1, int p2, int p3, String data) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002013 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002014
2015 if (DBG) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002016 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2017 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002018 " data=" + data);
2019 }
2020
2021 if (channel < 0) {
2022 return "";
2023 }
2024
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002025 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002026 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002027 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2028
Shishir Agrawal566b7612013-10-28 14:41:00 -07002029 // Append the returned status code to the end of the response payload.
2030 String s = Integer.toHexString(
2031 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002032 if (response.payload != null) {
2033 s = IccUtils.bytesToHexString(response.payload) + s;
2034 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002035 return s;
2036 }
Jake Hambye994d462014-02-03 13:10:13 -08002037
Evan Charltonc66da362014-05-16 14:06:40 -07002038 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002039 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002040 int p3, String data) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002041 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002042
2043 if (DBG) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002044 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2045 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002046 }
2047
2048 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002049 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002050 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2051
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002052 // Append the returned status code to the end of the response payload.
2053 String s = Integer.toHexString(
2054 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002055 if (response.payload != null) {
2056 s = IccUtils.bytesToHexString(response.payload) + s;
2057 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002058 return s;
2059 }
2060
2061 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002062 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002063 String filePath) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002064 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002065
2066 if (DBG) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002067 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002068 p1 + " " + p2 + " " + p3 + ":" + filePath);
2069 }
2070
2071 IccIoResult response =
2072 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002073 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2074 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002075
2076 if (DBG) {
2077 log("Exchange SIM_IO [R]" + response);
2078 }
2079
2080 byte[] result = null;
2081 int length = 2;
2082 if (response.payload != null) {
2083 length = 2 + response.payload.length;
2084 result = new byte[length];
2085 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2086 } else {
2087 result = new byte[length];
2088 }
2089
2090 result[length - 1] = (byte) response.sw2;
2091 result[length - 2] = (byte) response.sw1;
2092 return result;
2093 }
2094
2095 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002096 public String sendEnvelopeWithStatus(int subId, String content) {
2097 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002098
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002099 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002100 if (response.payload == null) {
2101 return "";
2102 }
2103
2104 // Append the returned status code to the end of the response payload.
2105 String s = Integer.toHexString(
2106 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2107 s = IccUtils.bytesToHexString(response.payload) + s;
2108 return s;
2109 }
2110
Jake Hambye994d462014-02-03 13:10:13 -08002111 /**
2112 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2113 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2114 *
2115 * @param itemID the ID of the item to read
2116 * @return the NV item as a String, or null on error.
2117 */
2118 @Override
2119 public String nvReadItem(int itemID) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002120 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002121 if (DBG) log("nvReadItem: item " + itemID);
2122 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2123 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2124 return value;
2125 }
2126
2127 /**
2128 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2129 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2130 *
2131 * @param itemID the ID of the item to read
2132 * @param itemValue the value to write, as a String
2133 * @return true on success; false on any failure
2134 */
2135 @Override
2136 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002137 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002138 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2139 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2140 new Pair<Integer, String>(itemID, itemValue));
2141 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2142 return success;
2143 }
2144
2145 /**
2146 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2147 * Used for device configuration by some CDMA operators.
2148 *
2149 * @param preferredRoamingList byte array containing the new PRL
2150 * @return true on success; false on any failure
2151 */
2152 @Override
2153 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002154 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002155 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2156 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2157 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2158 return success;
2159 }
2160
2161 /**
2162 * Perform the specified type of NV config reset.
2163 * Used for device configuration by some CDMA operators.
2164 *
2165 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2166 * @return true on success; false on any failure
2167 */
2168 @Override
2169 public boolean nvResetConfig(int resetType) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002170 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002171 if (DBG) log("nvResetConfig: type " + resetType);
2172 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2173 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2174 return success;
2175 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002176
2177 /**
Wink Saville36469e72014-06-11 15:17:00 -07002178 * {@hide}
2179 * Returns Default sim, 0 in the case of single standby.
2180 */
2181 public int getDefaultSim() {
2182 //TODO Need to get it from Telephony Devcontroller
2183 return 0;
2184 }
2185
Svet Ganovb320e182015-04-16 12:30:10 -07002186 public String[] getPcscfAddress(String apnType, String callingPackage) {
2187 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2188 return new String[0];
2189 }
2190
2191
ram87fca6f2014-07-18 18:58:44 +05302192 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002193 }
2194
2195 public void setImsRegistrationState(boolean registered) {
2196 enforceModifyPermission();
2197 mPhone.setImsRegistrationState(registered);
2198 }
2199
2200 /**
Stuart Scott54788802015-03-30 13:18:01 -07002201 * Set the network selection mode to automatic.
2202 *
2203 */
2204 @Override
2205 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002206 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002207 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2208 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2209 }
2210
2211 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002212 * Set the network selection mode to manual with the selected carrier.
2213 */
2214 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002215 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2216 boolean persistSelection) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002217 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002218 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002219 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2220 persistSelection);
2221 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002222 }
2223
2224 /**
2225 * Scans for available networks.
2226 */
2227 @Override
2228 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002229 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002230 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2231 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2232 CMD_PERFORM_NETWORK_SCAN, null, subId);
2233 return result;
2234 }
2235
2236 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002237 * Get the calculated preferred network type.
2238 * Used for debugging incorrect network type.
2239 *
2240 * @return the preferred network type, defined in RILConstants.java.
2241 */
2242 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002243 public int getCalculatedPreferredNetworkType(String callingPackage) {
2244 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2245 return RILConstants.PREFERRED_NETWORK_MODE;
2246 }
2247
Amit Mahajan43330e02014-11-18 11:54:45 -08002248 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002249 }
2250
2251 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002252 * Get the preferred network type.
2253 * Used for device configuration by some CDMA operators.
2254 *
2255 * @return the preferred network type, defined in RILConstants.java.
2256 */
2257 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002258 public int getPreferredNetworkType(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002259 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002260 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002261 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002262 int networkType = (result != null ? result[0] : -1);
2263 if (DBG) log("getPreferredNetworkType: " + networkType);
2264 return networkType;
2265 }
2266
2267 /**
2268 * Set the preferred network type.
2269 * Used for device configuration by some CDMA operators.
2270 *
2271 * @param networkType the preferred network type, defined in RILConstants.java.
2272 * @return true on success; false on any failure.
2273 */
2274 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002275 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002276 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002277 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2278 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002279 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002280 if (success) {
2281 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002282 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002283 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002284 return success;
2285 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002286
2287 /**
Junda Liu475951f2014-11-07 16:45:03 -08002288 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2289 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2290 * tethering.
2291 *
2292 * @return 0: Not required. 1: required. 2: Not set.
2293 * @hide
2294 */
2295 @Override
2296 public int getTetherApnRequired() {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002297 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002298 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2299 Settings.Global.TETHER_DUN_REQUIRED, 2);
2300 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2301 // config_tether_apndata.
2302 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2303 dunRequired = 1;
2304 }
2305 return dunRequired;
2306 }
2307
2308 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002309 * Set mobile data enabled
2310 * Used by the user through settings etc to turn on/off mobile data
2311 *
2312 * @param enable {@code true} turn turn data on, else {@code false}
2313 */
2314 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002315 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002316 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002317 int phoneId = mSubscriptionController.getPhoneId(subId);
2318 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2319 Phone phone = PhoneFactory.getPhone(phoneId);
2320 if (phone != null) {
2321 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2322 phone.setDataEnabled(enable);
2323 } else {
2324 loge("setDataEnabled: no phone for subId=" + subId);
2325 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002326 }
2327
2328 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002329 * Get whether mobile data is enabled.
2330 *
2331 * Note that this used to be available from ConnectivityService, gated by
2332 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2333 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002334 *
2335 * @return {@code true} if data is enabled else {@code false}
2336 */
2337 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002338 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002339 try {
2340 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2341 null);
2342 } catch (Exception e) {
2343 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2344 null);
2345 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002346 int phoneId = mSubscriptionController.getPhoneId(subId);
2347 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2348 Phone phone = PhoneFactory.getPhone(phoneId);
2349 if (phone != null) {
2350 boolean retVal = phone.getDataEnabled();
2351 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2352 return retVal;
2353 } else {
2354 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2355 return false;
2356 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002357 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002358
2359 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002360 public int getCarrierPrivilegeStatus(int subId) {
2361 final Phone phone = getPhone(subId);
2362 if (phone == null) {
2363 loge("getCarrierPrivilegeStatus: Invalid subId");
2364 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2365 }
2366 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002367 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002368 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002369 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2370 }
2371 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002372 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002373 }
Junda Liu29340342014-07-10 15:23:27 -07002374
2375 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002376 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002377 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002378 if (card == null) {
2379 loge("checkCarrierPrivilegesForPackage: No UICC");
2380 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2381 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002382 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2383 }
2384
2385 @Override
2386 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2387 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2388 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2389 UiccCard card = UiccController.getInstance().getUiccCard(i);
2390 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002391 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002392 continue;
2393 }
2394
2395 result = card.getCarrierPrivilegeStatus(
2396 mPhone.getContext().getPackageManager(), pkgName);
2397 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2398 break;
2399 }
2400 }
2401
2402 return result;
Junda Liu29340342014-07-10 15:23:27 -07002403 }
Derek Tan89e89d42014-07-08 17:00:10 -07002404
2405 @Override
Junda Liue64de782015-04-16 17:19:16 -07002406 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2407 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2408 loge("phoneId " + phoneId + " is not valid.");
2409 return null;
2410 }
2411 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002412 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002413 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002414 return null ;
2415 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002416 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002417 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002418 }
2419
Wink Savilleb564aae2014-10-23 10:18:09 -07002420 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002421 final Phone phone = getPhone(subId);
2422 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002423 if (card == null) {
2424 loge("getIccId: No UICC");
2425 return null;
2426 }
2427 String iccId = card.getIccId();
2428 if (TextUtils.isEmpty(iccId)) {
2429 loge("getIccId: ICC ID is null or empty.");
2430 return null;
2431 }
2432 return iccId;
2433 }
2434
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002435 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002436 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2437 String number) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002438 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002439
Jeff Sharkey85190e62014-12-05 09:40:12 -08002440 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002441 final Phone phone = getPhone(subId);
2442 if (phone == null) {
2443 return false;
2444 }
2445 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002446
2447 if (DBG_MERGE) {
2448 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2449 + subscriberId + " to " + number);
2450 }
2451
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002452 if (TextUtils.isEmpty(iccId)) {
2453 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002454 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002455
Jeff Sharkey85190e62014-12-05 09:40:12 -08002456 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2457
2458 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002459 if (alphaTag == null) {
2460 editor.remove(alphaTagPrefKey);
2461 } else {
2462 editor.putString(alphaTagPrefKey, alphaTag);
2463 }
2464
Jeff Sharkey85190e62014-12-05 09:40:12 -08002465 // Record both the line number and IMSI for this ICCID, since we need to
2466 // track all merged IMSIs based on line number
2467 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2468 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002469 if (number == null) {
2470 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002471 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002472 } else {
2473 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002474 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002475 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002476
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002477 editor.commit();
2478 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002479 }
2480
2481 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002482 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002483 // This is open to apps with WRITE_SMS.
2484 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Jack Yue7939d82015-12-10 16:04:02 -08002485 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002486 return null;
2487 }
Derek Tan97ebb422014-09-05 16:55:38 -07002488
2489 String iccId = getIccId(subId);
2490 if (iccId != null) {
2491 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Jack Yue7939d82015-12-10 16:04:02 -08002492 if (DBG_MERGE) {
2493 log("getLine1NumberForDisplay returning " +
2494 mTelephonySharedPreferences.getString(numberPrefKey, null));
2495 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002496 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002497 }
Jack Yue7939d82015-12-10 16:04:02 -08002498 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002499 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002500 }
2501
2502 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002503 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2504 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2505 return null;
2506 }
Derek Tan97ebb422014-09-05 16:55:38 -07002507
2508 String iccId = getIccId(subId);
2509 if (iccId != null) {
2510 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002511 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002512 }
Derek Tan97ebb422014-09-05 16:55:38 -07002513 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002514 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002515
2516 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002517 public String[] getMergedSubscriberIds(String callingPackage) {
2518 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2519 return null;
2520 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002521 final Context context = mPhone.getContext();
2522 final TelephonyManager tele = TelephonyManager.from(context);
2523 final SubscriptionManager sub = SubscriptionManager.from(context);
2524
2525 // Figure out what subscribers are currently active
2526 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002527 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2528 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2529 final long identity = Binder.clearCallingIdentity();
2530 try {
2531 final int[] subIds = sub.getActiveSubscriptionIdList();
2532 for (int subId : subIds) {
2533 activeSubscriberIds.add(tele.getSubscriberId(subId));
2534 }
2535 } finally {
2536 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002537 }
2538
2539 // First pass, find a number override for an active subscriber
2540 String mergeNumber = null;
2541 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2542 for (String key : prefs.keySet()) {
2543 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2544 final String subscriberId = (String) prefs.get(key);
2545 if (activeSubscriberIds.contains(subscriberId)) {
2546 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2547 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2548 mergeNumber = (String) prefs.get(numberKey);
2549 if (DBG_MERGE) {
2550 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2551 + " for active subscriber " + subscriberId);
2552 }
2553 if (!TextUtils.isEmpty(mergeNumber)) {
2554 break;
2555 }
2556 }
2557 }
2558 }
2559
2560 // Shortcut when no active merged subscribers
2561 if (TextUtils.isEmpty(mergeNumber)) {
2562 return null;
2563 }
2564
2565 // Second pass, find all subscribers under that line override
2566 final ArraySet<String> result = new ArraySet<>();
2567 for (String key : prefs.keySet()) {
2568 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2569 final String number = (String) prefs.get(key);
2570 if (mergeNumber.equals(number)) {
2571 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2572 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2573 final String subscriberId = (String) prefs.get(subscriberKey);
2574 if (!TextUtils.isEmpty(subscriberId)) {
2575 result.add(subscriberId);
2576 }
2577 }
2578 }
2579 }
2580
2581 final String[] resultArray = result.toArray(new String[result.size()]);
2582 Arrays.sort(resultArray);
2583 if (DBG_MERGE) {
2584 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2585 }
2586 return resultArray;
2587 }
2588
2589 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002590 public boolean setOperatorBrandOverride(int subId, String brand) {
2591 enforceCarrierPrivilege(subId);
2592 final Phone phone = getPhone(subId);
2593 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002594 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002595
2596 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002597 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002598 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2599 List<String> cdmaNonRoamingList) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002600 enforceCarrierPrivilege(subId);
2601 final Phone phone = getPhone(subId);
2602 if (phone == null) {
2603 return false;
2604 }
2605 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002606 cdmaNonRoamingList);
2607 }
2608
2609 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002610 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2611 enforceModifyPermission();
2612
2613 int returnValue = 0;
2614 try {
2615 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2616 if(result.exception == null) {
2617 if (result.result != null) {
2618 byte[] responseData = (byte[])(result.result);
2619 if(responseData.length > oemResp.length) {
2620 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2621 responseData.length + "bytes. Buffer Size is " +
2622 oemResp.length + "bytes.");
2623 }
2624 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2625 returnValue = responseData.length;
2626 }
2627 } else {
2628 CommandException ex = (CommandException) result.exception;
2629 returnValue = ex.getCommandError().ordinal();
2630 if(returnValue > 0) returnValue *= -1;
2631 }
2632 } catch (RuntimeException e) {
2633 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2634 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2635 if(returnValue > 0) returnValue *= -1;
2636 }
2637
2638 return returnValue;
2639 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002640
2641 @Override
2642 public void setRadioCapability(RadioAccessFamily[] rafs) {
2643 try {
2644 ProxyController.getInstance().setRadioCapability(rafs);
2645 } catch (RuntimeException e) {
2646 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2647 }
2648 }
2649
2650 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002651 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2652 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2653 return RadioAccessFamily.RAF_UNKNOWN;
2654 }
2655
Wink Saville5d475dd2014-10-17 15:00:58 -07002656 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2657 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002658
2659 @Override
2660 public void enableVideoCalling(boolean enable) {
2661 enforceModifyPermission();
2662 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2663 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2664 editor.commit();
2665 }
2666
2667 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002668 public boolean isVideoCallingEnabled(String callingPackage) {
2669 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2670 return false;
2671 }
2672
Andrew Lee77527ac2014-10-21 16:57:39 -07002673 // Check the user preference and the system-level IMS setting. Even if the user has
2674 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2675 // In the long run, we may instead need to check if there exists a connection service
2676 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002677 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2678 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2679 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002680 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002681
Andrew Leea1239f22015-03-02 17:44:07 -08002682 @Override
2683 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002684 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002685 }
2686
2687 @Override
2688 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002689 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002690 }
2691
Andrew Lee9431b832015-03-09 18:46:45 -07002692 @Override
2693 public boolean isTtyModeSupported() {
2694 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2695 TelephonyManager telephonyManager =
2696 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2697 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2698 }
2699
2700 @Override
2701 public boolean isHearingAidCompatibilitySupported() {
2702 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2703 }
2704
Sanket Padawe7310cc72015-01-14 09:53:20 -08002705 /**
2706 * Returns the unique device ID of phone, for example, the IMEI for
2707 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2708 *
2709 * <p>Requires Permission:
2710 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2711 */
2712 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002713 public String getDeviceId(String callingPackage) {
2714 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2715 return null;
2716 }
2717
Sanket Padawe7310cc72015-01-14 09:53:20 -08002718 final Phone phone = PhoneFactory.getPhone(0);
2719 if (phone != null) {
2720 return phone.getDeviceId();
2721 } else {
2722 return null;
2723 }
2724 }
2725
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002726 /*
2727 * {@hide}
2728 * Returns the IMS Registration Status
2729 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002730 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002731 public boolean isImsRegistered() {
2732 return mPhone.isImsRegistered();
2733 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002734
2735 @Override
2736 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2737 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2738 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002739
Nathan Haroldc55097a2015-03-11 18:14:50 -07002740 /*
2741 * {@hide}
2742 * Returns the IMS Registration Status
2743 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002744 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002745 return mPhone.isWifiCallingEnabled();
2746 }
2747
2748 /*
2749 * {@hide}
2750 * Returns the IMS Registration Status
2751 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002752 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002753 return mPhone.isVolteEnabled();
2754 }
Svet Ganovb320e182015-04-16 12:30:10 -07002755
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002756 /*
2757 * {@hide} Returns the IMS Registration Status
2758 */
2759 public boolean isVideoTelephonyAvailable() {
2760 return mPhone.isVideoEnabled();
2761 }
2762
Svet Ganovb320e182015-04-16 12:30:10 -07002763 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002764 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002765 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002766 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2767
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002768 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002769 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002770 } catch (SecurityException e) {
2771 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2772 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002773 }
Svet Ganovb320e182015-04-16 12:30:10 -07002774
2775 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2776 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2777 return false;
2778 }
2779
2780 return true;
2781 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002782
Makoto Onukifee69342015-06-29 14:44:50 -07002783 /**
Jack Yue7939d82015-12-10 16:04:02 -08002784 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002785 */
2786 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002787 // Default SMS app can always read it.
2788 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2789 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2790 return true;
2791 }
2792 try {
2793 return canReadPhoneState(callingPackage, message);
Jack Yue7939d82015-12-10 16:04:02 -08002794 } catch (SecurityException readPhoneStateSecurityException) {
2795 try {
2796 // Can be read with READ_SMS too.
2797 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2798 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2799 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2800 } catch (SecurityException readSmsSecurityException) {
2801 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2802 // permissions
2803 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2804 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2805 " or " + android.Manifest.permission.READ_SMS + ".");
2806 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002807 }
Makoto Onukifee69342015-06-29 14:44:50 -07002808 }
2809
Stuart Scott8eef64f2015-04-08 15:13:54 -07002810 @Override
2811 public void factoryReset(int subId) {
2812 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002813 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2814 return;
2815 }
2816
Svet Ganovcc087f82015-05-12 20:35:54 -07002817 final long identity = Binder.clearCallingIdentity();
2818 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002819 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2820 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002821 // Enable data
2822 setDataEnabled(subId, true);
2823 // Set network selection mode to automatic
2824 setNetworkSelectionModeAutomatic(subId);
2825 // Set preferred mobile network type to the best available
2826 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2827 // Turn off roaming
2828 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2829 }
2830 } finally {
2831 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002832 }
2833 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002834
2835 @Override
2836 public String getLocaleFromDefaultSim() {
2837 // We query all subscriptions instead of just the active ones, because
2838 // this might be called early on in the provisioning flow when the
2839 // subscriptions potentially aren't active yet.
2840 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2841 if (slist == null || slist.isEmpty()) {
2842 return null;
2843 }
2844
2845 // This function may be called very early, say, from the setup wizard, at
2846 // which point we won't have a default subscription set. If that's the case
2847 // we just choose the first, which will be valid in "most cases".
2848 final int defaultSubId = getDefaultSubscription();
2849 SubscriptionInfo info = null;
2850 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2851 info = slist.get(0);
2852 } else {
2853 for (SubscriptionInfo item : slist) {
2854 if (item.getSubscriptionId() == defaultSubId) {
2855 info = item;
2856 break;
2857 }
2858 }
2859
2860 if (info == null) {
2861 return null;
2862 }
2863 }
2864
2865 // Try and fetch the locale from the carrier properties or from the SIM language
2866 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002867 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002868 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002869 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002870 if (defaultPhone != null) {
2871 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2872 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002873 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002874 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2875 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002876 } else {
2877 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002878 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002879 }
2880 }
2881
Narayan Kamath011676f2015-07-29 12:04:08 +01002882 // The SIM language preferences only store a language (e.g. fr = French), not an
2883 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2884 // the SIM and carrier preferences does not include a country we add the country
2885 // determined from the SIM MCC to provide an exact locale.
2886 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002887 if (mccLocale != null) {
2888 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2889 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002890 }
2891
Tony Hill183b2de2015-06-24 14:53:58 +01002892 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002893 return null;
2894 }
2895
2896 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2897 final long identity = Binder.clearCallingIdentity();
2898 try {
2899 return mSubscriptionController.getAllSubInfoList(
2900 mPhone.getContext().getOpPackageName());
2901 } finally {
2902 Binder.restoreCallingIdentity(identity);
2903 }
2904 }
2905
2906 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2907 final long identity = Binder.clearCallingIdentity();
2908 try {
2909 return mSubscriptionController.getActiveSubscriptionInfoList(
2910 mPhone.getContext().getOpPackageName());
2911 } finally {
2912 Binder.restoreCallingIdentity(identity);
2913 }
2914 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002915
2916 /**
2917 * {@hide}
2918 * Returns the modem stats
2919 */
2920 @Override
2921 public ModemActivityInfo getModemActivityInfo() {
2922 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2923 }
Jack Yue7939d82015-12-10 16:04:02 -08002924
2925 /**
2926 * {@hide}
2927 * Returns the service state information on specified subscription.
2928 */
2929 @Override
2930 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2931
2932 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2933 return null;
2934 }
2935
2936 final Phone phone = getPhone(subId);
2937 if (phone == null) {
2938 return null;
2939 }
2940
2941 return phone.getServiceState();
2942 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002943}