blob: 2f7c2033755fca044cab6da69e4f6617a85258ae [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
Ta-wei Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080023import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070024import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.Context;
26import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070027import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080028import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070029import android.content.pm.PackageManager;
Jack Yu84291ec2017-05-26 16:07:50 -070030import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.net.Uri;
32import android.os.AsyncResult;
33import android.os.Binder;
34import android.os.Bundle;
35import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070036import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Looper;
38import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070039import android.os.Messenger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Process;
Tyler Gunn65d45c22017-06-05 11:22:26 -070041import android.os.PersistableBundle;
Adam Lesinski903a54c2016-04-11 14:49:52 -070042import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.ServiceManager;
44import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070045import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070046import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070047import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070049import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080050import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080051import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070052import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070053import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070055import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070056import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070057import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080058import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070059import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070060import android.telephony.RadioAccessFamily;
Tyler Gunn65d45c22017-06-05 11:22:26 -070061import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080063import android.telephony.SignalStrength;
Jack Yu84291ec2017-05-26 16:07:50 -070064import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080065import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080066import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070067import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070068import android.telephony.TelephonyManager;
Tyler Gunn65d45c22017-06-05 11:22:26 -070069import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070070import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080072import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080074import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080075import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080076
Andrew Lee312e8172014-10-23 17:01:36 -070077import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080078import com.android.ims.internal.IImsServiceController;
79import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -070081import com.android.internal.telephony.CallStateException;
Shishir Agrawal302c8692015-06-19 13:49:39 -070082import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070083import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080086import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010087import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -070088import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -070089import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080091import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070092import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070093import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070094import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070095import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070096import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080097import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070098import com.android.internal.telephony.uicc.IccIoResult;
99import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800100import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700101import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800102import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700103import com.android.internal.telephony.uicc.UiccController;
fionaxu7ed723d2017-05-30 18:58:54 -0700104import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800105import com.android.internal.util.HexDump;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700106import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800107import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700108import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700109import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800110
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700111import java.io.FileDescriptor;
112import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800113import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800115import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800116import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100117import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800118import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119
120/**
121 * Implementation of the ITelephony interface.
122 */
Santos Cordon117fee72014-05-16 17:56:12 -0700123public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 private static final String LOG_TAG = "PhoneInterfaceManager";
125 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
126 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800127 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128
129 // Message codes used with mMainThreadHandler
130 private static final int CMD_HANDLE_PIN_MMI = 1;
131 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
132 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
133 private static final int CMD_ANSWER_RINGING_CALL = 4;
134 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700135 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
136 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700137 private static final int CMD_OPEN_CHANNEL = 9;
138 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
139 private static final int CMD_CLOSE_CHANNEL = 11;
140 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800141 private static final int CMD_NV_READ_ITEM = 13;
142 private static final int EVENT_NV_READ_ITEM_DONE = 14;
143 private static final int CMD_NV_WRITE_ITEM = 15;
144 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
145 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
146 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
147 private static final int CMD_NV_RESET_CONFIG = 19;
148 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800149 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
150 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
151 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
152 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800153 private static final int CMD_SEND_ENVELOPE = 25;
154 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700155 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
156 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
157 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
158 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
159 private static final int CMD_EXCHANGE_SIM_IO = 31;
160 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800161 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
162 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700163 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
164 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700165 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
166 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700167 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
168 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
169 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
170 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700171 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
172 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
173 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
174 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700175 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800176 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
177 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178
179 /** The singleton instance. */
180 private static PhoneInterfaceManager sInstance;
181
Wink Saville3ab207e2014-11-20 13:07:20 -0800182 private PhoneGlobals mApp;
183 private Phone mPhone;
184 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700185 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800186 private AppOpsManager mAppOps;
187 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800188 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800189 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190
Derek Tan97ebb422014-09-05 16:55:38 -0700191 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
192 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800193 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700194
yinxub1bed742017-04-17 11:45:04 -0700195 private NetworkScanRequestTracker mNetworkScanRequestTracker;
196
Derek Tan89e89d42014-07-08 17:00:10 -0700197 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700198 * A request object to use for transmitting data to an ICC.
199 */
200 private static final class IccAPDUArgument {
201 public int channel, cla, command, p1, p2, p3;
202 public String data;
203
204 public IccAPDUArgument(int channel, int cla, int command,
205 int p1, int p2, int p3, String data) {
206 this.channel = channel;
207 this.cla = cla;
208 this.command = command;
209 this.p1 = p1;
210 this.p2 = p2;
211 this.p3 = p3;
212 this.data = data;
213 }
214 }
215
216 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700217 * A request object to use for transmitting data to an ICC.
218 */
219 private static final class ManualNetworkSelectionArgument {
220 public OperatorInfo operatorInfo;
221 public boolean persistSelection;
222
223 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
224 this.operatorInfo = operatorInfo;
225 this.persistSelection = persistSelection;
226 }
227 }
228
229 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
231 * request after sending. The main thread will notify the request when it is complete.
232 */
233 private static final class MainThreadRequest {
234 /** The argument to use for the request */
235 public Object argument;
236 /** The result of the request that is run on the main thread */
237 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800238 // The subscriber id that this request applies to. Defaults to
239 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
240 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241
242 public MainThreadRequest(Object argument) {
243 this.argument = argument;
244 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800245
246 public MainThreadRequest(Object argument, Integer subId) {
247 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800248 if (subId != null) {
249 this.subId = subId;
250 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800251 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 }
253
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800254 private static final class IncomingThirdPartyCallArgs {
255 public final ComponentName component;
256 public final String callId;
257 public final String callerDisplayName;
258
259 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
260 String callerDisplayName) {
261 this.component = component;
262 this.callId = callId;
263 this.callerDisplayName = callerDisplayName;
264 }
265 }
266
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 /**
268 * A handler that processes messages on the main thread in the phone process. Since many
269 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
270 * inbound binder threads to the main thread in the phone process. The Binder thread
271 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
272 * on, which will be notified when the operation completes and will contain the result of the
273 * request.
274 *
275 * <p>If a MainThreadRequest object is provided in the msg.obj field,
276 * note that request.result must be set to something non-null for the calling thread to
277 * unblock.
278 */
279 private final class MainThreadHandler extends Handler {
280 @Override
281 public void handleMessage(Message msg) {
282 MainThreadRequest request;
283 Message onCompleted;
284 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800285 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700286 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287
288 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700289 case CMD_HANDLE_USSD_REQUEST: {
290 request = (MainThreadRequest) msg.obj;
291 final Phone phone = getPhoneFromRequest(request);
292 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
293 String ussdRequest = ussdObject.first;
294 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700295
296 if (!isUssdApiAllowed(request.subId)) {
297 // Carrier does not support use of this API, return failure.
298 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
299 UssdResponse response = new UssdResponse(ussdRequest, null);
300 Bundle returnData = new Bundle();
301 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
302 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
303
304 request.result = true;
305 synchronized (request) {
306 request.notifyAll();
307 }
308 return;
309 }
310
Tyler Gunn52dcf772017-04-26 11:30:31 -0700311 try {
312 request.result = phone != null ?
313 phone.handleUssdRequest(ussdRequest, wrappedCallback)
314 : false;
315 } catch (CallStateException cse) {
316 request.result = false;
317 }
pkanwar32d516d2016-10-14 19:37:38 -0700318 // Wake up the requesting thread
319 synchronized (request) {
320 request.notifyAll();
321 }
322 break;
323 }
324
Yorke Lee716f67e2015-06-17 15:39:16 -0700325 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700326 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700327 final Phone phone = getPhoneFromRequest(request);
328 request.result = phone != null ?
329 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
330 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 // Wake up the requesting thread
332 synchronized (request) {
333 request.notifyAll();
334 }
335 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700336 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337
338 case CMD_HANDLE_NEIGHBORING_CELL:
339 request = (MainThreadRequest) msg.obj;
340 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
341 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700342 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 break;
344
345 case EVENT_NEIGHBORING_CELL_DONE:
346 ar = (AsyncResult) msg.obj;
347 request = (MainThreadRequest) ar.userObj;
348 if (ar.exception == null && ar.result != null) {
349 request.result = ar.result;
350 } else {
351 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800352 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 }
354 // Wake up the requesting thread
355 synchronized (request) {
356 request.notifyAll();
357 }
358 break;
359
360 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700361 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800362 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700363 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700364 break;
365
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700366 case CMD_END_CALL:
367 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800368 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700369 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700370 Phone phone = getPhone(end_subId);
371 if (phone == null) {
372 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
373 break;
374 }
375 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
377 // CDMA: If the user presses the Power button we treat it as
378 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700379 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
381 // GSM: End the call as per the Phone state
382 hungUp = PhoneUtils.hangup(mCM);
383 } else {
384 throw new IllegalStateException("Unexpected phone type: " + phoneType);
385 }
386 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
387 request.result = hungUp;
388 // Wake up the requesting thread
389 synchronized (request) {
390 request.notifyAll();
391 }
392 break;
393
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700394 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700395 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700396 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800397 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700398 if (uiccCard == null) {
399 loge("iccTransmitApduLogicalChannel: No UICC");
400 request.result = new IccIoResult(0x6F, 0, (byte[])null);
401 synchronized (request) {
402 request.notifyAll();
403 }
404 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700405 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
406 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700407 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700408 iccArgument.channel, iccArgument.cla, iccArgument.command,
409 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700410 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700411 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700412 break;
413
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700415 ar = (AsyncResult) msg.obj;
416 request = (MainThreadRequest) ar.userObj;
417 if (ar.exception == null && ar.result != null) {
418 request.result = ar.result;
419 } else {
420 request.result = new IccIoResult(0x6F, 0, (byte[])null);
421 if (ar.result == null) {
422 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800423 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700424 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800425 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700426 } else {
427 loge("iccTransmitApduLogicalChannel: Unknown exception");
428 }
429 }
430 synchronized (request) {
431 request.notifyAll();
432 }
433 break;
434
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700435 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
436 request = (MainThreadRequest) msg.obj;
437 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800438 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700439 if (uiccCard == null) {
440 loge("iccTransmitApduBasicChannel: No UICC");
441 request.result = new IccIoResult(0x6F, 0, (byte[])null);
442 synchronized (request) {
443 request.notifyAll();
444 }
445 } else {
446 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
447 request);
448 uiccCard.iccTransmitApduBasicChannel(
449 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
450 iccArgument.p3, iccArgument.data, onCompleted);
451 }
452 break;
453
454 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
455 ar = (AsyncResult) msg.obj;
456 request = (MainThreadRequest) ar.userObj;
457 if (ar.exception == null && ar.result != null) {
458 request.result = ar.result;
459 } else {
460 request.result = new IccIoResult(0x6F, 0, (byte[])null);
461 if (ar.result == null) {
462 loge("iccTransmitApduBasicChannel: Empty response");
463 } else if (ar.exception instanceof CommandException) {
464 loge("iccTransmitApduBasicChannel: CommandException: " +
465 ar.exception);
466 } else {
467 loge("iccTransmitApduBasicChannel: Unknown exception");
468 }
469 }
470 synchronized (request) {
471 request.notifyAll();
472 }
473 break;
474
475 case CMD_EXCHANGE_SIM_IO:
476 request = (MainThreadRequest) msg.obj;
477 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800478 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700479 if (uiccCard == null) {
480 loge("iccExchangeSimIO: No UICC");
481 request.result = new IccIoResult(0x6F, 0, (byte[])null);
482 synchronized (request) {
483 request.notifyAll();
484 }
485 } else {
486 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
487 request);
488 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
489 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
490 iccArgument.data, onCompleted);
491 }
492 break;
493
494 case EVENT_EXCHANGE_SIM_IO_DONE:
495 ar = (AsyncResult) msg.obj;
496 request = (MainThreadRequest) ar.userObj;
497 if (ar.exception == null && ar.result != null) {
498 request.result = ar.result;
499 } else {
500 request.result = new IccIoResult(0x6f, 0, (byte[])null);
501 }
502 synchronized (request) {
503 request.notifyAll();
504 }
505 break;
506
Derek Tan4d5e5c12014-02-04 11:54:58 -0800507 case CMD_SEND_ENVELOPE:
508 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800509 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700510 if (uiccCard == null) {
511 loge("sendEnvelopeWithStatus: No UICC");
512 request.result = new IccIoResult(0x6F, 0, (byte[])null);
513 synchronized (request) {
514 request.notifyAll();
515 }
516 } else {
517 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
518 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
519 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800520 break;
521
522 case EVENT_SEND_ENVELOPE_DONE:
523 ar = (AsyncResult) msg.obj;
524 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700525 if (ar.exception == null && ar.result != null) {
526 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800527 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700528 request.result = new IccIoResult(0x6F, 0, (byte[])null);
529 if (ar.result == null) {
530 loge("sendEnvelopeWithStatus: Empty response");
531 } else if (ar.exception instanceof CommandException) {
532 loge("sendEnvelopeWithStatus: CommandException: " +
533 ar.exception);
534 } else {
535 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
536 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800537 }
538 synchronized (request) {
539 request.notifyAll();
540 }
541 break;
542
Shishir Agrawal566b7612013-10-28 14:41:00 -0700543 case CMD_OPEN_CHANNEL:
544 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800545 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800546 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700547 if (uiccCard == null) {
548 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800549 request.result = new IccOpenLogicalChannelResponse(-1,
550 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700551 synchronized (request) {
552 request.notifyAll();
553 }
554 } else {
555 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800556 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
557 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700558 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700559 break;
560
561 case EVENT_OPEN_CHANNEL_DONE:
562 ar = (AsyncResult) msg.obj;
563 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700564 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700565 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700566 int[] result = (int[]) ar.result;
567 int channelId = result[0];
568 byte[] selectResponse = null;
569 if (result.length > 1) {
570 selectResponse = new byte[result.length - 1];
571 for (int i = 1; i < result.length; ++i) {
572 selectResponse[i - 1] = (byte) result[i];
573 }
574 }
575 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700576 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700577 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700578 if (ar.result == null) {
579 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700580 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700581 if (ar.exception != null) {
582 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
583 }
584
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700585 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700586 if (ar.exception instanceof CommandException) {
587 CommandException.Error error =
588 ((CommandException) (ar.exception)).getCommandError();
589 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700590 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700591 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700592 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700593 }
594 }
595 openChannelResp = new IccOpenLogicalChannelResponse(
596 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700597 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700598 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700599 synchronized (request) {
600 request.notifyAll();
601 }
602 break;
603
604 case CMD_CLOSE_CHANNEL:
605 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800606 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700607 if (uiccCard == null) {
608 loge("iccCloseLogicalChannel: No UICC");
609 request.result = new IccIoResult(0x6F, 0, (byte[])null);
610 synchronized (request) {
611 request.notifyAll();
612 }
613 } else {
614 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
615 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
616 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700617 break;
618
619 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800620 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
621 break;
622
623 case CMD_NV_READ_ITEM:
624 request = (MainThreadRequest) msg.obj;
625 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
626 mPhone.nvReadItem((Integer) request.argument, onCompleted);
627 break;
628
629 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700630 ar = (AsyncResult) msg.obj;
631 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800632 if (ar.exception == null && ar.result != null) {
633 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700634 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800635 request.result = "";
636 if (ar.result == null) {
637 loge("nvReadItem: Empty response");
638 } else if (ar.exception instanceof CommandException) {
639 loge("nvReadItem: CommandException: " +
640 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700641 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800642 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700643 }
644 }
645 synchronized (request) {
646 request.notifyAll();
647 }
648 break;
649
Jake Hambye994d462014-02-03 13:10:13 -0800650 case CMD_NV_WRITE_ITEM:
651 request = (MainThreadRequest) msg.obj;
652 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
653 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
654 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
655 break;
656
657 case EVENT_NV_WRITE_ITEM_DONE:
658 handleNullReturnEvent(msg, "nvWriteItem");
659 break;
660
661 case CMD_NV_WRITE_CDMA_PRL:
662 request = (MainThreadRequest) msg.obj;
663 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
664 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
665 break;
666
667 case EVENT_NV_WRITE_CDMA_PRL_DONE:
668 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
669 break;
670
671 case CMD_NV_RESET_CONFIG:
672 request = (MainThreadRequest) msg.obj;
673 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
674 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
675 break;
676
677 case EVENT_NV_RESET_CONFIG_DONE:
678 handleNullReturnEvent(msg, "nvResetConfig");
679 break;
680
Jake Hamby7c27be32014-03-03 13:25:59 -0800681 case CMD_GET_PREFERRED_NETWORK_TYPE:
682 request = (MainThreadRequest) msg.obj;
683 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700684 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800685 break;
686
687 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
688 ar = (AsyncResult) msg.obj;
689 request = (MainThreadRequest) ar.userObj;
690 if (ar.exception == null && ar.result != null) {
691 request.result = ar.result; // Integer
692 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800693 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800694 if (ar.result == null) {
695 loge("getPreferredNetworkType: Empty response");
696 } else if (ar.exception instanceof CommandException) {
697 loge("getPreferredNetworkType: CommandException: " +
698 ar.exception);
699 } else {
700 loge("getPreferredNetworkType: Unknown exception");
701 }
702 }
703 synchronized (request) {
704 request.notifyAll();
705 }
706 break;
707
708 case CMD_SET_PREFERRED_NETWORK_TYPE:
709 request = (MainThreadRequest) msg.obj;
710 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
711 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700712 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800713 break;
714
715 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
716 handleNullReturnEvent(msg, "setPreferredNetworkType");
717 break;
718
Steven Liu4bf01bc2014-07-17 11:05:29 -0500719 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
720 request = (MainThreadRequest)msg.obj;
721 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
722 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
723 break;
724
725 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
726 ar = (AsyncResult)msg.obj;
727 request = (MainThreadRequest)ar.userObj;
728 request.result = ar;
729 synchronized (request) {
730 request.notifyAll();
731 }
732 break;
733
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800734 case CMD_SET_VOICEMAIL_NUMBER:
735 request = (MainThreadRequest) msg.obj;
736 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
737 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800738 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
739 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800740 break;
741
742 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
743 handleNullReturnEvent(msg, "setVoicemailNumber");
744 break;
745
Stuart Scott54788802015-03-30 13:18:01 -0700746 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
747 request = (MainThreadRequest) msg.obj;
748 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
749 request);
750 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
751 break;
752
753 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
754 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
755 break;
756
Shishir Agrawal302c8692015-06-19 13:49:39 -0700757 case CMD_PERFORM_NETWORK_SCAN:
758 request = (MainThreadRequest) msg.obj;
759 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
760 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
761 break;
762
763 case EVENT_PERFORM_NETWORK_SCAN_DONE:
764 ar = (AsyncResult) msg.obj;
765 request = (MainThreadRequest) ar.userObj;
766 CellNetworkScanResult cellScanResult;
767 if (ar.exception == null && ar.result != null) {
768 cellScanResult = new CellNetworkScanResult(
769 CellNetworkScanResult.STATUS_SUCCESS,
770 (List<OperatorInfo>) ar.result);
771 } else {
772 if (ar.result == null) {
773 loge("getCellNetworkScanResults: Empty response");
774 }
775 if (ar.exception != null) {
776 loge("getCellNetworkScanResults: Exception: " + ar.exception);
777 }
778 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
779 if (ar.exception instanceof CommandException) {
780 CommandException.Error error =
781 ((CommandException) (ar.exception)).getCommandError();
782 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
783 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
784 } else if (error == CommandException.Error.GENERIC_FAILURE) {
785 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
786 }
787 }
788 cellScanResult = new CellNetworkScanResult(errorCode, null);
789 }
790 request.result = cellScanResult;
791 synchronized (request) {
792 request.notifyAll();
793 }
794 break;
795
796 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
797 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700798 ManualNetworkSelectionArgument selArg =
799 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700800 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
801 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700802 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
803 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700804 break;
805
806 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
807 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
808 break;
809
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700810 case CMD_GET_MODEM_ACTIVITY_INFO:
811 request = (MainThreadRequest) msg.obj;
812 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700813 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700814 break;
815
816 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
817 ar = (AsyncResult) msg.obj;
818 request = (MainThreadRequest) ar.userObj;
819 if (ar.exception == null && ar.result != null) {
820 request.result = ar.result;
821 } else {
822 if (ar.result == null) {
823 loge("queryModemActivityInfo: Empty response");
824 } else if (ar.exception instanceof CommandException) {
825 loge("queryModemActivityInfo: CommandException: " +
826 ar.exception);
827 } else {
828 loge("queryModemActivityInfo: Unknown exception");
829 }
830 }
Amit Mahajand4766222016-01-28 15:28:28 -0800831 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
832 if (request.result == null) {
833 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
834 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700835 synchronized (request) {
836 request.notifyAll();
837 }
838 break;
839
Meng Wang1a7c35a2016-05-05 20:56:15 -0700840 case CMD_SET_ALLOWED_CARRIERS:
841 request = (MainThreadRequest) msg.obj;
842 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
843 mPhone.setAllowedCarriers(
844 (List<CarrierIdentifier>) request.argument,
845 onCompleted);
846 break;
847
848 case EVENT_SET_ALLOWED_CARRIERS_DONE:
849 ar = (AsyncResult) msg.obj;
850 request = (MainThreadRequest) ar.userObj;
851 if (ar.exception == null && ar.result != null) {
852 request.result = ar.result;
853 } else {
854 if (ar.result == null) {
855 loge("setAllowedCarriers: Empty response");
856 } else if (ar.exception instanceof CommandException) {
857 loge("setAllowedCarriers: CommandException: " +
858 ar.exception);
859 } else {
860 loge("setAllowedCarriers: Unknown exception");
861 }
862 }
863 // Result cannot be null. Return -1 on error.
864 if (request.result == null) {
865 request.result = new int[]{-1};
866 }
867 synchronized (request) {
868 request.notifyAll();
869 }
870 break;
871
872 case CMD_GET_ALLOWED_CARRIERS:
873 request = (MainThreadRequest) msg.obj;
874 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
875 mPhone.getAllowedCarriers(onCompleted);
876 break;
877
878 case EVENT_GET_ALLOWED_CARRIERS_DONE:
879 ar = (AsyncResult) msg.obj;
880 request = (MainThreadRequest) ar.userObj;
881 if (ar.exception == null && ar.result != null) {
882 request.result = ar.result;
883 } else {
884 if (ar.result == null) {
885 loge("getAllowedCarriers: Empty response");
886 } else if (ar.exception instanceof CommandException) {
887 loge("getAllowedCarriers: CommandException: " +
888 ar.exception);
889 } else {
890 loge("getAllowedCarriers: Unknown exception");
891 }
892 }
893 // Result cannot be null. Return empty list of CarrierIdentifier.
894 if (request.result == null) {
895 request.result = new ArrayList<CarrierIdentifier>(0);
896 }
897 synchronized (request) {
898 request.notifyAll();
899 }
900 break;
901
Nathan Haroldb3014052017-01-25 15:57:32 -0800902 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
903 ar = (AsyncResult) msg.obj;
904 request = (MainThreadRequest) ar.userObj;
905 if (ar.exception == null && ar.result != null) {
906 request.result = ar.result;
907 } else {
908 request.result = new IllegalArgumentException(
909 "Failed to retrieve Forbidden Plmns");
910 if (ar.result == null) {
911 loge("getForbiddenPlmns: Empty response");
912 } else {
913 loge("getForbiddenPlmns: Unknown exception");
914 }
915 }
916 synchronized (request) {
917 request.notifyAll();
918 }
919 break;
920
921 case CMD_GET_FORBIDDEN_PLMNS:
922 request = (MainThreadRequest) msg.obj;
923 uiccCard = getUiccCardFromRequest(request);
924 if (uiccCard == null) {
925 loge("getForbiddenPlmns() UiccCard is null");
926 request.result = new IllegalArgumentException(
927 "getForbiddenPlmns() UiccCard is null");
928 synchronized (request) {
929 request.notifyAll();
930 }
931 break;
932 }
933 Integer appType = (Integer) request.argument;
934 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
935 if (uiccApp == null) {
936 loge("getForbiddenPlmns() no app with specified type -- "
937 + appType);
938 request.result = new IllegalArgumentException("Failed to get UICC App");
939 synchronized (request) {
940 request.notifyAll();
941 }
942 break;
943 } else {
944 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
945 + " specified type -- " + appType);
946 }
947 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
948 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
949 onCompleted);
950 break;
951
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 default:
953 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
954 break;
955 }
956 }
Jake Hambye994d462014-02-03 13:10:13 -0800957
958 private void handleNullReturnEvent(Message msg, String command) {
959 AsyncResult ar = (AsyncResult) msg.obj;
960 MainThreadRequest request = (MainThreadRequest) ar.userObj;
961 if (ar.exception == null) {
962 request.result = true;
963 } else {
964 request.result = false;
965 if (ar.exception instanceof CommandException) {
966 loge(command + ": CommandException: " + ar.exception);
967 } else {
968 loge(command + ": Unknown exception");
969 }
970 }
971 synchronized (request) {
972 request.notifyAll();
973 }
974 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 }
976
977 /**
978 * Posts the specified command to be executed on the main thread,
979 * waits for the request to complete, and returns the result.
980 * @see #sendRequestAsync
981 */
982 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800983 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700984 }
985
986 /**
987 * Posts the specified command to be executed on the main thread,
988 * waits for the request to complete, and returns the result.
989 * @see #sendRequestAsync
990 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800991 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700992 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
993 throw new RuntimeException("This method will deadlock if called from the main thread.");
994 }
995
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800996 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 Message msg = mMainThreadHandler.obtainMessage(command, request);
998 msg.sendToTarget();
999
1000 // Wait for the request to complete
1001 synchronized (request) {
1002 while (request.result == null) {
1003 try {
1004 request.wait();
1005 } catch (InterruptedException e) {
1006 // Do nothing, go back and wait until the request is complete
1007 }
1008 }
1009 }
1010 return request.result;
1011 }
1012
1013 /**
1014 * Asynchronous ("fire and forget") version of sendRequest():
1015 * Posts the specified command to be executed on the main thread, and
1016 * returns immediately.
1017 * @see #sendRequest
1018 */
1019 private void sendRequestAsync(int command) {
1020 mMainThreadHandler.sendEmptyMessage(command);
1021 }
1022
1023 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001024 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1025 * @see {@link #sendRequest(int,Object)}
1026 */
1027 private void sendRequestAsync(int command, Object argument) {
1028 MainThreadRequest request = new MainThreadRequest(argument);
1029 Message msg = mMainThreadHandler.obtainMessage(command, request);
1030 msg.sendToTarget();
1031 }
1032
1033 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 * Initialize the singleton PhoneInterfaceManager instance.
1035 * This is only done once, at startup, from PhoneApp.onCreate().
1036 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001037 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 synchronized (PhoneInterfaceManager.class) {
1039 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001040 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001041 } else {
1042 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1043 }
1044 return sInstance;
1045 }
1046 }
1047
1048 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001049 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 mApp = app;
1051 mPhone = phone;
1052 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001053 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1055 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001056 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001057 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001058 mSubscriptionController = SubscriptionController.getInstance();
yinxub1bed742017-04-17 11:45:04 -07001059 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Wink Saville3ab207e2014-11-20 13:07:20 -08001060
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 publish();
1062 }
1063
1064 private void publish() {
1065 if (DBG) log("publish: " + this);
1066
1067 ServiceManager.addService("phone", this);
1068 }
1069
Stuart Scott584921c2015-01-15 17:10:34 -08001070 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001071 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1072 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001073 }
1074
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001075 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1076 Phone phone = getPhoneFromRequest(request);
1077 return phone == null ? null :
1078 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1079 }
1080
Wink Saville36469e72014-06-11 15:17:00 -07001081 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001082 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001083 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001084 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 //
1086 // Implementation of the ITelephony interface.
1087 //
1088
1089 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001090 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001091 }
1092
Wink Savilleb564aae2014-10-23 10:18:09 -07001093 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 if (DBG) log("dial: " + number);
1095 // No permission check needed here: This is just a wrapper around the
1096 // ACTION_DIAL intent, which is available to any app since it puts up
1097 // the UI before it does anything.
1098
1099 String url = createTelUrl(number);
1100 if (url == null) {
1101 return;
1102 }
1103
1104 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001105 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1107 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1108 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1109 mApp.startActivity(intent);
1110 }
1111 }
1112
1113 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001114 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001115 }
1116
Wink Savilleb564aae2014-10-23 10:18:09 -07001117 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 if (DBG) log("call: " + number);
1119
1120 // This is just a wrapper around the ACTION_CALL intent, but we still
1121 // need to do a permission check since we're calling startActivity()
1122 // from the context of the phone app.
1123 enforceCallPermission();
1124
1125 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1126 != AppOpsManager.MODE_ALLOWED) {
1127 return;
1128 }
1129
1130 String url = createTelUrl(number);
1131 if (url == null) {
1132 return;
1133 }
1134
Wink Saville08874612014-08-31 19:19:58 -07001135 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001136 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001137 if (slist != null) {
1138 for (SubscriptionInfo subInfoRecord : slist) {
1139 if (subInfoRecord.getSubscriptionId() == subId) {
1140 isValid = true;
1141 break;
1142 }
Wink Saville08874612014-08-31 19:19:58 -07001143 }
1144 }
1145 if (isValid == false) {
1146 return;
1147 }
1148
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001150 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1152 mApp.startActivity(intent);
1153 }
1154
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001155 /**
1156 * End a call based on call state
1157 * @return true is a call was ended
1158 */
1159 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001160 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001161 }
1162
1163 /**
1164 * End a call based on the call state of the subId
1165 * @return true is a call was ended
1166 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001167 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001169 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 }
1171
1172 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001173 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001174 }
1175
Wink Savilleb564aae2014-10-23 10:18:09 -07001176 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001177 if (DBG) log("answerRingingCall...");
1178 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1179 // but that can probably wait till the big TelephonyManager API overhaul.
1180 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1181 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001182 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183 }
1184
1185 /**
1186 * Make the actual telephony calls to implement answerRingingCall().
1187 * This should only be called from the main thread of the Phone app.
1188 * @see #answerRingingCall
1189 *
1190 * TODO: it would be nice to return true if we answered the call, or
1191 * false if there wasn't actually a ringing incoming call, or some
1192 * other error occurred. (In other words, pass back the return value
1193 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1194 * But that would require calling this method via sendRequest() rather
1195 * than sendRequestAsync(), and right now we don't actually *need* that
1196 * return value, so let's just return void for now.
1197 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001198 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001199 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001201 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1202 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 if (hasActiveCall && hasHoldingCall) {
1204 // Both lines are in use!
1205 // TODO: provide a flag to let the caller specify what
1206 // policy to use if both lines are in use. (The current
1207 // behavior is hardwired to "answer incoming, end ongoing",
1208 // which is how the CALL button is specced to behave.)
1209 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1210 return;
1211 } else {
1212 // answerCall() will automatically hold the current active
1213 // call, if there is one.
1214 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1215 return;
1216 }
1217 } else {
1218 // No call was ringing.
1219 return;
1220 }
1221 }
1222
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001224 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001225 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001226 public void silenceRinger() {
1227 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001228 }
1229
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001230 @Override
1231 public boolean isOffhook(String callingPackage) {
1232 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001233 }
1234
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001235 @Override
1236 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1237 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1238 return false;
1239 }
1240
Sanket Padawe356d7632015-06-22 14:03:32 -07001241 final Phone phone = getPhone(subId);
1242 if (phone != null) {
1243 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1244 } else {
1245 return false;
1246 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001247 }
1248
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001249 @Override
1250 public boolean isRinging(String callingPackage) {
1251 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001252 }
1253
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001254 @Override
1255 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1256 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1257 return false;
1258 }
1259
Sanket Padawe356d7632015-06-22 14:03:32 -07001260 final Phone phone = getPhone(subId);
1261 if (phone != null) {
1262 return (phone.getState() == PhoneConstants.State.RINGING);
1263 } else {
1264 return false;
1265 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 }
1267
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001268 @Override
1269 public boolean isIdle(String callingPackage) {
1270 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001271 }
1272
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001273 @Override
1274 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1275 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1276 return false;
1277 }
1278
Sanket Padawe356d7632015-06-22 14:03:32 -07001279 final Phone phone = getPhone(subId);
1280 if (phone != null) {
1281 return (phone.getState() == PhoneConstants.State.IDLE);
1282 } else {
1283 return false;
1284 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 }
1286
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001289 }
1290
Wink Savilleb564aae2014-10-23 10:18:09 -07001291 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001292 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001293 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1294 }
1295
1296 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001297 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001298 }
1299
Wink Savilleb564aae2014-10-23 10:18:09 -07001300 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001301 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001302 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1303 }
1304
1305 /** {@hide} */
1306 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001307 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001308 }
1309
Wink Savilleb564aae2014-10-23 10:18:09 -07001310 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001311 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001312 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001313 checkSimPin.start();
1314 return checkSimPin.unlockSim(null, pin);
1315 }
1316
Wink Saville9de0f752013-10-22 19:04:03 -07001317 /** {@hide} */
1318 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001319 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001320 }
1321
Wink Savilleb564aae2014-10-23 10:18:09 -07001322 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001324 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 checkSimPuk.start();
1326 return checkSimPuk.unlockSim(puk, pin);
1327 }
1328
1329 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001330 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 * a synchronous one.
1332 */
1333 private static class UnlockSim extends Thread {
1334
1335 private final IccCard mSimCard;
1336
1337 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001338 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1339 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340
1341 // For replies from SimCard interface
1342 private Handler mHandler;
1343
1344 // For async handler to identify request type
1345 private static final int SUPPLY_PIN_COMPLETE = 100;
1346
1347 public UnlockSim(IccCard simCard) {
1348 mSimCard = simCard;
1349 }
1350
1351 @Override
1352 public void run() {
1353 Looper.prepare();
1354 synchronized (UnlockSim.this) {
1355 mHandler = new Handler() {
1356 @Override
1357 public void handleMessage(Message msg) {
1358 AsyncResult ar = (AsyncResult) msg.obj;
1359 switch (msg.what) {
1360 case SUPPLY_PIN_COMPLETE:
1361 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1362 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001363 mRetryCount = msg.arg1;
1364 if (ar.exception != null) {
1365 if (ar.exception instanceof CommandException &&
1366 ((CommandException)(ar.exception)).getCommandError()
1367 == CommandException.Error.PASSWORD_INCORRECT) {
1368 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1369 } else {
1370 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1371 }
1372 } else {
1373 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1374 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375 mDone = true;
1376 UnlockSim.this.notifyAll();
1377 }
1378 break;
1379 }
1380 }
1381 };
1382 UnlockSim.this.notifyAll();
1383 }
1384 Looper.loop();
1385 }
1386
1387 /*
1388 * Use PIN or PUK to unlock SIM card
1389 *
1390 * If PUK is null, unlock SIM card with PIN
1391 *
1392 * If PUK is not null, unlock SIM card with PUK and set PIN code
1393 */
Wink Saville9de0f752013-10-22 19:04:03 -07001394 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395
1396 while (mHandler == null) {
1397 try {
1398 wait();
1399 } catch (InterruptedException e) {
1400 Thread.currentThread().interrupt();
1401 }
1402 }
1403 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1404
1405 if (puk == null) {
1406 mSimCard.supplyPin(pin, callback);
1407 } else {
1408 mSimCard.supplyPuk(puk, pin, callback);
1409 }
1410
1411 while (!mDone) {
1412 try {
1413 Log.d(LOG_TAG, "wait for done");
1414 wait();
1415 } catch (InterruptedException e) {
1416 // Restore the interrupted status
1417 Thread.currentThread().interrupt();
1418 }
1419 }
1420 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001421 int[] resultArray = new int[2];
1422 resultArray[0] = mResult;
1423 resultArray[1] = mRetryCount;
1424 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426 }
1427
1428 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001429 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001430
1431 }
1432
Wink Savilleb564aae2014-10-23 10:18:09 -07001433 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 // No permission check needed here: this call is harmless, and it's
1435 // needed for the ServiceState.requestStateUpdate() call (which is
1436 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001437 final Phone phone = getPhone(subId);
1438 if (phone != null) {
1439 phone.updateServiceLocation();
1440 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 }
1442
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001443 @Override
1444 public boolean isRadioOn(String callingPackage) {
1445 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001446 }
1447
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001448 @Override
1449 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1450 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1451 return false;
1452 }
1453 return isRadioOnForSubscriber(subId);
1454 }
1455
1456 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001457 final Phone phone = getPhone(subId);
1458 if (phone != null) {
1459 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1460 } else {
1461 return false;
1462 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001463 }
1464
1465 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001466 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001467
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 }
Wink Saville36469e72014-06-11 15:17:00 -07001469
Wink Savilleb564aae2014-10-23 10:18:09 -07001470 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001472 final Phone phone = getPhone(subId);
1473 if (phone != null) {
1474 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1475 }
Wink Saville36469e72014-06-11 15:17:00 -07001476 }
1477
1478 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001479 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001480 }
1481
Wink Savilleb564aae2014-10-23 10:18:09 -07001482 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001483 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001484 final Phone phone = getPhone(subId);
1485 if (phone == null) {
1486 return false;
1487 }
1488 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001489 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001490 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001491 }
1492 return true;
1493 }
Wink Saville36469e72014-06-11 15:17:00 -07001494
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001495 public boolean needMobileRadioShutdown() {
1496 /*
1497 * If any of the Radios are available, it will need to be
1498 * shutdown. So return true if any Radio is available.
1499 */
1500 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1501 Phone phone = PhoneFactory.getPhone(i);
1502 if (phone != null && phone.isRadioAvailable()) return true;
1503 }
1504 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1505 return false;
1506 }
1507
1508 public void shutdownMobileRadios() {
1509 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1510 logv("Shutting down Phone " + i);
1511 shutdownRadioUsingPhoneId(i);
1512 }
1513 }
1514
1515 private void shutdownRadioUsingPhoneId(int phoneId) {
1516 enforceModifyPermission();
1517 Phone phone = PhoneFactory.getPhone(phoneId);
1518 if (phone != null && phone.isRadioAvailable()) {
1519 phone.shutdownRadio();
1520 }
1521 }
1522
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001524 enforceModifyPermission();
Wei Liu9ae2a062016-08-08 11:09:34 -07001525 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1526 if (defaultPhone != null) {
1527 defaultPhone.setRadioPower(turnOn);
1528 return true;
1529 } else {
1530 loge("There's no default phone.");
1531 return false;
1532 }
Wink Saville36469e72014-06-11 15:17:00 -07001533 }
1534
Wink Savilleb564aae2014-10-23 10:18:09 -07001535 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001537 final Phone phone = getPhone(subId);
1538 if (phone != null) {
1539 phone.setRadioPower(turnOn);
1540 return true;
1541 } else {
1542 return false;
1543 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 }
1545
Wink Saville36469e72014-06-11 15:17:00 -07001546 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001547 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 public boolean enableDataConnectivity() {
1549 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001550 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001551 final Phone phone = getPhone(subId);
1552 if (phone != null) {
1553 phone.setDataEnabled(true);
1554 return true;
1555 } else {
1556 return false;
1557 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001558 }
1559
Wink Saville36469e72014-06-11 15:17:00 -07001560 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001561 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 public boolean disableDataConnectivity() {
1563 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001564 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001565 final Phone phone = getPhone(subId);
1566 if (phone != null) {
1567 phone.setDataEnabled(false);
1568 return true;
1569 } else {
1570 return false;
1571 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 }
1573
Sanket Padawe356d7632015-06-22 14:03:32 -07001574 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001575 public boolean isDataConnectivityPossible(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001576 final Phone phone = getPhone(subId);
1577 if (phone != null) {
Jack Yuacf8a132017-05-01 17:00:48 -07001578 return phone.isDataAllowed();
Sanket Padawe356d7632015-06-22 14:03:32 -07001579 } else {
1580 return false;
1581 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 }
1583
1584 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001585 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001586 }
1587
pkanwarae03a6b2016-11-06 20:37:09 -08001588 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001589 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001590 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1591 return;
1592 }
1593 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1594 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1595 };
1596
Wink Savilleb564aae2014-10-23 10:18:09 -07001597 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001599 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1600 return false;
1601 }
Wink Saville36469e72014-06-11 15:17:00 -07001602 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 }
1604
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001606 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001607 }
1608
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001609 public int getCallStateForSlot(int slotIndex) {
1610 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001611 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001612 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001613 }
1614
Sanket Padawe356d7632015-06-22 14:03:32 -07001615 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001617 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001618 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001619 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001620 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001621 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001622 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001623 }
1624
Sanket Padawe356d7632015-06-22 14:03:32 -07001625 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001627 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001628 if (phone != null) {
1629 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1630 } else {
1631 return TelephonyManager.DATA_ACTIVITY_NONE;
1632 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 }
1634
1635 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001636 public Bundle getCellLocation(String callingPackage) {
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001637 if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
1638 callingPackage, Binder.getCallingUid())) {
Svetoslav64fad262015-04-14 14:35:21 -07001639 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001640 }
1641
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001642 if (DBG_LOC) log("getCellLocation: is active user");
1643 Bundle data = new Bundle();
1644 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1645 if (phone == null) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 return null;
1647 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001649 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1650 phone.getCellLocation(workSource).fillInNotifierBundle(data);
1651 return data;
Svetoslav64fad262015-04-14 14:35:21 -07001652 }
1653
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001654 @Override
1655 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001656 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001657 }
1658
Sanket Padawe356d7632015-06-22 14:03:32 -07001659 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001660 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 mApp.enforceCallingOrSelfPermission(
1662 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001663 final Phone phone = getPhone(subId);
1664 if (phone != null) {
1665 phone.enableLocationUpdates();
1666 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001667 }
1668
1669 @Override
1670 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001671 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001672 }
1673
Sanket Padawe356d7632015-06-22 14:03:32 -07001674 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001675 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 mApp.enforceCallingOrSelfPermission(
1677 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001678 final Phone phone = getPhone(subId);
1679 if (phone != null) {
1680 phone.disableLocationUpdates();
1681 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001682 }
1683
1684 @Override
1685 @SuppressWarnings("unchecked")
1686 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001687 if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
1688 callingPackage, Binder.getCallingUid())) {
Svetoslav64fad262015-04-14 14:35:21 -07001689 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001690 }
1691
1692 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1693 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1694 return null;
1695 }
Svetoslav64fad262015-04-14 14:35:21 -07001696
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001697 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001699 ArrayList<NeighboringCellInfo> cells = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001700
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001701 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1702 try {
1703 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
1704 CMD_HANDLE_NEIGHBORING_CELL, workSource,
1705 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
1706 } catch (RuntimeException e) {
1707 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001708 }
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001709 return cells;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001710 }
1711
1712
1713 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001714 public List<CellInfo> getAllCellInfo(String callingPackage) {
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001715 if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
1716 callingPackage, Binder.getCallingUid())) {
Svetoslav64fad262015-04-14 14:35:21 -07001717 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001718 }
1719
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001720 if (DBG_LOC) log("getAllCellInfo: is active user");
1721 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1722 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1723 for (Phone phone : PhoneFactory.getPhones()) {
1724 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1725 if (info != null) cellInfos.addAll(info);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001726 }
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001727 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001728 }
1729
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001730 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001731 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001732 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001733 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1734 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001735 }
1736
Shishir Agrawala9f32182016-04-12 12:00:16 -07001737 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001738 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001739 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1740 return null;
1741 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001742 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001743 return phone == null ? null : phone.getImei();
1744 }
1745
1746 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001747 public String getMeidForSlot(int slotIndex, String callingPackage) {
1748 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1749 return null;
1750 }
1751 Phone phone = PhoneFactory.getPhone(slotIndex);
1752 return phone == null ? null : phone.getMeid();
1753 }
1754
1755 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001756 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001757 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1758 return null;
1759 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001760 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001761 return phone == null ? null : phone.getDeviceSvn();
1762 }
1763
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001764 //
1765 // Internal helper methods.
1766 //
1767
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001768 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001769 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1770 *
1771 * @throws SecurityException if the caller does not have the required permission
1772 */
1773 private void enforceModifyPermission() {
1774 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1775 }
1776
1777 /**
Junda Liua2e36012014-07-09 18:30:01 -07001778 * Make sure either system app or the caller has carrier privilege.
1779 *
1780 * @throws SecurityException if the caller does not have the required permission/privilege
1781 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001782 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001783 int permission = mApp.checkCallingOrSelfPermission(
1784 android.Manifest.permission.MODIFY_PHONE_STATE);
1785 if (permission == PackageManager.PERMISSION_GRANTED) {
1786 return;
1787 }
1788
1789 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001790 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001791 }
1792
1793 /**
1794 * Make sure the caller has carrier privilege.
1795 *
1796 * @throws SecurityException if the caller does not have the required permission
1797 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001798 private void enforceCarrierPrivilege(int subId) {
1799 if (getCarrierPrivilegeStatus(subId) !=
1800 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001801 loge("No Carrier Privilege.");
1802 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001803 }
1804 }
1805
1806 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001807 * Make sure the caller has the CALL_PHONE permission.
1808 *
1809 * @throws SecurityException if the caller does not have the required permission
1810 */
1811 private void enforceCallPermission() {
1812 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1813 }
1814
Stuart Scott8eef64f2015-04-08 15:13:54 -07001815 private void enforceConnectivityInternalPermission() {
1816 mApp.enforceCallingOrSelfPermission(
1817 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1818 "ConnectivityService");
1819 }
1820
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001821 private String createTelUrl(String number) {
1822 if (TextUtils.isEmpty(number)) {
1823 return null;
1824 }
1825
Jake Hambye994d462014-02-03 13:10:13 -08001826 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001827 }
1828
Ihab Awadf9e92732013-12-05 18:02:52 -08001829 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001830 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1831 }
1832
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001833 private static void logv(String msg) {
1834 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1835 }
1836
Ihab Awadf9e92732013-12-05 18:02:52 -08001837 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001838 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1839 }
1840
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001841 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001842 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001843 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001844 }
1845
Sanket Padawe356d7632015-06-22 14:03:32 -07001846 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001847 public int getActivePhoneTypeForSlot(int slotIndex) {
1848 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001849 if (phone == null) {
1850 return PhoneConstants.PHONE_TYPE_NONE;
1851 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001852 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001853 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001854 }
1855
1856 /**
1857 * Returns the CDMA ERI icon index to display
1858 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001859 @Override
1860 public int getCdmaEriIconIndex(String callingPackage) {
1861 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001862 }
1863
Sanket Padawe356d7632015-06-22 14:03:32 -07001864 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001865 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1866 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1867 return -1;
1868 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001869 final Phone phone = getPhone(subId);
1870 if (phone != null) {
1871 return phone.getCdmaEriIconIndex();
1872 } else {
1873 return -1;
1874 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001875 }
1876
1877 /**
1878 * Returns the CDMA ERI icon mode,
1879 * 0 - ON
1880 * 1 - FLASHING
1881 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001882 @Override
1883 public int getCdmaEriIconMode(String callingPackage) {
1884 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001885 }
1886
Sanket Padawe356d7632015-06-22 14:03:32 -07001887 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001888 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1889 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1890 return -1;
1891 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001892 final Phone phone = getPhone(subId);
1893 if (phone != null) {
1894 return phone.getCdmaEriIconMode();
1895 } else {
1896 return -1;
1897 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001898 }
1899
1900 /**
1901 * Returns the CDMA ERI text,
1902 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001903 @Override
1904 public String getCdmaEriText(String callingPackage) {
1905 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001906 }
1907
Sanket Padawe356d7632015-06-22 14:03:32 -07001908 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001909 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1910 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1911 return null;
1912 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001913 final Phone phone = getPhone(subId);
1914 if (phone != null) {
1915 return phone.getCdmaEriText();
1916 } else {
1917 return null;
1918 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001919 }
1920
1921 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001922 * Returns the CDMA MDN.
1923 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001925 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001926 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001927 final Phone phone = getPhone(subId);
1928 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1929 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001930 } else {
1931 return null;
1932 }
1933 }
1934
1935 /**
1936 * Returns the CDMA MIN.
1937 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001938 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001939 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001940 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001941 final Phone phone = getPhone(subId);
1942 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1943 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001944 } else {
1945 return null;
1946 }
1947 }
1948
1949 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001950 * Returns true if CDMA provisioning needs to run.
1951 */
1952 public boolean needsOtaServiceProvisioning() {
1953 return mPhone.needsOtaServiceProvisioning();
1954 }
1955
1956 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001957 * Sets the voice mail number of a given subId.
1958 */
1959 @Override
1960 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001961 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001962 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1963 new Pair<String, String>(alphaTag, number), new Integer(subId));
1964 return success;
1965 }
1966
Ta-wei Yen87c49842016-05-13 21:19:52 -07001967 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07001968 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
1969 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1970 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
1971 if (!TextUtils.equals(callingPackage, systemDialer)) {
1972 throw new SecurityException("caller must be system dialer");
1973 }
1974 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
1975 if (phoneAccountHandle == null){
1976 return null;
1977 }
1978 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
1979 }
1980
1981 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08001982 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08001983 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1984 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
1985 return null;
1986 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08001987 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
1988 }
1989
1990 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001991 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1992 VisualVoicemailSmsFilterSettings settings) {
1993 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001994 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001995 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1996 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001997 }
1998
1999 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002000 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2001 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002002 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002003 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002004 }
2005
2006 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002007 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2008 String callingPackage, int subId) {
2009 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002010 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002011 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002012 }
2013
2014 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002015 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002016 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002017 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002018 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2019 }
2020
2021 @Override
2022 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2023 String number, int port, String text, PendingIntent sentIntent) {
2024 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002025 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002026 enforceSendSmsPermission();
2027 // Make the calls as the phone process.
2028 final long identity = Binder.clearCallingIdentity();
2029 try {
2030 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2031 if (port == 0) {
2032 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2033 sentIntent, null, false);
2034 } else {
2035 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2036 smsManager.sendDataMessageWithSelfPermissions(number, null,
2037 (short) port, data, sentIntent, null);
2038 }
2039 } finally {
2040 Binder.restoreCallingIdentity(identity);
2041 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002042 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002043 /**
fionaxu0152e512016-11-14 13:36:14 -08002044 * Sets the voice activation state of a given subId.
2045 */
2046 @Override
2047 public void setVoiceActivationState(int subId, int activationState) {
2048 enforceModifyPermissionOrCarrierPrivilege(subId);
2049 final Phone phone = getPhone(subId);
2050 if (phone != null) {
2051 phone.setVoiceActivationState(activationState);
2052 } else {
2053 loge("setVoiceActivationState fails with invalid subId: " + subId);
2054 }
2055 }
2056
2057 /**
2058 * Sets the data activation state of a given subId.
2059 */
2060 @Override
2061 public void setDataActivationState(int subId, int activationState) {
2062 enforceModifyPermissionOrCarrierPrivilege(subId);
2063 final Phone phone = getPhone(subId);
2064 if (phone != null) {
2065 phone.setDataActivationState(activationState);
2066 } else {
2067 loge("setVoiceActivationState fails with invalid subId: " + subId);
2068 }
2069 }
2070
2071 /**
2072 * Returns the voice activation state of a given subId.
2073 */
2074 @Override
2075 public int getVoiceActivationState(int subId, String callingPackage) {
2076 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2077 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2078 }
2079 final Phone phone = getPhone(subId);
2080 if (phone != null) {
2081 return phone.getVoiceActivationState();
2082 } else {
2083 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2084 }
2085 }
2086
2087 /**
2088 * Returns the data activation state of a given subId.
2089 */
2090 @Override
2091 public int getDataActivationState(int subId, String callingPackage) {
2092 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2093 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2094 }
2095 final Phone phone = getPhone(subId);
2096 if (phone != null) {
2097 return phone.getDataActivationState();
2098 } else {
2099 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2100 }
2101 }
2102
2103 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002104 * Returns the unread count of voicemails
2105 */
2106 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002107 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002108 }
2109
2110 /**
2111 * Returns the unread count of voicemails for a subId
2112 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002113 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002114 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002115 final Phone phone = getPhone(subId);
2116 if (phone != null) {
2117 return phone.getVoiceMessageCount();
2118 } else {
2119 return 0;
2120 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002121 }
2122
2123 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002124 * returns true, if the device is in a state where both voice and data
2125 * are supported simultaneously. This can change based on location or network condition.
2126 */
2127 @Override
2128 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2129 final Phone phone = getPhone(subId);
2130 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2131 }
2132
2133 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002134 * Send the dialer code if called from the current default dialer or the caller has
2135 * carrier privilege.
2136 * @param inputCode The dialer code to send
2137 */
2138 @Override
2139 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2140 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2141 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2142 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2143 enforceCarrierPrivilege(getDefaultSubscription());
2144 }
2145 mPhone.sendDialerSpecialCode(inputCode);
2146 }
2147
2148 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002149 * Returns the data network type.
2150 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002151 *
2152 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2153 */
2154 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002155 public int getNetworkType() {
2156 final Phone phone = getPhone(getDefaultSubscription());
2157 if (phone != null) {
2158 return phone.getServiceState().getDataNetworkType();
2159 } else {
2160 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2161 }
Wink Saville36469e72014-06-11 15:17:00 -07002162 }
2163
2164 /**
2165 * Returns the network type for a subId
2166 */
2167 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002168 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2169 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2170 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2171 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002172
Sanket Padawe356d7632015-06-22 14:03:32 -07002173 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002174 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002175 return phone.getServiceState().getDataNetworkType();
2176 } else {
2177 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2178 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179 }
2180
2181 /**
2182 * Returns the data network type
2183 */
2184 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002185 public int getDataNetworkType(String callingPackage) {
2186 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002187 }
2188
2189 /**
2190 * Returns the data network type for a subId
2191 */
2192 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002193 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2194 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2195 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2196 }
2197
Sanket Padawe356d7632015-06-22 14:03:32 -07002198 final Phone phone = getPhone(subId);
2199 if (phone != null) {
2200 return phone.getServiceState().getDataNetworkType();
2201 } else {
2202 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2203 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002204 }
2205
2206 /**
Wink Saville36469e72014-06-11 15:17:00 -07002207 * Returns the Voice network type for a subId
2208 */
2209 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002210 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2211 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2212 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2213 }
2214
Sanket Padawe356d7632015-06-22 14:03:32 -07002215 final Phone phone = getPhone(subId);
2216 if (phone != null) {
2217 return phone.getServiceState().getVoiceNetworkType();
2218 } else {
2219 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2220 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002221 }
2222
2223 /**
2224 * @return true if a ICC card is present
2225 */
2226 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002227 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002228 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2229 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002230 }
2231
2232 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002233 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002234 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002235 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002236 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2237 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002238 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002239 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002240 } else {
2241 return false;
2242 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002243 }
2244
2245 /**
2246 * Return if the current radio is LTE on CDMA. This
2247 * is a tri-state return value as for a period of time
2248 * the mode may be unknown.
2249 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002250 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002251 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002252 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002253 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002254 @Override
2255 public int getLteOnCdmaMode(String callingPackage) {
2256 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002257 }
2258
Sanket Padawe356d7632015-06-22 14:03:32 -07002259 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002260 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2261 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2262 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2263 }
2264
Sanket Padawe356d7632015-06-22 14:03:32 -07002265 final Phone phone = getPhone(subId);
2266 if (phone == null) {
2267 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2268 } else {
2269 return phone.getLteOnCdmaMode();
2270 }
Wink Saville36469e72014-06-11 15:17:00 -07002271 }
2272
2273 public void setPhone(Phone phone) {
2274 mPhone = phone;
2275 }
2276
2277 /**
2278 * {@hide}
2279 * Returns Default subId, 0 in the case of single standby.
2280 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002281 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002282 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002283 }
2284
Shishir Agrawala9f32182016-04-12 12:00:16 -07002285 private int getSlotForDefaultSubscription() {
2286 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2287 }
2288
Wink Savilleb564aae2014-10-23 10:18:09 -07002289 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002290 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002291 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002292
2293 /**
2294 * @see android.telephony.TelephonyManager.WifiCallingChoices
2295 */
2296 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002297 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2298 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002299 }
2300
2301 /**
2302 * @see android.telephony.TelephonyManager.WifiCallingChoices
2303 */
2304 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002305 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2306 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2307 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002308 }
2309
Sailesh Nepald1e68152013-12-12 19:08:02 -08002310 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002311 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002312 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002313 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002314
Shishir Agrawal566b7612013-10-28 14:41:00 -07002315 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002316 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002317 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002318
Ajay Nambid7454d32015-12-03 13:50:00 -08002319 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002320 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002321 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002322 if (DBG) log("iccOpenLogicalChannel: " + response);
2323 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002324 }
2325
2326 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002327 public boolean iccCloseLogicalChannel(int subId, int channel) {
2328 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002329
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002330 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002331 if (channel < 0) {
2332 return false;
2333 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002334 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002335 if (DBG) log("iccCloseLogicalChannel: " + success);
2336 return success;
2337 }
2338
2339 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002340 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002341 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002342 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002343
2344 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002345 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2346 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002347 " data=" + data);
2348 }
2349
2350 if (channel < 0) {
2351 return "";
2352 }
2353
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002354 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002355 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002356 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2357
Shishir Agrawal566b7612013-10-28 14:41:00 -07002358 // Append the returned status code to the end of the response payload.
2359 String s = Integer.toHexString(
2360 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002361 if (response.payload != null) {
2362 s = IccUtils.bytesToHexString(response.payload) + s;
2363 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002364 return s;
2365 }
Jake Hambye994d462014-02-03 13:10:13 -08002366
Evan Charltonc66da362014-05-16 14:06:40 -07002367 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002368 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002369 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002370 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002371
2372 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002373 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2374 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002375 }
2376
2377 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002378 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002379 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2380
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002381 // Append the returned status code to the end of the response payload.
2382 String s = Integer.toHexString(
2383 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002384 if (response.payload != null) {
2385 s = IccUtils.bytesToHexString(response.payload) + s;
2386 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002387 return s;
2388 }
2389
2390 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002391 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002392 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002393 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002394
2395 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002397 p1 + " " + p2 + " " + p3 + ":" + filePath);
2398 }
2399
2400 IccIoResult response =
2401 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002402 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2403 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002404
2405 if (DBG) {
2406 log("Exchange SIM_IO [R]" + response);
2407 }
2408
2409 byte[] result = null;
2410 int length = 2;
2411 if (response.payload != null) {
2412 length = 2 + response.payload.length;
2413 result = new byte[length];
2414 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2415 } else {
2416 result = new byte[length];
2417 }
2418
2419 result[length - 1] = (byte) response.sw2;
2420 result[length - 2] = (byte) response.sw1;
2421 return result;
2422 }
2423
Nathan Haroldb3014052017-01-25 15:57:32 -08002424 /**
2425 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2426 * on a particular subscription
2427 */
2428 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002429 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2430 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002431 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2432 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2433 return null;
2434 }
2435 Object response = sendRequest(
2436 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2437 if (response instanceof String[]) {
2438 return (String[]) response;
2439 }
2440 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2441 return null;
2442 }
2443
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002444 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002445 public String sendEnvelopeWithStatus(int subId, String content) {
2446 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002447
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002448 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002449 if (response.payload == null) {
2450 return "";
2451 }
2452
2453 // Append the returned status code to the end of the response payload.
2454 String s = Integer.toHexString(
2455 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2456 s = IccUtils.bytesToHexString(response.payload) + s;
2457 return s;
2458 }
2459
Jake Hambye994d462014-02-03 13:10:13 -08002460 /**
2461 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2462 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2463 *
2464 * @param itemID the ID of the item to read
2465 * @return the NV item as a String, or null on error.
2466 */
2467 @Override
2468 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002469 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002470 if (DBG) log("nvReadItem: item " + itemID);
2471 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2472 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2473 return value;
2474 }
2475
2476 /**
2477 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2478 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2479 *
2480 * @param itemID the ID of the item to read
2481 * @param itemValue the value to write, as a String
2482 * @return true on success; false on any failure
2483 */
2484 @Override
2485 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002486 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002487 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2488 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2489 new Pair<Integer, String>(itemID, itemValue));
2490 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2491 return success;
2492 }
2493
2494 /**
2495 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2496 * Used for device configuration by some CDMA operators.
2497 *
2498 * @param preferredRoamingList byte array containing the new PRL
2499 * @return true on success; false on any failure
2500 */
2501 @Override
2502 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002503 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002504 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2505 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2506 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2507 return success;
2508 }
2509
2510 /**
2511 * Perform the specified type of NV config reset.
2512 * Used for device configuration by some CDMA operators.
2513 *
2514 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2515 * @return true on success; false on any failure
2516 */
2517 @Override
2518 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002519 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002520 if (DBG) log("nvResetConfig: type " + resetType);
2521 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2522 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2523 return success;
2524 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002525
2526 /**
Wink Saville36469e72014-06-11 15:17:00 -07002527 * {@hide}
2528 * Returns Default sim, 0 in the case of single standby.
2529 */
2530 public int getDefaultSim() {
2531 //TODO Need to get it from Telephony Devcontroller
2532 return 0;
2533 }
2534
Svet Ganovb320e182015-04-16 12:30:10 -07002535 public String[] getPcscfAddress(String apnType, String callingPackage) {
2536 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2537 return new String[0];
2538 }
2539
2540
ram87fca6f2014-07-18 18:58:44 +05302541 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002542 }
2543
Brad Ebinger51f743a2017-01-23 13:50:20 -08002544 /**
2545 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2546 * feature or {@link null} if the service is not available. If an ImsServiceController is
2547 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2548 * feature updates.
2549 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002550 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002551 IImsServiceFeatureListener callback) {
2552 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002553 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002554 callback);
2555 }
2556
Wink Saville36469e72014-06-11 15:17:00 -07002557 public void setImsRegistrationState(boolean registered) {
2558 enforceModifyPermission();
2559 mPhone.setImsRegistrationState(registered);
2560 }
2561
2562 /**
Stuart Scott54788802015-03-30 13:18:01 -07002563 * Set the network selection mode to automatic.
2564 *
2565 */
2566 @Override
2567 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002568 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002569 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2570 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2571 }
2572
2573 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002574 * Set the network selection mode to manual with the selected carrier.
2575 */
2576 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002577 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2578 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002579 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002580 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002581 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2582 persistSelection);
2583 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002584 }
2585
2586 /**
2587 * Scans for available networks.
2588 */
2589 @Override
2590 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002591 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002592 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2593 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2594 CMD_PERFORM_NETWORK_SCAN, null, subId);
2595 return result;
2596 }
2597
2598 /**
yinxub1bed742017-04-17 11:45:04 -07002599 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07002600 *
yinxub1bed742017-04-17 11:45:04 -07002601 * @param subId id of the subscription
2602 * @param request contains the radio access networks with bands/channels to scan
2603 * @param messenger callback messenger for scan results or errors
2604 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07002605 * @return the id of the requested scan which can be used to stop the scan.
2606 */
2607 @Override
2608 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2609 IBinder binder) {
yinxub1bed742017-04-17 11:45:04 -07002610 enforceModifyPermissionOrCarrierPrivilege(subId);
2611 return mNetworkScanRequestTracker.startNetworkScan(
2612 request, messenger, binder, getPhone(subId));
yinxu504e1392017-04-12 16:03:22 -07002613 }
2614
2615 /**
2616 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07002617 *
2618 * @param subId id of the subscription
2619 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07002620 */
2621 @Override
2622 public void stopNetworkScan(int subId, int scanId) {
yinxub1bed742017-04-17 11:45:04 -07002623 enforceModifyPermissionOrCarrierPrivilege(subId);
2624 mNetworkScanRequestTracker.stopNetworkScan(scanId);
yinxu504e1392017-04-12 16:03:22 -07002625 }
2626
2627 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002628 * Get the calculated preferred network type.
2629 * Used for debugging incorrect network type.
2630 *
2631 * @return the preferred network type, defined in RILConstants.java.
2632 */
2633 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002634 public int getCalculatedPreferredNetworkType(String callingPackage) {
2635 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2636 return RILConstants.PREFERRED_NETWORK_MODE;
2637 }
2638
Amit Mahajan43330e02014-11-18 11:54:45 -08002639 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002640 }
2641
2642 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002643 * Get the preferred network type.
2644 * Used for device configuration by some CDMA operators.
2645 *
2646 * @return the preferred network type, defined in RILConstants.java.
2647 */
2648 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002649 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002650 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002651 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002652 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002653 int networkType = (result != null ? result[0] : -1);
2654 if (DBG) log("getPreferredNetworkType: " + networkType);
2655 return networkType;
2656 }
2657
2658 /**
2659 * Set the preferred network type.
2660 * Used for device configuration by some CDMA operators.
2661 *
2662 * @param networkType the preferred network type, defined in RILConstants.java.
2663 * @return true on success; false on any failure.
2664 */
2665 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002666 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002667 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002668 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2669 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002670 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002671 if (success) {
2672 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002673 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002674 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002675 return success;
2676 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002677
2678 /**
Junda Liu475951f2014-11-07 16:45:03 -08002679 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2680 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2681 * tethering.
2682 *
2683 * @return 0: Not required. 1: required. 2: Not set.
2684 * @hide
2685 */
2686 @Override
2687 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002688 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002689 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2690 Settings.Global.TETHER_DUN_REQUIRED, 2);
2691 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2692 // config_tether_apndata.
2693 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2694 dunRequired = 1;
2695 }
2696 return dunRequired;
2697 }
2698
2699 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002700 * Set mobile data enabled
2701 * Used by the user through settings etc to turn on/off mobile data
2702 *
2703 * @param enable {@code true} turn turn data on, else {@code false}
2704 */
2705 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002706 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002707 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002708 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002709 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002710 Phone phone = PhoneFactory.getPhone(phoneId);
2711 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002712 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002713 phone.setDataEnabled(enable);
2714 } else {
2715 loge("setDataEnabled: no phone for subId=" + subId);
2716 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002717 }
2718
2719 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002720 * Get whether mobile data is enabled.
2721 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002722 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002723 *
2724 * @return {@code true} if data is enabled else {@code false}
2725 */
2726 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002727 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002728 try {
2729 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2730 null);
2731 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002732 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002733 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002734 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002735 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002736 Phone phone = PhoneFactory.getPhone(phoneId);
2737 if (phone != null) {
2738 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002739 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002740 return retVal;
2741 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002742 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002743 return false;
2744 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002745 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002746
2747 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002748 public int getCarrierPrivilegeStatus(int subId) {
2749 final Phone phone = getPhone(subId);
2750 if (phone == null) {
2751 loge("getCarrierPrivilegeStatus: Invalid subId");
2752 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2753 }
2754 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002755 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002756 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002757 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2758 }
2759 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002760 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002761 }
Junda Liu29340342014-07-10 15:23:27 -07002762
2763 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002764 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002765 if (TextUtils.isEmpty(pkgName))
2766 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002767 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002768 if (card == null) {
2769 loge("checkCarrierPrivilegesForPackage: No UICC");
2770 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2771 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002772 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2773 }
2774
2775 @Override
2776 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002777 if (TextUtils.isEmpty(pkgName))
2778 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002779 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2780 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2781 UiccCard card = UiccController.getInstance().getUiccCard(i);
2782 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002783 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002784 continue;
2785 }
2786
2787 result = card.getCarrierPrivilegeStatus(
2788 mPhone.getContext().getPackageManager(), pkgName);
2789 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2790 break;
2791 }
2792 }
2793
2794 return result;
Junda Liu29340342014-07-10 15:23:27 -07002795 }
Derek Tan89e89d42014-07-08 17:00:10 -07002796
2797 @Override
Junda Liue64de782015-04-16 17:19:16 -07002798 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2799 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2800 loge("phoneId " + phoneId + " is not valid.");
2801 return null;
2802 }
2803 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002804 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002805 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002806 return null ;
2807 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002808 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002809 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002810 }
2811
Amith Yamasani6e118872016-02-19 12:53:51 -08002812 @Override
2813 public List<String> getPackagesWithCarrierPrivileges() {
2814 PackageManager pm = mPhone.getContext().getPackageManager();
2815 List<String> privilegedPackages = new ArrayList<>();
2816 List<PackageInfo> packages = null;
2817 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2818 UiccCard card = UiccController.getInstance().getUiccCard(i);
2819 if (card == null) {
2820 // No UICC in that slot.
2821 continue;
2822 }
2823 if (card.hasCarrierPrivilegeRules()) {
2824 if (packages == null) {
2825 // Only check packages in user 0 for now
2826 packages = pm.getInstalledPackagesAsUser(
2827 PackageManager.MATCH_DISABLED_COMPONENTS
2828 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2829 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2830 }
2831 for (int p = packages.size() - 1; p >= 0; p--) {
2832 PackageInfo pkgInfo = packages.get(p);
2833 if (pkgInfo != null && pkgInfo.packageName != null
2834 && card.getCarrierPrivilegeStatus(pkgInfo)
2835 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2836 privilegedPackages.add(pkgInfo.packageName);
2837 }
2838 }
2839 }
2840 }
2841 return privilegedPackages;
2842 }
2843
Wink Savilleb564aae2014-10-23 10:18:09 -07002844 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002845 final Phone phone = getPhone(subId);
2846 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002847 if (card == null) {
2848 loge("getIccId: No UICC");
2849 return null;
2850 }
2851 String iccId = card.getIccId();
2852 if (TextUtils.isEmpty(iccId)) {
2853 loge("getIccId: ICC ID is null or empty.");
2854 return null;
2855 }
2856 return iccId;
2857 }
2858
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002859 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002860 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2861 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002862 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002863
Jeff Sharkey85190e62014-12-05 09:40:12 -08002864 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002865 final Phone phone = getPhone(subId);
2866 if (phone == null) {
2867 return false;
2868 }
2869 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002870
2871 if (DBG_MERGE) {
2872 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2873 + subscriberId + " to " + number);
2874 }
2875
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002876 if (TextUtils.isEmpty(iccId)) {
2877 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002878 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002879
Jeff Sharkey85190e62014-12-05 09:40:12 -08002880 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2881
2882 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002883 if (alphaTag == null) {
2884 editor.remove(alphaTagPrefKey);
2885 } else {
2886 editor.putString(alphaTagPrefKey, alphaTag);
2887 }
2888
Jeff Sharkey85190e62014-12-05 09:40:12 -08002889 // Record both the line number and IMSI for this ICCID, since we need to
2890 // track all merged IMSIs based on line number
2891 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2892 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002893 if (number == null) {
2894 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002895 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002896 } else {
2897 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002898 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002899 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002900
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002901 editor.commit();
2902 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002903 }
2904
2905 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002906 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002907 // This is open to apps with WRITE_SMS.
2908 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002909 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002910 return null;
2911 }
Derek Tan97ebb422014-09-05 16:55:38 -07002912
2913 String iccId = getIccId(subId);
2914 if (iccId != null) {
2915 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002916 if (DBG_MERGE) {
2917 log("getLine1NumberForDisplay returning " +
2918 mTelephonySharedPreferences.getString(numberPrefKey, null));
2919 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002920 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002921 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002922 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002923 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002924 }
2925
2926 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002927 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2928 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2929 return null;
2930 }
Derek Tan97ebb422014-09-05 16:55:38 -07002931
2932 String iccId = getIccId(subId);
2933 if (iccId != null) {
2934 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002935 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002936 }
Derek Tan97ebb422014-09-05 16:55:38 -07002937 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002938 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002939
2940 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002941 public String[] getMergedSubscriberIds(String callingPackage) {
2942 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2943 return null;
2944 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002945 final Context context = mPhone.getContext();
2946 final TelephonyManager tele = TelephonyManager.from(context);
2947 final SubscriptionManager sub = SubscriptionManager.from(context);
2948
2949 // Figure out what subscribers are currently active
2950 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002951 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2952 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2953 final long identity = Binder.clearCallingIdentity();
2954 try {
2955 final int[] subIds = sub.getActiveSubscriptionIdList();
2956 for (int subId : subIds) {
2957 activeSubscriberIds.add(tele.getSubscriberId(subId));
2958 }
2959 } finally {
2960 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002961 }
2962
2963 // First pass, find a number override for an active subscriber
2964 String mergeNumber = null;
2965 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2966 for (String key : prefs.keySet()) {
2967 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2968 final String subscriberId = (String) prefs.get(key);
2969 if (activeSubscriberIds.contains(subscriberId)) {
2970 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2971 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2972 mergeNumber = (String) prefs.get(numberKey);
2973 if (DBG_MERGE) {
2974 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2975 + " for active subscriber " + subscriberId);
2976 }
2977 if (!TextUtils.isEmpty(mergeNumber)) {
2978 break;
2979 }
2980 }
2981 }
2982 }
2983
2984 // Shortcut when no active merged subscribers
2985 if (TextUtils.isEmpty(mergeNumber)) {
2986 return null;
2987 }
2988
2989 // Second pass, find all subscribers under that line override
2990 final ArraySet<String> result = new ArraySet<>();
2991 for (String key : prefs.keySet()) {
2992 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2993 final String number = (String) prefs.get(key);
2994 if (mergeNumber.equals(number)) {
2995 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2996 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2997 final String subscriberId = (String) prefs.get(subscriberKey);
2998 if (!TextUtils.isEmpty(subscriberId)) {
2999 result.add(subscriberId);
3000 }
3001 }
3002 }
3003 }
3004
3005 final String[] resultArray = result.toArray(new String[result.size()]);
3006 Arrays.sort(resultArray);
3007 if (DBG_MERGE) {
3008 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3009 }
3010 return resultArray;
3011 }
3012
3013 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003014 public boolean setOperatorBrandOverride(int subId, String brand) {
3015 enforceCarrierPrivilege(subId);
3016 final Phone phone = getPhone(subId);
3017 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003018 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003019
3020 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003021 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003022 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3023 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003024 enforceCarrierPrivilege(subId);
3025 final Phone phone = getPhone(subId);
3026 if (phone == null) {
3027 return false;
3028 }
3029 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003030 cdmaNonRoamingList);
3031 }
3032
3033 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003034 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003035 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3036 enforceModifyPermission();
3037
3038 int returnValue = 0;
3039 try {
3040 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3041 if(result.exception == null) {
3042 if (result.result != null) {
3043 byte[] responseData = (byte[])(result.result);
3044 if(responseData.length > oemResp.length) {
3045 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3046 responseData.length + "bytes. Buffer Size is " +
3047 oemResp.length + "bytes.");
3048 }
3049 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3050 returnValue = responseData.length;
3051 }
3052 } else {
3053 CommandException ex = (CommandException) result.exception;
3054 returnValue = ex.getCommandError().ordinal();
3055 if(returnValue > 0) returnValue *= -1;
3056 }
3057 } catch (RuntimeException e) {
3058 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3059 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3060 if(returnValue > 0) returnValue *= -1;
3061 }
3062
3063 return returnValue;
3064 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003065
3066 @Override
3067 public void setRadioCapability(RadioAccessFamily[] rafs) {
3068 try {
3069 ProxyController.getInstance().setRadioCapability(rafs);
3070 } catch (RuntimeException e) {
3071 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3072 }
3073 }
3074
3075 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003076 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3077 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3078 return RadioAccessFamily.RAF_UNKNOWN;
3079 }
3080
Wink Saville5d475dd2014-10-17 15:00:58 -07003081 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3082 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003083
3084 @Override
3085 public void enableVideoCalling(boolean enable) {
3086 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003087 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003088 }
3089
3090 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003091 public boolean isVideoCallingEnabled(String callingPackage) {
3092 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3093 return false;
3094 }
3095
Andrew Lee77527ac2014-10-21 16:57:39 -07003096 // Check the user preference and the system-level IMS setting. Even if the user has
3097 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3098 // In the long run, we may instead need to check if there exists a connection service
3099 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003100 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3101 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003102 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003103 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003104
Andrew Leea1239f22015-03-02 17:44:07 -08003105 @Override
3106 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003107 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003108 }
3109
3110 @Override
3111 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003112 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003113 }
3114
Andrew Lee9431b832015-03-09 18:46:45 -07003115 @Override
3116 public boolean isTtyModeSupported() {
3117 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3118 TelephonyManager telephonyManager =
3119 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003120 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003121 }
3122
3123 @Override
3124 public boolean isHearingAidCompatibilitySupported() {
3125 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3126 }
3127
Sanket Padawe7310cc72015-01-14 09:53:20 -08003128 /**
3129 * Returns the unique device ID of phone, for example, the IMEI for
3130 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3131 *
3132 * <p>Requires Permission:
3133 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3134 */
3135 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003136 public String getDeviceId(String callingPackage) {
3137 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3138 return null;
3139 }
3140
Sanket Padawe7310cc72015-01-14 09:53:20 -08003141 final Phone phone = PhoneFactory.getPhone(0);
3142 if (phone != null) {
3143 return phone.getDeviceId();
3144 } else {
3145 return null;
3146 }
3147 }
3148
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003149 /*
3150 * {@hide}
3151 * Returns the IMS Registration Status
3152 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003153 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003154 public boolean isImsRegistered() {
3155 return mPhone.isImsRegistered();
3156 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003157
3158 @Override
3159 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3160 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3161 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003162
Nathan Haroldc55097a2015-03-11 18:14:50 -07003163 /*
3164 * {@hide}
3165 * Returns the IMS Registration Status
3166 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003167 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003168 return mPhone.isWifiCallingEnabled();
3169 }
3170
3171 /*
3172 * {@hide}
3173 * Returns the IMS Registration Status
3174 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003175 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003176 return mPhone.isVolteEnabled();
3177 }
Svet Ganovb320e182015-04-16 12:30:10 -07003178
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003179 /*
3180 * {@hide} Returns the IMS Registration Status
3181 */
3182 public boolean isVideoTelephonyAvailable() {
3183 return mPhone.isVideoEnabled();
3184 }
3185
Svet Ganovb320e182015-04-16 12:30:10 -07003186 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003187 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003188 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003189 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3190
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003191 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003192 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003193 } catch (SecurityException e) {
3194 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3195 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003196 }
Svet Ganovb320e182015-04-16 12:30:10 -07003197
3198 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3199 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3200 return false;
3201 }
3202
3203 return true;
3204 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003205
Makoto Onukifee69342015-06-29 14:44:50 -07003206 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003207 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003208 */
3209 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003210 // Default SMS app can always read it.
3211 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3212 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3213 return true;
3214 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003215
Makoto Onukie4072d12015-08-03 15:12:23 -07003216 try {
3217 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003218 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003219 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003220 // Can be read with READ_SMS too.
3221 try {
3222 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3223 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3224 if (opCode != AppOpsManager.OP_NONE) {
3225 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3226 == AppOpsManager.MODE_ALLOWED;
3227 } else {
3228 return true;
3229 }
3230 } catch (SecurityException readSmsSecurityException) {
3231 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003232 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003233 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003234 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003235 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003236 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003237 if (opCode != AppOpsManager.OP_NONE) {
3238 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3239 == AppOpsManager.MODE_ALLOWED;
3240 } else {
3241 return true;
3242 }
3243 } catch (SecurityException readPhoneNumberSecurityException) {
3244 }
3245
3246 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3247 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3248 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003249 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003250 }
3251
Stuart Scott8eef64f2015-04-08 15:13:54 -07003252 @Override
3253 public void factoryReset(int subId) {
3254 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003255 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3256 return;
3257 }
3258
Svet Ganovcc087f82015-05-12 20:35:54 -07003259 final long identity = Binder.clearCallingIdentity();
3260 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003261 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3262 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003263 // Enable data
3264 setDataEnabled(subId, true);
3265 // Set network selection mode to automatic
3266 setNetworkSelectionModeAutomatic(subId);
3267 // Set preferred mobile network type to the best available
3268 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3269 // Turn off roaming
Malcolm Chenf6b97f42017-04-19 16:03:24 -07003270 mPhone.setDataRoamingEnabled(false);
Svet Ganovcc087f82015-05-12 20:35:54 -07003271 }
3272 } finally {
3273 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003274 }
3275 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003276
3277 @Override
3278 public String getLocaleFromDefaultSim() {
3279 // We query all subscriptions instead of just the active ones, because
3280 // this might be called early on in the provisioning flow when the
3281 // subscriptions potentially aren't active yet.
3282 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3283 if (slist == null || slist.isEmpty()) {
3284 return null;
3285 }
3286
3287 // This function may be called very early, say, from the setup wizard, at
3288 // which point we won't have a default subscription set. If that's the case
3289 // we just choose the first, which will be valid in "most cases".
3290 final int defaultSubId = getDefaultSubscription();
3291 SubscriptionInfo info = null;
3292 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3293 info = slist.get(0);
3294 } else {
3295 for (SubscriptionInfo item : slist) {
3296 if (item.getSubscriptionId() == defaultSubId) {
3297 info = item;
3298 break;
3299 }
3300 }
3301
3302 if (info == null) {
3303 return null;
3304 }
3305 }
3306
3307 // Try and fetch the locale from the carrier properties or from the SIM language
3308 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003309 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003310 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003311 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003312 if (defaultPhone != null) {
3313 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3314 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003315 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003316 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3317 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003318 } else {
3319 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003320 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003321 }
3322 }
3323
Narayan Kamath011676f2015-07-29 12:04:08 +01003324 // The SIM language preferences only store a language (e.g. fr = French), not an
3325 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3326 // the SIM and carrier preferences does not include a country we add the country
3327 // determined from the SIM MCC to provide an exact locale.
3328 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003329 if (mccLocale != null) {
3330 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3331 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003332 }
3333
Tony Hill183b2de2015-06-24 14:53:58 +01003334 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003335 return null;
3336 }
3337
3338 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3339 final long identity = Binder.clearCallingIdentity();
3340 try {
3341 return mSubscriptionController.getAllSubInfoList(
3342 mPhone.getContext().getOpPackageName());
3343 } finally {
3344 Binder.restoreCallingIdentity(identity);
3345 }
3346 }
3347
3348 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3349 final long identity = Binder.clearCallingIdentity();
3350 try {
3351 return mSubscriptionController.getActiveSubscriptionInfoList(
3352 mPhone.getContext().getOpPackageName());
3353 } finally {
3354 Binder.restoreCallingIdentity(identity);
3355 }
3356 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003357
3358 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003359 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3360 * representing the state of the modem.
3361 *
3362 * NOTE: This clears the modem state, so there should only every be one caller.
3363 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003364 */
3365 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003366 public void requestModemActivityInfo(ResultReceiver result) {
3367 enforceModifyPermission();
3368
3369 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3370 Bundle bundle = new Bundle();
3371 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3372 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003373 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003374
3375 /**
3376 * {@hide}
3377 * Returns the service state information on specified subscription.
3378 */
3379 @Override
3380 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3381
3382 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3383 return null;
3384 }
3385
3386 final Phone phone = getPhone(subId);
3387 if (phone == null) {
3388 return null;
3389 }
3390
3391 return phone.getServiceState();
3392 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003393
3394 /**
3395 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3396 *
3397 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3398 * voicemail ringtone.
3399 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3400 * PhoneAccount.
3401 */
3402 @Override
3403 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003404 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003405 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003406 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003407 }
3408
fionaxu7ed723d2017-05-30 18:58:54 -07003409 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
Nancy Chen31f9ba12016-01-06 11:42:12 -08003410 }
3411
3412 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003413 * Sets the per-account voicemail ringtone.
3414 *
3415 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3416 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3417 *
3418 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3419 * voicemail ringtone.
3420 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3421 * PhoneAccount.
3422 */
3423 @Override
3424 public void setVoicemailRingtoneUri(String callingPackage,
3425 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3426 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3427 if (!TextUtils.equals(callingPackage,
3428 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3429 enforceModifyPermissionOrCarrierPrivilege(
3430 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3431 }
3432 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3433 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003434 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003435 }
fionaxu7ed723d2017-05-30 18:58:54 -07003436 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003437 }
3438
3439 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003440 * Returns whether vibration is set for voicemail notification in Phone settings.
3441 *
3442 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3443 * voicemail vibration setting.
3444 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3445 */
3446 @Override
3447 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003448 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003449 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003450 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003451 }
3452
fionaxu7ed723d2017-05-30 18:58:54 -07003453 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
Nancy Chen31f9ba12016-01-06 11:42:12 -08003454 }
3455
Youhan Wange64578a2016-05-02 15:32:42 -07003456 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003457 * Sets the per-account voicemail vibration.
3458 *
3459 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3460 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3461 *
3462 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3463 * voicemail vibration setting.
3464 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3465 * specific PhoneAccount.
3466 */
3467 @Override
3468 public void setVoicemailVibrationEnabled(String callingPackage,
3469 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3470 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3471 if (!TextUtils.equals(callingPackage,
3472 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3473 enforceModifyPermissionOrCarrierPrivilege(
3474 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3475 }
3476
3477 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3478 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003479 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003480 }
fionaxu7ed723d2017-05-30 18:58:54 -07003481 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003482 }
3483
3484 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003485 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3486 *
3487 * @throws SecurityException if the caller does not have the required permission
3488 */
3489 private void enforceReadPrivilegedPermission() {
3490 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3491 null);
3492 }
3493
3494 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003495 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3496 * permission.
3497 *
3498 * @throws SecurityException if the caller does not have the required permission
3499 */
3500 private void enforceSendSmsPermission() {
3501 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3502 }
3503
3504 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003505 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003506 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003507 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003508 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003509 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003510 ComponentName componentName =
3511 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3512 if(componentName == null) {
3513 throw new SecurityException("Caller not current active visual voicemail package[null]");
3514 }
3515 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003516 if (!callingPackage.equals(vvmPackage)) {
3517 throw new SecurityException("Caller not current active visual voicemail package[" +
3518 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003519 }
3520 }
3521
3522 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003523 * Return the application ID for the app type.
3524 *
3525 * @param subId the subscription ID that this request applies to.
3526 * @param appType the uicc app type.
3527 * @return Application ID for specificied app type, or null if no uicc.
3528 */
3529 @Override
3530 public String getAidForAppType(int subId, int appType) {
3531 enforceReadPrivilegedPermission();
3532 Phone phone = getPhone(subId);
3533 if (phone == null) {
3534 return null;
3535 }
3536 String aid = null;
3537 try {
3538 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3539 .getApplicationByType(appType).getAid();
3540 } catch (Exception e) {
3541 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3542 }
3543 return aid;
3544 }
3545
Youhan Wang4001d252016-05-11 10:29:41 -07003546 /**
3547 * Return the Electronic Serial Number.
3548 *
3549 * @param subId the subscription ID that this request applies to.
3550 * @return ESN or null if error.
3551 */
3552 @Override
3553 public String getEsn(int subId) {
3554 enforceReadPrivilegedPermission();
3555 Phone phone = getPhone(subId);
3556 if (phone == null) {
3557 return null;
3558 }
3559 String esn = null;
3560 try {
3561 esn = phone.getEsn();
3562 } catch (Exception e) {
3563 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3564 }
3565 return esn;
3566 }
3567
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003568 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003569 * Return the Preferred Roaming List Version.
3570 *
3571 * @param subId the subscription ID that this request applies to.
3572 * @return PRLVersion or null if error.
3573 */
3574 @Override
3575 public String getCdmaPrlVersion(int subId) {
3576 enforceReadPrivilegedPermission();
3577 Phone phone = getPhone(subId);
3578 if (phone == null) {
3579 return null;
3580 }
3581 String cdmaPrlVersion = null;
3582 try {
3583 cdmaPrlVersion = phone.getCdmaPrlVersion();
3584 } catch (Exception e) {
3585 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3586 }
3587 return cdmaPrlVersion;
3588 }
3589
3590 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003591 * Get snapshot of Telephony histograms
3592 * @return List of Telephony histograms
3593 * @hide
3594 */
3595 @Override
3596 public List<TelephonyHistogram> getTelephonyHistograms() {
3597 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3598 return RIL.getTelephonyRILTimingHistograms();
3599 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003600
3601 /**
3602 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003603 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003604 * Require system privileges. In the future we may add this to carrier APIs.
3605 *
3606 * @return The number of carriers set successfully, should match length of carriers
3607 */
3608 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003609 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003610 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003611
Meng Wang9b7c4e92017-02-17 11:41:27 -08003612 if (carriers == null) {
3613 throw new NullPointerException("carriers cannot be null");
3614 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003615
3616 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003617 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3618 return retVal[0];
3619 }
3620
3621 /**
3622 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003623 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003624 * Require system privileges. In the future we may add this to carrier APIs.
3625 *
3626 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3627 * means all carriers are allowed.
3628 */
3629 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003630 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003631 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003632 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003633 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3634 }
3635
fionaxu59545b42016-05-25 15:53:37 -07003636 /**
3637 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3638 * @param subId the subscription ID that this action applies to.
3639 * @param enabled control enable or disable metered apns.
3640 * {@hide}
3641 */
3642 @Override
3643 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3644 enforceModifyPermission();
3645 final Phone phone = getPhone(subId);
3646 if (phone == null) {
3647 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3648 return;
3649 }
3650 try {
3651 phone.carrierActionSetMeteredApnsEnabled(enabled);
3652 } catch (Exception e) {
3653 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3654 }
3655 }
3656
3657 /**
3658 * Action set from carrier signalling broadcast receivers to enable/disable radio
3659 * @param subId the subscription ID that this action applies to.
3660 * @param enabled control enable or disable radio.
3661 * {@hide}
3662 */
3663 @Override
3664 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3665 enforceModifyPermission();
3666 final Phone phone = getPhone(subId);
3667 if (phone == null) {
3668 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3669 return;
3670 }
3671 try {
3672 phone.carrierActionSetRadioEnabled(enabled);
3673 } catch (Exception e) {
3674 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3675 }
3676 }
3677
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003678 /**
3679 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3680 * bug report is being generated.
3681 */
3682 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003683 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003684 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3685 != PackageManager.PERMISSION_GRANTED) {
3686 writer.println("Permission Denial: can't dump Phone from pid="
3687 + Binder.getCallingPid()
3688 + ", uid=" + Binder.getCallingUid()
3689 + "without permission "
3690 + android.Manifest.permission.DUMP);
3691 return;
3692 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003693 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003694 }
Jack Yueb89b242016-06-22 13:27:47 -07003695
3696 /**
Jack Yu84291ec2017-05-26 16:07:50 -07003697 * Get aggregated video call data usage since boot.
3698 *
3699 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
3700 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07003701 * {@hide}
3702 */
3703 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07003704 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07003705 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3706 null);
3707
Jack Yu84291ec2017-05-26 16:07:50 -07003708 // NetworkStatsService keeps tracking the active network interface and identity. It
3709 // records the delta with the corresponding network identity. We just return the total video
3710 // call data usage snapshot since boot.
3711 Phone phone = getPhone(subId);
3712 if (phone != null) {
3713 return phone.getVtDataUsage(perUidStats);
Jack Yueb89b242016-06-22 13:27:47 -07003714 }
Jack Yu84291ec2017-05-26 16:07:50 -07003715 return null;
Jack Yueb89b242016-06-22 13:27:47 -07003716 }
Jack Yu75ab2952016-07-08 14:29:33 -07003717
3718 /**
3719 * Policy control of data connection. Usually used when data limit is passed.
3720 * @param enabled True if enabling the data, otherwise disabling.
3721 * @param subId Subscription index
3722 * {@hide}
3723 */
3724 @Override
3725 public void setPolicyDataEnabled(boolean enabled, int subId) {
3726 enforceModifyPermission();
3727 Phone phone = getPhone(subId);
3728 if (phone != null) {
3729 phone.setPolicyDataEnabled(enabled);
3730 }
3731 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003732
3733 /**
3734 * Get Client request stats
3735 * @return List of Client Request Stats
3736 * @hide
3737 */
3738 @Override
3739 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3740 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3741 return null;
3742 }
3743
3744 Phone phone = getPhone(subId);
3745 if (phone != null) {
3746 return phone.getClientRequestStats();
3747 }
3748
3749 return null;
3750 }
3751
3752 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3753 if (workSource != null) {
3754 return workSource;
3755 }
3756
3757 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3758 workSource = new WorkSource(uid, packageName);
3759 return workSource;
3760 }
Jack Yueb4124c2017-02-16 15:32:43 -08003761
3762 /**
Grace Chen70990072017-03-24 17:21:30 -07003763 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08003764 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003765 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07003766 * @param state State of SIM (power down, power up, pass through)
3767 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
3768 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
3769 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08003770 *
3771 **/
3772 @Override
Grace Chen70990072017-03-24 17:21:30 -07003773 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08003774 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003775 Phone phone = PhoneFactory.getPhone(slotIndex);
3776
Jack Yueb4124c2017-02-16 15:32:43 -08003777 if (phone != null) {
Grace Chen70990072017-03-24 17:21:30 -07003778 phone.setSimPowerState(state);
Jack Yueb4124c2017-02-16 15:32:43 -08003779 }
3780 }
Shuo Qiandd210312017-04-12 22:11:33 +00003781
Tyler Gunn65d45c22017-06-05 11:22:26 -07003782 private boolean isUssdApiAllowed(int subId) {
3783 CarrierConfigManager configManager =
3784 (CarrierConfigManager) mPhone.getContext().getSystemService(
3785 Context.CARRIER_CONFIG_SERVICE);
3786 if (configManager == null) {
3787 return false;
3788 }
3789 PersistableBundle pb = configManager.getConfigForSubId(subId);
3790 if (pb == null) {
3791 return false;
3792 }
3793 return pb.getBoolean(
3794 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
3795 }
3796
Shuo Qiandd210312017-04-12 22:11:33 +00003797 /**
3798 * Check if phone is in emergency callback mode
3799 * @return true if phone is in emergency callback mode
3800 * @param subId sub id
3801 */
3802 public boolean getEmergencyCallbackMode(int subId) {
3803 final Phone phone = getPhone(subId);
3804 if (phone != null) {
3805 return phone.isInEcm();
3806 } else {
3807 return false;
3808 }
3809 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08003810
3811 /**
3812 * Get the current signal strength information for the given subscription.
3813 * Because this information is not updated when the device is in a low power state
3814 * it should not be relied-upon to be current.
3815 * @param subId Subscription index
3816 * @return the most recent cached signal strength info from the modem
3817 */
3818 @Override
3819 public SignalStrength getSignalStrength(int subId) {
3820 Phone p = getPhone(subId);
3821 if (p == null) {
3822 return null;
3823 }
3824
3825 return p.getSignalStrength();
3826 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003827}