blob: 1f15cc7106d30a330fb22edfa5fa0d61b64313d4 [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
pkanwar32d516d2016-10-14 19:37:38 -07001489 public void handleUssdRequest(String ussdRequest, ResultReceiver wrappedCallback) {
1490 enforceCallPermission();
1491 int subId = getDefaultSubscription();
1492 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1493 return;
1494 }
1495 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1496 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1497 };
1498
1499
Wink Savilleb564aae2014-10-23 10:18:09 -07001500 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001502 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1503 return false;
1504 }
Wink Saville36469e72014-06-11 15:17:00 -07001505 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 }
1507
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001509 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001510 }
1511
Shishir Agrawala9f32182016-04-12 12:00:16 -07001512 public int getCallStateForSlot(int slotId) {
1513 Phone phone = PhoneFactory.getPhone(slotId);
1514 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1515 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001516 }
1517
Sanket Padawe356d7632015-06-22 14:03:32 -07001518 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001519 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001520 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001521 if (phone != null) {
1522 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1523 } else {
1524 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1525 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 }
1527
Sanket Padawe356d7632015-06-22 14:03:32 -07001528 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001530 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001531 if (phone != null) {
1532 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1533 } else {
1534 return TelephonyManager.DATA_ACTIVITY_NONE;
1535 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 }
1537
1538 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001539 public Bundle getCellLocation(String callingPackage) {
1540 enforceFineOrCoarseLocationPermission("getCellLocation");
1541
1542 // OP_COARSE_LOCATION controls both fine and coarse location.
1543 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1544 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001545 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001546 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 }
1548
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001549 if (checkIfCallerIsSelfOrForegroundUser() ||
1550 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001551 if (DBG_LOC) log("getCellLocation: is active user");
1552 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001553 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001554 if (phone == null) {
1555 return null;
1556 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001557
1558 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1559 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 return data;
1561 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001562 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563 return null;
1564 }
1565 }
1566
Svetoslav64fad262015-04-14 14:35:21 -07001567 private void enforceFineOrCoarseLocationPermission(String message) {
1568 try {
1569 mApp.enforceCallingOrSelfPermission(
1570 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1571 } catch (SecurityException e) {
1572 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1573 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1574 // is the weaker precondition
1575 mApp.enforceCallingOrSelfPermission(
1576 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1577 }
1578 }
1579
1580
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 @Override
1582 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001583 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001584 }
1585
Sanket Padawe356d7632015-06-22 14:03:32 -07001586 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001587 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 mApp.enforceCallingOrSelfPermission(
1589 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001590 final Phone phone = getPhone(subId);
1591 if (phone != null) {
1592 phone.enableLocationUpdates();
1593 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 }
1595
1596 @Override
1597 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001598 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001599 }
1600
Sanket Padawe356d7632015-06-22 14:03:32 -07001601 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001602 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 mApp.enforceCallingOrSelfPermission(
1604 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001605 final Phone phone = getPhone(subId);
1606 if (phone != null) {
1607 phone.disableLocationUpdates();
1608 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001609 }
1610
1611 @Override
1612 @SuppressWarnings("unchecked")
1613 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001614 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1615
1616 // OP_COARSE_LOCATION controls both fine and coarse location.
1617 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1618 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1619 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001620 }
1621
1622 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1623 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1624 return null;
1625 }
Svetoslav64fad262015-04-14 14:35:21 -07001626
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001627 if (checkIfCallerIsSelfOrForegroundUser() ||
1628 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1630
1631 ArrayList<NeighboringCellInfo> cells = null;
1632
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001633 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001634 try {
1635 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001636 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001637 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001638 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001639 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001640 }
1641 return cells;
1642 } else {
1643 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1644 return null;
1645 }
1646 }
1647
1648
1649 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001650 public List<CellInfo> getAllCellInfo(String callingPackage) {
1651 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1652
1653 // OP_COARSE_LOCATION controls both fine and coarse location.
1654 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1655 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1656 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 }
1658
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001659 if (checkIfCallerIsSelfOrForegroundUser() ||
1660 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001662 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001663 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1664 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001665 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1666 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001667 }
1668 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001669 } else {
1670 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1671 return null;
1672 }
1673 }
1674
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001675 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 public void setCellInfoListRate(int rateInMillis) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001677 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1678 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001679 }
1680
Shishir Agrawala9f32182016-04-12 12:00:16 -07001681 @Override
1682 public String getImeiForSlot(int slotId, String callingPackage) {
1683 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1684 return null;
1685 }
1686 Phone phone = PhoneFactory.getPhone(slotId);
1687 return phone == null ? null : phone.getImei();
1688 }
1689
1690 @Override
1691 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1692 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1693 return null;
1694 }
1695 Phone phone = PhoneFactory.getPhone(slotId);
1696 return phone == null ? null : phone.getDeviceSvn();
1697 }
1698
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001699 //
1700 // Internal helper methods.
1701 //
1702
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001703 /**
1704 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1705 */
1706 private boolean checkCallerInteractAcrossUsersFull() {
1707 return mPhone.getContext().checkCallingOrSelfPermission(
1708 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1709 == PackageManager.PERMISSION_GRANTED;
1710 }
1711
Jake Hambye994d462014-02-03 13:10:13 -08001712 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 boolean ok;
1714
1715 boolean self = Binder.getCallingUid() == Process.myUid();
1716 if (!self) {
1717 // Get the caller's user id then clear the calling identity
1718 // which will be restored in the finally clause.
1719 int callingUser = UserHandle.getCallingUserId();
1720 long ident = Binder.clearCallingIdentity();
1721
1722 try {
1723 // With calling identity cleared the current user is the foreground user.
1724 int foregroundUser = ActivityManager.getCurrentUser();
1725 ok = (foregroundUser == callingUser);
1726 if (DBG_LOC) {
1727 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1728 + " callingUser=" + callingUser + " ok=" + ok);
1729 }
1730 } catch (Exception ex) {
1731 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1732 ok = false;
1733 } finally {
1734 Binder.restoreCallingIdentity(ident);
1735 }
1736 } else {
1737 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1738 ok = true;
1739 }
1740 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1741 return ok;
1742 }
1743
1744 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001745 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1746 *
1747 * @throws SecurityException if the caller does not have the required permission
1748 */
1749 private void enforceModifyPermission() {
1750 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1751 }
1752
1753 /**
Junda Liua2e36012014-07-09 18:30:01 -07001754 * Make sure either system app or the caller has carrier privilege.
1755 *
1756 * @throws SecurityException if the caller does not have the required permission/privilege
1757 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001758 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001759 int permission = mApp.checkCallingOrSelfPermission(
1760 android.Manifest.permission.MODIFY_PHONE_STATE);
1761 if (permission == PackageManager.PERMISSION_GRANTED) {
1762 return;
1763 }
1764
1765 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001766 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001767 }
1768
1769 /**
1770 * Make sure the caller has carrier privilege.
1771 *
1772 * @throws SecurityException if the caller does not have the required permission
1773 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001774 private void enforceCarrierPrivilege(int subId) {
1775 if (getCarrierPrivilegeStatus(subId) !=
1776 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001777 loge("No Carrier Privilege.");
1778 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001779 }
1780 }
1781
1782 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001783 * Make sure the caller has the CALL_PHONE permission.
1784 *
1785 * @throws SecurityException if the caller does not have the required permission
1786 */
1787 private void enforceCallPermission() {
1788 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1789 }
1790
Stuart Scott8eef64f2015-04-08 15:13:54 -07001791 private void enforceConnectivityInternalPermission() {
1792 mApp.enforceCallingOrSelfPermission(
1793 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1794 "ConnectivityService");
1795 }
1796
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001797 private String createTelUrl(String number) {
1798 if (TextUtils.isEmpty(number)) {
1799 return null;
1800 }
1801
Jake Hambye994d462014-02-03 13:10:13 -08001802 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001803 }
1804
Ihab Awadf9e92732013-12-05 18:02:52 -08001805 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1807 }
1808
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001809 private static void logv(String msg) {
1810 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1811 }
1812
Ihab Awadf9e92732013-12-05 18:02:52 -08001813 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001814 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1815 }
1816
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001817 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001818 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001819 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001820 }
1821
Sanket Padawe356d7632015-06-22 14:03:32 -07001822 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001823 public int getActivePhoneTypeForSlot(int slotId) {
1824 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001825 if (phone == null) {
1826 return PhoneConstants.PHONE_TYPE_NONE;
1827 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001828 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001829 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001830 }
1831
1832 /**
1833 * Returns the CDMA ERI icon index to display
1834 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001835 @Override
1836 public int getCdmaEriIconIndex(String callingPackage) {
1837 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001838 }
1839
Sanket Padawe356d7632015-06-22 14:03:32 -07001840 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001841 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1842 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1843 return -1;
1844 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001845 final Phone phone = getPhone(subId);
1846 if (phone != null) {
1847 return phone.getCdmaEriIconIndex();
1848 } else {
1849 return -1;
1850 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001851 }
1852
1853 /**
1854 * Returns the CDMA ERI icon mode,
1855 * 0 - ON
1856 * 1 - FLASHING
1857 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001858 @Override
1859 public int getCdmaEriIconMode(String callingPackage) {
1860 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001861 }
1862
Sanket Padawe356d7632015-06-22 14:03:32 -07001863 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001864 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1865 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1866 return -1;
1867 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001868 final Phone phone = getPhone(subId);
1869 if (phone != null) {
1870 return phone.getCdmaEriIconMode();
1871 } else {
1872 return -1;
1873 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 }
1875
1876 /**
1877 * Returns the CDMA ERI text,
1878 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001879 @Override
1880 public String getCdmaEriText(String callingPackage) {
1881 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001882 }
1883
Sanket Padawe356d7632015-06-22 14:03:32 -07001884 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001885 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1886 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1887 return null;
1888 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001889 final Phone phone = getPhone(subId);
1890 if (phone != null) {
1891 return phone.getCdmaEriText();
1892 } else {
1893 return null;
1894 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001895 }
1896
1897 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001898 * Returns the CDMA MDN.
1899 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001900 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001901 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001902 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 final Phone phone = getPhone(subId);
1904 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1905 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001906 } else {
1907 return null;
1908 }
1909 }
1910
1911 /**
1912 * Returns the CDMA MIN.
1913 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001914 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001915 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001916 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001917 final Phone phone = getPhone(subId);
1918 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1919 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001920 } else {
1921 return null;
1922 }
1923 }
1924
1925 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001926 * Returns true if CDMA provisioning needs to run.
1927 */
1928 public boolean needsOtaServiceProvisioning() {
1929 return mPhone.needsOtaServiceProvisioning();
1930 }
1931
1932 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001933 * Sets the voice mail number of a given subId.
1934 */
1935 @Override
1936 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001937 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001938 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1939 new Pair<String, String>(alphaTag, number), new Integer(subId));
1940 return success;
1941 }
1942
Ta-wei Yen87c49842016-05-13 21:19:52 -07001943 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001944 public void setVisualVoicemailEnabled(String callingPackage,
1945 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1946 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1947 if (!TextUtils.equals(callingPackage,
1948 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1949 enforceModifyPermissionOrCarrierPrivilege(
1950 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1951 }
1952 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1953 }
1954
1955 @Override
1956 public boolean isVisualVoicemailEnabled(String callingPackage,
1957 PhoneAccountHandle phoneAccountHandle) {
1958 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1959 return false;
1960 }
1961 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1962 }
1963
1964 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001965 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1966 VisualVoicemailSmsFilterSettings settings) {
1967 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001968 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001969 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1970 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001971 }
1972
1973 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001974 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1975 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001976 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001977 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001978 }
1979
1980 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001981 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1982 String callingPackage, int subId) {
1983 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001984 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001985 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001986 }
1987
1988 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001989 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1990 String packageName, int subId) {
1991 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001992 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001993 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001994 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001995 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001996 * Returns the unread count of voicemails
1997 */
1998 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001999 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002000 }
2001
2002 /**
2003 * Returns the unread count of voicemails for a subId
2004 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002005 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002006 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002007 final Phone phone = getPhone(subId);
2008 if (phone != null) {
2009 return phone.getVoiceMessageCount();
2010 } else {
2011 return 0;
2012 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002013 }
2014
2015 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002016 * Returns the data network type.
2017 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002018 *
2019 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2020 */
2021 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002022 public int getNetworkType() {
2023 final Phone phone = getPhone(getDefaultSubscription());
2024 if (phone != null) {
2025 return phone.getServiceState().getDataNetworkType();
2026 } else {
2027 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2028 }
Wink Saville36469e72014-06-11 15:17:00 -07002029 }
2030
2031 /**
2032 * Returns the network type for a subId
2033 */
2034 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002035 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2036 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2037 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2038 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002039
Sanket Padawe356d7632015-06-22 14:03:32 -07002040 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002041 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002042 return phone.getServiceState().getDataNetworkType();
2043 } else {
2044 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2045 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002046 }
2047
2048 /**
2049 * Returns the data network type
2050 */
2051 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002052 public int getDataNetworkType(String callingPackage) {
2053 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002054 }
2055
2056 /**
2057 * Returns the data network type for a subId
2058 */
2059 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002060 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2061 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2062 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2063 }
2064
Sanket Padawe356d7632015-06-22 14:03:32 -07002065 final Phone phone = getPhone(subId);
2066 if (phone != null) {
2067 return phone.getServiceState().getDataNetworkType();
2068 } else {
2069 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2070 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002071 }
2072
2073 /**
Wink Saville36469e72014-06-11 15:17:00 -07002074 * Returns the Voice network type for a subId
2075 */
2076 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002077 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2078 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2079 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2080 }
2081
Sanket Padawe356d7632015-06-22 14:03:32 -07002082 final Phone phone = getPhone(subId);
2083 if (phone != null) {
2084 return phone.getServiceState().getVoiceNetworkType();
2085 } else {
2086 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2087 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002088 }
2089
2090 /**
2091 * @return true if a ICC card is present
2092 */
2093 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002094 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002095 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002096 }
2097
2098 /**
2099 * @return true if a ICC card is present for a slotId
2100 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002101 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002102 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002103 final Phone phone = PhoneFactory.getPhone(slotId);
2104 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002105 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002106 } else {
2107 return false;
2108 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002109 }
2110
2111 /**
2112 * Return if the current radio is LTE on CDMA. This
2113 * is a tri-state return value as for a period of time
2114 * the mode may be unknown.
2115 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002116 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002117 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002118 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002119 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002120 @Override
2121 public int getLteOnCdmaMode(String callingPackage) {
2122 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002123 }
2124
Sanket Padawe356d7632015-06-22 14:03:32 -07002125 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002126 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2127 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2128 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2129 }
2130
Sanket Padawe356d7632015-06-22 14:03:32 -07002131 final Phone phone = getPhone(subId);
2132 if (phone == null) {
2133 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2134 } else {
2135 return phone.getLteOnCdmaMode();
2136 }
Wink Saville36469e72014-06-11 15:17:00 -07002137 }
2138
2139 public void setPhone(Phone phone) {
2140 mPhone = phone;
2141 }
2142
2143 /**
2144 * {@hide}
2145 * Returns Default subId, 0 in the case of single standby.
2146 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002147 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002148 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002149 }
2150
Shishir Agrawala9f32182016-04-12 12:00:16 -07002151 private int getSlotForDefaultSubscription() {
2152 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2153 }
2154
Wink Savilleb564aae2014-10-23 10:18:09 -07002155 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002156 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002157 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002158
2159 /**
2160 * @see android.telephony.TelephonyManager.WifiCallingChoices
2161 */
2162 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002163 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2164 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002165 }
2166
2167 /**
2168 * @see android.telephony.TelephonyManager.WifiCallingChoices
2169 */
2170 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002171 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2172 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2173 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002174 }
2175
Sailesh Nepald1e68152013-12-12 19:08:02 -08002176 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002177 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002178 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002179 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002180
Shishir Agrawal566b7612013-10-28 14:41:00 -07002181 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002182 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2183 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002184
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002185 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002186 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002187 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002188 if (DBG) log("iccOpenLogicalChannel: " + response);
2189 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002190 }
2191
2192 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002193 public boolean iccCloseLogicalChannel(int subId, int channel) {
2194 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002195
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002196 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002197 if (channel < 0) {
2198 return false;
2199 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002200 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002201 if (DBG) log("iccCloseLogicalChannel: " + success);
2202 return success;
2203 }
2204
2205 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002206 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002207 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002208 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002209
2210 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002211 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2212 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002213 " data=" + data);
2214 }
2215
2216 if (channel < 0) {
2217 return "";
2218 }
2219
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002220 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002221 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002222 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2223
Shishir Agrawal566b7612013-10-28 14:41:00 -07002224 // Append the returned status code to the end of the response payload.
2225 String s = Integer.toHexString(
2226 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002227 if (response.payload != null) {
2228 s = IccUtils.bytesToHexString(response.payload) + s;
2229 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002230 return s;
2231 }
Jake Hambye994d462014-02-03 13:10:13 -08002232
Evan Charltonc66da362014-05-16 14:06:40 -07002233 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002234 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002235 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002236 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002237
2238 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002239 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2240 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002241 }
2242
2243 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002244 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002245 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2246
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002247 // Append the returned status code to the end of the response payload.
2248 String s = Integer.toHexString(
2249 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002250 if (response.payload != null) {
2251 s = IccUtils.bytesToHexString(response.payload) + s;
2252 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002253 return s;
2254 }
2255
2256 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002257 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002258 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002259 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002260
2261 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002262 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002263 p1 + " " + p2 + " " + p3 + ":" + filePath);
2264 }
2265
2266 IccIoResult response =
2267 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002268 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2269 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002270
2271 if (DBG) {
2272 log("Exchange SIM_IO [R]" + response);
2273 }
2274
2275 byte[] result = null;
2276 int length = 2;
2277 if (response.payload != null) {
2278 length = 2 + response.payload.length;
2279 result = new byte[length];
2280 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2281 } else {
2282 result = new byte[length];
2283 }
2284
2285 result[length - 1] = (byte) response.sw2;
2286 result[length - 2] = (byte) response.sw1;
2287 return result;
2288 }
2289
2290 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002291 public String sendEnvelopeWithStatus(int subId, String content) {
2292 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002293
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002294 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002295 if (response.payload == null) {
2296 return "";
2297 }
2298
2299 // Append the returned status code to the end of the response payload.
2300 String s = Integer.toHexString(
2301 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2302 s = IccUtils.bytesToHexString(response.payload) + s;
2303 return s;
2304 }
2305
Jake Hambye994d462014-02-03 13:10:13 -08002306 /**
2307 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2308 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2309 *
2310 * @param itemID the ID of the item to read
2311 * @return the NV item as a String, or null on error.
2312 */
2313 @Override
2314 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002315 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002316 if (DBG) log("nvReadItem: item " + itemID);
2317 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2318 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2319 return value;
2320 }
2321
2322 /**
2323 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2324 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2325 *
2326 * @param itemID the ID of the item to read
2327 * @param itemValue the value to write, as a String
2328 * @return true on success; false on any failure
2329 */
2330 @Override
2331 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002332 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002333 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2334 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2335 new Pair<Integer, String>(itemID, itemValue));
2336 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2337 return success;
2338 }
2339
2340 /**
2341 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2342 * Used for device configuration by some CDMA operators.
2343 *
2344 * @param preferredRoamingList byte array containing the new PRL
2345 * @return true on success; false on any failure
2346 */
2347 @Override
2348 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002349 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002350 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2351 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2352 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2353 return success;
2354 }
2355
2356 /**
2357 * Perform the specified type of NV config reset.
2358 * Used for device configuration by some CDMA operators.
2359 *
2360 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2361 * @return true on success; false on any failure
2362 */
2363 @Override
2364 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002365 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002366 if (DBG) log("nvResetConfig: type " + resetType);
2367 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2368 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2369 return success;
2370 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002371
2372 /**
Wink Saville36469e72014-06-11 15:17:00 -07002373 * {@hide}
2374 * Returns Default sim, 0 in the case of single standby.
2375 */
2376 public int getDefaultSim() {
2377 //TODO Need to get it from Telephony Devcontroller
2378 return 0;
2379 }
2380
Svet Ganovb320e182015-04-16 12:30:10 -07002381 public String[] getPcscfAddress(String apnType, String callingPackage) {
2382 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2383 return new String[0];
2384 }
2385
2386
ram87fca6f2014-07-18 18:58:44 +05302387 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002388 }
2389
2390 public void setImsRegistrationState(boolean registered) {
2391 enforceModifyPermission();
2392 mPhone.setImsRegistrationState(registered);
2393 }
2394
2395 /**
Stuart Scott54788802015-03-30 13:18:01 -07002396 * Set the network selection mode to automatic.
2397 *
2398 */
2399 @Override
2400 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002402 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2403 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2404 }
2405
2406 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002407 * Set the network selection mode to manual with the selected carrier.
2408 */
2409 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002410 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2411 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002412 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002413 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002414 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2415 persistSelection);
2416 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002417 }
2418
2419 /**
2420 * Scans for available networks.
2421 */
2422 @Override
2423 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002424 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002425 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2426 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2427 CMD_PERFORM_NETWORK_SCAN, null, subId);
2428 return result;
2429 }
2430
2431 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002432 * Get the calculated preferred network type.
2433 * Used for debugging incorrect network type.
2434 *
2435 * @return the preferred network type, defined in RILConstants.java.
2436 */
2437 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002438 public int getCalculatedPreferredNetworkType(String callingPackage) {
2439 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2440 return RILConstants.PREFERRED_NETWORK_MODE;
2441 }
2442
Amit Mahajan43330e02014-11-18 11:54:45 -08002443 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002444 }
2445
2446 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002447 * Get the preferred network type.
2448 * Used for device configuration by some CDMA operators.
2449 *
2450 * @return the preferred network type, defined in RILConstants.java.
2451 */
2452 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002453 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002454 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002455 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002456 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002457 int networkType = (result != null ? result[0] : -1);
2458 if (DBG) log("getPreferredNetworkType: " + networkType);
2459 return networkType;
2460 }
2461
2462 /**
2463 * Set the preferred network type.
2464 * Used for device configuration by some CDMA operators.
2465 *
2466 * @param networkType the preferred network type, defined in RILConstants.java.
2467 * @return true on success; false on any failure.
2468 */
2469 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002470 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002471 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002472 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2473 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002474 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002475 if (success) {
2476 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002477 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002478 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002479 return success;
2480 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002481
2482 /**
Junda Liu475951f2014-11-07 16:45:03 -08002483 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2484 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2485 * tethering.
2486 *
2487 * @return 0: Not required. 1: required. 2: Not set.
2488 * @hide
2489 */
2490 @Override
2491 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002492 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002493 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2494 Settings.Global.TETHER_DUN_REQUIRED, 2);
2495 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2496 // config_tether_apndata.
2497 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2498 dunRequired = 1;
2499 }
2500 return dunRequired;
2501 }
2502
2503 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002504 * Set mobile data enabled
2505 * Used by the user through settings etc to turn on/off mobile data
2506 *
2507 * @param enable {@code true} turn turn data on, else {@code false}
2508 */
2509 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002510 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002511 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002512 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002513 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002514 Phone phone = PhoneFactory.getPhone(phoneId);
2515 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002516 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002517 phone.setDataEnabled(enable);
2518 } else {
2519 loge("setDataEnabled: no phone for subId=" + subId);
2520 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002521 }
2522
2523 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002524 * Get whether mobile data is enabled.
2525 *
2526 * Note that this used to be available from ConnectivityService, gated by
2527 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2528 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002529 *
2530 * @return {@code true} if data is enabled else {@code false}
2531 */
2532 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002533 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002534 try {
2535 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2536 null);
2537 } catch (Exception e) {
2538 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2539 null);
2540 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002541 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002542 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002543 Phone phone = PhoneFactory.getPhone(phoneId);
2544 if (phone != null) {
2545 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002546 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002547 return retVal;
2548 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002549 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002550 return false;
2551 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002552 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002553
2554 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002555 public int getCarrierPrivilegeStatus(int subId) {
2556 final Phone phone = getPhone(subId);
2557 if (phone == null) {
2558 loge("getCarrierPrivilegeStatus: Invalid subId");
2559 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2560 }
2561 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002562 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002563 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002564 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2565 }
2566 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002567 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002568 }
Junda Liu29340342014-07-10 15:23:27 -07002569
2570 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002571 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002572 if (TextUtils.isEmpty(pkgName))
2573 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002574 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002575 if (card == null) {
2576 loge("checkCarrierPrivilegesForPackage: No UICC");
2577 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2578 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002579 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2580 }
2581
2582 @Override
2583 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002584 if (TextUtils.isEmpty(pkgName))
2585 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002586 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2587 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2588 UiccCard card = UiccController.getInstance().getUiccCard(i);
2589 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002590 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002591 continue;
2592 }
2593
2594 result = card.getCarrierPrivilegeStatus(
2595 mPhone.getContext().getPackageManager(), pkgName);
2596 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2597 break;
2598 }
2599 }
2600
2601 return result;
Junda Liu29340342014-07-10 15:23:27 -07002602 }
Derek Tan89e89d42014-07-08 17:00:10 -07002603
2604 @Override
Junda Liue64de782015-04-16 17:19:16 -07002605 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2606 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2607 loge("phoneId " + phoneId + " is not valid.");
2608 return null;
2609 }
2610 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002611 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002612 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002613 return null ;
2614 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002615 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002616 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002617 }
2618
Amith Yamasani6e118872016-02-19 12:53:51 -08002619 @Override
2620 public List<String> getPackagesWithCarrierPrivileges() {
2621 PackageManager pm = mPhone.getContext().getPackageManager();
2622 List<String> privilegedPackages = new ArrayList<>();
2623 List<PackageInfo> packages = null;
2624 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2625 UiccCard card = UiccController.getInstance().getUiccCard(i);
2626 if (card == null) {
2627 // No UICC in that slot.
2628 continue;
2629 }
2630 if (card.hasCarrierPrivilegeRules()) {
2631 if (packages == null) {
2632 // Only check packages in user 0 for now
2633 packages = pm.getInstalledPackagesAsUser(
2634 PackageManager.MATCH_DISABLED_COMPONENTS
2635 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2636 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2637 }
2638 for (int p = packages.size() - 1; p >= 0; p--) {
2639 PackageInfo pkgInfo = packages.get(p);
2640 if (pkgInfo != null && pkgInfo.packageName != null
2641 && card.getCarrierPrivilegeStatus(pkgInfo)
2642 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2643 privilegedPackages.add(pkgInfo.packageName);
2644 }
2645 }
2646 }
2647 }
2648 return privilegedPackages;
2649 }
2650
Wink Savilleb564aae2014-10-23 10:18:09 -07002651 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002652 final Phone phone = getPhone(subId);
2653 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002654 if (card == null) {
2655 loge("getIccId: No UICC");
2656 return null;
2657 }
2658 String iccId = card.getIccId();
2659 if (TextUtils.isEmpty(iccId)) {
2660 loge("getIccId: ICC ID is null or empty.");
2661 return null;
2662 }
2663 return iccId;
2664 }
2665
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002666 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002667 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2668 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002669 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002670
Jeff Sharkey85190e62014-12-05 09:40:12 -08002671 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002672 final Phone phone = getPhone(subId);
2673 if (phone == null) {
2674 return false;
2675 }
2676 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002677
2678 if (DBG_MERGE) {
2679 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2680 + subscriberId + " to " + number);
2681 }
2682
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002683 if (TextUtils.isEmpty(iccId)) {
2684 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002685 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002686
Jeff Sharkey85190e62014-12-05 09:40:12 -08002687 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2688
2689 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002690 if (alphaTag == null) {
2691 editor.remove(alphaTagPrefKey);
2692 } else {
2693 editor.putString(alphaTagPrefKey, alphaTag);
2694 }
2695
Jeff Sharkey85190e62014-12-05 09:40:12 -08002696 // Record both the line number and IMSI for this ICCID, since we need to
2697 // track all merged IMSIs based on line number
2698 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2699 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002700 if (number == null) {
2701 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002702 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002703 } else {
2704 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002705 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002706 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002707
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002708 editor.commit();
2709 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002710 }
2711
2712 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002713 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002714 // This is open to apps with WRITE_SMS.
2715 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002716 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002717 return null;
2718 }
Derek Tan97ebb422014-09-05 16:55:38 -07002719
2720 String iccId = getIccId(subId);
2721 if (iccId != null) {
2722 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002723 if (DBG_MERGE) {
2724 log("getLine1NumberForDisplay returning " +
2725 mTelephonySharedPreferences.getString(numberPrefKey, null));
2726 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002727 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002728 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002729 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002730 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002731 }
2732
2733 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002734 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2735 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2736 return null;
2737 }
Derek Tan97ebb422014-09-05 16:55:38 -07002738
2739 String iccId = getIccId(subId);
2740 if (iccId != null) {
2741 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002742 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002743 }
Derek Tan97ebb422014-09-05 16:55:38 -07002744 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002745 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002746
2747 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002748 public String[] getMergedSubscriberIds(String callingPackage) {
2749 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2750 return null;
2751 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002752 final Context context = mPhone.getContext();
2753 final TelephonyManager tele = TelephonyManager.from(context);
2754 final SubscriptionManager sub = SubscriptionManager.from(context);
2755
2756 // Figure out what subscribers are currently active
2757 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002758 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2759 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2760 final long identity = Binder.clearCallingIdentity();
2761 try {
2762 final int[] subIds = sub.getActiveSubscriptionIdList();
2763 for (int subId : subIds) {
2764 activeSubscriberIds.add(tele.getSubscriberId(subId));
2765 }
2766 } finally {
2767 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002768 }
2769
2770 // First pass, find a number override for an active subscriber
2771 String mergeNumber = null;
2772 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2773 for (String key : prefs.keySet()) {
2774 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2775 final String subscriberId = (String) prefs.get(key);
2776 if (activeSubscriberIds.contains(subscriberId)) {
2777 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2778 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2779 mergeNumber = (String) prefs.get(numberKey);
2780 if (DBG_MERGE) {
2781 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2782 + " for active subscriber " + subscriberId);
2783 }
2784 if (!TextUtils.isEmpty(mergeNumber)) {
2785 break;
2786 }
2787 }
2788 }
2789 }
2790
2791 // Shortcut when no active merged subscribers
2792 if (TextUtils.isEmpty(mergeNumber)) {
2793 return null;
2794 }
2795
2796 // Second pass, find all subscribers under that line override
2797 final ArraySet<String> result = new ArraySet<>();
2798 for (String key : prefs.keySet()) {
2799 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2800 final String number = (String) prefs.get(key);
2801 if (mergeNumber.equals(number)) {
2802 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2803 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2804 final String subscriberId = (String) prefs.get(subscriberKey);
2805 if (!TextUtils.isEmpty(subscriberId)) {
2806 result.add(subscriberId);
2807 }
2808 }
2809 }
2810 }
2811
2812 final String[] resultArray = result.toArray(new String[result.size()]);
2813 Arrays.sort(resultArray);
2814 if (DBG_MERGE) {
2815 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2816 }
2817 return resultArray;
2818 }
2819
2820 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002821 public boolean setOperatorBrandOverride(int subId, String brand) {
2822 enforceCarrierPrivilege(subId);
2823 final Phone phone = getPhone(subId);
2824 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002825 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002826
2827 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002828 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002829 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2830 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002831 enforceCarrierPrivilege(subId);
2832 final Phone phone = getPhone(subId);
2833 if (phone == null) {
2834 return false;
2835 }
2836 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002837 cdmaNonRoamingList);
2838 }
2839
2840 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002841 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2842 enforceModifyPermission();
2843
2844 int returnValue = 0;
2845 try {
2846 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2847 if(result.exception == null) {
2848 if (result.result != null) {
2849 byte[] responseData = (byte[])(result.result);
2850 if(responseData.length > oemResp.length) {
2851 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2852 responseData.length + "bytes. Buffer Size is " +
2853 oemResp.length + "bytes.");
2854 }
2855 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2856 returnValue = responseData.length;
2857 }
2858 } else {
2859 CommandException ex = (CommandException) result.exception;
2860 returnValue = ex.getCommandError().ordinal();
2861 if(returnValue > 0) returnValue *= -1;
2862 }
2863 } catch (RuntimeException e) {
2864 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2865 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2866 if(returnValue > 0) returnValue *= -1;
2867 }
2868
2869 return returnValue;
2870 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002871
2872 @Override
2873 public void setRadioCapability(RadioAccessFamily[] rafs) {
2874 try {
2875 ProxyController.getInstance().setRadioCapability(rafs);
2876 } catch (RuntimeException e) {
2877 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2878 }
2879 }
2880
2881 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002882 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2883 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2884 return RadioAccessFamily.RAF_UNKNOWN;
2885 }
2886
Wink Saville5d475dd2014-10-17 15:00:58 -07002887 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2888 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002889
2890 @Override
2891 public void enableVideoCalling(boolean enable) {
2892 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002893 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002894 }
2895
2896 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002897 public boolean isVideoCallingEnabled(String callingPackage) {
2898 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2899 return false;
2900 }
2901
Andrew Lee77527ac2014-10-21 16:57:39 -07002902 // Check the user preference and the system-level IMS setting. Even if the user has
2903 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2904 // In the long run, we may instead need to check if there exists a connection service
2905 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002906 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2907 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002908 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002909 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002910
Andrew Leea1239f22015-03-02 17:44:07 -08002911 @Override
2912 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002913 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002914 }
2915
2916 @Override
2917 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002918 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002919 }
2920
Andrew Lee9431b832015-03-09 18:46:45 -07002921 @Override
2922 public boolean isTtyModeSupported() {
2923 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2924 TelephonyManager telephonyManager =
2925 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002926 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002927 }
2928
2929 @Override
2930 public boolean isHearingAidCompatibilitySupported() {
2931 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2932 }
2933
Sanket Padawe7310cc72015-01-14 09:53:20 -08002934 /**
2935 * Returns the unique device ID of phone, for example, the IMEI for
2936 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2937 *
2938 * <p>Requires Permission:
2939 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2940 */
2941 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002942 public String getDeviceId(String callingPackage) {
2943 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2944 return null;
2945 }
2946
Sanket Padawe7310cc72015-01-14 09:53:20 -08002947 final Phone phone = PhoneFactory.getPhone(0);
2948 if (phone != null) {
2949 return phone.getDeviceId();
2950 } else {
2951 return null;
2952 }
2953 }
2954
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002955 /*
2956 * {@hide}
2957 * Returns the IMS Registration Status
2958 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002959 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002960 public boolean isImsRegistered() {
2961 return mPhone.isImsRegistered();
2962 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002963
2964 @Override
2965 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2966 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2967 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002968
Nathan Haroldc55097a2015-03-11 18:14:50 -07002969 /*
2970 * {@hide}
2971 * Returns the IMS Registration Status
2972 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002973 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002974 return mPhone.isWifiCallingEnabled();
2975 }
2976
2977 /*
2978 * {@hide}
2979 * Returns the IMS Registration Status
2980 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002981 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002982 return mPhone.isVolteEnabled();
2983 }
Svet Ganovb320e182015-04-16 12:30:10 -07002984
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002985 /*
2986 * {@hide} Returns the IMS Registration Status
2987 */
2988 public boolean isVideoTelephonyAvailable() {
2989 return mPhone.isVideoEnabled();
2990 }
2991
Svet Ganovb320e182015-04-16 12:30:10 -07002992 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002993 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002994 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002995 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2996
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002997 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002998 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002999 } catch (SecurityException e) {
3000 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3001 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003002 }
Svet Ganovb320e182015-04-16 12:30:10 -07003003
3004 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3005 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3006 return false;
3007 }
3008
3009 return true;
3010 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003011
Makoto Onukifee69342015-06-29 14:44:50 -07003012 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003013 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003014 */
3015 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003016 // Default SMS app can always read it.
3017 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3018 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3019 return true;
3020 }
3021 try {
3022 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003023 } catch (SecurityException readPhoneStateSecurityException) {
3024 try {
3025 // Can be read with READ_SMS too.
3026 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3027 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3028 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3029 } catch (SecurityException readSmsSecurityException) {
3030 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3031 // permissions
3032 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3033 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3034 " or " + android.Manifest.permission.READ_SMS + ".");
3035 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003036 }
Makoto Onukifee69342015-06-29 14:44:50 -07003037 }
3038
Stuart Scott8eef64f2015-04-08 15:13:54 -07003039 @Override
3040 public void factoryReset(int subId) {
3041 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003042 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3043 return;
3044 }
3045
Svet Ganovcc087f82015-05-12 20:35:54 -07003046 final long identity = Binder.clearCallingIdentity();
3047 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003048 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3049 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003050 // Enable data
3051 setDataEnabled(subId, true);
3052 // Set network selection mode to automatic
3053 setNetworkSelectionModeAutomatic(subId);
3054 // Set preferred mobile network type to the best available
3055 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3056 // Turn off roaming
3057 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3058 }
3059 } finally {
3060 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003061 }
3062 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003063
3064 @Override
3065 public String getLocaleFromDefaultSim() {
3066 // We query all subscriptions instead of just the active ones, because
3067 // this might be called early on in the provisioning flow when the
3068 // subscriptions potentially aren't active yet.
3069 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3070 if (slist == null || slist.isEmpty()) {
3071 return null;
3072 }
3073
3074 // This function may be called very early, say, from the setup wizard, at
3075 // which point we won't have a default subscription set. If that's the case
3076 // we just choose the first, which will be valid in "most cases".
3077 final int defaultSubId = getDefaultSubscription();
3078 SubscriptionInfo info = null;
3079 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3080 info = slist.get(0);
3081 } else {
3082 for (SubscriptionInfo item : slist) {
3083 if (item.getSubscriptionId() == defaultSubId) {
3084 info = item;
3085 break;
3086 }
3087 }
3088
3089 if (info == null) {
3090 return null;
3091 }
3092 }
3093
3094 // Try and fetch the locale from the carrier properties or from the SIM language
3095 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003096 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003097 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003098 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003099 if (defaultPhone != null) {
3100 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3101 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003102 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003103 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3104 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003105 } else {
3106 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003107 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003108 }
3109 }
3110
Narayan Kamath011676f2015-07-29 12:04:08 +01003111 // The SIM language preferences only store a language (e.g. fr = French), not an
3112 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3113 // the SIM and carrier preferences does not include a country we add the country
3114 // determined from the SIM MCC to provide an exact locale.
3115 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003116 if (mccLocale != null) {
3117 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3118 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003119 }
3120
Tony Hill183b2de2015-06-24 14:53:58 +01003121 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003122 return null;
3123 }
3124
3125 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3126 final long identity = Binder.clearCallingIdentity();
3127 try {
3128 return mSubscriptionController.getAllSubInfoList(
3129 mPhone.getContext().getOpPackageName());
3130 } finally {
3131 Binder.restoreCallingIdentity(identity);
3132 }
3133 }
3134
3135 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3136 final long identity = Binder.clearCallingIdentity();
3137 try {
3138 return mSubscriptionController.getActiveSubscriptionInfoList(
3139 mPhone.getContext().getOpPackageName());
3140 } finally {
3141 Binder.restoreCallingIdentity(identity);
3142 }
3143 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003144
3145 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003146 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3147 * representing the state of the modem.
3148 *
3149 * NOTE: This clears the modem state, so there should only every be one caller.
3150 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003151 */
3152 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003153 public void requestModemActivityInfo(ResultReceiver result) {
3154 enforceModifyPermission();
3155
3156 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3157 Bundle bundle = new Bundle();
3158 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3159 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003160 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003161
3162 /**
3163 * {@hide}
3164 * Returns the service state information on specified subscription.
3165 */
3166 @Override
3167 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3168
3169 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3170 return null;
3171 }
3172
3173 final Phone phone = getPhone(subId);
3174 if (phone == null) {
3175 return null;
3176 }
3177
3178 return phone.getServiceState();
3179 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003180
3181 /**
3182 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3183 *
3184 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3185 * voicemail ringtone.
3186 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3187 * PhoneAccount.
3188 */
3189 @Override
3190 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3191 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3192 if (phone == null) {
3193 return null;
3194 }
3195
3196 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3197 }
3198
3199 /**
3200 * Returns whether vibration is set for voicemail notification in Phone settings.
3201 *
3202 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3203 * voicemail vibration setting.
3204 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3205 */
3206 @Override
3207 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3208 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3209 if (phone == null) {
3210 return false;
3211 }
3212
3213 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3214 }
3215
Youhan Wange64578a2016-05-02 15:32:42 -07003216 /**
3217 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3218 *
3219 * @throws SecurityException if the caller does not have the required permission
3220 */
3221 private void enforceReadPrivilegedPermission() {
3222 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3223 null);
3224 }
3225
3226 /**
3227 * Return the application ID for the app type.
3228 *
3229 * @param subId the subscription ID that this request applies to.
3230 * @param appType the uicc app type.
3231 * @return Application ID for specificied app type, or null if no uicc.
3232 */
3233 @Override
3234 public String getAidForAppType(int subId, int appType) {
3235 enforceReadPrivilegedPermission();
3236 Phone phone = getPhone(subId);
3237 if (phone == null) {
3238 return null;
3239 }
3240 String aid = null;
3241 try {
3242 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3243 .getApplicationByType(appType).getAid();
3244 } catch (Exception e) {
3245 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3246 }
3247 return aid;
3248 }
3249
Youhan Wang4001d252016-05-11 10:29:41 -07003250 /**
3251 * Return the Electronic Serial Number.
3252 *
3253 * @param subId the subscription ID that this request applies to.
3254 * @return ESN or null if error.
3255 */
3256 @Override
3257 public String getEsn(int subId) {
3258 enforceReadPrivilegedPermission();
3259 Phone phone = getPhone(subId);
3260 if (phone == null) {
3261 return null;
3262 }
3263 String esn = null;
3264 try {
3265 esn = phone.getEsn();
3266 } catch (Exception e) {
3267 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3268 }
3269 return esn;
3270 }
3271
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003272 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003273 * Return the Preferred Roaming List Version.
3274 *
3275 * @param subId the subscription ID that this request applies to.
3276 * @return PRLVersion or null if error.
3277 */
3278 @Override
3279 public String getCdmaPrlVersion(int subId) {
3280 enforceReadPrivilegedPermission();
3281 Phone phone = getPhone(subId);
3282 if (phone == null) {
3283 return null;
3284 }
3285 String cdmaPrlVersion = null;
3286 try {
3287 cdmaPrlVersion = phone.getCdmaPrlVersion();
3288 } catch (Exception e) {
3289 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3290 }
3291 return cdmaPrlVersion;
3292 }
3293
3294 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003295 * Get snapshot of Telephony histograms
3296 * @return List of Telephony histograms
3297 * @hide
3298 */
3299 @Override
3300 public List<TelephonyHistogram> getTelephonyHistograms() {
3301 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3302 return RIL.getTelephonyRILTimingHistograms();
3303 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003304
3305 /**
3306 * {@hide}
3307 * Set the allowed carrier list for slotId
3308 * Require system privileges. In the future we may add this to carrier APIs.
3309 *
3310 * @return The number of carriers set successfully, should match length of carriers
3311 */
3312 @Override
3313 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3314 enforceModifyPermission();
3315 int subId = SubscriptionManager.getSubId(slotId)[0];
3316 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3317 return retVal[0];
3318 }
3319
3320 /**
3321 * {@hide}
3322 * Get the allowed carrier list for slotId.
3323 * Require system privileges. In the future we may add this to carrier APIs.
3324 *
3325 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3326 * means all carriers are allowed.
3327 */
3328 @Override
3329 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3330 enforceReadPrivilegedPermission();
3331 int subId = SubscriptionManager.getSubId(slotId)[0];
3332 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3333 }
3334
fionaxu59545b42016-05-25 15:53:37 -07003335 /**
3336 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3337 * @param subId the subscription ID that this action applies to.
3338 * @param enabled control enable or disable metered apns.
3339 * {@hide}
3340 */
3341 @Override
3342 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3343 enforceModifyPermission();
3344 final Phone phone = getPhone(subId);
3345 if (phone == null) {
3346 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3347 return;
3348 }
3349 try {
3350 phone.carrierActionSetMeteredApnsEnabled(enabled);
3351 } catch (Exception e) {
3352 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3353 }
3354 }
3355
3356 /**
3357 * Action set from carrier signalling broadcast receivers to enable/disable radio
3358 * @param subId the subscription ID that this action applies to.
3359 * @param enabled control enable or disable radio.
3360 * {@hide}
3361 */
3362 @Override
3363 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3364 enforceModifyPermission();
3365 final Phone phone = getPhone(subId);
3366 if (phone == null) {
3367 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3368 return;
3369 }
3370 try {
3371 phone.carrierActionSetRadioEnabled(enabled);
3372 } catch (Exception e) {
3373 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3374 }
3375 }
3376
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003377 /**
3378 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3379 * bug report is being generated.
3380 */
3381 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003382 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003383 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3384 != PackageManager.PERMISSION_GRANTED) {
3385 writer.println("Permission Denial: can't dump Phone from pid="
3386 + Binder.getCallingPid()
3387 + ", uid=" + Binder.getCallingUid()
3388 + "without permission "
3389 + android.Manifest.permission.DUMP);
3390 return;
3391 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003392 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003393 }
Jack Yueb89b242016-06-22 13:27:47 -07003394
3395 /**
3396 * Get aggregated video call data usage from all subscriptions since boot.
3397 * @return total data usage in bytes
3398 * {@hide}
3399 */
3400 @Override
3401 public long getVtDataUsage() {
3402 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3403 null);
3404
3405 // NetworkStatsService keeps tracking the active network interface and identity. It will
3406 // record the delta with the corresponding network identity. What we need to do here is
3407 // returning total video call data usage from all subscriptions since boot.
3408
3409 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3410 // simultaneous VT calls.
3411 final Phone[] phones = PhoneFactory.getPhones();
3412 long total = 0;
3413 for (Phone phone : phones) {
3414 total += phone.getVtDataUsage();
3415 }
3416 return total;
3417 }
Jack Yu75ab2952016-07-08 14:29:33 -07003418
3419 /**
3420 * Policy control of data connection. Usually used when data limit is passed.
3421 * @param enabled True if enabling the data, otherwise disabling.
3422 * @param subId Subscription index
3423 * {@hide}
3424 */
3425 @Override
3426 public void setPolicyDataEnabled(boolean enabled, int subId) {
3427 enforceModifyPermission();
3428 Phone phone = getPhone(subId);
3429 if (phone != null) {
3430 phone.setPolicyDataEnabled(enabled);
3431 }
3432 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003433
3434 /**
3435 * Get Client request stats
3436 * @return List of Client Request Stats
3437 * @hide
3438 */
3439 @Override
3440 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3441 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3442 return null;
3443 }
3444
3445 Phone phone = getPhone(subId);
3446 if (phone != null) {
3447 return phone.getClientRequestStats();
3448 }
3449
3450 return null;
3451 }
3452
3453 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3454 if (workSource != null) {
3455 return workSource;
3456 }
3457
3458 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3459 workSource = new WorkSource(uid, packageName);
3460 return workSource;
3461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003462}