blob: 84dac1effb6a924ccf017227bab85f2e4dca80e5 [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
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070041import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070044import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080045import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080046import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070047import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070050import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070051import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070052import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070054import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080056import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080057import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070058import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070059import android.telephony.TelephonyManager;
60import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080064import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.Slog;
Andrew Lee312e8172014-10-23 17:01:36 -070066import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070067import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070068import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080072import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010073import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070074import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070075import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070076import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070077import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070078import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070079import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070080import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080081import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070082import com.android.internal.telephony.uicc.IccIoResult;
83import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070084import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070085import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080086import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070087import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080088import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070089import java.io.FileDescriptor;
90import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080092import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080093import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010094import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080095import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096
97/**
98 * Implementation of the ITelephony interface.
99 */
Santos Cordon117fee72014-05-16 17:56:12 -0700100public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101 private static final String LOG_TAG = "PhoneInterfaceManager";
102 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
103 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800104 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105
106 // Message codes used with mMainThreadHandler
107 private static final int CMD_HANDLE_PIN_MMI = 1;
108 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
109 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
110 private static final int CMD_ANSWER_RINGING_CALL = 4;
111 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700112 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
113 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700114 private static final int CMD_OPEN_CHANNEL = 9;
115 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
116 private static final int CMD_CLOSE_CHANNEL = 11;
117 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800118 private static final int CMD_NV_READ_ITEM = 13;
119 private static final int EVENT_NV_READ_ITEM_DONE = 14;
120 private static final int CMD_NV_WRITE_ITEM = 15;
121 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
122 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
123 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
124 private static final int CMD_NV_RESET_CONFIG = 19;
125 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800126 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
127 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
128 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
129 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800130 private static final int CMD_SEND_ENVELOPE = 25;
131 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700132 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
133 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
134 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
135 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
136 private static final int CMD_EXCHANGE_SIM_IO = 31;
137 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800138 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
139 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700140 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
141 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700142 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
143 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700144 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
145 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
146 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
147 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700148 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
149 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
150 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
151 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700152 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153
154 /** The singleton instance. */
155 private static PhoneInterfaceManager sInstance;
156
Wink Saville3ab207e2014-11-20 13:07:20 -0800157 private PhoneGlobals mApp;
158 private Phone mPhone;
159 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700160 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800161 private AppOpsManager mAppOps;
162 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800163 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800164 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165
Derek Tan97ebb422014-09-05 16:55:38 -0700166 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
167 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800168 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700169
170 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700171 * A request object to use for transmitting data to an ICC.
172 */
173 private static final class IccAPDUArgument {
174 public int channel, cla, command, p1, p2, p3;
175 public String data;
176
177 public IccAPDUArgument(int channel, int cla, int command,
178 int p1, int p2, int p3, String data) {
179 this.channel = channel;
180 this.cla = cla;
181 this.command = command;
182 this.p1 = p1;
183 this.p2 = p2;
184 this.p3 = p3;
185 this.data = data;
186 }
187 }
188
189 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700190 * A request object to use for transmitting data to an ICC.
191 */
192 private static final class ManualNetworkSelectionArgument {
193 public OperatorInfo operatorInfo;
194 public boolean persistSelection;
195
196 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
197 this.operatorInfo = operatorInfo;
198 this.persistSelection = persistSelection;
199 }
200 }
201
202 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
204 * request after sending. The main thread will notify the request when it is complete.
205 */
206 private static final class MainThreadRequest {
207 /** The argument to use for the request */
208 public Object argument;
209 /** The result of the request that is run on the main thread */
210 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800211 // The subscriber id that this request applies to. Defaults to
212 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
213 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214
215 public MainThreadRequest(Object argument) {
216 this.argument = argument;
217 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800218
219 public MainThreadRequest(Object argument, Integer subId) {
220 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800221 if (subId != null) {
222 this.subId = subId;
223 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800224 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 }
226
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800227 private static final class IncomingThirdPartyCallArgs {
228 public final ComponentName component;
229 public final String callId;
230 public final String callerDisplayName;
231
232 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
233 String callerDisplayName) {
234 this.component = component;
235 this.callId = callId;
236 this.callerDisplayName = callerDisplayName;
237 }
238 }
239
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 /**
241 * A handler that processes messages on the main thread in the phone process. Since many
242 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
243 * inbound binder threads to the main thread in the phone process. The Binder thread
244 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
245 * on, which will be notified when the operation completes and will contain the result of the
246 * request.
247 *
248 * <p>If a MainThreadRequest object is provided in the msg.obj field,
249 * note that request.result must be set to something non-null for the calling thread to
250 * unblock.
251 */
252 private final class MainThreadHandler extends Handler {
253 @Override
254 public void handleMessage(Message msg) {
255 MainThreadRequest request;
256 Message onCompleted;
257 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800258 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700259 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260
261 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700262 case CMD_HANDLE_USSD_REQUEST: {
263 request = (MainThreadRequest) msg.obj;
264 final Phone phone = getPhoneFromRequest(request);
265 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
266 String ussdRequest = ussdObject.first;
267 ResultReceiver wrappedCallback = ussdObject.second;
268 request.result = phone != null ?
269 phone.handleUssdRequest(ussdRequest, wrappedCallback)
270 :false;
271 // Wake up the requesting thread
272 synchronized (request) {
273 request.notifyAll();
274 }
275 break;
276 }
277
Yorke Lee716f67e2015-06-17 15:39:16 -0700278 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700280 final Phone phone = getPhoneFromRequest(request);
281 request.result = phone != null ?
282 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
283 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284 // Wake up the requesting thread
285 synchronized (request) {
286 request.notifyAll();
287 }
288 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700289 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290
291 case CMD_HANDLE_NEIGHBORING_CELL:
292 request = (MainThreadRequest) msg.obj;
293 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
294 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700295 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 break;
297
298 case EVENT_NEIGHBORING_CELL_DONE:
299 ar = (AsyncResult) msg.obj;
300 request = (MainThreadRequest) ar.userObj;
301 if (ar.exception == null && ar.result != null) {
302 request.result = ar.result;
303 } else {
304 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800305 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 }
307 // Wake up the requesting thread
308 synchronized (request) {
309 request.notifyAll();
310 }
311 break;
312
313 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700314 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800315 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700316 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700317 break;
318
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 case CMD_END_CALL:
320 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800321 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700322 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700323 Phone phone = getPhone(end_subId);
324 if (phone == null) {
325 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
326 break;
327 }
328 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
330 // CDMA: If the user presses the Power button we treat it as
331 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700332 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
334 // GSM: End the call as per the Phone state
335 hungUp = PhoneUtils.hangup(mCM);
336 } else {
337 throw new IllegalStateException("Unexpected phone type: " + phoneType);
338 }
339 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
340 request.result = hungUp;
341 // Wake up the requesting thread
342 synchronized (request) {
343 request.notifyAll();
344 }
345 break;
346
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700347 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700349 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800350 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700351 if (uiccCard == null) {
352 loge("iccTransmitApduLogicalChannel: No UICC");
353 request.result = new IccIoResult(0x6F, 0, (byte[])null);
354 synchronized (request) {
355 request.notifyAll();
356 }
357 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700358 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
359 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700360 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700361 iccArgument.channel, iccArgument.cla, iccArgument.command,
362 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700363 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700364 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700365 break;
366
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700367 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700368 ar = (AsyncResult) msg.obj;
369 request = (MainThreadRequest) ar.userObj;
370 if (ar.exception == null && ar.result != null) {
371 request.result = ar.result;
372 } else {
373 request.result = new IccIoResult(0x6F, 0, (byte[])null);
374 if (ar.result == null) {
375 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800376 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700377 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800378 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700379 } else {
380 loge("iccTransmitApduLogicalChannel: Unknown exception");
381 }
382 }
383 synchronized (request) {
384 request.notifyAll();
385 }
386 break;
387
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700388 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
389 request = (MainThreadRequest) msg.obj;
390 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800391 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700392 if (uiccCard == null) {
393 loge("iccTransmitApduBasicChannel: No UICC");
394 request.result = new IccIoResult(0x6F, 0, (byte[])null);
395 synchronized (request) {
396 request.notifyAll();
397 }
398 } else {
399 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
400 request);
401 uiccCard.iccTransmitApduBasicChannel(
402 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
403 iccArgument.p3, iccArgument.data, onCompleted);
404 }
405 break;
406
407 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
408 ar = (AsyncResult) msg.obj;
409 request = (MainThreadRequest) ar.userObj;
410 if (ar.exception == null && ar.result != null) {
411 request.result = ar.result;
412 } else {
413 request.result = new IccIoResult(0x6F, 0, (byte[])null);
414 if (ar.result == null) {
415 loge("iccTransmitApduBasicChannel: Empty response");
416 } else if (ar.exception instanceof CommandException) {
417 loge("iccTransmitApduBasicChannel: CommandException: " +
418 ar.exception);
419 } else {
420 loge("iccTransmitApduBasicChannel: Unknown exception");
421 }
422 }
423 synchronized (request) {
424 request.notifyAll();
425 }
426 break;
427
428 case CMD_EXCHANGE_SIM_IO:
429 request = (MainThreadRequest) msg.obj;
430 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800431 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700432 if (uiccCard == null) {
433 loge("iccExchangeSimIO: No UICC");
434 request.result = new IccIoResult(0x6F, 0, (byte[])null);
435 synchronized (request) {
436 request.notifyAll();
437 }
438 } else {
439 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
440 request);
441 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
442 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
443 iccArgument.data, onCompleted);
444 }
445 break;
446
447 case EVENT_EXCHANGE_SIM_IO_DONE:
448 ar = (AsyncResult) msg.obj;
449 request = (MainThreadRequest) ar.userObj;
450 if (ar.exception == null && ar.result != null) {
451 request.result = ar.result;
452 } else {
453 request.result = new IccIoResult(0x6f, 0, (byte[])null);
454 }
455 synchronized (request) {
456 request.notifyAll();
457 }
458 break;
459
Derek Tan4d5e5c12014-02-04 11:54:58 -0800460 case CMD_SEND_ENVELOPE:
461 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800462 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700463 if (uiccCard == null) {
464 loge("sendEnvelopeWithStatus: No UICC");
465 request.result = new IccIoResult(0x6F, 0, (byte[])null);
466 synchronized (request) {
467 request.notifyAll();
468 }
469 } else {
470 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
471 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
472 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800473 break;
474
475 case EVENT_SEND_ENVELOPE_DONE:
476 ar = (AsyncResult) msg.obj;
477 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700478 if (ar.exception == null && ar.result != null) {
479 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800480 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700481 request.result = new IccIoResult(0x6F, 0, (byte[])null);
482 if (ar.result == null) {
483 loge("sendEnvelopeWithStatus: Empty response");
484 } else if (ar.exception instanceof CommandException) {
485 loge("sendEnvelopeWithStatus: CommandException: " +
486 ar.exception);
487 } else {
488 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
489 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800490 }
491 synchronized (request) {
492 request.notifyAll();
493 }
494 break;
495
Shishir Agrawal566b7612013-10-28 14:41:00 -0700496 case CMD_OPEN_CHANNEL:
497 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800498 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700499 if (uiccCard == null) {
500 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800501 request.result = new IccOpenLogicalChannelResponse(-1,
502 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700503 synchronized (request) {
504 request.notifyAll();
505 }
506 } else {
507 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
508 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
509 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 break;
511
512 case EVENT_OPEN_CHANNEL_DONE:
513 ar = (AsyncResult) msg.obj;
514 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700515 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700516 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700517 int[] result = (int[]) ar.result;
518 int channelId = result[0];
519 byte[] selectResponse = null;
520 if (result.length > 1) {
521 selectResponse = new byte[result.length - 1];
522 for (int i = 1; i < result.length; ++i) {
523 selectResponse[i - 1] = (byte) result[i];
524 }
525 }
526 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700527 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700529 if (ar.result == null) {
530 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700532 if (ar.exception != null) {
533 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
534 }
535
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700536 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700537 if (ar.exception instanceof CommandException) {
538 CommandException.Error error =
539 ((CommandException) (ar.exception)).getCommandError();
540 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700541 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700542 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700543 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700544 }
545 }
546 openChannelResp = new IccOpenLogicalChannelResponse(
547 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700549 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 synchronized (request) {
551 request.notifyAll();
552 }
553 break;
554
555 case CMD_CLOSE_CHANNEL:
556 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800557 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700558 if (uiccCard == null) {
559 loge("iccCloseLogicalChannel: No UICC");
560 request.result = new IccIoResult(0x6F, 0, (byte[])null);
561 synchronized (request) {
562 request.notifyAll();
563 }
564 } else {
565 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
566 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
567 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700568 break;
569
570 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800571 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
572 break;
573
574 case CMD_NV_READ_ITEM:
575 request = (MainThreadRequest) msg.obj;
576 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
577 mPhone.nvReadItem((Integer) request.argument, onCompleted);
578 break;
579
580 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700581 ar = (AsyncResult) msg.obj;
582 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800583 if (ar.exception == null && ar.result != null) {
584 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700585 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800586 request.result = "";
587 if (ar.result == null) {
588 loge("nvReadItem: Empty response");
589 } else if (ar.exception instanceof CommandException) {
590 loge("nvReadItem: CommandException: " +
591 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700592 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800593 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700594 }
595 }
596 synchronized (request) {
597 request.notifyAll();
598 }
599 break;
600
Jake Hambye994d462014-02-03 13:10:13 -0800601 case CMD_NV_WRITE_ITEM:
602 request = (MainThreadRequest) msg.obj;
603 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
604 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
605 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
606 break;
607
608 case EVENT_NV_WRITE_ITEM_DONE:
609 handleNullReturnEvent(msg, "nvWriteItem");
610 break;
611
612 case CMD_NV_WRITE_CDMA_PRL:
613 request = (MainThreadRequest) msg.obj;
614 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
615 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
616 break;
617
618 case EVENT_NV_WRITE_CDMA_PRL_DONE:
619 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
620 break;
621
622 case CMD_NV_RESET_CONFIG:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
625 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
626 break;
627
628 case EVENT_NV_RESET_CONFIG_DONE:
629 handleNullReturnEvent(msg, "nvResetConfig");
630 break;
631
Jake Hamby7c27be32014-03-03 13:25:59 -0800632 case CMD_GET_PREFERRED_NETWORK_TYPE:
633 request = (MainThreadRequest) msg.obj;
634 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700635 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800636 break;
637
638 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
639 ar = (AsyncResult) msg.obj;
640 request = (MainThreadRequest) ar.userObj;
641 if (ar.exception == null && ar.result != null) {
642 request.result = ar.result; // Integer
643 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800644 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800645 if (ar.result == null) {
646 loge("getPreferredNetworkType: Empty response");
647 } else if (ar.exception instanceof CommandException) {
648 loge("getPreferredNetworkType: CommandException: " +
649 ar.exception);
650 } else {
651 loge("getPreferredNetworkType: Unknown exception");
652 }
653 }
654 synchronized (request) {
655 request.notifyAll();
656 }
657 break;
658
659 case CMD_SET_PREFERRED_NETWORK_TYPE:
660 request = (MainThreadRequest) msg.obj;
661 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
662 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700663 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800664 break;
665
666 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
667 handleNullReturnEvent(msg, "setPreferredNetworkType");
668 break;
669
Steven Liu4bf01bc2014-07-17 11:05:29 -0500670 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
671 request = (MainThreadRequest)msg.obj;
672 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
673 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
674 break;
675
676 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
677 ar = (AsyncResult)msg.obj;
678 request = (MainThreadRequest)ar.userObj;
679 request.result = ar;
680 synchronized (request) {
681 request.notifyAll();
682 }
683 break;
684
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800685 case CMD_SET_VOICEMAIL_NUMBER:
686 request = (MainThreadRequest) msg.obj;
687 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
688 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800689 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
690 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800691 break;
692
693 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
694 handleNullReturnEvent(msg, "setVoicemailNumber");
695 break;
696
Stuart Scott54788802015-03-30 13:18:01 -0700697 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
698 request = (MainThreadRequest) msg.obj;
699 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
700 request);
701 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
702 break;
703
704 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
705 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
706 break;
707
Shishir Agrawal302c8692015-06-19 13:49:39 -0700708 case CMD_PERFORM_NETWORK_SCAN:
709 request = (MainThreadRequest) msg.obj;
710 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
711 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
712 break;
713
714 case EVENT_PERFORM_NETWORK_SCAN_DONE:
715 ar = (AsyncResult) msg.obj;
716 request = (MainThreadRequest) ar.userObj;
717 CellNetworkScanResult cellScanResult;
718 if (ar.exception == null && ar.result != null) {
719 cellScanResult = new CellNetworkScanResult(
720 CellNetworkScanResult.STATUS_SUCCESS,
721 (List<OperatorInfo>) ar.result);
722 } else {
723 if (ar.result == null) {
724 loge("getCellNetworkScanResults: Empty response");
725 }
726 if (ar.exception != null) {
727 loge("getCellNetworkScanResults: Exception: " + ar.exception);
728 }
729 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
730 if (ar.exception instanceof CommandException) {
731 CommandException.Error error =
732 ((CommandException) (ar.exception)).getCommandError();
733 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
734 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
735 } else if (error == CommandException.Error.GENERIC_FAILURE) {
736 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
737 }
738 }
739 cellScanResult = new CellNetworkScanResult(errorCode, null);
740 }
741 request.result = cellScanResult;
742 synchronized (request) {
743 request.notifyAll();
744 }
745 break;
746
747 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
748 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700749 ManualNetworkSelectionArgument selArg =
750 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700751 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
752 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700753 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
754 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700755 break;
756
757 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
758 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
759 break;
760
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700761 case CMD_GET_MODEM_ACTIVITY_INFO:
762 request = (MainThreadRequest) msg.obj;
763 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700764 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700765 break;
766
767 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
768 ar = (AsyncResult) msg.obj;
769 request = (MainThreadRequest) ar.userObj;
770 if (ar.exception == null && ar.result != null) {
771 request.result = ar.result;
772 } else {
773 if (ar.result == null) {
774 loge("queryModemActivityInfo: Empty response");
775 } else if (ar.exception instanceof CommandException) {
776 loge("queryModemActivityInfo: CommandException: " +
777 ar.exception);
778 } else {
779 loge("queryModemActivityInfo: Unknown exception");
780 }
781 }
Amit Mahajand4766222016-01-28 15:28:28 -0800782 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
783 if (request.result == null) {
784 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
785 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700786 synchronized (request) {
787 request.notifyAll();
788 }
789 break;
790
Meng Wang1a7c35a2016-05-05 20:56:15 -0700791 case CMD_SET_ALLOWED_CARRIERS:
792 request = (MainThreadRequest) msg.obj;
793 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
794 mPhone.setAllowedCarriers(
795 (List<CarrierIdentifier>) request.argument,
796 onCompleted);
797 break;
798
799 case EVENT_SET_ALLOWED_CARRIERS_DONE:
800 ar = (AsyncResult) msg.obj;
801 request = (MainThreadRequest) ar.userObj;
802 if (ar.exception == null && ar.result != null) {
803 request.result = ar.result;
804 } else {
805 if (ar.result == null) {
806 loge("setAllowedCarriers: Empty response");
807 } else if (ar.exception instanceof CommandException) {
808 loge("setAllowedCarriers: CommandException: " +
809 ar.exception);
810 } else {
811 loge("setAllowedCarriers: Unknown exception");
812 }
813 }
814 // Result cannot be null. Return -1 on error.
815 if (request.result == null) {
816 request.result = new int[]{-1};
817 }
818 synchronized (request) {
819 request.notifyAll();
820 }
821 break;
822
823 case CMD_GET_ALLOWED_CARRIERS:
824 request = (MainThreadRequest) msg.obj;
825 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
826 mPhone.getAllowedCarriers(onCompleted);
827 break;
828
829 case EVENT_GET_ALLOWED_CARRIERS_DONE:
830 ar = (AsyncResult) msg.obj;
831 request = (MainThreadRequest) ar.userObj;
832 if (ar.exception == null && ar.result != null) {
833 request.result = ar.result;
834 } else {
835 if (ar.result == null) {
836 loge("getAllowedCarriers: Empty response");
837 } else if (ar.exception instanceof CommandException) {
838 loge("getAllowedCarriers: CommandException: " +
839 ar.exception);
840 } else {
841 loge("getAllowedCarriers: Unknown exception");
842 }
843 }
844 // Result cannot be null. Return empty list of CarrierIdentifier.
845 if (request.result == null) {
846 request.result = new ArrayList<CarrierIdentifier>(0);
847 }
848 synchronized (request) {
849 request.notifyAll();
850 }
851 break;
852
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 default:
854 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
855 break;
856 }
857 }
Jake Hambye994d462014-02-03 13:10:13 -0800858
859 private void handleNullReturnEvent(Message msg, String command) {
860 AsyncResult ar = (AsyncResult) msg.obj;
861 MainThreadRequest request = (MainThreadRequest) ar.userObj;
862 if (ar.exception == null) {
863 request.result = true;
864 } else {
865 request.result = false;
866 if (ar.exception instanceof CommandException) {
867 loge(command + ": CommandException: " + ar.exception);
868 } else {
869 loge(command + ": Unknown exception");
870 }
871 }
872 synchronized (request) {
873 request.notifyAll();
874 }
875 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 }
877
878 /**
879 * Posts the specified command to be executed on the main thread,
880 * waits for the request to complete, and returns the result.
881 * @see #sendRequestAsync
882 */
883 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800884 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700885 }
886
887 /**
888 * Posts the specified command to be executed on the main thread,
889 * waits for the request to complete, and returns the result.
890 * @see #sendRequestAsync
891 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800892 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
894 throw new RuntimeException("This method will deadlock if called from the main thread.");
895 }
896
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800897 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700898 Message msg = mMainThreadHandler.obtainMessage(command, request);
899 msg.sendToTarget();
900
901 // Wait for the request to complete
902 synchronized (request) {
903 while (request.result == null) {
904 try {
905 request.wait();
906 } catch (InterruptedException e) {
907 // Do nothing, go back and wait until the request is complete
908 }
909 }
910 }
911 return request.result;
912 }
913
914 /**
915 * Asynchronous ("fire and forget") version of sendRequest():
916 * Posts the specified command to be executed on the main thread, and
917 * returns immediately.
918 * @see #sendRequest
919 */
920 private void sendRequestAsync(int command) {
921 mMainThreadHandler.sendEmptyMessage(command);
922 }
923
924 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700925 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
926 * @see {@link #sendRequest(int,Object)}
927 */
928 private void sendRequestAsync(int command, Object argument) {
929 MainThreadRequest request = new MainThreadRequest(argument);
930 Message msg = mMainThreadHandler.obtainMessage(command, request);
931 msg.sendToTarget();
932 }
933
934 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700935 * Initialize the singleton PhoneInterfaceManager instance.
936 * This is only done once, at startup, from PhoneApp.onCreate().
937 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700938 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 synchronized (PhoneInterfaceManager.class) {
940 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700941 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 } else {
943 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
944 }
945 return sInstance;
946 }
947 }
948
949 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700950 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 mApp = app;
952 mPhone = phone;
953 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700954 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
956 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700957 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700958 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800959 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800960
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 publish();
962 }
963
964 private void publish() {
965 if (DBG) log("publish: " + this);
966
967 ServiceManager.addService("phone", this);
968 }
969
Stuart Scott584921c2015-01-15 17:10:34 -0800970 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800971 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
972 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800973 }
974
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800975 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
976 Phone phone = getPhoneFromRequest(request);
977 return phone == null ? null :
978 UiccController.getInstance().getUiccCard(phone.getPhoneId());
979 }
980
Wink Saville36469e72014-06-11 15:17:00 -0700981 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700982 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800983 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700984 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 //
986 // Implementation of the ITelephony interface.
987 //
988
989 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700990 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700991 }
992
Wink Savilleb564aae2014-10-23 10:18:09 -0700993 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700994 if (DBG) log("dial: " + number);
995 // No permission check needed here: This is just a wrapper around the
996 // ACTION_DIAL intent, which is available to any app since it puts up
997 // the UI before it does anything.
998
999 String url = createTelUrl(number);
1000 if (url == null) {
1001 return;
1002 }
1003
1004 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001005 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1007 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1008 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1009 mApp.startActivity(intent);
1010 }
1011 }
1012
1013 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001014 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001015 }
1016
Wink Savilleb564aae2014-10-23 10:18:09 -07001017 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 if (DBG) log("call: " + number);
1019
1020 // This is just a wrapper around the ACTION_CALL intent, but we still
1021 // need to do a permission check since we're calling startActivity()
1022 // from the context of the phone app.
1023 enforceCallPermission();
1024
1025 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1026 != AppOpsManager.MODE_ALLOWED) {
1027 return;
1028 }
1029
1030 String url = createTelUrl(number);
1031 if (url == null) {
1032 return;
1033 }
1034
Wink Saville08874612014-08-31 19:19:58 -07001035 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001036 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001037 if (slist != null) {
1038 for (SubscriptionInfo subInfoRecord : slist) {
1039 if (subInfoRecord.getSubscriptionId() == subId) {
1040 isValid = true;
1041 break;
1042 }
Wink Saville08874612014-08-31 19:19:58 -07001043 }
1044 }
1045 if (isValid == false) {
1046 return;
1047 }
1048
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001049 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001050 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1052 mApp.startActivity(intent);
1053 }
1054
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 /**
1056 * End a call based on call state
1057 * @return true is a call was ended
1058 */
1059 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001060 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001061 }
1062
1063 /**
1064 * End a call based on the call state of the subId
1065 * @return true is a call was ended
1066 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001067 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001069 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 }
1071
1072 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001073 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001074 }
1075
Wink Savilleb564aae2014-10-23 10:18:09 -07001076 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 if (DBG) log("answerRingingCall...");
1078 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1079 // but that can probably wait till the big TelephonyManager API overhaul.
1080 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1081 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001082 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 }
1084
1085 /**
1086 * Make the actual telephony calls to implement answerRingingCall().
1087 * This should only be called from the main thread of the Phone app.
1088 * @see #answerRingingCall
1089 *
1090 * TODO: it would be nice to return true if we answered the call, or
1091 * false if there wasn't actually a ringing incoming call, or some
1092 * other error occurred. (In other words, pass back the return value
1093 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1094 * But that would require calling this method via sendRequest() rather
1095 * than sendRequestAsync(), and right now we don't actually *need* that
1096 * return value, so let's just return void for now.
1097 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001098 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001099 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001101 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1102 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001103 if (hasActiveCall && hasHoldingCall) {
1104 // Both lines are in use!
1105 // TODO: provide a flag to let the caller specify what
1106 // policy to use if both lines are in use. (The current
1107 // behavior is hardwired to "answer incoming, end ongoing",
1108 // which is how the CALL button is specced to behave.)
1109 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1110 return;
1111 } else {
1112 // answerCall() will automatically hold the current active
1113 // call, if there is one.
1114 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1115 return;
1116 }
1117 } else {
1118 // No call was ringing.
1119 return;
1120 }
1121 }
1122
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001123 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001124 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001126 public void silenceRinger() {
1127 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 }
1129
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001130 @Override
1131 public boolean isOffhook(String callingPackage) {
1132 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001133 }
1134
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001135 @Override
1136 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1137 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1138 return false;
1139 }
1140
Sanket Padawe356d7632015-06-22 14:03:32 -07001141 final Phone phone = getPhone(subId);
1142 if (phone != null) {
1143 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1144 } else {
1145 return false;
1146 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 }
1148
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001149 @Override
1150 public boolean isRinging(String callingPackage) {
1151 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001152 }
1153
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001154 @Override
1155 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1156 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1157 return false;
1158 }
1159
Sanket Padawe356d7632015-06-22 14:03:32 -07001160 final Phone phone = getPhone(subId);
1161 if (phone != null) {
1162 return (phone.getState() == PhoneConstants.State.RINGING);
1163 } else {
1164 return false;
1165 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 }
1167
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001168 @Override
1169 public boolean isIdle(String callingPackage) {
1170 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001171 }
1172
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001173 @Override
1174 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1175 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1176 return false;
1177 }
1178
Sanket Padawe356d7632015-06-22 14:03:32 -07001179 final Phone phone = getPhone(subId);
1180 if (phone != null) {
1181 return (phone.getState() == PhoneConstants.State.IDLE);
1182 } else {
1183 return false;
1184 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001185 }
1186
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001187 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001188 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001189 }
1190
Wink Savilleb564aae2014-10-23 10:18:09 -07001191 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001192 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001193 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1194 }
1195
1196 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001197 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001198 }
1199
Wink Savilleb564aae2014-10-23 10:18:09 -07001200 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001201 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001202 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1203 }
1204
1205 /** {@hide} */
1206 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001207 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001208 }
1209
Wink Savilleb564aae2014-10-23 10:18:09 -07001210 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001212 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 checkSimPin.start();
1214 return checkSimPin.unlockSim(null, pin);
1215 }
1216
Wink Saville9de0f752013-10-22 19:04:03 -07001217 /** {@hide} */
1218 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001219 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001220 }
1221
Wink Savilleb564aae2014-10-23 10:18:09 -07001222 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001224 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001225 checkSimPuk.start();
1226 return checkSimPuk.unlockSim(puk, pin);
1227 }
1228
1229 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001230 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 * a synchronous one.
1232 */
1233 private static class UnlockSim extends Thread {
1234
1235 private final IccCard mSimCard;
1236
1237 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001238 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1239 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240
1241 // For replies from SimCard interface
1242 private Handler mHandler;
1243
1244 // For async handler to identify request type
1245 private static final int SUPPLY_PIN_COMPLETE = 100;
1246
1247 public UnlockSim(IccCard simCard) {
1248 mSimCard = simCard;
1249 }
1250
1251 @Override
1252 public void run() {
1253 Looper.prepare();
1254 synchronized (UnlockSim.this) {
1255 mHandler = new Handler() {
1256 @Override
1257 public void handleMessage(Message msg) {
1258 AsyncResult ar = (AsyncResult) msg.obj;
1259 switch (msg.what) {
1260 case SUPPLY_PIN_COMPLETE:
1261 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1262 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001263 mRetryCount = msg.arg1;
1264 if (ar.exception != null) {
1265 if (ar.exception instanceof CommandException &&
1266 ((CommandException)(ar.exception)).getCommandError()
1267 == CommandException.Error.PASSWORD_INCORRECT) {
1268 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1269 } else {
1270 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1271 }
1272 } else {
1273 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1274 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 mDone = true;
1276 UnlockSim.this.notifyAll();
1277 }
1278 break;
1279 }
1280 }
1281 };
1282 UnlockSim.this.notifyAll();
1283 }
1284 Looper.loop();
1285 }
1286
1287 /*
1288 * Use PIN or PUK to unlock SIM card
1289 *
1290 * If PUK is null, unlock SIM card with PIN
1291 *
1292 * If PUK is not null, unlock SIM card with PUK and set PIN code
1293 */
Wink Saville9de0f752013-10-22 19:04:03 -07001294 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295
1296 while (mHandler == null) {
1297 try {
1298 wait();
1299 } catch (InterruptedException e) {
1300 Thread.currentThread().interrupt();
1301 }
1302 }
1303 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1304
1305 if (puk == null) {
1306 mSimCard.supplyPin(pin, callback);
1307 } else {
1308 mSimCard.supplyPuk(puk, pin, callback);
1309 }
1310
1311 while (!mDone) {
1312 try {
1313 Log.d(LOG_TAG, "wait for done");
1314 wait();
1315 } catch (InterruptedException e) {
1316 // Restore the interrupted status
1317 Thread.currentThread().interrupt();
1318 }
1319 }
1320 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001321 int[] resultArray = new int[2];
1322 resultArray[0] = mResult;
1323 resultArray[1] = mRetryCount;
1324 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 }
1326 }
1327
1328 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001329 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001330
1331 }
1332
Wink Savilleb564aae2014-10-23 10:18:09 -07001333 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 // No permission check needed here: this call is harmless, and it's
1335 // needed for the ServiceState.requestStateUpdate() call (which is
1336 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001337 final Phone phone = getPhone(subId);
1338 if (phone != null) {
1339 phone.updateServiceLocation();
1340 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 }
1342
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001343 @Override
1344 public boolean isRadioOn(String callingPackage) {
1345 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001346 }
1347
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001348 @Override
1349 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1350 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1351 return false;
1352 }
1353 return isRadioOnForSubscriber(subId);
1354 }
1355
1356 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001357 final Phone phone = getPhone(subId);
1358 if (phone != null) {
1359 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1360 } else {
1361 return false;
1362 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 }
1364
1365 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001366 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001367
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 }
Wink Saville36469e72014-06-11 15:17:00 -07001369
Wink Savilleb564aae2014-10-23 10:18:09 -07001370 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001372 final Phone phone = getPhone(subId);
1373 if (phone != null) {
1374 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1375 }
Wink Saville36469e72014-06-11 15:17:00 -07001376 }
1377
1378 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001379 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001380 }
1381
Wink Savilleb564aae2014-10-23 10:18:09 -07001382 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001383 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001384 final Phone phone = getPhone(subId);
1385 if (phone == null) {
1386 return false;
1387 }
1388 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001389 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001390 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 }
1392 return true;
1393 }
Wink Saville36469e72014-06-11 15:17:00 -07001394
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001395 public boolean needMobileRadioShutdown() {
1396 /*
1397 * If any of the Radios are available, it will need to be
1398 * shutdown. So return true if any Radio is available.
1399 */
1400 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1401 Phone phone = PhoneFactory.getPhone(i);
1402 if (phone != null && phone.isRadioAvailable()) return true;
1403 }
1404 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1405 return false;
1406 }
1407
1408 public void shutdownMobileRadios() {
1409 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1410 logv("Shutting down Phone " + i);
1411 shutdownRadioUsingPhoneId(i);
1412 }
1413 }
1414
1415 private void shutdownRadioUsingPhoneId(int phoneId) {
1416 enforceModifyPermission();
1417 Phone phone = PhoneFactory.getPhone(phoneId);
1418 if (phone != null && phone.isRadioAvailable()) {
1419 phone.shutdownRadio();
1420 }
1421 }
1422
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001423 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001424 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1425 if (defaultPhone != null) {
1426 defaultPhone.setRadioPower(turnOn);
1427 return true;
1428 } else {
1429 loge("There's no default phone.");
1430 return false;
1431 }
Wink Saville36469e72014-06-11 15:17:00 -07001432 }
1433
Wink Savilleb564aae2014-10-23 10:18:09 -07001434 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001436 final Phone phone = getPhone(subId);
1437 if (phone != null) {
1438 phone.setRadioPower(turnOn);
1439 return true;
1440 } else {
1441 return false;
1442 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
1444
Wink Saville36469e72014-06-11 15:17:00 -07001445 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001446 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 public boolean enableDataConnectivity() {
1448 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001449 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 final Phone phone = getPhone(subId);
1451 if (phone != null) {
1452 phone.setDataEnabled(true);
1453 return true;
1454 } else {
1455 return false;
1456 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 }
1458
Wink Saville36469e72014-06-11 15:17:00 -07001459 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001460 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 public boolean disableDataConnectivity() {
1462 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001463 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001464 final Phone phone = getPhone(subId);
1465 if (phone != null) {
1466 phone.setDataEnabled(false);
1467 return true;
1468 } else {
1469 return false;
1470 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 }
1472
Wink Saville36469e72014-06-11 15:17:00 -07001473 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001474 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001476 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001477 final Phone phone = getPhone(subId);
1478 if (phone != null) {
1479 return phone.isDataConnectivityPossible();
1480 } else {
1481 return false;
1482 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 }
1484
1485 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001486 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001487 }
1488
pkanwarae03a6b2016-11-06 20:37:09 -08001489 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001490 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001491 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1492 return;
1493 }
1494 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1495 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1496 };
1497
1498
Wink Savilleb564aae2014-10-23 10:18:09 -07001499 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001501 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1502 return false;
1503 }
Wink Saville36469e72014-06-11 15:17:00 -07001504 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 }
1506
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001508 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001509 }
1510
Shishir Agrawala9f32182016-04-12 12:00:16 -07001511 public int getCallStateForSlot(int slotId) {
1512 Phone phone = PhoneFactory.getPhone(slotId);
1513 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1514 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001515 }
1516
Sanket Padawe356d7632015-06-22 14:03:32 -07001517 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001519 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001520 if (phone != null) {
1521 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1522 } else {
1523 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1524 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001525 }
1526
Sanket Padawe356d7632015-06-22 14:03:32 -07001527 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001529 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001530 if (phone != null) {
1531 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1532 } else {
1533 return TelephonyManager.DATA_ACTIVITY_NONE;
1534 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 }
1536
1537 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001538 public Bundle getCellLocation(String callingPackage) {
1539 enforceFineOrCoarseLocationPermission("getCellLocation");
1540
1541 // OP_COARSE_LOCATION controls both fine and coarse location.
1542 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1543 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001544 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001545 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001546 }
1547
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001548 if (checkIfCallerIsSelfOrForegroundUser() ||
1549 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 if (DBG_LOC) log("getCellLocation: is active user");
1551 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001552 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001553 if (phone == null) {
1554 return null;
1555 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001556
1557 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1558 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001559 return data;
1560 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001561 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 return null;
1563 }
1564 }
1565
Svetoslav64fad262015-04-14 14:35:21 -07001566 private void enforceFineOrCoarseLocationPermission(String message) {
1567 try {
1568 mApp.enforceCallingOrSelfPermission(
1569 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1570 } catch (SecurityException e) {
1571 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1572 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1573 // is the weaker precondition
1574 mApp.enforceCallingOrSelfPermission(
1575 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1576 }
1577 }
1578
1579
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 @Override
1581 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001582 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001583 }
1584
Sanket Padawe356d7632015-06-22 14:03:32 -07001585 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001586 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001587 mApp.enforceCallingOrSelfPermission(
1588 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001589 final Phone phone = getPhone(subId);
1590 if (phone != null) {
1591 phone.enableLocationUpdates();
1592 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001593 }
1594
1595 @Override
1596 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001597 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001598 }
1599
Sanket Padawe356d7632015-06-22 14:03:32 -07001600 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001601 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001602 mApp.enforceCallingOrSelfPermission(
1603 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001604 final Phone phone = getPhone(subId);
1605 if (phone != null) {
1606 phone.disableLocationUpdates();
1607 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 }
1609
1610 @Override
1611 @SuppressWarnings("unchecked")
1612 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001613 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1614
1615 // OP_COARSE_LOCATION controls both fine and coarse location.
1616 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1617 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1618 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 }
1620
1621 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1622 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1623 return null;
1624 }
Svetoslav64fad262015-04-14 14:35:21 -07001625
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001626 if (checkIfCallerIsSelfOrForegroundUser() ||
1627 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1629
1630 ArrayList<NeighboringCellInfo> cells = null;
1631
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001632 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 try {
1634 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001635 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001636 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001637 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001638 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001639 }
1640 return cells;
1641 } else {
1642 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1643 return null;
1644 }
1645 }
1646
1647
1648 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001649 public List<CellInfo> getAllCellInfo(String callingPackage) {
1650 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1651
1652 // OP_COARSE_LOCATION controls both fine and coarse location.
1653 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1654 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1655 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 }
1657
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001658 if (checkIfCallerIsSelfOrForegroundUser() ||
1659 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001660 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001661 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001662 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1663 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001664 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1665 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001666 }
1667 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 } else {
1669 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1670 return null;
1671 }
1672 }
1673
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001674 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001675 public void setCellInfoListRate(int rateInMillis) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001676 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1677 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 }
1679
Shishir Agrawala9f32182016-04-12 12:00:16 -07001680 @Override
1681 public String getImeiForSlot(int slotId, String callingPackage) {
1682 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1683 return null;
1684 }
1685 Phone phone = PhoneFactory.getPhone(slotId);
1686 return phone == null ? null : phone.getImei();
1687 }
1688
1689 @Override
1690 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1691 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1692 return null;
1693 }
1694 Phone phone = PhoneFactory.getPhone(slotId);
1695 return phone == null ? null : phone.getDeviceSvn();
1696 }
1697
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 //
1699 // Internal helper methods.
1700 //
1701
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001702 /**
1703 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1704 */
1705 private boolean checkCallerInteractAcrossUsersFull() {
1706 return mPhone.getContext().checkCallingOrSelfPermission(
1707 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1708 == PackageManager.PERMISSION_GRANTED;
1709 }
1710
Jake Hambye994d462014-02-03 13:10:13 -08001711 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001712 boolean ok;
1713
1714 boolean self = Binder.getCallingUid() == Process.myUid();
1715 if (!self) {
1716 // Get the caller's user id then clear the calling identity
1717 // which will be restored in the finally clause.
1718 int callingUser = UserHandle.getCallingUserId();
1719 long ident = Binder.clearCallingIdentity();
1720
1721 try {
1722 // With calling identity cleared the current user is the foreground user.
1723 int foregroundUser = ActivityManager.getCurrentUser();
1724 ok = (foregroundUser == callingUser);
1725 if (DBG_LOC) {
1726 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1727 + " callingUser=" + callingUser + " ok=" + ok);
1728 }
1729 } catch (Exception ex) {
1730 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1731 ok = false;
1732 } finally {
1733 Binder.restoreCallingIdentity(ident);
1734 }
1735 } else {
1736 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1737 ok = true;
1738 }
1739 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1740 return ok;
1741 }
1742
1743 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001744 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1745 *
1746 * @throws SecurityException if the caller does not have the required permission
1747 */
1748 private void enforceModifyPermission() {
1749 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1750 }
1751
1752 /**
Junda Liua2e36012014-07-09 18:30:01 -07001753 * Make sure either system app or the caller has carrier privilege.
1754 *
1755 * @throws SecurityException if the caller does not have the required permission/privilege
1756 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001757 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001758 int permission = mApp.checkCallingOrSelfPermission(
1759 android.Manifest.permission.MODIFY_PHONE_STATE);
1760 if (permission == PackageManager.PERMISSION_GRANTED) {
1761 return;
1762 }
1763
1764 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001765 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001766 }
1767
1768 /**
1769 * Make sure the caller has carrier privilege.
1770 *
1771 * @throws SecurityException if the caller does not have the required permission
1772 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001773 private void enforceCarrierPrivilege(int subId) {
1774 if (getCarrierPrivilegeStatus(subId) !=
1775 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001776 loge("No Carrier Privilege.");
1777 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001778 }
1779 }
1780
1781 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001782 * Make sure the caller has the CALL_PHONE permission.
1783 *
1784 * @throws SecurityException if the caller does not have the required permission
1785 */
1786 private void enforceCallPermission() {
1787 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1788 }
1789
Stuart Scott8eef64f2015-04-08 15:13:54 -07001790 private void enforceConnectivityInternalPermission() {
1791 mApp.enforceCallingOrSelfPermission(
1792 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1793 "ConnectivityService");
1794 }
1795
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001796 private String createTelUrl(String number) {
1797 if (TextUtils.isEmpty(number)) {
1798 return null;
1799 }
1800
Jake Hambye994d462014-02-03 13:10:13 -08001801 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001802 }
1803
Ihab Awadf9e92732013-12-05 18:02:52 -08001804 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001805 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1806 }
1807
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001808 private static void logv(String msg) {
1809 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1810 }
1811
Ihab Awadf9e92732013-12-05 18:02:52 -08001812 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001813 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1814 }
1815
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001816 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001818 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001819 }
1820
Sanket Padawe356d7632015-06-22 14:03:32 -07001821 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001822 public int getActivePhoneTypeForSlot(int slotId) {
1823 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001824 if (phone == null) {
1825 return PhoneConstants.PHONE_TYPE_NONE;
1826 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001827 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001828 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001829 }
1830
1831 /**
1832 * Returns the CDMA ERI icon index to display
1833 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001834 @Override
1835 public int getCdmaEriIconIndex(String callingPackage) {
1836 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001837 }
1838
Sanket Padawe356d7632015-06-22 14:03:32 -07001839 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001840 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1841 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1842 return -1;
1843 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001844 final Phone phone = getPhone(subId);
1845 if (phone != null) {
1846 return phone.getCdmaEriIconIndex();
1847 } else {
1848 return -1;
1849 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001850 }
1851
1852 /**
1853 * Returns the CDMA ERI icon mode,
1854 * 0 - ON
1855 * 1 - FLASHING
1856 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001857 @Override
1858 public int getCdmaEriIconMode(String callingPackage) {
1859 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001860 }
1861
Sanket Padawe356d7632015-06-22 14:03:32 -07001862 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001863 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1864 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1865 return -1;
1866 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001867 final Phone phone = getPhone(subId);
1868 if (phone != null) {
1869 return phone.getCdmaEriIconMode();
1870 } else {
1871 return -1;
1872 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001873 }
1874
1875 /**
1876 * Returns the CDMA ERI text,
1877 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001878 @Override
1879 public String getCdmaEriText(String callingPackage) {
1880 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001881 }
1882
Sanket Padawe356d7632015-06-22 14:03:32 -07001883 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001884 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1885 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1886 return null;
1887 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001888 final Phone phone = getPhone(subId);
1889 if (phone != null) {
1890 return phone.getCdmaEriText();
1891 } else {
1892 return null;
1893 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001894 }
1895
1896 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001897 * Returns the CDMA MDN.
1898 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001900 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001901 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001902 final Phone phone = getPhone(subId);
1903 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1904 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001905 } else {
1906 return null;
1907 }
1908 }
1909
1910 /**
1911 * Returns the CDMA MIN.
1912 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001913 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001914 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001915 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001916 final Phone phone = getPhone(subId);
1917 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1918 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001919 } else {
1920 return null;
1921 }
1922 }
1923
1924 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001925 * Returns true if CDMA provisioning needs to run.
1926 */
1927 public boolean needsOtaServiceProvisioning() {
1928 return mPhone.needsOtaServiceProvisioning();
1929 }
1930
1931 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001932 * Sets the voice mail number of a given subId.
1933 */
1934 @Override
1935 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001936 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001937 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1938 new Pair<String, String>(alphaTag, number), new Integer(subId));
1939 return success;
1940 }
1941
Ta-wei Yen87c49842016-05-13 21:19:52 -07001942 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001943 public void setVisualVoicemailEnabled(String callingPackage,
1944 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1945 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1946 if (!TextUtils.equals(callingPackage,
1947 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1948 enforceModifyPermissionOrCarrierPrivilege(
1949 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1950 }
1951 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1952 }
1953
1954 @Override
1955 public boolean isVisualVoicemailEnabled(String callingPackage,
1956 PhoneAccountHandle phoneAccountHandle) {
1957 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1958 return false;
1959 }
1960 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1961 }
1962
1963 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001964 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1965 VisualVoicemailSmsFilterSettings settings) {
1966 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001967 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001968 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1969 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001970 }
1971
1972 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001973 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1974 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001975 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001976 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001977 }
1978
1979 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001980 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1981 String callingPackage, int subId) {
1982 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001983 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001984 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001985 }
1986
1987 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001988 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1989 String packageName, int subId) {
1990 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001991 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001992 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001993 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001994 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001995 * Returns the unread count of voicemails
1996 */
1997 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001998 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001999 }
2000
2001 /**
2002 * Returns the unread count of voicemails for a subId
2003 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002004 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002005 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002006 final Phone phone = getPhone(subId);
2007 if (phone != null) {
2008 return phone.getVoiceMessageCount();
2009 } else {
2010 return 0;
2011 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002012 }
2013
2014 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002015 * Returns the data network type.
2016 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002017 *
2018 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2019 */
2020 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002021 public int getNetworkType() {
2022 final Phone phone = getPhone(getDefaultSubscription());
2023 if (phone != null) {
2024 return phone.getServiceState().getDataNetworkType();
2025 } else {
2026 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2027 }
Wink Saville36469e72014-06-11 15:17:00 -07002028 }
2029
2030 /**
2031 * Returns the network type for a subId
2032 */
2033 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002034 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2035 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2036 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2037 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002038
Sanket Padawe356d7632015-06-22 14:03:32 -07002039 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002040 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002041 return phone.getServiceState().getDataNetworkType();
2042 } else {
2043 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2044 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002045 }
2046
2047 /**
2048 * Returns the data network type
2049 */
2050 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002051 public int getDataNetworkType(String callingPackage) {
2052 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002053 }
2054
2055 /**
2056 * Returns the data network type for a subId
2057 */
2058 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002059 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2060 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2061 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2062 }
2063
Sanket Padawe356d7632015-06-22 14:03:32 -07002064 final Phone phone = getPhone(subId);
2065 if (phone != null) {
2066 return phone.getServiceState().getDataNetworkType();
2067 } else {
2068 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2069 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002070 }
2071
2072 /**
Wink Saville36469e72014-06-11 15:17:00 -07002073 * Returns the Voice network type for a subId
2074 */
2075 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002076 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2077 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2078 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2079 }
2080
Sanket Padawe356d7632015-06-22 14:03:32 -07002081 final Phone phone = getPhone(subId);
2082 if (phone != null) {
2083 return phone.getServiceState().getVoiceNetworkType();
2084 } else {
2085 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2086 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002087 }
2088
2089 /**
2090 * @return true if a ICC card is present
2091 */
2092 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002093 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002094 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002095 }
2096
2097 /**
2098 * @return true if a ICC card is present for a slotId
2099 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002100 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002101 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002102 final Phone phone = PhoneFactory.getPhone(slotId);
2103 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002104 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002105 } else {
2106 return false;
2107 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002108 }
2109
2110 /**
2111 * Return if the current radio is LTE on CDMA. This
2112 * is a tri-state return value as for a period of time
2113 * the mode may be unknown.
2114 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002115 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002116 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002117 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002118 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002119 @Override
2120 public int getLteOnCdmaMode(String callingPackage) {
2121 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002122 }
2123
Sanket Padawe356d7632015-06-22 14:03:32 -07002124 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002125 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2126 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2127 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2128 }
2129
Sanket Padawe356d7632015-06-22 14:03:32 -07002130 final Phone phone = getPhone(subId);
2131 if (phone == null) {
2132 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2133 } else {
2134 return phone.getLteOnCdmaMode();
2135 }
Wink Saville36469e72014-06-11 15:17:00 -07002136 }
2137
2138 public void setPhone(Phone phone) {
2139 mPhone = phone;
2140 }
2141
2142 /**
2143 * {@hide}
2144 * Returns Default subId, 0 in the case of single standby.
2145 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002146 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002147 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002148 }
2149
Shishir Agrawala9f32182016-04-12 12:00:16 -07002150 private int getSlotForDefaultSubscription() {
2151 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2152 }
2153
Wink Savilleb564aae2014-10-23 10:18:09 -07002154 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002155 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002156 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002157
2158 /**
2159 * @see android.telephony.TelephonyManager.WifiCallingChoices
2160 */
2161 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002162 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2163 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002164 }
2165
2166 /**
2167 * @see android.telephony.TelephonyManager.WifiCallingChoices
2168 */
2169 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002170 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2171 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2172 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002173 }
2174
Sailesh Nepald1e68152013-12-12 19:08:02 -08002175 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002176 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002177 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002178 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002179
Shishir Agrawal566b7612013-10-28 14:41:00 -07002180 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002181 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2182 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002183
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002184 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002185 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002186 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002187 if (DBG) log("iccOpenLogicalChannel: " + response);
2188 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002189 }
2190
2191 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002192 public boolean iccCloseLogicalChannel(int subId, int channel) {
2193 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002194
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002195 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002196 if (channel < 0) {
2197 return false;
2198 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002199 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002200 if (DBG) log("iccCloseLogicalChannel: " + success);
2201 return success;
2202 }
2203
2204 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002205 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002206 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002207 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002208
2209 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002210 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2211 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002212 " data=" + data);
2213 }
2214
2215 if (channel < 0) {
2216 return "";
2217 }
2218
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002219 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002220 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002221 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2222
Shishir Agrawal566b7612013-10-28 14:41:00 -07002223 // Append the returned status code to the end of the response payload.
2224 String s = Integer.toHexString(
2225 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002226 if (response.payload != null) {
2227 s = IccUtils.bytesToHexString(response.payload) + s;
2228 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002229 return s;
2230 }
Jake Hambye994d462014-02-03 13:10:13 -08002231
Evan Charltonc66da362014-05-16 14:06:40 -07002232 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002233 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002234 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002235 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002236
2237 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002238 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2239 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002240 }
2241
2242 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002243 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002244 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2245
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002246 // Append the returned status code to the end of the response payload.
2247 String s = Integer.toHexString(
2248 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002249 if (response.payload != null) {
2250 s = IccUtils.bytesToHexString(response.payload) + s;
2251 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002252 return s;
2253 }
2254
2255 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002256 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002257 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002258 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002259
2260 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002261 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002262 p1 + " " + p2 + " " + p3 + ":" + filePath);
2263 }
2264
2265 IccIoResult response =
2266 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002267 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2268 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002269
2270 if (DBG) {
2271 log("Exchange SIM_IO [R]" + response);
2272 }
2273
2274 byte[] result = null;
2275 int length = 2;
2276 if (response.payload != null) {
2277 length = 2 + response.payload.length;
2278 result = new byte[length];
2279 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2280 } else {
2281 result = new byte[length];
2282 }
2283
2284 result[length - 1] = (byte) response.sw2;
2285 result[length - 2] = (byte) response.sw1;
2286 return result;
2287 }
2288
2289 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002290 public String sendEnvelopeWithStatus(int subId, String content) {
2291 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002292
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002293 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002294 if (response.payload == null) {
2295 return "";
2296 }
2297
2298 // Append the returned status code to the end of the response payload.
2299 String s = Integer.toHexString(
2300 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2301 s = IccUtils.bytesToHexString(response.payload) + s;
2302 return s;
2303 }
2304
Jake Hambye994d462014-02-03 13:10:13 -08002305 /**
2306 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2307 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2308 *
2309 * @param itemID the ID of the item to read
2310 * @return the NV item as a String, or null on error.
2311 */
2312 @Override
2313 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002314 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002315 if (DBG) log("nvReadItem: item " + itemID);
2316 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2317 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2318 return value;
2319 }
2320
2321 /**
2322 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2323 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2324 *
2325 * @param itemID the ID of the item to read
2326 * @param itemValue the value to write, as a String
2327 * @return true on success; false on any failure
2328 */
2329 @Override
2330 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002331 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002332 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2333 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2334 new Pair<Integer, String>(itemID, itemValue));
2335 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2336 return success;
2337 }
2338
2339 /**
2340 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2341 * Used for device configuration by some CDMA operators.
2342 *
2343 * @param preferredRoamingList byte array containing the new PRL
2344 * @return true on success; false on any failure
2345 */
2346 @Override
2347 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002348 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002349 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2350 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2351 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2352 return success;
2353 }
2354
2355 /**
2356 * Perform the specified type of NV config reset.
2357 * Used for device configuration by some CDMA operators.
2358 *
2359 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2360 * @return true on success; false on any failure
2361 */
2362 @Override
2363 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002364 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002365 if (DBG) log("nvResetConfig: type " + resetType);
2366 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2367 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2368 return success;
2369 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002370
2371 /**
Wink Saville36469e72014-06-11 15:17:00 -07002372 * {@hide}
2373 * Returns Default sim, 0 in the case of single standby.
2374 */
2375 public int getDefaultSim() {
2376 //TODO Need to get it from Telephony Devcontroller
2377 return 0;
2378 }
2379
Svet Ganovb320e182015-04-16 12:30:10 -07002380 public String[] getPcscfAddress(String apnType, String callingPackage) {
2381 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2382 return new String[0];
2383 }
2384
2385
ram87fca6f2014-07-18 18:58:44 +05302386 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002387 }
2388
2389 public void setImsRegistrationState(boolean registered) {
2390 enforceModifyPermission();
2391 mPhone.setImsRegistrationState(registered);
2392 }
2393
2394 /**
Stuart Scott54788802015-03-30 13:18:01 -07002395 * Set the network selection mode to automatic.
2396 *
2397 */
2398 @Override
2399 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002401 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2402 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2403 }
2404
2405 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002406 * Set the network selection mode to manual with the selected carrier.
2407 */
2408 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002409 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2410 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002411 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002412 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002413 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2414 persistSelection);
2415 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002416 }
2417
2418 /**
2419 * Scans for available networks.
2420 */
2421 @Override
2422 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002423 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002424 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2425 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2426 CMD_PERFORM_NETWORK_SCAN, null, subId);
2427 return result;
2428 }
2429
2430 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002431 * Get the calculated preferred network type.
2432 * Used for debugging incorrect network type.
2433 *
2434 * @return the preferred network type, defined in RILConstants.java.
2435 */
2436 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002437 public int getCalculatedPreferredNetworkType(String callingPackage) {
2438 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2439 return RILConstants.PREFERRED_NETWORK_MODE;
2440 }
2441
Amit Mahajan43330e02014-11-18 11:54:45 -08002442 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002443 }
2444
2445 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002446 * Get the preferred network type.
2447 * Used for device configuration by some CDMA operators.
2448 *
2449 * @return the preferred network type, defined in RILConstants.java.
2450 */
2451 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002452 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002453 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002454 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002455 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002456 int networkType = (result != null ? result[0] : -1);
2457 if (DBG) log("getPreferredNetworkType: " + networkType);
2458 return networkType;
2459 }
2460
2461 /**
2462 * Set the preferred network type.
2463 * Used for device configuration by some CDMA operators.
2464 *
2465 * @param networkType the preferred network type, defined in RILConstants.java.
2466 * @return true on success; false on any failure.
2467 */
2468 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002469 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002470 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002471 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2472 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002473 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002474 if (success) {
2475 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002476 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002477 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002478 return success;
2479 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002480
2481 /**
Junda Liu475951f2014-11-07 16:45:03 -08002482 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2483 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2484 * tethering.
2485 *
2486 * @return 0: Not required. 1: required. 2: Not set.
2487 * @hide
2488 */
2489 @Override
2490 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002491 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002492 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2493 Settings.Global.TETHER_DUN_REQUIRED, 2);
2494 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2495 // config_tether_apndata.
2496 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2497 dunRequired = 1;
2498 }
2499 return dunRequired;
2500 }
2501
2502 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002503 * Set mobile data enabled
2504 * Used by the user through settings etc to turn on/off mobile data
2505 *
2506 * @param enable {@code true} turn turn data on, else {@code false}
2507 */
2508 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002509 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002510 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002511 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002512 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002513 Phone phone = PhoneFactory.getPhone(phoneId);
2514 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002515 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002516 phone.setDataEnabled(enable);
2517 } else {
2518 loge("setDataEnabled: no phone for subId=" + subId);
2519 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002520 }
2521
2522 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002523 * Get whether mobile data is enabled.
2524 *
2525 * Note that this used to be available from ConnectivityService, gated by
2526 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2527 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002528 *
2529 * @return {@code true} if data is enabled else {@code false}
2530 */
2531 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002532 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002533 try {
2534 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2535 null);
2536 } catch (Exception e) {
2537 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2538 null);
2539 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002540 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002541 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002542 Phone phone = PhoneFactory.getPhone(phoneId);
2543 if (phone != null) {
2544 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002545 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002546 return retVal;
2547 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002548 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002549 return false;
2550 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002551 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002552
2553 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002554 public int getCarrierPrivilegeStatus(int subId) {
2555 final Phone phone = getPhone(subId);
2556 if (phone == null) {
2557 loge("getCarrierPrivilegeStatus: Invalid subId");
2558 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2559 }
2560 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002561 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002562 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002563 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2564 }
2565 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002566 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002567 }
Junda Liu29340342014-07-10 15:23:27 -07002568
2569 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002570 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002571 if (TextUtils.isEmpty(pkgName))
2572 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002573 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002574 if (card == null) {
2575 loge("checkCarrierPrivilegesForPackage: No UICC");
2576 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2577 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002578 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2579 }
2580
2581 @Override
2582 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002583 if (TextUtils.isEmpty(pkgName))
2584 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002585 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2586 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2587 UiccCard card = UiccController.getInstance().getUiccCard(i);
2588 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002589 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002590 continue;
2591 }
2592
2593 result = card.getCarrierPrivilegeStatus(
2594 mPhone.getContext().getPackageManager(), pkgName);
2595 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2596 break;
2597 }
2598 }
2599
2600 return result;
Junda Liu29340342014-07-10 15:23:27 -07002601 }
Derek Tan89e89d42014-07-08 17:00:10 -07002602
2603 @Override
Junda Liue64de782015-04-16 17:19:16 -07002604 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2605 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2606 loge("phoneId " + phoneId + " is not valid.");
2607 return null;
2608 }
2609 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002610 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002611 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002612 return null ;
2613 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002614 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002615 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002616 }
2617
Amith Yamasani6e118872016-02-19 12:53:51 -08002618 @Override
2619 public List<String> getPackagesWithCarrierPrivileges() {
2620 PackageManager pm = mPhone.getContext().getPackageManager();
2621 List<String> privilegedPackages = new ArrayList<>();
2622 List<PackageInfo> packages = null;
2623 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2624 UiccCard card = UiccController.getInstance().getUiccCard(i);
2625 if (card == null) {
2626 // No UICC in that slot.
2627 continue;
2628 }
2629 if (card.hasCarrierPrivilegeRules()) {
2630 if (packages == null) {
2631 // Only check packages in user 0 for now
2632 packages = pm.getInstalledPackagesAsUser(
2633 PackageManager.MATCH_DISABLED_COMPONENTS
2634 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2635 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2636 }
2637 for (int p = packages.size() - 1; p >= 0; p--) {
2638 PackageInfo pkgInfo = packages.get(p);
2639 if (pkgInfo != null && pkgInfo.packageName != null
2640 && card.getCarrierPrivilegeStatus(pkgInfo)
2641 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2642 privilegedPackages.add(pkgInfo.packageName);
2643 }
2644 }
2645 }
2646 }
2647 return privilegedPackages;
2648 }
2649
Wink Savilleb564aae2014-10-23 10:18:09 -07002650 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002651 final Phone phone = getPhone(subId);
2652 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002653 if (card == null) {
2654 loge("getIccId: No UICC");
2655 return null;
2656 }
2657 String iccId = card.getIccId();
2658 if (TextUtils.isEmpty(iccId)) {
2659 loge("getIccId: ICC ID is null or empty.");
2660 return null;
2661 }
2662 return iccId;
2663 }
2664
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002665 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002666 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2667 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002668 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002669
Jeff Sharkey85190e62014-12-05 09:40:12 -08002670 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002671 final Phone phone = getPhone(subId);
2672 if (phone == null) {
2673 return false;
2674 }
2675 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002676
2677 if (DBG_MERGE) {
2678 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2679 + subscriberId + " to " + number);
2680 }
2681
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002682 if (TextUtils.isEmpty(iccId)) {
2683 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002684 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002685
Jeff Sharkey85190e62014-12-05 09:40:12 -08002686 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2687
2688 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002689 if (alphaTag == null) {
2690 editor.remove(alphaTagPrefKey);
2691 } else {
2692 editor.putString(alphaTagPrefKey, alphaTag);
2693 }
2694
Jeff Sharkey85190e62014-12-05 09:40:12 -08002695 // Record both the line number and IMSI for this ICCID, since we need to
2696 // track all merged IMSIs based on line number
2697 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2698 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002699 if (number == null) {
2700 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002701 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002702 } else {
2703 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002704 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002705 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002706
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002707 editor.commit();
2708 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002709 }
2710
2711 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002712 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002713 // This is open to apps with WRITE_SMS.
2714 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002715 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002716 return null;
2717 }
Derek Tan97ebb422014-09-05 16:55:38 -07002718
2719 String iccId = getIccId(subId);
2720 if (iccId != null) {
2721 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002722 if (DBG_MERGE) {
2723 log("getLine1NumberForDisplay returning " +
2724 mTelephonySharedPreferences.getString(numberPrefKey, null));
2725 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002726 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002727 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002728 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002729 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002730 }
2731
2732 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002733 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2734 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2735 return null;
2736 }
Derek Tan97ebb422014-09-05 16:55:38 -07002737
2738 String iccId = getIccId(subId);
2739 if (iccId != null) {
2740 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002741 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002742 }
Derek Tan97ebb422014-09-05 16:55:38 -07002743 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002744 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002745
2746 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002747 public String[] getMergedSubscriberIds(String callingPackage) {
2748 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2749 return null;
2750 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002751 final Context context = mPhone.getContext();
2752 final TelephonyManager tele = TelephonyManager.from(context);
2753 final SubscriptionManager sub = SubscriptionManager.from(context);
2754
2755 // Figure out what subscribers are currently active
2756 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002757 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2758 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2759 final long identity = Binder.clearCallingIdentity();
2760 try {
2761 final int[] subIds = sub.getActiveSubscriptionIdList();
2762 for (int subId : subIds) {
2763 activeSubscriberIds.add(tele.getSubscriberId(subId));
2764 }
2765 } finally {
2766 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002767 }
2768
2769 // First pass, find a number override for an active subscriber
2770 String mergeNumber = null;
2771 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2772 for (String key : prefs.keySet()) {
2773 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2774 final String subscriberId = (String) prefs.get(key);
2775 if (activeSubscriberIds.contains(subscriberId)) {
2776 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2777 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2778 mergeNumber = (String) prefs.get(numberKey);
2779 if (DBG_MERGE) {
2780 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2781 + " for active subscriber " + subscriberId);
2782 }
2783 if (!TextUtils.isEmpty(mergeNumber)) {
2784 break;
2785 }
2786 }
2787 }
2788 }
2789
2790 // Shortcut when no active merged subscribers
2791 if (TextUtils.isEmpty(mergeNumber)) {
2792 return null;
2793 }
2794
2795 // Second pass, find all subscribers under that line override
2796 final ArraySet<String> result = new ArraySet<>();
2797 for (String key : prefs.keySet()) {
2798 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2799 final String number = (String) prefs.get(key);
2800 if (mergeNumber.equals(number)) {
2801 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2802 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2803 final String subscriberId = (String) prefs.get(subscriberKey);
2804 if (!TextUtils.isEmpty(subscriberId)) {
2805 result.add(subscriberId);
2806 }
2807 }
2808 }
2809 }
2810
2811 final String[] resultArray = result.toArray(new String[result.size()]);
2812 Arrays.sort(resultArray);
2813 if (DBG_MERGE) {
2814 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2815 }
2816 return resultArray;
2817 }
2818
2819 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002820 public boolean setOperatorBrandOverride(int subId, String brand) {
2821 enforceCarrierPrivilege(subId);
2822 final Phone phone = getPhone(subId);
2823 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002824 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002825
2826 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002827 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002828 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2829 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002830 enforceCarrierPrivilege(subId);
2831 final Phone phone = getPhone(subId);
2832 if (phone == null) {
2833 return false;
2834 }
2835 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002836 cdmaNonRoamingList);
2837 }
2838
2839 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002840 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2841 enforceModifyPermission();
2842
2843 int returnValue = 0;
2844 try {
2845 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2846 if(result.exception == null) {
2847 if (result.result != null) {
2848 byte[] responseData = (byte[])(result.result);
2849 if(responseData.length > oemResp.length) {
2850 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2851 responseData.length + "bytes. Buffer Size is " +
2852 oemResp.length + "bytes.");
2853 }
2854 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2855 returnValue = responseData.length;
2856 }
2857 } else {
2858 CommandException ex = (CommandException) result.exception;
2859 returnValue = ex.getCommandError().ordinal();
2860 if(returnValue > 0) returnValue *= -1;
2861 }
2862 } catch (RuntimeException e) {
2863 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2864 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2865 if(returnValue > 0) returnValue *= -1;
2866 }
2867
2868 return returnValue;
2869 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002870
2871 @Override
2872 public void setRadioCapability(RadioAccessFamily[] rafs) {
2873 try {
2874 ProxyController.getInstance().setRadioCapability(rafs);
2875 } catch (RuntimeException e) {
2876 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2877 }
2878 }
2879
2880 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002881 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2882 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2883 return RadioAccessFamily.RAF_UNKNOWN;
2884 }
2885
Wink Saville5d475dd2014-10-17 15:00:58 -07002886 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2887 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002888
2889 @Override
2890 public void enableVideoCalling(boolean enable) {
2891 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002892 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002893 }
2894
2895 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002896 public boolean isVideoCallingEnabled(String callingPackage) {
2897 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2898 return false;
2899 }
2900
Andrew Lee77527ac2014-10-21 16:57:39 -07002901 // Check the user preference and the system-level IMS setting. Even if the user has
2902 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2903 // In the long run, we may instead need to check if there exists a connection service
2904 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002905 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2906 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002907 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002908 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002909
Andrew Leea1239f22015-03-02 17:44:07 -08002910 @Override
2911 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002912 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002913 }
2914
2915 @Override
2916 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002917 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002918 }
2919
Andrew Lee9431b832015-03-09 18:46:45 -07002920 @Override
2921 public boolean isTtyModeSupported() {
2922 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2923 TelephonyManager telephonyManager =
2924 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002925 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002926 }
2927
2928 @Override
2929 public boolean isHearingAidCompatibilitySupported() {
2930 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2931 }
2932
Sanket Padawe7310cc72015-01-14 09:53:20 -08002933 /**
2934 * Returns the unique device ID of phone, for example, the IMEI for
2935 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2936 *
2937 * <p>Requires Permission:
2938 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2939 */
2940 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002941 public String getDeviceId(String callingPackage) {
2942 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2943 return null;
2944 }
2945
Sanket Padawe7310cc72015-01-14 09:53:20 -08002946 final Phone phone = PhoneFactory.getPhone(0);
2947 if (phone != null) {
2948 return phone.getDeviceId();
2949 } else {
2950 return null;
2951 }
2952 }
2953
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002954 /*
2955 * {@hide}
2956 * Returns the IMS Registration Status
2957 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002958 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002959 public boolean isImsRegistered() {
2960 return mPhone.isImsRegistered();
2961 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002962
2963 @Override
2964 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2965 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2966 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002967
Nathan Haroldc55097a2015-03-11 18:14:50 -07002968 /*
2969 * {@hide}
2970 * Returns the IMS Registration Status
2971 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002972 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002973 return mPhone.isWifiCallingEnabled();
2974 }
2975
2976 /*
2977 * {@hide}
2978 * Returns the IMS Registration Status
2979 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002980 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002981 return mPhone.isVolteEnabled();
2982 }
Svet Ganovb320e182015-04-16 12:30:10 -07002983
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002984 /*
2985 * {@hide} Returns the IMS Registration Status
2986 */
2987 public boolean isVideoTelephonyAvailable() {
2988 return mPhone.isVideoEnabled();
2989 }
2990
Svet Ganovb320e182015-04-16 12:30:10 -07002991 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002992 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002993 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002994 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2995
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002996 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002997 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002998 } catch (SecurityException e) {
2999 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3000 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003001 }
Svet Ganovb320e182015-04-16 12:30:10 -07003002
3003 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3004 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3005 return false;
3006 }
3007
3008 return true;
3009 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003010
Makoto Onukifee69342015-06-29 14:44:50 -07003011 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003012 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003013 */
3014 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003015 // Default SMS app can always read it.
3016 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3017 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3018 return true;
3019 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003020
Makoto Onukie4072d12015-08-03 15:12:23 -07003021 try {
3022 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003023 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003024 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003025 // Can be read with READ_SMS too.
3026 try {
3027 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3028 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3029 if (opCode != AppOpsManager.OP_NONE) {
3030 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3031 == AppOpsManager.MODE_ALLOWED;
3032 } else {
3033 return true;
3034 }
3035 } catch (SecurityException readSmsSecurityException) {
3036 }
3037 // Can be read with READ_PHONE_NUMBER too.
3038 try {
3039 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBER,
3040 message);
3041 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBER);
3042 if (opCode != AppOpsManager.OP_NONE) {
3043 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3044 == AppOpsManager.MODE_ALLOWED;
3045 } else {
3046 return true;
3047 }
3048 } catch (SecurityException readPhoneNumberSecurityException) {
3049 }
3050
3051 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3052 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3053 ", " + android.Manifest.permission.READ_SMS + ", or " +
3054 android.Manifest.permission.READ_PHONE_NUMBER);
Makoto Onukifee69342015-06-29 14:44:50 -07003055 }
3056
Stuart Scott8eef64f2015-04-08 15:13:54 -07003057 @Override
3058 public void factoryReset(int subId) {
3059 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003060 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3061 return;
3062 }
3063
Svet Ganovcc087f82015-05-12 20:35:54 -07003064 final long identity = Binder.clearCallingIdentity();
3065 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003066 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3067 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003068 // Enable data
3069 setDataEnabled(subId, true);
3070 // Set network selection mode to automatic
3071 setNetworkSelectionModeAutomatic(subId);
3072 // Set preferred mobile network type to the best available
3073 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3074 // Turn off roaming
3075 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3076 }
3077 } finally {
3078 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003079 }
3080 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003081
3082 @Override
3083 public String getLocaleFromDefaultSim() {
3084 // We query all subscriptions instead of just the active ones, because
3085 // this might be called early on in the provisioning flow when the
3086 // subscriptions potentially aren't active yet.
3087 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3088 if (slist == null || slist.isEmpty()) {
3089 return null;
3090 }
3091
3092 // This function may be called very early, say, from the setup wizard, at
3093 // which point we won't have a default subscription set. If that's the case
3094 // we just choose the first, which will be valid in "most cases".
3095 final int defaultSubId = getDefaultSubscription();
3096 SubscriptionInfo info = null;
3097 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3098 info = slist.get(0);
3099 } else {
3100 for (SubscriptionInfo item : slist) {
3101 if (item.getSubscriptionId() == defaultSubId) {
3102 info = item;
3103 break;
3104 }
3105 }
3106
3107 if (info == null) {
3108 return null;
3109 }
3110 }
3111
3112 // Try and fetch the locale from the carrier properties or from the SIM language
3113 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003114 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003115 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003116 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003117 if (defaultPhone != null) {
3118 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3119 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003120 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003121 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3122 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003123 } else {
3124 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003125 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003126 }
3127 }
3128
Narayan Kamath011676f2015-07-29 12:04:08 +01003129 // The SIM language preferences only store a language (e.g. fr = French), not an
3130 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3131 // the SIM and carrier preferences does not include a country we add the country
3132 // determined from the SIM MCC to provide an exact locale.
3133 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003134 if (mccLocale != null) {
3135 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3136 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003137 }
3138
Tony Hill183b2de2015-06-24 14:53:58 +01003139 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003140 return null;
3141 }
3142
3143 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3144 final long identity = Binder.clearCallingIdentity();
3145 try {
3146 return mSubscriptionController.getAllSubInfoList(
3147 mPhone.getContext().getOpPackageName());
3148 } finally {
3149 Binder.restoreCallingIdentity(identity);
3150 }
3151 }
3152
3153 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3154 final long identity = Binder.clearCallingIdentity();
3155 try {
3156 return mSubscriptionController.getActiveSubscriptionInfoList(
3157 mPhone.getContext().getOpPackageName());
3158 } finally {
3159 Binder.restoreCallingIdentity(identity);
3160 }
3161 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003162
3163 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003164 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3165 * representing the state of the modem.
3166 *
3167 * NOTE: This clears the modem state, so there should only every be one caller.
3168 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003169 */
3170 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003171 public void requestModemActivityInfo(ResultReceiver result) {
3172 enforceModifyPermission();
3173
3174 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3175 Bundle bundle = new Bundle();
3176 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3177 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003178 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003179
3180 /**
3181 * {@hide}
3182 * Returns the service state information on specified subscription.
3183 */
3184 @Override
3185 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3186
3187 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3188 return null;
3189 }
3190
3191 final Phone phone = getPhone(subId);
3192 if (phone == null) {
3193 return null;
3194 }
3195
3196 return phone.getServiceState();
3197 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003198
3199 /**
3200 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3201 *
3202 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3203 * voicemail ringtone.
3204 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3205 * PhoneAccount.
3206 */
3207 @Override
3208 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3209 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3210 if (phone == null) {
3211 return null;
3212 }
3213
3214 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3215 }
3216
3217 /**
3218 * Returns whether vibration is set for voicemail notification in Phone settings.
3219 *
3220 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3221 * voicemail vibration setting.
3222 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3223 */
3224 @Override
3225 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3226 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3227 if (phone == null) {
3228 return false;
3229 }
3230
3231 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3232 }
3233
Youhan Wange64578a2016-05-02 15:32:42 -07003234 /**
3235 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3236 *
3237 * @throws SecurityException if the caller does not have the required permission
3238 */
3239 private void enforceReadPrivilegedPermission() {
3240 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3241 null);
3242 }
3243
3244 /**
3245 * Return the application ID for the app type.
3246 *
3247 * @param subId the subscription ID that this request applies to.
3248 * @param appType the uicc app type.
3249 * @return Application ID for specificied app type, or null if no uicc.
3250 */
3251 @Override
3252 public String getAidForAppType(int subId, int appType) {
3253 enforceReadPrivilegedPermission();
3254 Phone phone = getPhone(subId);
3255 if (phone == null) {
3256 return null;
3257 }
3258 String aid = null;
3259 try {
3260 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3261 .getApplicationByType(appType).getAid();
3262 } catch (Exception e) {
3263 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3264 }
3265 return aid;
3266 }
3267
Youhan Wang4001d252016-05-11 10:29:41 -07003268 /**
3269 * Return the Electronic Serial Number.
3270 *
3271 * @param subId the subscription ID that this request applies to.
3272 * @return ESN or null if error.
3273 */
3274 @Override
3275 public String getEsn(int subId) {
3276 enforceReadPrivilegedPermission();
3277 Phone phone = getPhone(subId);
3278 if (phone == null) {
3279 return null;
3280 }
3281 String esn = null;
3282 try {
3283 esn = phone.getEsn();
3284 } catch (Exception e) {
3285 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3286 }
3287 return esn;
3288 }
3289
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003290 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003291 * Return the Preferred Roaming List Version.
3292 *
3293 * @param subId the subscription ID that this request applies to.
3294 * @return PRLVersion or null if error.
3295 */
3296 @Override
3297 public String getCdmaPrlVersion(int subId) {
3298 enforceReadPrivilegedPermission();
3299 Phone phone = getPhone(subId);
3300 if (phone == null) {
3301 return null;
3302 }
3303 String cdmaPrlVersion = null;
3304 try {
3305 cdmaPrlVersion = phone.getCdmaPrlVersion();
3306 } catch (Exception e) {
3307 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3308 }
3309 return cdmaPrlVersion;
3310 }
3311
3312 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003313 * Get snapshot of Telephony histograms
3314 * @return List of Telephony histograms
3315 * @hide
3316 */
3317 @Override
3318 public List<TelephonyHistogram> getTelephonyHistograms() {
3319 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3320 return RIL.getTelephonyRILTimingHistograms();
3321 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003322
3323 /**
3324 * {@hide}
3325 * Set the allowed carrier list for slotId
3326 * Require system privileges. In the future we may add this to carrier APIs.
3327 *
3328 * @return The number of carriers set successfully, should match length of carriers
3329 */
3330 @Override
3331 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3332 enforceModifyPermission();
3333 int subId = SubscriptionManager.getSubId(slotId)[0];
3334 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3335 return retVal[0];
3336 }
3337
3338 /**
3339 * {@hide}
3340 * Get the allowed carrier list for slotId.
3341 * Require system privileges. In the future we may add this to carrier APIs.
3342 *
3343 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3344 * means all carriers are allowed.
3345 */
3346 @Override
3347 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3348 enforceReadPrivilegedPermission();
3349 int subId = SubscriptionManager.getSubId(slotId)[0];
3350 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3351 }
3352
fionaxu59545b42016-05-25 15:53:37 -07003353 /**
3354 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3355 * @param subId the subscription ID that this action applies to.
3356 * @param enabled control enable or disable metered apns.
3357 * {@hide}
3358 */
3359 @Override
3360 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3361 enforceModifyPermission();
3362 final Phone phone = getPhone(subId);
3363 if (phone == null) {
3364 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3365 return;
3366 }
3367 try {
3368 phone.carrierActionSetMeteredApnsEnabled(enabled);
3369 } catch (Exception e) {
3370 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3371 }
3372 }
3373
3374 /**
3375 * Action set from carrier signalling broadcast receivers to enable/disable radio
3376 * @param subId the subscription ID that this action applies to.
3377 * @param enabled control enable or disable radio.
3378 * {@hide}
3379 */
3380 @Override
3381 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3382 enforceModifyPermission();
3383 final Phone phone = getPhone(subId);
3384 if (phone == null) {
3385 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3386 return;
3387 }
3388 try {
3389 phone.carrierActionSetRadioEnabled(enabled);
3390 } catch (Exception e) {
3391 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3392 }
3393 }
3394
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003395 /**
3396 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3397 * bug report is being generated.
3398 */
3399 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003400 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003401 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3402 != PackageManager.PERMISSION_GRANTED) {
3403 writer.println("Permission Denial: can't dump Phone from pid="
3404 + Binder.getCallingPid()
3405 + ", uid=" + Binder.getCallingUid()
3406 + "without permission "
3407 + android.Manifest.permission.DUMP);
3408 return;
3409 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003410 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003411 }
Jack Yueb89b242016-06-22 13:27:47 -07003412
3413 /**
3414 * Get aggregated video call data usage from all subscriptions since boot.
3415 * @return total data usage in bytes
3416 * {@hide}
3417 */
3418 @Override
3419 public long getVtDataUsage() {
3420 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3421 null);
3422
3423 // NetworkStatsService keeps tracking the active network interface and identity. It will
3424 // record the delta with the corresponding network identity. What we need to do here is
3425 // returning total video call data usage from all subscriptions since boot.
3426
3427 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3428 // simultaneous VT calls.
3429 final Phone[] phones = PhoneFactory.getPhones();
3430 long total = 0;
3431 for (Phone phone : phones) {
3432 total += phone.getVtDataUsage();
3433 }
3434 return total;
3435 }
Jack Yu75ab2952016-07-08 14:29:33 -07003436
3437 /**
3438 * Policy control of data connection. Usually used when data limit is passed.
3439 * @param enabled True if enabling the data, otherwise disabling.
3440 * @param subId Subscription index
3441 * {@hide}
3442 */
3443 @Override
3444 public void setPolicyDataEnabled(boolean enabled, int subId) {
3445 enforceModifyPermission();
3446 Phone phone = getPhone(subId);
3447 if (phone != null) {
3448 phone.setPolicyDataEnabled(enabled);
3449 }
3450 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003451
3452 /**
3453 * Get Client request stats
3454 * @return List of Client Request Stats
3455 * @hide
3456 */
3457 @Override
3458 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3459 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3460 return null;
3461 }
3462
3463 Phone phone = getPhone(subId);
3464 if (phone != null) {
3465 return phone.getClientRequestStats();
3466 }
3467
3468 return null;
3469 }
3470
3471 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3472 if (workSource != null) {
3473 return workSource;
3474 }
3475
3476 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3477 workSource = new WorkSource(uid, packageName);
3478 return workSource;
3479 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003480}