blob: 768e868e0ff863a87238983d591350ac02f90fb6 [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
Hall Liud892bec2018-11-30 14:51:45 -080019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20
Ta-wei Yen87c49842016-05-13 21:19:52 -070021import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
22
Ta-wei Yen30a69c82016-12-27 14:52:32 -080023import android.Manifest.permission;
Tyler Gunnf70ed162019-04-03 15:28:53 -070024import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080026import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070027import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070028import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.Context;
30import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070031import android.content.SharedPreferences;
Nathan Harold31d7ff32018-10-15 20:20:30 -070032import android.content.pm.ApplicationInfo;
Derek Tan740e1672017-06-27 14:56:27 -070033import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080034import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070035import android.content.pm.PackageManager;
Jack Yu84291ec2017-05-26 16:07:50 -070036import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.net.Uri;
38import android.os.AsyncResult;
39import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080040import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Bundle;
42import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070043import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.Looper;
45import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070046import android.os.Messenger;
Malcolm Chen6ca97372019-07-01 16:28:21 -070047import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070048import android.os.PersistableBundle;
Brad Ebinger5f64b052017-12-14 14:26:15 -080049import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070050import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.os.ServiceManager;
Brad Ebingerdac2f002018-04-03 15:17:52 -070052import android.os.ShellCallback;
Pengquan Meng85728fb2018-03-12 16:31:21 -070053import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070055import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070056import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070057import android.preference.PreferenceManager;
Naina Nalluri1264a9f2019-09-17 14:10:30 -070058import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080059import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070060import android.provider.Telephony;
Santos Cordon7a1885b2015-02-03 11:15:19 -080061import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080062import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070063import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070064import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080065import android.telephony.CarrierRestrictionRules;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070067import android.telephony.CellInfoGsm;
68import android.telephony.CellInfoWcdma;
Nathan Harold3ff88932018-08-14 10:19:49 -070069import android.telephony.CellLocation;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070070import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070071import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070072import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080073import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070074import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080075import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070076import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080077import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080078import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070079import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080080import android.telephony.RadioAccessSpecifier;
Tyler Gunn65d45c22017-06-05 11:22:26 -070081import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080083import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080084import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080085import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070086import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070087import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080088import android.telephony.TelephonyScanManager;
Jordan Liu5aa07002018-12-18 15:44:48 -080089import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000090import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070091import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070092import android.telephony.VisualVoicemailSmsFilterSettings;
Nathan Harold3ff88932018-08-14 10:19:49 -070093import android.telephony.cdma.CdmaCellLocation;
Jack Yub5d8f642018-11-26 11:20:48 -080094import android.telephony.data.ApnSetting;
Jack Yu41407ee2019-05-13 16:54:09 -070095import android.telephony.data.ApnSetting.ApnType;
Jack Yub5d8f642018-11-26 11:20:48 -080096import android.telephony.emergency.EmergencyNumber;
Nathan Harold3ff88932018-08-14 10:19:49 -070097import android.telephony.gsm.GsmCellLocation;
Brad Ebinger1c8542e2019-01-14 13:43:14 -080098import android.telephony.ims.ProvisioningManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -070099import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800100import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700101import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800102import android.telephony.ims.aidl.IImsMmTelFeature;
103import android.telephony.ims.aidl.IImsRcsFeature;
104import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700105import android.telephony.ims.aidl.IImsRegistrationCallback;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800106import android.telephony.ims.feature.MmTelFeature;
107import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800108import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800110import android.util.ArraySet;
Hall Liuaa4283b2020-05-21 17:09:35 -0700111import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800113import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800114import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800115
Brad Ebinger35c841c2018-10-01 10:40:55 -0700116import com.android.ims.ImsException;
Andrew Lee312e8172014-10-23 17:01:36 -0700117import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800118import com.android.ims.internal.IImsServiceFeatureCallback;
Jordan Liua39e6c12020-03-04 13:59:23 -0800119import com.android.internal.annotations.VisibleForTesting;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700120import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700121import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700122import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800123import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700124import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700125import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800127import com.android.internal.telephony.HalVersion;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700128import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800129import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800131import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700132import com.android.internal.telephony.LocaleTracker;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100133import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -0700134import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700135import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700137import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800138import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700139import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700140import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700141import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700142import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700143import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700144import com.android.internal.telephony.ServiceStateTracker;
Makoto Onukida3bf792018-09-18 16:06:29 -0700145import com.android.internal.telephony.SmsApplication;
146import com.android.internal.telephony.SmsApplication.SmsApplicationData;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700147import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700148import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800149import com.android.internal.telephony.SubscriptionController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800150import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700151import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800152import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700153import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800154import com.android.internal.telephony.ims.ImsResolver;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800155import com.android.internal.telephony.metrics.TelephonyMetrics;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700156import com.android.internal.telephony.uicc.IccIoResult;
157import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800158import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700159import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800160import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700161import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800162import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000163import com.android.internal.telephony.uicc.UiccSlot;
fionaxu7ed723d2017-05-30 18:58:54 -0700164import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800165import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700166import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700167import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800168import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700169import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700170import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800171
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700172import java.io.FileDescriptor;
173import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800175import java.util.Arrays;
Makoto Onukida3bf792018-09-18 16:06:29 -0700176import java.util.Collection;
sqian11b7a0e2018-12-05 18:48:28 -0800177import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800178import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800179import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100180import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800181import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700182import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800183import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184
185/**
186 * Implementation of the ITelephony interface.
187 */
Santos Cordon117fee72014-05-16 17:56:12 -0700188public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 private static final String LOG_TAG = "PhoneInterfaceManager";
190 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
191 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800192 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700193
194 // Message codes used with mMainThreadHandler
195 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700196 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
197 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700198 private static final int CMD_OPEN_CHANNEL = 9;
199 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
200 private static final int CMD_CLOSE_CHANNEL = 11;
201 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800202 private static final int CMD_NV_READ_ITEM = 13;
203 private static final int EVENT_NV_READ_ITEM_DONE = 14;
204 private static final int CMD_NV_WRITE_ITEM = 15;
205 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
206 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
207 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700208 private static final int CMD_RESET_MODEM_CONFIG = 19;
209 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800210 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
211 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
212 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
213 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800214 private static final int CMD_SEND_ENVELOPE = 25;
215 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000216 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
217 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700218 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
219 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
220 private static final int CMD_EXCHANGE_SIM_IO = 31;
221 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800222 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
223 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700224 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
225 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700226 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
227 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700228 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
229 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
230 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
231 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700232 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
233 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
234 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
235 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700236 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800237 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
238 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000239 private static final int CMD_SWITCH_SLOTS = 50;
240 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700241 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
242 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
243 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
244 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
245 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
246 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
247 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
248 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700249 private static final int CMD_GET_ALL_CELL_INFO = 60;
250 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
251 private static final int CMD_GET_CELL_LOCATION = 62;
252 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700253 private static final int CMD_MODEM_REBOOT = 64;
254 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700255 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
256 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800257 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
258 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700259 private static final int CMD_GET_MODEM_STATUS = 70;
260 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
Naina Nalluri1264a9f2019-09-17 14:10:30 -0700261 private static final int CMD_ERASE_MODEM_CONFIG = 72;
262 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 73;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800264 // Parameters of select command.
265 private static final int SELECT_COMMAND = 0xA4;
266 private static final int SELECT_P1 = 0x04;
267 private static final int SELECT_P2 = 0;
268 private static final int SELECT_P3 = 0x10;
269
Pengquan Meng85728fb2018-03-12 16:31:21 -0700270 private static final String DEFAULT_NETWORK_MODE_PROPERTY_NAME = "ro.telephony.default_network";
271 private static final String DEFAULT_DATA_ROAMING_PROPERTY_NAME = "ro.com.android.dataroaming";
272 private static final String DEFAULT_MOBILE_DATA_PROPERTY_NAME = "ro.com.android.mobiledata";
273
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 /** The singleton instance. */
275 private static PhoneInterfaceManager sInstance;
276
Wink Saville3ab207e2014-11-20 13:07:20 -0800277 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800278 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700279 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800280 private AppOpsManager mAppOps;
281 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800282 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800283 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700284 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285
Derek Tan97ebb422014-09-05 16:55:38 -0700286 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
287 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800288 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800289 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700290
Michelecea4cf22018-12-21 15:00:11 -0800291 // String to store multi SIM allowed
292 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
293
Derek Tan740e1672017-06-27 14:56:27 -0700294 // The AID of ISD-R.
295 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
296
yinxub1bed742017-04-17 11:45:04 -0700297 private NetworkScanRequestTracker mNetworkScanRequestTracker;
298
David Kelly5e06a7f2018-03-12 14:10:59 +0000299 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
300 private static final int MANUFACTURER_CODE_LENGTH = 8;
301
Derek Tan89e89d42014-07-08 17:00:10 -0700302 /**
Naina Nalluri1264a9f2019-09-17 14:10:30 -0700303 * Experiment flag to enable erase modem config on reset network, default value is false
304 */
305 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
306 "reset_network_erase_modem_config_enabled";
307
308 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700309 * A request object to use for transmitting data to an ICC.
310 */
311 private static final class IccAPDUArgument {
312 public int channel, cla, command, p1, p2, p3;
313 public String data;
314
315 public IccAPDUArgument(int channel, int cla, int command,
316 int p1, int p2, int p3, String data) {
317 this.channel = channel;
318 this.cla = cla;
319 this.command = command;
320 this.p1 = p1;
321 this.p2 = p2;
322 this.p3 = p3;
323 this.data = data;
324 }
325 }
326
327 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700328 * A request object to use for transmitting data to an ICC.
329 */
330 private static final class ManualNetworkSelectionArgument {
331 public OperatorInfo operatorInfo;
332 public boolean persistSelection;
333
334 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
335 this.operatorInfo = operatorInfo;
336 this.persistSelection = persistSelection;
337 }
338 }
339
340 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
342 * request after sending. The main thread will notify the request when it is complete.
343 */
344 private static final class MainThreadRequest {
345 /** The argument to use for the request */
346 public Object argument;
347 /** The result of the request that is run on the main thread */
348 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800349 // The subscriber id that this request applies to. Defaults to
350 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
351 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352
Nathan Harold92bed182018-10-12 18:16:49 -0700353 // In cases where subId is unavailable, the caller needs to specify the phone.
354 public Phone phone;
355
vagdeviaf9a5b92018-08-15 16:01:53 -0700356 public WorkSource workSource;
357
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 public MainThreadRequest(Object argument) {
359 this.argument = argument;
360 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800361
Nathan Harold92bed182018-10-12 18:16:49 -0700362 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
363 this.argument = argument;
364 if (phone != null) {
365 this.phone = phone;
366 }
367 this.workSource = workSource;
368 }
369
vagdeviaf9a5b92018-08-15 16:01:53 -0700370 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800371 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800372 if (subId != null) {
373 this.subId = subId;
374 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700375 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800376 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700377 }
378
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800379 private static final class IncomingThirdPartyCallArgs {
380 public final ComponentName component;
381 public final String callId;
382 public final String callerDisplayName;
383
384 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
385 String callerDisplayName) {
386 this.component = component;
387 this.callId = callId;
388 this.callerDisplayName = callerDisplayName;
389 }
390 }
391
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 /**
393 * A handler that processes messages on the main thread in the phone process. Since many
394 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
395 * inbound binder threads to the main thread in the phone process. The Binder thread
396 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
397 * on, which will be notified when the operation completes and will contain the result of the
398 * request.
399 *
400 * <p>If a MainThreadRequest object is provided in the msg.obj field,
401 * note that request.result must be set to something non-null for the calling thread to
402 * unblock.
403 */
404 private final class MainThreadHandler extends Handler {
405 @Override
406 public void handleMessage(Message msg) {
407 MainThreadRequest request;
408 Message onCompleted;
409 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800410 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700411 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800412 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700413
414 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700415 case CMD_HANDLE_USSD_REQUEST: {
416 request = (MainThreadRequest) msg.obj;
417 final Phone phone = getPhoneFromRequest(request);
418 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
419 String ussdRequest = ussdObject.first;
420 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700421
Pengquan Menga1bb6272018-09-06 09:59:22 -0700422 if (!isUssdApiAllowed(request.subId)) {
423 // Carrier does not support use of this API, return failure.
424 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
425 UssdResponse response = new UssdResponse(ussdRequest, null);
426 Bundle returnData = new Bundle();
427 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
428 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700429
Pengquan Menga1bb6272018-09-06 09:59:22 -0700430 request.result = true;
431 notifyRequester(request);
432 return;
433 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700434
Pengquan Menga1bb6272018-09-06 09:59:22 -0700435 try {
436 request.result = phone != null
437 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
438 } catch (CallStateException cse) {
439 request.result = false;
440 }
441 // Wake up the requesting thread
442 notifyRequester(request);
443 break;
pkanwar32d516d2016-10-14 19:37:38 -0700444 }
445
Yorke Lee716f67e2015-06-17 15:39:16 -0700446 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700447 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700448 final Phone phone = getPhoneFromRequest(request);
449 request.result = phone != null ?
450 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
451 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700452 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700453 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700454 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700455 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700456
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700457 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700458 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700459 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800460 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700461 if (uiccCard == null) {
462 loge("iccTransmitApduLogicalChannel: No UICC");
463 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700464 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700465 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700466 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
467 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700468 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700469 iccArgument.channel, iccArgument.cla, iccArgument.command,
470 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700472 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700473 break;
474
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700475 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 ar = (AsyncResult) msg.obj;
477 request = (MainThreadRequest) ar.userObj;
478 if (ar.exception == null && ar.result != null) {
479 request.result = ar.result;
480 } else {
481 request.result = new IccIoResult(0x6F, 0, (byte[])null);
482 if (ar.result == null) {
483 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800484 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800486 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700487 } else {
488 loge("iccTransmitApduLogicalChannel: Unknown exception");
489 }
490 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700491 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 break;
493
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700494 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
495 request = (MainThreadRequest) msg.obj;
496 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800497 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700498 if (uiccCard == null) {
499 loge("iccTransmitApduBasicChannel: No UICC");
500 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700501 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700502 } else {
503 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
504 request);
505 uiccCard.iccTransmitApduBasicChannel(
506 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
507 iccArgument.p3, iccArgument.data, onCompleted);
508 }
509 break;
510
511 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
512 ar = (AsyncResult) msg.obj;
513 request = (MainThreadRequest) ar.userObj;
514 if (ar.exception == null && ar.result != null) {
515 request.result = ar.result;
516 } else {
517 request.result = new IccIoResult(0x6F, 0, (byte[])null);
518 if (ar.result == null) {
519 loge("iccTransmitApduBasicChannel: Empty response");
520 } else if (ar.exception instanceof CommandException) {
521 loge("iccTransmitApduBasicChannel: CommandException: " +
522 ar.exception);
523 } else {
524 loge("iccTransmitApduBasicChannel: Unknown exception");
525 }
526 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700527 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700528 break;
529
530 case CMD_EXCHANGE_SIM_IO:
531 request = (MainThreadRequest) msg.obj;
532 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800533 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700534 if (uiccCard == null) {
535 loge("iccExchangeSimIO: No UICC");
536 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700537 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700538 } else {
539 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
540 request);
541 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
542 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
543 iccArgument.data, onCompleted);
544 }
545 break;
546
547 case EVENT_EXCHANGE_SIM_IO_DONE:
548 ar = (AsyncResult) msg.obj;
549 request = (MainThreadRequest) ar.userObj;
550 if (ar.exception == null && ar.result != null) {
551 request.result = ar.result;
552 } else {
553 request.result = new IccIoResult(0x6f, 0, (byte[])null);
554 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700555 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700556 break;
557
Derek Tan4d5e5c12014-02-04 11:54:58 -0800558 case CMD_SEND_ENVELOPE:
559 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800560 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700561 if (uiccCard == null) {
562 loge("sendEnvelopeWithStatus: No UICC");
563 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700564 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700565 } else {
566 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
567 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
568 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800569 break;
570
571 case EVENT_SEND_ENVELOPE_DONE:
572 ar = (AsyncResult) msg.obj;
573 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700574 if (ar.exception == null && ar.result != null) {
575 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800576 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700577 request.result = new IccIoResult(0x6F, 0, (byte[])null);
578 if (ar.result == null) {
579 loge("sendEnvelopeWithStatus: Empty response");
580 } else if (ar.exception instanceof CommandException) {
581 loge("sendEnvelopeWithStatus: CommandException: " +
582 ar.exception);
583 } else {
584 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
585 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800586 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700587 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800588 break;
589
Shishir Agrawal566b7612013-10-28 14:41:00 -0700590 case CMD_OPEN_CHANNEL:
591 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800592 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800593 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700594 if (uiccCard == null) {
595 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800596 request.result = new IccOpenLogicalChannelResponse(-1,
597 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700598 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700599 } else {
600 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800601 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
602 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700603 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700604 break;
605
606 case EVENT_OPEN_CHANNEL_DONE:
607 ar = (AsyncResult) msg.obj;
608 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700609 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700610 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700611 int[] result = (int[]) ar.result;
612 int channelId = result[0];
613 byte[] selectResponse = null;
614 if (result.length > 1) {
615 selectResponse = new byte[result.length - 1];
616 for (int i = 1; i < result.length; ++i) {
617 selectResponse[i - 1] = (byte) result[i];
618 }
619 }
620 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700621 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700623 if (ar.result == null) {
624 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700625 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700626 if (ar.exception != null) {
627 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
628 }
629
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700630 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700631 if (ar.exception instanceof CommandException) {
632 CommandException.Error error =
633 ((CommandException) (ar.exception)).getCommandError();
634 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700635 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700636 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700637 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700638 }
639 }
640 openChannelResp = new IccOpenLogicalChannelResponse(
641 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700642 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700643 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700644 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700645 break;
646
647 case CMD_CLOSE_CHANNEL:
648 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800649 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700650 if (uiccCard == null) {
651 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900652 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700653 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700654 } else {
655 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
656 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
657 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700658 break;
659
660 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800661 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
662 break;
663
664 case CMD_NV_READ_ITEM:
665 request = (MainThreadRequest) msg.obj;
666 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800667 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
668 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800669 break;
670
671 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700672 ar = (AsyncResult) msg.obj;
673 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800674 if (ar.exception == null && ar.result != null) {
675 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700676 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800677 request.result = "";
678 if (ar.result == null) {
679 loge("nvReadItem: Empty response");
680 } else if (ar.exception instanceof CommandException) {
681 loge("nvReadItem: CommandException: " +
682 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700683 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800684 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700685 }
686 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700687 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700688 break;
689
Jake Hambye994d462014-02-03 13:10:13 -0800690 case CMD_NV_WRITE_ITEM:
691 request = (MainThreadRequest) msg.obj;
692 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
693 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800694 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700695 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800696 break;
697
698 case EVENT_NV_WRITE_ITEM_DONE:
699 handleNullReturnEvent(msg, "nvWriteItem");
700 break;
701
702 case CMD_NV_WRITE_CDMA_PRL:
703 request = (MainThreadRequest) msg.obj;
704 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800705 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800706 break;
707
708 case EVENT_NV_WRITE_CDMA_PRL_DONE:
709 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
710 break;
711
chen xu6dac5ab2018-10-26 17:39:23 -0700712 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800713 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700714 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800715 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800716 break;
717
chen xu6dac5ab2018-10-26 17:39:23 -0700718 case EVENT_RESET_MODEM_CONFIG_DONE:
719 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800720 break;
721
Jake Hamby7c27be32014-03-03 13:25:59 -0800722 case CMD_GET_PREFERRED_NETWORK_TYPE:
723 request = (MainThreadRequest) msg.obj;
724 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700725 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800726 break;
727
728 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
729 ar = (AsyncResult) msg.obj;
730 request = (MainThreadRequest) ar.userObj;
731 if (ar.exception == null && ar.result != null) {
732 request.result = ar.result; // Integer
733 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800734 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800735 if (ar.result == null) {
736 loge("getPreferredNetworkType: Empty response");
737 } else if (ar.exception instanceof CommandException) {
738 loge("getPreferredNetworkType: CommandException: " +
739 ar.exception);
740 } else {
741 loge("getPreferredNetworkType: Unknown exception");
742 }
743 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700744 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800745 break;
746
747 case CMD_SET_PREFERRED_NETWORK_TYPE:
748 request = (MainThreadRequest) msg.obj;
749 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
750 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700751 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800752 break;
753
754 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
755 handleNullReturnEvent(msg, "setPreferredNetworkType");
756 break;
757
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000758 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
759 request = (MainThreadRequest)msg.obj;
760 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800761 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000762 break;
763
764 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
765 ar = (AsyncResult)msg.obj;
766 request = (MainThreadRequest)ar.userObj;
767 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700768 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000769 break;
770
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800771 case CMD_SET_VOICEMAIL_NUMBER:
772 request = (MainThreadRequest) msg.obj;
773 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
774 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800775 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
776 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800777 break;
778
779 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
780 handleNullReturnEvent(msg, "setVoicemailNumber");
781 break;
782
Stuart Scott54788802015-03-30 13:18:01 -0700783 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
784 request = (MainThreadRequest) msg.obj;
785 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
786 request);
787 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
788 break;
789
790 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
791 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
792 break;
793
Shishir Agrawal302c8692015-06-19 13:49:39 -0700794 case CMD_PERFORM_NETWORK_SCAN:
795 request = (MainThreadRequest) msg.obj;
796 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
797 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
798 break;
799
800 case EVENT_PERFORM_NETWORK_SCAN_DONE:
801 ar = (AsyncResult) msg.obj;
802 request = (MainThreadRequest) ar.userObj;
803 CellNetworkScanResult cellScanResult;
804 if (ar.exception == null && ar.result != null) {
805 cellScanResult = new CellNetworkScanResult(
806 CellNetworkScanResult.STATUS_SUCCESS,
807 (List<OperatorInfo>) ar.result);
808 } else {
809 if (ar.result == null) {
810 loge("getCellNetworkScanResults: Empty response");
811 }
812 if (ar.exception != null) {
813 loge("getCellNetworkScanResults: Exception: " + ar.exception);
814 }
815 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
816 if (ar.exception instanceof CommandException) {
817 CommandException.Error error =
818 ((CommandException) (ar.exception)).getCommandError();
819 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
820 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
821 } else if (error == CommandException.Error.GENERIC_FAILURE) {
822 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
823 }
824 }
825 cellScanResult = new CellNetworkScanResult(errorCode, null);
826 }
827 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700828 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700829 break;
830
831 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
832 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700833 ManualNetworkSelectionArgument selArg =
834 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700835 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
836 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700837 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
838 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700839 break;
840
841 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -0700842 ar = (AsyncResult) msg.obj;
843 request = (MainThreadRequest) ar.userObj;
844 if (ar.exception == null) {
845 request.result = true;
846 } else {
847 request.result = false;
848 loge("setNetworkSelectionModeManual " + ar.exception);
849 }
850 notifyRequester(request);
851 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700852 break;
853
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700854 case CMD_GET_MODEM_ACTIVITY_INFO:
855 request = (MainThreadRequest) msg.obj;
856 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -0700857 if (defaultPhone != null) {
858 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
859 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700860 break;
861
862 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
863 ar = (AsyncResult) msg.obj;
864 request = (MainThreadRequest) ar.userObj;
865 if (ar.exception == null && ar.result != null) {
866 request.result = ar.result;
867 } else {
868 if (ar.result == null) {
869 loge("queryModemActivityInfo: Empty response");
870 } else if (ar.exception instanceof CommandException) {
871 loge("queryModemActivityInfo: CommandException: " +
872 ar.exception);
873 } else {
874 loge("queryModemActivityInfo: Unknown exception");
875 }
876 }
Amit Mahajand4766222016-01-28 15:28:28 -0800877 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
878 if (request.result == null) {
879 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
880 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700881 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700882 break;
883
Meng Wang1a7c35a2016-05-05 20:56:15 -0700884 case CMD_SET_ALLOWED_CARRIERS:
885 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -0800886 CarrierRestrictionRules argument =
887 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700888 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -0800889 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700890 break;
891
892 case EVENT_SET_ALLOWED_CARRIERS_DONE:
893 ar = (AsyncResult) msg.obj;
894 request = (MainThreadRequest) ar.userObj;
895 if (ar.exception == null && ar.result != null) {
896 request.result = ar.result;
897 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800898 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
899 if (ar.exception instanceof CommandException) {
900 loge("setAllowedCarriers: CommandException: " + ar.exception);
901 CommandException.Error error =
902 ((CommandException) (ar.exception)).getCommandError();
903 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
904 request.result =
905 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
906 }
Meng Wang1a7c35a2016-05-05 20:56:15 -0700907 } else {
908 loge("setAllowedCarriers: Unknown exception");
909 }
910 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700911 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700912 break;
913
914 case CMD_GET_ALLOWED_CARRIERS:
915 request = (MainThreadRequest) msg.obj;
916 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800917 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700918 break;
919
920 case EVENT_GET_ALLOWED_CARRIERS_DONE:
921 ar = (AsyncResult) msg.obj;
922 request = (MainThreadRequest) ar.userObj;
923 if (ar.exception == null && ar.result != null) {
924 request.result = ar.result;
925 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800926 request.result = new IllegalStateException(
927 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -0700928 if (ar.result == null) {
929 loge("getAllowedCarriers: Empty response");
930 } else if (ar.exception instanceof CommandException) {
931 loge("getAllowedCarriers: CommandException: " +
932 ar.exception);
933 } else {
934 loge("getAllowedCarriers: Unknown exception");
935 }
936 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700937 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700938 break;
939
Nathan Haroldb3014052017-01-25 15:57:32 -0800940 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
941 ar = (AsyncResult) msg.obj;
942 request = (MainThreadRequest) ar.userObj;
943 if (ar.exception == null && ar.result != null) {
944 request.result = ar.result;
945 } else {
946 request.result = new IllegalArgumentException(
947 "Failed to retrieve Forbidden Plmns");
948 if (ar.result == null) {
949 loge("getForbiddenPlmns: Empty response");
950 } else {
951 loge("getForbiddenPlmns: Unknown exception");
952 }
953 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700954 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800955 break;
956
957 case CMD_GET_FORBIDDEN_PLMNS:
958 request = (MainThreadRequest) msg.obj;
959 uiccCard = getUiccCardFromRequest(request);
960 if (uiccCard == null) {
961 loge("getForbiddenPlmns() UiccCard is null");
962 request.result = new IllegalArgumentException(
963 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700964 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800965 break;
966 }
967 Integer appType = (Integer) request.argument;
968 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
969 if (uiccApp == null) {
970 loge("getForbiddenPlmns() no app with specified type -- "
971 + appType);
972 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700973 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800974 break;
975 } else {
976 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
977 + " specified type -- " + appType);
978 }
979 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
980 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
981 onCompleted);
982 break;
983
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000984 case CMD_SWITCH_SLOTS:
985 request = (MainThreadRequest) msg.obj;
986 int[] physicalSlots = (int[]) request.argument;
987 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
988 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
989 break;
990
991 case EVENT_SWITCH_SLOTS_DONE:
992 ar = (AsyncResult) msg.obj;
993 request = (MainThreadRequest) ar.userObj;
994 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700995 notifyRequester(request);
996 break;
997 case CMD_GET_NETWORK_SELECTION_MODE:
998 request = (MainThreadRequest) msg.obj;
999 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1000 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1001 break;
1002
1003 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1004 ar = (AsyncResult) msg.obj;
1005 request = (MainThreadRequest) ar.userObj;
1006 if (ar.exception != null) {
1007 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1008 } else {
1009 int mode = ((int[]) ar.result)[0];
1010 if (mode == 0) {
1011 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1012 } else {
1013 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1014 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001015 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001016 notifyRequester(request);
1017 break;
1018 case CMD_GET_CDMA_ROAMING_MODE:
1019 request = (MainThreadRequest) msg.obj;
1020 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1021 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1022 break;
1023 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1024 ar = (AsyncResult) msg.obj;
1025 request = (MainThreadRequest) ar.userObj;
1026 if (ar.exception != null) {
1027 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1028 } else {
1029 request.result = ((int[]) ar.result)[0];
1030 }
1031 notifyRequester(request);
1032 break;
1033 case CMD_SET_CDMA_ROAMING_MODE:
1034 request = (MainThreadRequest) msg.obj;
1035 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1036 int mode = (int) request.argument;
1037 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1038 break;
1039 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1040 ar = (AsyncResult) msg.obj;
1041 request = (MainThreadRequest) ar.userObj;
1042 request.result = ar.exception == null;
1043 notifyRequester(request);
1044 break;
1045 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1046 request = (MainThreadRequest) msg.obj;
1047 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1048 int subscriptionMode = (int) request.argument;
1049 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1050 break;
1051 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1052 ar = (AsyncResult) msg.obj;
1053 request = (MainThreadRequest) ar.userObj;
1054 request.result = ar.exception == null;
1055 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001056 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001057 case CMD_GET_ALL_CELL_INFO:
1058 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001059 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001060 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001061 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001062 case EVENT_GET_ALL_CELL_INFO_DONE:
1063 ar = (AsyncResult) msg.obj;
1064 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001065 // If a timeout occurs, the response will be null
1066 request.result = (ar.exception == null && ar.result != null)
1067 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001068 synchronized (request) {
1069 request.notifyAll();
1070 }
1071 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001072 case CMD_REQUEST_CELL_INFO_UPDATE:
1073 request = (MainThreadRequest) msg.obj;
1074 request.phone.requestCellInfoUpdate(request.workSource,
1075 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1076 break;
1077 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1078 ar = (AsyncResult) msg.obj;
1079 request = (MainThreadRequest) ar.userObj;
1080 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1081 try {
1082 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001083 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Nathan Harolde82c4b82018-12-18 19:40:37 -08001084 cb.onError(TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1085 new android.os.ParcelableException(ar.exception));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001086 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001087 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Nathan Harolde82c4b82018-12-18 19:40:37 -08001088 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001089 } else {
1090 // use the result as returned
1091 cb.onCellInfo((List<CellInfo>) ar.result);
1092 }
1093 } catch (RemoteException re) {
1094 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1095 }
1096 break;
1097 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001098 request = (MainThreadRequest) msg.obj;
1099 WorkSource ws = (WorkSource) request.argument;
1100 Phone phone = getPhoneFromRequest(request);
1101 phone.getCellLocation(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
1102 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001103 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001104 ar = (AsyncResult) msg.obj;
1105 request = (MainThreadRequest) ar.userObj;
1106 if (ar.exception == null) {
1107 request.result = ar.result;
1108 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001109 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001110 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
1111 ? new CdmaCellLocation() : new GsmCellLocation();
1112 }
1113
1114 synchronized (request) {
1115 request.notifyAll();
1116 }
1117 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001118 case CMD_MODEM_REBOOT:
1119 request = (MainThreadRequest) msg.obj;
1120 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001121 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001122 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001123 case EVENT_CMD_MODEM_REBOOT_DONE:
1124 handleNullReturnEvent(msg, "rebootModem");
1125 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001126 case CMD_REQUEST_ENABLE_MODEM:
1127 request = (MainThreadRequest) msg.obj;
1128 boolean enable = (boolean) request.argument;
1129 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001130 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001131 PhoneConfigurationManager.getInstance()
1132 .enablePhone(request.phone, enable, onCompleted);
1133 break;
1134 case EVENT_ENABLE_MODEM_DONE:
1135 ar = (AsyncResult) msg.obj;
1136 request = (MainThreadRequest) ar.userObj;
1137 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001138 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001139 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001140 if ((boolean) request.result) {
1141 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1142 updateModemStateMetrics();
1143 } else {
1144 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1145 + ar.exception);
1146 }
1147 notifyRequester(request);
1148 break;
1149 case CMD_GET_MODEM_STATUS:
1150 request = (MainThreadRequest) msg.obj;
1151 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1152 PhoneConfigurationManager.getInstance()
1153 .getPhoneStatusFromModem(request.phone, onCompleted);
1154 break;
1155 case EVENT_GET_MODEM_STATUS_DONE:
1156 ar = (AsyncResult) msg.obj;
1157 request = (MainThreadRequest) ar.userObj;
1158 int id = request.phone.getPhoneId();
1159 if (ar.exception == null && ar.result != null) {
1160 request.result = ar.result;
1161 //update the cache as modem status has changed
1162 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1163 (boolean) request.result);
1164 } else {
1165 // Return true if modem status cannot be retrieved. For most cases,
1166 // modem status is on. And for older version modems, GET_MODEM_STATUS
1167 // and disable modem are not supported. Modem is always on.
1168 // TODO: this should be fixed in R to support a third
1169 // status UNKNOWN b/131631629
1170 request.result = true;
1171 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1172 + ar.exception);
1173 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001174 notifyRequester(request);
1175 break;
Naina Nalluri1264a9f2019-09-17 14:10:30 -07001176 case CMD_ERASE_MODEM_CONFIG:
1177 request = (MainThreadRequest) msg.obj;
1178 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1179 defaultPhone.eraseModemConfig(onCompleted);
1180 break;
1181 case EVENT_ERASE_MODEM_CONFIG_DONE:
1182 handleNullReturnEvent(msg, "eraseModemConfig");
1183 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001184 default:
1185 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1186 break;
1187 }
1188 }
Jake Hambye994d462014-02-03 13:10:13 -08001189
Pengquan Menga1bb6272018-09-06 09:59:22 -07001190 private void notifyRequester(MainThreadRequest request) {
1191 synchronized (request) {
1192 request.notifyAll();
1193 }
1194 }
1195
Jake Hambye994d462014-02-03 13:10:13 -08001196 private void handleNullReturnEvent(Message msg, String command) {
1197 AsyncResult ar = (AsyncResult) msg.obj;
1198 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1199 if (ar.exception == null) {
1200 request.result = true;
1201 } else {
1202 request.result = false;
1203 if (ar.exception instanceof CommandException) {
1204 loge(command + ": CommandException: " + ar.exception);
1205 } else {
1206 loge(command + ": Unknown exception");
1207 }
1208 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001209 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 }
1212
1213 /**
1214 * Posts the specified command to be executed on the main thread,
1215 * waits for the request to complete, and returns the result.
1216 * @see #sendRequestAsync
1217 */
1218 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001219 return sendRequest(
1220 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001221 }
1222
1223 /**
1224 * Posts the specified command to be executed on the main thread,
1225 * waits for the request to complete, and returns the result.
1226 * @see #sendRequestAsync
1227 */
1228 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1229 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001230 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001231 }
1232
1233 /**
1234 * Posts the specified command to be executed on the main thread,
1235 * waits for the request to complete, and returns the result.
1236 * @see #sendRequestAsync
1237 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001238 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001239 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001240 }
1241
1242 /**
1243 * Posts the specified command to be executed on the main thread,
1244 * waits for the request to complete, and returns the result.
1245 * @see #sendRequestAsync
1246 */
Nathan Harold92bed182018-10-12 18:16:49 -07001247 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1248 return sendRequest(command, argument, subId, null, workSource);
1249 }
1250
1251 /**
1252 * Posts the specified command to be executed on the main thread,
1253 * waits for the request to complete, and returns the result.
1254 * @see #sendRequestAsync
1255 */
1256 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1257 return sendRequest(
1258 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1259 }
1260
1261 /**
1262 * Posts the specified command to be executed on the main thread,
1263 * waits for the request to complete, and returns the result.
1264 * @see #sendRequestAsync
1265 */
1266 private Object sendRequest(
1267 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1269 throw new RuntimeException("This method will deadlock if called from the main thread.");
1270 }
1271
Nathan Harold92bed182018-10-12 18:16:49 -07001272 MainThreadRequest request = null;
1273 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1274 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1275 } else if (phone != null) {
1276 request = new MainThreadRequest(argument, phone, workSource);
1277 } else {
1278 request = new MainThreadRequest(argument, subId, workSource);
1279 }
1280
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001281 Message msg = mMainThreadHandler.obtainMessage(command, request);
1282 msg.sendToTarget();
1283
1284 // Wait for the request to complete
1285 synchronized (request) {
1286 while (request.result == null) {
1287 try {
1288 request.wait();
1289 } catch (InterruptedException e) {
1290 // Do nothing, go back and wait until the request is complete
1291 }
1292 }
1293 }
1294 return request.result;
1295 }
1296
1297 /**
1298 * Asynchronous ("fire and forget") version of sendRequest():
1299 * Posts the specified command to be executed on the main thread, and
1300 * returns immediately.
1301 * @see #sendRequest
1302 */
1303 private void sendRequestAsync(int command) {
1304 mMainThreadHandler.sendEmptyMessage(command);
1305 }
1306
1307 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001308 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001309 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001310 */
1311 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001312 sendRequestAsync(command, argument, null, null);
1313 }
1314
1315 /**
1316 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1317 * @see {@link #sendRequest(int,Object)}
1318 */
1319 private void sendRequestAsync(
1320 int command, Object argument, Phone phone, WorkSource workSource) {
1321 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001322 Message msg = mMainThreadHandler.obtainMessage(command, request);
1323 msg.sendToTarget();
1324 }
1325
1326 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001327 * Initialize the singleton PhoneInterfaceManager instance.
1328 * This is only done once, at startup, from PhoneApp.onCreate().
1329 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001330 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 synchronized (PhoneInterfaceManager.class) {
1332 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001333 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 } else {
1335 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1336 }
1337 return sInstance;
1338 }
1339 }
1340
1341 /** Private constructor; @see init() */
Jordan Liua39e6c12020-03-04 13:59:23 -08001342 @VisibleForTesting
1343 /* package */ PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001346 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001347 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1348 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001349 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001350 mTelephonySharedPreferences =
1351 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001352 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001353 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001354
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001355 publish();
1356 }
1357
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001358 private Phone getDefaultPhone() {
1359 Phone thePhone = getPhone(getDefaultSubscription());
1360 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1361 }
1362
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 private void publish() {
1364 if (DBG) log("publish: " + this);
1365
1366 ServiceManager.addService("phone", this);
1367 }
1368
Stuart Scott584921c2015-01-15 17:10:34 -08001369 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001370 if (request.phone != null) {
1371 return request.phone;
1372 } else {
1373 return getPhoneFromSubId(request.subId);
1374 }
1375 }
1376
1377 private Phone getPhoneFromSubId(int subId) {
1378 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1379 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001380 }
1381
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001382 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1383 Phone phone = getPhoneFromRequest(request);
1384 return phone == null ? null :
1385 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1386 }
1387
Wink Saville36469e72014-06-11 15:17:00 -07001388 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001389 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001390 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001391 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392
Naina Nalluri1264a9f2019-09-17 14:10:30 -07001393 private void sendEraseModemConfig(Phone phone) {
1394 if (phone != null) {
1395 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1396 mApp, phone.getSubId(), "eraseModemConfig");
1397 final long identity = Binder.clearCallingIdentity();
1398 try {
1399 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1400 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1401 } finally {
1402 Binder.restoreCallingIdentity(identity);
1403 }
1404 }
1405 }
1406
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001408 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001409 }
1410
Wink Savilleb564aae2014-10-23 10:18:09 -07001411 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 if (DBG) log("dial: " + number);
1413 // No permission check needed here: This is just a wrapper around the
1414 // ACTION_DIAL intent, which is available to any app since it puts up
1415 // the UI before it does anything.
1416
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001417 final long identity = Binder.clearCallingIdentity();
1418 try {
1419 String url = createTelUrl(number);
1420 if (url == null) {
1421 return;
1422 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001423
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001424 // PENDING: should we just silently fail if phone is offhook or ringing?
1425 PhoneConstants.State state = mCM.getState(subId);
1426 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1427 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1428 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1429 mApp.startActivity(intent);
1430 }
1431 } finally {
1432 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 }
1434 }
1435
1436 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001437 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001438 }
1439
Wink Savilleb564aae2014-10-23 10:18:09 -07001440 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 if (DBG) log("call: " + number);
1442
1443 // This is just a wrapper around the ACTION_CALL intent, but we still
1444 // need to do a permission check since we're calling startActivity()
1445 // from the context of the phone app.
1446 enforceCallPermission();
1447
1448 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1449 != AppOpsManager.MODE_ALLOWED) {
1450 return;
1451 }
1452
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001453 final long identity = Binder.clearCallingIdentity();
1454 try {
1455 String url = createTelUrl(number);
1456 if (url == null) {
1457 return;
1458 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001459
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001460 boolean isValid = false;
1461 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1462 if (slist != null) {
1463 for (SubscriptionInfo subInfoRecord : slist) {
1464 if (subInfoRecord.getSubscriptionId() == subId) {
1465 isValid = true;
1466 break;
1467 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001468 }
Wink Saville08874612014-08-31 19:19:58 -07001469 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001470 if (!isValid) {
1471 return;
1472 }
Wink Saville08874612014-08-31 19:19:58 -07001473
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001474 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1475 intent.putExtra(SUBSCRIPTION_KEY, subId);
1476 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1477 mApp.startActivity(intent);
1478 } finally {
1479 Binder.restoreCallingIdentity(identity);
1480 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 }
1482
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001484 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001485 }
1486
Wink Savilleb564aae2014-10-23 10:18:09 -07001487 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001488 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001489 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1490 }
1491
1492 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001493 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001494 }
1495
Wink Savilleb564aae2014-10-23 10:18:09 -07001496 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001497 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001498 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1499 }
1500
1501 /** {@hide} */
1502 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001503 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001504 }
1505
Wink Savilleb564aae2014-10-23 10:18:09 -07001506 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001508
1509 final long identity = Binder.clearCallingIdentity();
1510 try {
1511 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1512 checkSimPin.start();
1513 return checkSimPin.unlockSim(null, pin);
1514 } finally {
1515 Binder.restoreCallingIdentity(identity);
1516 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001517 }
1518
Wink Saville9de0f752013-10-22 19:04:03 -07001519 /** {@hide} */
1520 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001521 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001522 }
1523
Wink Savilleb564aae2014-10-23 10:18:09 -07001524 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001525 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001526
1527 final long identity = Binder.clearCallingIdentity();
1528 try {
1529 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1530 checkSimPuk.start();
1531 return checkSimPuk.unlockSim(puk, pin);
1532 } finally {
1533 Binder.restoreCallingIdentity(identity);
1534 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 }
1536
1537 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001538 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001539 * a synchronous one.
1540 */
1541 private static class UnlockSim extends Thread {
1542
1543 private final IccCard mSimCard;
1544
1545 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001546 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1547 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548
1549 // For replies from SimCard interface
1550 private Handler mHandler;
1551
1552 // For async handler to identify request type
1553 private static final int SUPPLY_PIN_COMPLETE = 100;
1554
1555 public UnlockSim(IccCard simCard) {
1556 mSimCard = simCard;
1557 }
1558
1559 @Override
1560 public void run() {
1561 Looper.prepare();
1562 synchronized (UnlockSim.this) {
1563 mHandler = new Handler() {
1564 @Override
1565 public void handleMessage(Message msg) {
1566 AsyncResult ar = (AsyncResult) msg.obj;
1567 switch (msg.what) {
1568 case SUPPLY_PIN_COMPLETE:
1569 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1570 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001571 mRetryCount = msg.arg1;
1572 if (ar.exception != null) {
1573 if (ar.exception instanceof CommandException &&
1574 ((CommandException)(ar.exception)).getCommandError()
1575 == CommandException.Error.PASSWORD_INCORRECT) {
1576 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1577 } else {
1578 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1579 }
1580 } else {
1581 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1582 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 mDone = true;
1584 UnlockSim.this.notifyAll();
1585 }
1586 break;
1587 }
1588 }
1589 };
1590 UnlockSim.this.notifyAll();
1591 }
1592 Looper.loop();
1593 }
1594
1595 /*
1596 * Use PIN or PUK to unlock SIM card
1597 *
1598 * If PUK is null, unlock SIM card with PIN
1599 *
1600 * If PUK is not null, unlock SIM card with PUK and set PIN code
1601 */
Wink Saville9de0f752013-10-22 19:04:03 -07001602 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603
1604 while (mHandler == null) {
1605 try {
1606 wait();
1607 } catch (InterruptedException e) {
1608 Thread.currentThread().interrupt();
1609 }
1610 }
1611 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1612
1613 if (puk == null) {
1614 mSimCard.supplyPin(pin, callback);
1615 } else {
1616 mSimCard.supplyPuk(puk, pin, callback);
1617 }
1618
1619 while (!mDone) {
1620 try {
1621 Log.d(LOG_TAG, "wait for done");
1622 wait();
1623 } catch (InterruptedException e) {
1624 // Restore the interrupted status
1625 Thread.currentThread().interrupt();
1626 }
1627 }
1628 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001629 int[] resultArray = new int[2];
1630 resultArray[0] = mResult;
1631 resultArray[1] = mRetryCount;
1632 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 }
1634 }
1635
1636 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001637 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001638
1639 }
1640
Wink Savilleb564aae2014-10-23 10:18:09 -07001641 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642 // No permission check needed here: this call is harmless, and it's
1643 // needed for the ServiceState.requestStateUpdate() call (which is
1644 // already intentionally exposed to 3rd parties.)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001645 final long identity = Binder.clearCallingIdentity();
1646 try {
1647 final Phone phone = getPhone(subId);
1648 if (phone != null) {
1649 phone.updateServiceLocation();
1650 }
1651 } finally {
1652 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001653 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001654 }
1655
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001656 @Override
1657 public boolean isRadioOn(String callingPackage) {
1658 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001659 }
1660
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001661 @Override
1662 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001663 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001664 mApp, subId, callingPackage, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001665 return false;
1666 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001667
1668 final long identity = Binder.clearCallingIdentity();
1669 try {
1670 return isRadioOnForSubscriber(subId);
1671 } finally {
1672 Binder.restoreCallingIdentity(identity);
1673 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001674 }
1675
1676 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001677 final long identity = Binder.clearCallingIdentity();
1678 try {
1679 final Phone phone = getPhone(subId);
1680 if (phone != null) {
1681 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1682 } else {
1683 return false;
1684 }
1685 } finally {
1686 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001687 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001688 }
1689
1690 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001691 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 }
Wink Saville36469e72014-06-11 15:17:00 -07001693
Wink Savilleb564aae2014-10-23 10:18:09 -07001694 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001695 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001696
1697 final long identity = Binder.clearCallingIdentity();
1698 try {
1699 final Phone phone = getPhone(subId);
1700 if (phone != null) {
1701 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1702 }
1703 } finally {
1704 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001705 }
Wink Saville36469e72014-06-11 15:17:00 -07001706 }
1707
1708 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001709 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001710 }
1711
Wink Savilleb564aae2014-10-23 10:18:09 -07001712 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001713 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001714
1715 final long identity = Binder.clearCallingIdentity();
1716 try {
1717 final Phone phone = getPhone(subId);
1718 if (phone == null) {
1719 return false;
1720 }
1721 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1722 toggleRadioOnOffForSubscriber(subId);
1723 }
1724 return true;
1725 } finally {
1726 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001727 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001728 }
Wink Saville36469e72014-06-11 15:17:00 -07001729
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001730 public boolean needMobileRadioShutdown() {
1731 /*
1732 * If any of the Radios are available, it will need to be
1733 * shutdown. So return true if any Radio is available.
1734 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001735 final long identity = Binder.clearCallingIdentity();
1736 try {
1737 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1738 Phone phone = PhoneFactory.getPhone(i);
1739 if (phone != null && phone.isRadioAvailable()) return true;
1740 }
1741 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1742 return false;
1743 } finally {
1744 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001745 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001746 }
1747
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001748 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001749 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001750 enforceModifyPermission();
1751
1752 final long identity = Binder.clearCallingIdentity();
1753 try {
1754 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1755 logv("Shutting down Phone " + i);
1756 shutdownRadioUsingPhoneId(i);
1757 }
1758 } finally {
1759 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001760 }
1761 }
1762
1763 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001764 Phone phone = PhoneFactory.getPhone(phoneId);
1765 if (phone != null && phone.isRadioAvailable()) {
1766 phone.shutdownRadio();
1767 }
1768 }
1769
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001770 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001771 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001772
1773 final long identity = Binder.clearCallingIdentity();
1774 try {
1775 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1776 if (defaultPhone != null) {
1777 defaultPhone.setRadioPower(turnOn);
1778 return true;
1779 } else {
1780 loge("There's no default phone.");
1781 return false;
1782 }
1783 } finally {
1784 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001785 }
Wink Saville36469e72014-06-11 15:17:00 -07001786 }
1787
Wink Savilleb564aae2014-10-23 10:18:09 -07001788 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001789 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001790
1791 final long identity = Binder.clearCallingIdentity();
1792 try {
1793 final Phone phone = getPhone(subId);
1794 if (phone != null) {
1795 phone.setRadioPower(turnOn);
1796 return true;
1797 } else {
1798 return false;
1799 }
1800 } finally {
1801 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001802 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001803 }
1804
Wink Saville36469e72014-06-11 15:17:00 -07001805 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001806 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001807 public boolean enableDataConnectivity() {
1808 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001809
1810 final long identity = Binder.clearCallingIdentity();
1811 try {
1812 int subId = mSubscriptionController.getDefaultDataSubId();
1813 final Phone phone = getPhone(subId);
1814 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001815 phone.getDataEnabledSettings().setUserDataEnabled(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001816 return true;
1817 } else {
1818 return false;
1819 }
1820 } finally {
1821 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001822 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001823 }
1824
Wink Saville36469e72014-06-11 15:17:00 -07001825 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001826 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001827 public boolean disableDataConnectivity() {
1828 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001829
1830 final long identity = Binder.clearCallingIdentity();
1831 try {
1832 int subId = mSubscriptionController.getDefaultDataSubId();
1833 final Phone phone = getPhone(subId);
1834 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001835 phone.getDataEnabledSettings().setUserDataEnabled(false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001836 return true;
1837 } else {
1838 return false;
1839 }
1840 } finally {
1841 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001842 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001843 }
1844
Sanket Padawe356d7632015-06-22 14:03:32 -07001845 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001846 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001847 final long identity = Binder.clearCallingIdentity();
1848 try {
1849 final Phone phone = getPhone(subId);
1850 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08001851 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001852 } else {
1853 return false;
1854 }
1855 } finally {
1856 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001857 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001858 }
1859
1860 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001861 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001862 }
1863
pkanwarae03a6b2016-11-06 20:37:09 -08001864 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001865 enforceCallPermission();
1866
1867 final long identity = Binder.clearCallingIdentity();
1868 try {
1869 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1870 return;
1871 }
1872 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1873 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1874 } finally {
1875 Binder.restoreCallingIdentity(identity);
1876 }
pkanwar32d516d2016-10-14 19:37:38 -07001877 };
1878
Wink Savilleb564aae2014-10-23 10:18:09 -07001879 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001881
1882 final long identity = Binder.clearCallingIdentity();
1883 try {
1884 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1885 return false;
1886 }
1887 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1888 } finally {
1889 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001890 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001891 }
1892
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001893 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001894 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001895 }
1896
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001897 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001898 final long identity = Binder.clearCallingIdentity();
1899 try {
1900 Phone phone = PhoneFactory.getPhone(slotIndex);
1901 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1902 PhoneConstantConversions.convertCallState(phone.getState());
1903 } finally {
1904 Binder.restoreCallingIdentity(identity);
1905 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001906 }
1907
Sanket Padawe356d7632015-06-22 14:03:32 -07001908 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 public int getDataState() {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001910 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
1911 }
1912
1913 @Override
1914 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001915 final long identity = Binder.clearCallingIdentity();
1916 try {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001917 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001918 if (phone != null) {
1919 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1920 } else {
1921 return PhoneConstantConversions.convertDataState(
1922 PhoneConstants.DataState.DISCONNECTED);
1923 }
1924 } finally {
1925 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001926 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001927 }
1928
Sanket Padawe356d7632015-06-22 14:03:32 -07001929 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001930 public int getDataActivity() {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001931 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
1932 }
1933
1934 @Override
1935 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001936 final long identity = Binder.clearCallingIdentity();
1937 try {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001938 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001939 if (phone != null) {
1940 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1941 } else {
1942 return TelephonyManager.DATA_ACTIVITY_NONE;
1943 }
1944 } finally {
1945 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001946 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001947 }
1948
1949 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001950 public Bundle getCellLocation(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001951 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08001952 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08001953
1954 LocationAccessPolicy.LocationPermissionResult locationResult =
1955 LocationAccessPolicy.checkLocationPermission(mApp,
1956 new LocationAccessPolicy.LocationPermissionQuery.Builder()
1957 .setCallingPackage(callingPackage)
1958 .setCallingPid(Binder.getCallingPid())
1959 .setCallingUid(Binder.getCallingUid())
1960 .setMethod("getCellLocation")
Hall Liuc3f8eb62020-01-24 18:07:12 -08001961 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08001962 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
1963 .build());
1964 switch (locationResult) {
1965 case DENIED_HARD:
1966 throw new SecurityException("Not allowed to access cell location");
1967 case DENIED_SOFT:
1968 return new Bundle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001969 }
1970
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001971 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001972 final long identity = Binder.clearCallingIdentity();
1973 try {
1974 if (DBG_LOC) log("getCellLocation: is active user");
1975 Bundle data = new Bundle();
Nathan Harold3ff88932018-08-14 10:19:49 -07001976 int subId = mSubscriptionController.getDefaultDataSubId();
1977 CellLocation cl = (CellLocation) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
1978 cl.fillInNotifierBundle(data);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001979 return data;
1980 } finally {
1981 Binder.restoreCallingIdentity(identity);
1982 }
Svetoslav64fad262015-04-14 14:35:21 -07001983 }
1984
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001985 @Override
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001986 public String getNetworkCountryIsoForPhone(int phoneId) {
1987 // Reporting the correct network country is ambiguous when IWLAN could conflict with
1988 // registered cell info, so return a NULL country instead.
1989 final long identity = Binder.clearCallingIdentity();
1990 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07001991 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
1992 // Get default phone in this case.
1993 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
1994 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001995 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07001996 // Todo: fix this when we can get the actual cellular network info when the device
1997 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001998 if (TelephonyManager.NETWORK_TYPE_IWLAN
1999 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
2000 return "";
2001 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002002 Phone phone = PhoneFactory.getPhone(phoneId);
2003 if (phone != null) {
2004 ServiceStateTracker sst = phone.getServiceStateTracker();
sqianb9d961a2019-07-31 20:23:45 -07002005 EmergencyNumberTracker emergencyNumberTracker = phone.getEmergencyNumberTracker();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002006 if (sst != null) {
2007 LocaleTracker lt = sst.getLocaleTracker();
2008 if (lt != null) {
sqianb9d961a2019-07-31 20:23:45 -07002009 if (!TextUtils.isEmpty(lt.getCurrentCountry())) {
2010 return lt.getCurrentCountry();
2011 } else if (emergencyNumberTracker != null) {
2012 return emergencyNumberTracker.getEmergencyCountryIso();
2013 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002014 }
2015 }
2016 }
2017 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002018 } finally {
2019 Binder.restoreCallingIdentity(identity);
2020 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002021 }
2022
2023 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002024 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002025 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002026 }
2027
Sanket Padawe356d7632015-06-22 14:03:32 -07002028 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002029 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002030 mApp.enforceCallingOrSelfPermission(
2031 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002032
2033 final long identity = Binder.clearCallingIdentity();
2034 try {
2035 final Phone phone = getPhone(subId);
2036 if (phone != null) {
2037 phone.enableLocationUpdates();
2038 }
2039 } finally {
2040 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002041 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002042 }
2043
2044 @Override
2045 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002046 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002047 }
2048
Sanket Padawe356d7632015-06-22 14:03:32 -07002049 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002050 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002051 mApp.enforceCallingOrSelfPermission(
2052 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002053
2054 final long identity = Binder.clearCallingIdentity();
2055 try {
2056 final Phone phone = getPhone(subId);
2057 if (phone != null) {
2058 phone.disableLocationUpdates();
2059 }
2060 } finally {
2061 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002062 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002063 }
2064
Nathan Harold31d7ff32018-10-15 20:20:30 -07002065 /**
2066 * Returns the target SDK version number for a given package name.
2067 *
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002068 * This call MUST be invoked before clearing the calling UID.
2069 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002070 * @return target SDK if the package is found or INT_MAX.
2071 */
2072 private int getTargetSdk(String packageName) {
2073 try {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002074 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
2075 packageName, 0, UserHandle.getUserId(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002076 if (ai != null) return ai.targetSdkVersion;
2077 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002078 loge("Failed to get package info for pkg="
2079 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002080 }
2081 return Integer.MAX_VALUE;
2082 }
2083
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002084 @Override
2085 @SuppressWarnings("unchecked")
Nathan Harold31d7ff32018-10-15 20:20:30 -07002086 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
2087 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002088 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2089 throw new SecurityException(
2090 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2091 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002092
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002093 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
2094 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2095 return null;
2096 }
Svetoslav64fad262015-04-14 14:35:21 -07002097
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002098 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002099
Nathan Haroldf180aac2018-06-01 18:43:55 -07002100 List<CellInfo> info = getAllCellInfo(callingPackage);
2101 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002102
Nathan Haroldf180aac2018-06-01 18:43:55 -07002103 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2104 for (CellInfo ci : info) {
2105 if (ci instanceof CellInfoGsm) {
2106 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2107 } else if (ci instanceof CellInfoWcdma) {
2108 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2109 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002110 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002111 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002112 }
2113
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002114 private List<CellInfo> getCachedCellInfo() {
2115 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2116 for (Phone phone : PhoneFactory.getPhones()) {
2117 List<CellInfo> info = phone.getAllCellInfo();
2118 if (info != null) cellInfos.addAll(info);
2119 }
2120 return cellInfos;
2121 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002122
2123 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002124 public List<CellInfo> getAllCellInfo(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002125 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002126 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002127
2128 LocationAccessPolicy.LocationPermissionResult locationResult =
2129 LocationAccessPolicy.checkLocationPermission(mApp,
2130 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2131 .setCallingPackage(callingPackage)
2132 .setCallingPid(Binder.getCallingPid())
2133 .setCallingUid(Binder.getCallingUid())
2134 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002135 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002136 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2137 .build());
2138 switch (locationResult) {
2139 case DENIED_HARD:
2140 throw new SecurityException("Not allowed to access cell info");
2141 case DENIED_SOFT:
2142 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002143 }
2144
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002145 final int targetSdk = getTargetSdk(callingPackage);
2146 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2147 return getCachedCellInfo();
2148 }
2149
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002150 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002151 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002152 final long identity = Binder.clearCallingIdentity();
2153 try {
2154 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2155 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002156 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002157 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002158 if (info != null) cellInfos.addAll(info);
2159 }
2160 return cellInfos;
2161 } finally {
2162 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002163 }
2164 }
2165
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002166 @Override
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002167 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage) {
2168 requestCellInfoUpdateInternal(
2169 subId, cb, callingPackage, getWorkSource(Binder.getCallingUid()));
2170 }
2171
2172 @Override
2173 public void requestCellInfoUpdateWithWorkSource(
2174 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
2175 enforceModifyPermission();
2176 requestCellInfoUpdateInternal(subId, cb, callingPackage, workSource);
2177 }
2178
2179 private void requestCellInfoUpdateInternal(
2180 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002181 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002182 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002183
2184 LocationAccessPolicy.LocationPermissionResult locationResult =
2185 LocationAccessPolicy.checkLocationPermission(mApp,
2186 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2187 .setCallingPackage(callingPackage)
2188 .setCallingPid(Binder.getCallingPid())
2189 .setCallingUid(Binder.getCallingUid())
2190 .setMethod("requestCellInfoUpdate")
Hall Liuaa4283b2020-05-21 17:09:35 -07002191 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2192 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002193 .build());
2194 switch (locationResult) {
2195 case DENIED_HARD:
Hall Liuaa4283b2020-05-21 17:09:35 -07002196 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2197 // Safetynet logging for b/154934934
2198 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2199 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002200 throw new SecurityException("Not allowed to access cell info");
2201 case DENIED_SOFT:
Hall Liuaa4283b2020-05-21 17:09:35 -07002202 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2203 // Safetynet logging for b/154934934
2204 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2205 }
Nathan Haroldb3c5da42019-05-09 10:26:08 -07002206 try {
2207 cb.onCellInfo(new ArrayList<CellInfo>());
2208 } catch (RemoteException re) {
2209 // Drop without consequences
2210 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002211 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002212 }
2213
Nathan Harold32e84c42019-05-09 10:13:47 -07002214
2215 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002216 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2217
2218 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2219 }
2220
2221 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002222 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002223 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002224 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002225
2226 final long identity = Binder.clearCallingIdentity();
2227 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002228 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002229 } finally {
2230 Binder.restoreCallingIdentity(identity);
2231 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002232 }
2233
Shishir Agrawala9f32182016-04-12 12:00:16 -07002234 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002235 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002236 Phone phone = PhoneFactory.getPhone(slotIndex);
2237 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002238 return null;
2239 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002240 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002241 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2242 callingPackage, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002243 return null;
2244 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002245
2246 final long identity = Binder.clearCallingIdentity();
2247 try {
2248 return phone.getImei();
2249 } finally {
2250 Binder.restoreCallingIdentity(identity);
2251 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002252 }
2253
2254 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002255 public String getTypeAllocationCodeForSlot(int slotIndex) {
2256 Phone phone = PhoneFactory.getPhone(slotIndex);
2257 String tac = null;
2258 if (phone != null) {
2259 String imei = phone.getImei();
2260 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2261 }
2262 return tac;
2263 }
2264
2265 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002266 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002267 Phone phone = PhoneFactory.getPhone(slotIndex);
2268 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002269 return null;
2270 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002271
Jeff Davidson913390f2018-02-23 17:11:49 -08002272 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002273 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2274 callingPackage, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002275 return null;
2276 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002277
2278 final long identity = Binder.clearCallingIdentity();
2279 try {
2280 return phone.getMeid();
2281 } finally {
2282 Binder.restoreCallingIdentity(identity);
2283 }
Jack Yu2af8d712017-03-15 17:14:14 -07002284 }
2285
2286 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002287 public String getManufacturerCodeForSlot(int slotIndex) {
2288 Phone phone = PhoneFactory.getPhone(slotIndex);
2289 String manufacturerCode = null;
2290 if (phone != null) {
2291 String meid = phone.getMeid();
2292 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2293 }
2294 return manufacturerCode;
2295 }
2296
2297 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002298 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002299 Phone phone = PhoneFactory.getPhone(slotIndex);
2300 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002301 return null;
2302 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002303 int subId = phone.getSubId();
2304 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2305 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2306 return null;
2307 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002308
2309 final long identity = Binder.clearCallingIdentity();
2310 try {
2311 return phone.getDeviceSvn();
2312 } finally {
2313 Binder.restoreCallingIdentity(identity);
2314 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002315 }
2316
fionaxu43304da2017-11-27 22:51:16 -08002317 @Override
2318 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002319 final long identity = Binder.clearCallingIdentity();
2320 try {
2321 final Phone phone = getPhone(subId);
2322 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2323 } finally {
2324 Binder.restoreCallingIdentity(identity);
2325 }
fionaxu43304da2017-11-27 22:51:16 -08002326 }
2327
2328 @Override
2329 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002330 final long identity = Binder.clearCallingIdentity();
2331 try {
2332 final Phone phone = getPhone(subId);
2333 return phone == null ? null : phone.getCarrierName();
2334 } finally {
2335 Binder.restoreCallingIdentity(identity);
2336 }
fionaxu43304da2017-11-27 22:51:16 -08002337 }
2338
calvinpanffe225e2018-11-01 19:43:06 +08002339 @Override
chen xu0026ca62019-03-06 15:28:50 -08002340 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002341 final long identity = Binder.clearCallingIdentity();
2342 try {
2343 final Phone phone = getPhone(subId);
2344 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002345 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002346 } finally {
2347 Binder.restoreCallingIdentity(identity);
2348 }
2349 }
2350
2351 @Override
chen xu0026ca62019-03-06 15:28:50 -08002352 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002353 final long identity = Binder.clearCallingIdentity();
2354 try {
2355 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002356 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002357 } finally {
2358 Binder.restoreCallingIdentity(identity);
2359 }
2360 }
2361
chen xu651eec72018-11-11 19:03:44 -08002362 @Override
chen xu864e11c2018-12-06 22:10:03 -08002363 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2364 if (!isSubscriptionMccMnc) {
2365 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2366 }
chen xu651eec72018-11-11 19:03:44 -08002367 final Phone phone = PhoneFactory.getPhone(slotIndex);
2368 if (phone == null) {
2369 return TelephonyManager.UNKNOWN_CARRIER_ID;
2370 }
2371 final long identity = Binder.clearCallingIdentity();
2372 try {
2373 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2374 } finally {
2375 Binder.restoreCallingIdentity(identity);
2376 }
2377 }
2378
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002379 //
2380 // Internal helper methods.
2381 //
2382
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002383 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002384 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2385 *
2386 * @throws SecurityException if the caller does not have the required permission
2387 */
2388 private void enforceModifyPermission() {
2389 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2390 }
2391
2392 /**
2393 * Make sure the caller has the CALL_PHONE permission.
2394 *
2395 * @throws SecurityException if the caller does not have the required permission
2396 */
2397 private void enforceCallPermission() {
2398 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2399 }
2400
Stuart Scott8eef64f2015-04-08 15:13:54 -07002401 private void enforceConnectivityInternalPermission() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002402 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
Stuart Scott8eef64f2015-04-08 15:13:54 -07002403 "ConnectivityService");
2404 }
2405
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002406 private String createTelUrl(String number) {
2407 if (TextUtils.isEmpty(number)) {
2408 return null;
2409 }
2410
Jake Hambye994d462014-02-03 13:10:13 -08002411 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002412 }
2413
Ihab Awadf9e92732013-12-05 18:02:52 -08002414 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002415 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2416 }
2417
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002418 private static void logv(String msg) {
2419 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2420 }
2421
Ihab Awadf9e92732013-12-05 18:02:52 -08002422 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002423 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2424 }
2425
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002426 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002427 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002428 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002429 }
2430
Sanket Padawe356d7632015-06-22 14:03:32 -07002431 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002432 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002433 final long identity = Binder.clearCallingIdentity();
2434 try {
2435 final Phone phone = PhoneFactory.getPhone(slotIndex);
2436 if (phone == null) {
2437 return PhoneConstants.PHONE_TYPE_NONE;
2438 } else {
2439 return phone.getPhoneType();
2440 }
2441 } finally {
2442 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002443 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002444 }
2445
2446 /**
2447 * Returns the CDMA ERI icon index to display
2448 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002449 @Override
2450 public int getCdmaEriIconIndex(String callingPackage) {
2451 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002452 }
2453
Sanket Padawe356d7632015-06-22 14:03:32 -07002454 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002455 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002456 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002457 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002458 return -1;
2459 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002460
2461 final long identity = Binder.clearCallingIdentity();
2462 try {
2463 final Phone phone = getPhone(subId);
2464 if (phone != null) {
2465 return phone.getCdmaEriIconIndex();
2466 } else {
2467 return -1;
2468 }
2469 } finally {
2470 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002471 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002472 }
2473
2474 /**
2475 * Returns the CDMA ERI icon mode,
2476 * 0 - ON
2477 * 1 - FLASHING
2478 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002479 @Override
2480 public int getCdmaEriIconMode(String callingPackage) {
2481 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002482 }
2483
Sanket Padawe356d7632015-06-22 14:03:32 -07002484 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002485 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002486 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002487 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002488 return -1;
2489 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002490
2491 final long identity = Binder.clearCallingIdentity();
2492 try {
2493 final Phone phone = getPhone(subId);
2494 if (phone != null) {
2495 return phone.getCdmaEriIconMode();
2496 } else {
2497 return -1;
2498 }
2499 } finally {
2500 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002501 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002502 }
2503
2504 /**
2505 * Returns the CDMA ERI text,
2506 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002507 @Override
2508 public String getCdmaEriText(String callingPackage) {
2509 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002510 }
2511
Sanket Padawe356d7632015-06-22 14:03:32 -07002512 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002513 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002514 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002515 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002516 return null;
2517 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002518
2519 final long identity = Binder.clearCallingIdentity();
2520 try {
2521 final Phone phone = getPhone(subId);
2522 if (phone != null) {
2523 return phone.getCdmaEriText();
2524 } else {
2525 return null;
2526 }
2527 } finally {
2528 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002529 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002530 }
2531
2532 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002533 * Returns the CDMA MDN.
2534 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002535 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002536 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002537 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2538 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002539
2540 final long identity = Binder.clearCallingIdentity();
2541 try {
2542 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002543 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002544 return phone.getLine1Number();
2545 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002546 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002547 return null;
2548 }
2549 } finally {
2550 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002551 }
2552 }
2553
2554 /**
2555 * Returns the CDMA MIN.
2556 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002557 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002558 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002559 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2560 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002561
2562 final long identity = Binder.clearCallingIdentity();
2563 try {
2564 final Phone phone = getPhone(subId);
2565 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2566 return phone.getCdmaMin();
2567 } else {
2568 return null;
2569 }
2570 } finally {
2571 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002572 }
2573 }
2574
Hall Liud892bec2018-11-30 14:51:45 -08002575 @Override
2576 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2577 INumberVerificationCallback callback, String callingPackage) {
2578 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2579 != PERMISSION_GRANTED) {
2580 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2581 }
2582 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2583
2584 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2585 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2586 throw new SecurityException("Calling package must be configured in the device config");
2587 }
2588
2589 if (range == null) {
2590 throw new NullPointerException("Range must be non-null");
2591 }
2592
2593 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002594 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002595
2596 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2597 }
2598
Junda Liuca05d5d2014-08-14 22:36:34 -07002599 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002600 * Returns true if CDMA provisioning needs to run.
2601 */
2602 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002603 final long identity = Binder.clearCallingIdentity();
2604 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002605 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002606 } finally {
2607 Binder.restoreCallingIdentity(identity);
2608 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002609 }
2610
2611 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002612 * Sets the voice mail number of a given subId.
2613 */
2614 @Override
2615 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002616 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002617
2618 final long identity = Binder.clearCallingIdentity();
2619 try {
2620 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2621 new Pair<String, String>(alphaTag, number), new Integer(subId));
2622 return success;
2623 } finally {
2624 Binder.restoreCallingIdentity(identity);
2625 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002626 }
2627
Ta-wei Yen87c49842016-05-13 21:19:52 -07002628 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002629 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2630 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002631 String systemDialer = TelecomManager.from(mApp).getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002632 if (!TextUtils.equals(callingPackage, systemDialer)) {
2633 throw new SecurityException("caller must be system dialer");
2634 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002635
2636 final long identity = Binder.clearCallingIdentity();
2637 try {
2638 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2639 if (phoneAccountHandle == null) {
2640 return null;
2641 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002642 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002643 } finally {
2644 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002645 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002646 }
2647
2648 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002649 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002650 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002651 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002652 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002653 return null;
2654 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002655
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002656 final long identity = Binder.clearCallingIdentity();
2657 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002658 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002659 } finally {
2660 Binder.restoreCallingIdentity(identity);
2661 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002662 }
2663
2664 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002665 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2666 VisualVoicemailSmsFilterSettings settings) {
2667 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002668
2669 final long identity = Binder.clearCallingIdentity();
2670 try {
2671 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002672 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002673 } finally {
2674 Binder.restoreCallingIdentity(identity);
2675 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002676 }
2677
2678 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002679 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2680 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002681
2682 final long identity = Binder.clearCallingIdentity();
2683 try {
2684 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002685 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002686 } finally {
2687 Binder.restoreCallingIdentity(identity);
2688 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002689 }
2690
2691 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002692 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2693 String callingPackage, int subId) {
2694 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002695
2696 final long identity = Binder.clearCallingIdentity();
2697 try {
2698 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002699 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002700 } finally {
2701 Binder.restoreCallingIdentity(identity);
2702 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002703 }
2704
2705 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002706 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002707 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002708
2709 final long identity = Binder.clearCallingIdentity();
2710 try {
2711 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002712 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002713 } finally {
2714 Binder.restoreCallingIdentity(identity);
2715 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002716 }
2717
2718 @Override
2719 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2720 String number, int port, String text, PendingIntent sentIntent) {
2721 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002722 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002723 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07002724 SmsController smsController = PhoneFactory.getSmsController();
2725 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, subId, number, port, text,
2726 sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002727 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07002728
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002729 /**
fionaxu0152e512016-11-14 13:36:14 -08002730 * Sets the voice activation state of a given subId.
2731 */
2732 @Override
2733 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002734 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2735 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002736
2737 final long identity = Binder.clearCallingIdentity();
2738 try {
2739 final Phone phone = getPhone(subId);
2740 if (phone != null) {
2741 phone.setVoiceActivationState(activationState);
2742 } else {
2743 loge("setVoiceActivationState fails with invalid subId: " + subId);
2744 }
2745 } finally {
2746 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002747 }
2748 }
2749
2750 /**
2751 * Sets the data activation state of a given subId.
2752 */
2753 @Override
2754 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002755 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2756 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002757
2758 final long identity = Binder.clearCallingIdentity();
2759 try {
2760 final Phone phone = getPhone(subId);
2761 if (phone != null) {
2762 phone.setDataActivationState(activationState);
2763 } else {
2764 loge("setVoiceActivationState fails with invalid subId: " + subId);
2765 }
2766 } finally {
2767 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002768 }
2769 }
2770
2771 /**
2772 * Returns the voice activation state of a given subId.
2773 */
2774 @Override
2775 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002776 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002777
fionaxu0152e512016-11-14 13:36:14 -08002778 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002779 final long identity = Binder.clearCallingIdentity();
2780 try {
2781 if (phone != null) {
2782 return phone.getVoiceActivationState();
2783 } else {
2784 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2785 }
2786 } finally {
2787 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002788 }
2789 }
2790
2791 /**
2792 * Returns the data activation state of a given subId.
2793 */
2794 @Override
2795 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002796 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002797
fionaxu0152e512016-11-14 13:36:14 -08002798 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002799 final long identity = Binder.clearCallingIdentity();
2800 try {
2801 if (phone != null) {
2802 return phone.getDataActivationState();
2803 } else {
2804 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2805 }
2806 } finally {
2807 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002808 }
2809 }
2810
2811 /**
Wink Saville36469e72014-06-11 15:17:00 -07002812 * Returns the unread count of voicemails for a subId
2813 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002814 @Override
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002815 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage) {
2816 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2817 mApp, subId, callingPackage, "getVoiceMessageCountForSubscriber")) {
2818 return 0;
2819 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002820 final long identity = Binder.clearCallingIdentity();
2821 try {
2822 final Phone phone = getPhone(subId);
2823 if (phone != null) {
2824 return phone.getVoiceMessageCount();
2825 } else {
2826 return 0;
2827 }
2828 } finally {
2829 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002830 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002831 }
2832
2833 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002834 * returns true, if the device is in a state where both voice and data
2835 * are supported simultaneously. This can change based on location or network condition.
2836 */
2837 @Override
2838 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002839 final long identity = Binder.clearCallingIdentity();
2840 try {
2841 final Phone phone = getPhone(subId);
2842 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2843 } finally {
2844 Binder.restoreCallingIdentity(identity);
2845 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002846 }
2847
2848 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002849 * Send the dialer code if called from the current default dialer or the caller has
2850 * carrier privilege.
2851 * @param inputCode The dialer code to send
2852 */
2853 @Override
2854 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002855 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002856 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002857 String defaultDialer = TelecomManager.from(defaultPhone.getContext())
2858 .getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002859 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002860 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2861 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002862 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002863
2864 final long identity = Binder.clearCallingIdentity();
2865 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002866 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002867 } finally {
2868 Binder.restoreCallingIdentity(identity);
2869 }
fionaxu235cc5e2017-03-06 22:25:57 -08002870 }
2871
Pengquan Menga1bb6272018-09-06 09:59:22 -07002872 @Override
2873 public int getNetworkSelectionMode(int subId) {
Pengquan Menge92a50d2018-09-21 15:54:48 -07002874 if (!isActiveSubscription(subId)) {
2875 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2876 }
2877
Pengquan Menga1bb6272018-09-06 09:59:22 -07002878 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2879 }
2880
Brad Ebinger35c841c2018-10-01 10:40:55 -07002881 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002882 public boolean isInEmergencySmsMode() {
2883 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2884 final long identity = Binder.clearCallingIdentity();
2885 try {
2886 for (Phone phone : PhoneFactory.getPhones()) {
2887 if (phone.isInEmergencySmsMode()) {
2888 return true;
2889 }
2890 }
2891 } finally {
2892 Binder.restoreCallingIdentity(identity);
2893 }
2894 return false;
2895 }
2896
2897 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002898 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
2899 throws RemoteException {
2900 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002901 final long token = Binder.clearCallingIdentity();
2902 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002903 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002904 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002905 .addRegistrationCallbackForSubscription(c, subId);
2906 } finally {
2907 Binder.restoreCallingIdentity(token);
2908 }
2909 }
2910
2911 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002912 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
2913 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002914 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2915 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2916 }
2917 Binder.withCleanCallingIdentity(() -> {
2918 try {
2919 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002920 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002921 .removeRegistrationCallbackForSubscription(c, subId);
2922 } catch (IllegalArgumentException e) {
2923 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
2924 + "is inactive, ignoring unregister.");
2925 // If the subscription is no longer active, just return, since the callback
2926 // will already have been removed internally.
2927 }
2928 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002929 }
2930
2931 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002932 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
2933 throws RemoteException {
2934 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002935 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2936 final long token = Binder.clearCallingIdentity();
2937 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002938 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002939 .addCapabilitiesCallbackForSubscription(c, subId);
2940 } finally {
2941 Binder.restoreCallingIdentity(token);
2942 }
2943 }
2944
2945 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002946 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
2947 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002948
2949 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2950 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2951 }
2952 Binder.withCleanCallingIdentity(() -> {
2953 try {
2954 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002955 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002956 .removeCapabilitiesCallbackForSubscription(c, subId);
2957 } catch (IllegalArgumentException e) {
2958 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
2959 + "is inactive, ignoring unregister.");
2960 // If the subscription is no longer active, just return, since the callback
2961 // will already have been removed internally.
2962 }
2963 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002964 }
2965
2966 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002967 public boolean isCapable(int subId, int capability, int regTech) {
2968 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002969 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2970 final long token = Binder.clearCallingIdentity();
2971 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002972 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002973 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
2974 } catch (ImsException e) {
2975 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
2976 return false;
Brad Ebinger6b5ac222019-02-04 14:36:52 -08002977 } catch (IllegalArgumentException e) {
2978 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
2979 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07002980 } finally {
2981 Binder.restoreCallingIdentity(token);
2982 }
2983 }
2984
2985 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002986 public boolean isAvailable(int subId, int capability, int regTech) {
2987 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002988 final long token = Binder.clearCallingIdentity();
2989 try {
2990 Phone phone = getPhone(subId);
2991 if (phone == null) return false;
2992 return phone.isImsCapabilityAvailable(capability, regTech);
2993 } finally {
2994 Binder.restoreCallingIdentity(token);
2995 }
2996 }
2997
2998 @Override
2999 public boolean isAdvancedCallingSettingEnabled(int subId) {
3000 enforceReadPrivilegedPermission("enforceReadPrivilegedPermission");
3001 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3002 final long token = Binder.clearCallingIdentity();
3003 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003004 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003005 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
3006 } finally {
3007 Binder.restoreCallingIdentity(token);
3008 }
3009 }
3010
3011 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003012 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003013 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003014 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003015 final long identity = Binder.clearCallingIdentity();
3016 try {
3017 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003018 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003019 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
3020 } finally {
3021 Binder.restoreCallingIdentity(identity);
3022 }
3023 }
3024
3025 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003026 public boolean isVtSettingEnabled(int subId) {
3027 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003028 final long identity = Binder.clearCallingIdentity();
3029 try {
3030 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003031 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003032 getSlotIndexOrException(subId)).isVtEnabledByUser();
3033 } finally {
3034 Binder.restoreCallingIdentity(identity);
3035 }
3036 }
3037
3038 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003039 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003040 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003041 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003042 final long identity = Binder.clearCallingIdentity();
3043 try {
3044 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003045 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003046 } finally {
3047 Binder.restoreCallingIdentity(identity);
3048 }
3049 }
3050
3051 @Override
3052 public boolean isVoWiFiSettingEnabled(int subId) {
3053 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
3054 final long identity = Binder.clearCallingIdentity();
3055 try {
3056 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003057 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003058 getSlotIndexOrException(subId)).isWfcEnabledByUser();
3059 } finally {
3060 Binder.restoreCallingIdentity(identity);
3061 }
3062 }
3063
3064 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003065 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003066 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003067 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003068 final long identity = Binder.clearCallingIdentity();
3069 try {
3070 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003071 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003072 } finally {
3073 Binder.restoreCallingIdentity(identity);
3074 }
3075 }
3076
3077 @Override
3078 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
3079 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3080 final long identity = Binder.clearCallingIdentity();
3081 try {
3082 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003083 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003084 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
3085 } finally {
3086 Binder.restoreCallingIdentity(identity);
3087 }
3088 }
3089
3090 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003091 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003092 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003093 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003094 final long identity = Binder.clearCallingIdentity();
3095 try {
3096 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003097 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003098 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
3099 } finally {
3100 Binder.restoreCallingIdentity(identity);
3101 }
3102 }
3103
3104 @Override
3105 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3106 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3107 "setVoWiFiNonPersistent");
3108 final long identity = Binder.clearCallingIdentity();
3109 try {
3110 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003111 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003112 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003113 } finally {
3114 Binder.restoreCallingIdentity(identity);
3115 }
3116 }
3117
3118 @Override
3119 public int getVoWiFiModeSetting(int subId) {
3120 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3121 final long identity = Binder.clearCallingIdentity();
3122 try {
3123 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003124 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003125 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
3126 } finally {
3127 Binder.restoreCallingIdentity(identity);
3128 }
3129 }
3130
3131 @Override
3132 public void setVoWiFiModeSetting(int subId, int mode) {
3133 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3134 "setVoWiFiModeSetting");
3135 final long identity = Binder.clearCallingIdentity();
3136 try {
3137 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003138 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003139 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
3140 } finally {
3141 Binder.restoreCallingIdentity(identity);
3142 }
3143 }
3144
3145 @Override
3146 public int getVoWiFiRoamingModeSetting(int subId) {
3147 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3148 final long identity = Binder.clearCallingIdentity();
3149 try {
3150 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003151 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003152 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
3153 } finally {
3154 Binder.restoreCallingIdentity(identity);
3155 }
3156 }
3157
3158 @Override
3159 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3160 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3161 "setVoWiFiRoamingModeSetting");
3162 final long identity = Binder.clearCallingIdentity();
3163 try {
3164 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003165 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003166 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
3167 } finally {
3168 Binder.restoreCallingIdentity(identity);
3169 }
3170 }
3171
3172 @Override
3173 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3174 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3175 "setRttCapabilityEnabled");
3176 final long identity = Binder.clearCallingIdentity();
3177 try {
3178 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003179 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003180 getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3181 } finally {
3182 Binder.restoreCallingIdentity(identity);
3183 }
3184 }
3185
3186 @Override
3187 public boolean isTtyOverVolteEnabled(int subId) {
3188 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3189 final long identity = Binder.clearCallingIdentity();
3190 try {
3191 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003192 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003193 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
3194 } finally {
3195 Binder.restoreCallingIdentity(identity);
3196 }
3197 }
3198
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003199 @Override
3200 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3201 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3202 final long identity = Binder.clearCallingIdentity();
3203 try {
3204 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003205 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003206 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003207 } finally {
3208 Binder.restoreCallingIdentity(identity);
3209 }
3210 }
3211
3212 @Override
3213 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3214 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3215 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003216 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3217 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3218 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003219 try {
3220 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003221 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003222 .removeProvisioningCallbackForSubscription(callback, subId);
3223 } catch (IllegalArgumentException e) {
3224 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3225 + "is inactive, ignoring unregister.");
3226 // If the subscription is no longer active, just return, since the callback will already
3227 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003228 } finally {
3229 Binder.restoreCallingIdentity(identity);
3230 }
3231 }
3232
3233 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003234 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3235 boolean isProvisioned) {
3236 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3237 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3238 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3239 }
3240 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3241 "setProvisioningStatusForCapability");
3242 final long identity = Binder.clearCallingIdentity();
3243 try {
3244 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3245 Phone phone = getPhone(subId);
3246 if (phone == null) {
3247 loge("setImsProvisioningStatusForCapability: phone instance null for subid "
3248 + subId);
3249 return;
3250 }
3251 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3252 return;
3253 }
3254
3255 // this capability requires provisioning, route to the correct API.
3256 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3257 switch (capability) {
3258 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3259 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3260 ims.setVolteProvisioned(isProvisioned);
3261 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3262 ims.setWfcProvisioned(isProvisioned);
3263 }
3264 break;
3265 }
3266 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3267 // There is currently no difference in VT provisioning type.
3268 ims.setVtProvisioned(isProvisioned);
3269 break;
3270 }
3271 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3272 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3273 // change the capability of the feature instead if needed.
3274 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3275 == isProvisioned) {
3276 // No change in provisioning.
3277 return;
3278 }
3279 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3280 try {
3281 ims.changeMmTelCapability(capability, tech, isProvisioned);
3282 } catch (ImsException e) {
3283 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3284 + ", Exception" + e.getMessage());
3285 }
3286 break;
3287 }
3288 default: {
3289 throw new IllegalArgumentException("Tried to set provisioning for capability '"
3290 + capability + "', which does not require provisioning.");
3291 }
3292 }
3293
3294 } finally {
3295 Binder.restoreCallingIdentity(identity);
3296 }
3297 }
3298
3299 @Override
3300 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3301 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3302 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3303 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3304 }
3305 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3306 final long identity = Binder.clearCallingIdentity();
3307 try {
3308 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3309 Phone phone = getPhone(subId);
3310 if (phone == null) {
3311 loge("getImsProvisioningStatusForCapability: phone instance null for subid "
3312 + subId);
3313 // We will fail with "true" as the provisioning status because this is the default
3314 // if we do not require provisioning.
3315 return true;
3316 }
3317
3318 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3319 return true;
3320 }
3321
3322 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3323 switch (capability) {
3324 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3325 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3326 return ims.isVolteProvisionedOnDevice();
3327 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3328 return ims.isWfcProvisionedOnDevice();
3329 }
3330 // This should never happen, since we are checking tech above to make sure it
3331 // is either LTE or IWLAN.
3332 throw new IllegalArgumentException("Invalid radio technology for voice "
3333 + "capability.");
3334 }
3335 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3336 // There is currently no difference in VT provisioning type.
3337 return ims.isVtProvisionedOnDevice();
3338 }
3339 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3340 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3341 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3342 }
3343 default: {
3344 throw new IllegalArgumentException("Tried to get provisioning for capability '"
3345 + capability + "', which does not require provisioning.");
3346 }
3347 }
3348
3349 } finally {
3350 Binder.restoreCallingIdentity(identity);
3351 }
3352 }
3353
3354 @Override
3355 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3356 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3357 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3358 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3359 }
3360 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3361 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3362 return (provisionedBits & capability) > 0;
3363 }
3364
3365 @Override
3366 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3367 boolean isProvisioned) {
3368 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3369 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3370 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3371 }
3372 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3373 "setProvisioningStatusForCapability");
3374 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3375 // If the current provisioning status for capability already matches isProvisioned,
3376 // do nothing.
3377 if (((provisionedBits & capability) > 0) == isProvisioned) {
3378 return;
3379 }
3380 if (isProvisioned) {
3381 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3382 } else {
3383 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3384 }
3385 }
3386
3387 /**
3388 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3389 * technology. The bitfield should mirror the bitfield defined by
3390 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3391 */
3392 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3393 String key = getMmTelProvisioningKey(subId, tech);
3394 // Default is no capabilities are provisioned.
3395 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3396 }
3397
3398 /**
3399 * Sets the MmTel capability provisioning bitfield (defined by
3400 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3401 * technology specified.
3402 *
3403 * Note: This is a synchronous command and should not be called on UI thread.
3404 */
3405 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3406 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3407 String key = getMmTelProvisioningKey(subId, tech);
3408 editor.putInt(key, newField);
3409 editor.commit();
3410 }
3411
3412 private static String getMmTelProvisioningKey(int subId, int tech) {
3413 // resulting key is provision_ims_mmtel_{subId}_{tech}
3414 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3415 }
3416
3417 /**
3418 * Query CarrierConfig to see if the specified capability requires provisioning for the
3419 * carrier associated with the subscription id.
3420 */
3421 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3422 int capability) {
3423 CarrierConfigManager configManager = new CarrierConfigManager(context);
3424 PersistableBundle c = configManager.getConfigForSubId(subId);
3425 boolean requireUtProvisioning = c.getBoolean(
3426 // By default, this config is true (even if there is no SIM). We also check to make
3427 // sure the subscription needs provisioning here, so we do not need to check for
3428 // the no-SIM case, where we would normally shortcut this to false.
3429 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, true)
3430 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3431 false);
3432 boolean requireVoiceVtProvisioning = c.getBoolean(
3433 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3434
3435 // First check to make sure that the capability requires provisioning.
3436 switch (capability) {
3437 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3438 // intentional fallthrough
3439 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3440 if (requireVoiceVtProvisioning) {
3441 // Voice and Video requires provisioning
3442 return true;
3443 }
3444 break;
3445 }
3446 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3447 if (requireUtProvisioning) {
3448 // UT requires provisioning
3449 return true;
3450 }
3451 break;
3452 }
3453 }
3454 return false;
3455 }
3456
3457 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003458 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003459 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3460 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3461 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003462 enforceReadPrivilegedPermission("getImsProvisioningInt");
3463 final long identity = Binder.clearCallingIdentity();
3464 try {
3465 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003466 int slotId = getSlotIndex(subId);
3467 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3468 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3469 + subId + "' for key:" + key);
3470 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3471 }
3472 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003473 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003474 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3475 + subId + "' for key:" + key);
3476 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003477 } finally {
3478 Binder.restoreCallingIdentity(identity);
3479 }
3480 }
3481
3482 @Override
3483 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003484 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3485 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3486 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003487 enforceReadPrivilegedPermission("getImsProvisioningString");
3488 final long identity = Binder.clearCallingIdentity();
3489 try {
3490 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003491 int slotId = getSlotIndex(subId);
3492 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3493 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3494 + subId + "' for key:" + key);
3495 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3496 }
3497 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003498 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003499 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3500 + subId + "' for key:" + key);
3501 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003502 } finally {
3503 Binder.restoreCallingIdentity(identity);
3504 }
3505 }
3506
3507 @Override
3508 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003509 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3510 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3511 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003512 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3513 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003514 final long identity = Binder.clearCallingIdentity();
3515 try {
3516 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003517 int slotId = getSlotIndex(subId);
3518 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3519 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3520 + subId + "' for key:" + key);
3521 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3522 }
3523 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003524 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003525 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3526 + "' for key:" + key);
3527 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003528 } finally {
3529 Binder.restoreCallingIdentity(identity);
3530 }
3531 }
3532
3533 @Override
3534 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003535 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3536 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3537 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003538 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3539 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003540 final long identity = Binder.clearCallingIdentity();
3541 try {
3542 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003543 int slotId = getSlotIndex(subId);
3544 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3545 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3546 + subId + "' for key:" + key);
3547 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3548 }
3549 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003550 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003551 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3552 + "' for key:" + key);
3553 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003554 } finally {
3555 Binder.restoreCallingIdentity(identity);
3556 }
3557 }
3558
Brad Ebinger35c841c2018-10-01 10:40:55 -07003559 private int getSlotIndexOrException(int subId) throws IllegalArgumentException {
3560 int slotId = SubscriptionManager.getSlotIndex(subId);
3561 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003562 throw new IllegalArgumentException("Invalid Subscription Id, subId=" + subId);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003563 }
3564 return slotId;
3565 }
3566
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003567 private int getSlotIndex(int subId) {
3568 int slotId = SubscriptionManager.getSlotIndex(subId);
3569 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3570 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3571 }
3572 return slotId;
3573 }
3574
Wink Saville36469e72014-06-11 15:17:00 -07003575 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07003576 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07003577 */
3578 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003579 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Nathan Harold9042f0b2019-05-21 15:51:27 -07003580 if (getTargetSdk(callingPackage) >= android.os.Build.VERSION_CODES.Q
3581 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
3582 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003583 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3584 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003585
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003586 final long identity = Binder.clearCallingIdentity();
3587 try {
3588 final Phone phone = getPhone(subId);
3589 if (phone != null) {
3590 return phone.getServiceState().getDataNetworkType();
3591 } else {
3592 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3593 }
3594 } finally {
3595 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003596 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003597 }
3598
3599 /**
3600 * Returns the data network type
3601 */
3602 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003603 public int getDataNetworkType(String callingPackage) {
3604 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003605 }
3606
3607 /**
3608 * Returns the data network type for a subId
3609 */
3610 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003611 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003612 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003613 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003614 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3615 }
3616
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003617 final long identity = Binder.clearCallingIdentity();
3618 try {
3619 final Phone phone = getPhone(subId);
3620 if (phone != null) {
3621 return phone.getServiceState().getDataNetworkType();
3622 } else {
3623 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3624 }
3625 } finally {
3626 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003627 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003628 }
3629
3630 /**
Wink Saville36469e72014-06-11 15:17:00 -07003631 * Returns the Voice network type for a subId
3632 */
3633 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003634 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003635 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003636 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003637 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3638 }
3639
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003640 final long identity = Binder.clearCallingIdentity();
3641 try {
3642 final Phone phone = getPhone(subId);
3643 if (phone != null) {
3644 return phone.getServiceState().getVoiceNetworkType();
3645 } else {
3646 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3647 }
3648 } finally {
3649 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003650 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003651 }
3652
3653 /**
3654 * @return true if a ICC card is present
3655 */
3656 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07003657 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003658 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
3659 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07003660 }
3661
3662 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003663 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07003664 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003665 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003666 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003667 final long identity = Binder.clearCallingIdentity();
3668 try {
3669 final Phone phone = PhoneFactory.getPhone(slotIndex);
3670 if (phone != null) {
3671 return phone.getIccCard().hasIccCard();
3672 } else {
3673 return false;
3674 }
3675 } finally {
3676 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08003677 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003678 }
3679
3680 /**
3681 * Return if the current radio is LTE on CDMA. This
3682 * is a tri-state return value as for a period of time
3683 * the mode may be unknown.
3684 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003685 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003686 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08003687 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003688 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003689 @Override
3690 public int getLteOnCdmaMode(String callingPackage) {
3691 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003692 }
3693
Sanket Padawe356d7632015-06-22 14:03:32 -07003694 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003695 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003696 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003697 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003698 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3699 }
3700
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003701 final long identity = Binder.clearCallingIdentity();
3702 try {
3703 final Phone phone = getPhone(subId);
3704 if (phone == null) {
3705 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3706 } else {
3707 return phone.getLteOnCdmaMode();
3708 }
3709 } finally {
3710 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003711 }
Wink Saville36469e72014-06-11 15:17:00 -07003712 }
3713
Wink Saville36469e72014-06-11 15:17:00 -07003714 /**
3715 * {@hide}
3716 * Returns Default subId, 0 in the case of single standby.
3717 */
Wink Savilleb564aae2014-10-23 10:18:09 -07003718 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003719 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07003720 }
3721
Shishir Agrawala9f32182016-04-12 12:00:16 -07003722 private int getSlotForDefaultSubscription() {
3723 return mSubscriptionController.getPhoneId(getDefaultSubscription());
3724 }
3725
Wink Savilleb564aae2014-10-23 10:18:09 -07003726 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003727 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003728 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003729
Pengquan Menge92a50d2018-09-21 15:54:48 -07003730 private boolean isActiveSubscription(int subId) {
3731 return mSubscriptionController.isActiveSubId(subId);
3732 }
3733
Ihab Awadf2177b72013-11-25 13:33:23 -08003734 /**
3735 * @see android.telephony.TelephonyManager.WifiCallingChoices
3736 */
3737 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003738 final long identity = Binder.clearCallingIdentity();
3739 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003740 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003741 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
3742 getWhenToMakeWifiCallsDefaultPreference());
3743 } finally {
3744 Binder.restoreCallingIdentity(identity);
3745 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003746 }
3747
3748 /**
3749 * @see android.telephony.TelephonyManager.WifiCallingChoices
3750 */
3751 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003752 final long identity = Binder.clearCallingIdentity();
3753 try {
3754 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003755 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003756 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
3757 } finally {
3758 Binder.restoreCallingIdentity(identity);
3759 }
Ihab Awadf9e92732013-12-05 18:02:52 -08003760 }
3761
Sailesh Nepald1e68152013-12-12 19:08:02 -08003762 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07003763 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08003764 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08003765 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08003766
Jordan Liu4c733742019-02-28 12:03:40 -08003767 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
3768 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
3769 if (phoneId == -1) {
3770 throw new IllegalArgumentException("Given slot index: " + slotIndex
3771 + " does not correspond to an active phone");
3772 }
3773 return PhoneFactory.getPhone(phoneId);
3774 }
3775
Shishir Agrawal566b7612013-10-28 14:41:00 -07003776 @Override
Derek Tan740e1672017-06-27 14:56:27 -07003777 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
3778 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003779 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3780 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003781 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003782 if (DBG) {
3783 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
3784 }
3785 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
3786 p2);
3787 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003788
Jordan Liu4c733742019-02-28 12:03:40 -08003789
3790 @Override
3791 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
3792 int slotIndex, String callingPackage, String aid, int p2) {
3793 enforceModifyPermission();
3794 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3795 if (DBG) {
3796 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
3797 }
3798 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3799 callingPackage, aid, p2);
3800 }
3801
3802 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
3803 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003804 final long identity = Binder.clearCallingIdentity();
3805 try {
3806 if (TextUtils.equals(ISDR_AID, aid)) {
3807 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003808 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3809 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003810 if (bestComponent == null
3811 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3812 loge("The calling package is not allowed to access ISD-R.");
3813 throw new SecurityException(
3814 "The calling package is not allowed to access ISD-R.");
3815 }
Derek Tan740e1672017-06-27 14:56:27 -07003816 }
Derek Tan740e1672017-06-27 14:56:27 -07003817
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003818 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08003819 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
3820 null /* workSource */);
3821 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003822 return response;
3823 } finally {
3824 Binder.restoreCallingIdentity(identity);
3825 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003826 }
3827
3828 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003829 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003830 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3831 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003832 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
3833 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
3834 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003835
Jordan Liu4c733742019-02-28 12:03:40 -08003836 @Override
3837 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
3838 enforceModifyPermission();
3839 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
3840 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3841 channel);
3842 }
3843
3844 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003845 final long identity = Binder.clearCallingIdentity();
3846 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003847 if (channel < 0) {
3848 return false;
3849 }
Jordan Liu4c733742019-02-28 12:03:40 -08003850 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
3851 null /* workSource */);
3852 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003853 return success;
3854 } finally {
3855 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003856 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003857 }
3858
3859 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003860 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07003861 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003862 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3863 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003864 if (DBG) {
3865 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
3866 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3867 + p3 + " data=" + data);
3868 }
3869 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
3870 command, p1, p2, p3, data);
3871 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003872
Jordan Liu4c733742019-02-28 12:03:40 -08003873 @Override
3874 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
3875 int command, int p1, int p2, int p3, String data) {
3876 enforceModifyPermission();
3877 if (DBG) {
3878 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
3879 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3880 + p3 + " data=" + data);
3881 }
3882 return iccTransmitApduLogicalChannelWithPermission(
3883 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
3884 data);
3885 }
3886
3887 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
3888 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003889 final long identity = Binder.clearCallingIdentity();
3890 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07003891 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003892 return "";
3893 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003894
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003895 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003896 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
3897 null /* workSource */);
3898 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003899
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003900 // Append the returned status code to the end of the response payload.
3901 String s = Integer.toHexString(
3902 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3903 if (response.payload != null) {
3904 s = IccUtils.bytesToHexString(response.payload) + s;
3905 }
3906 return s;
3907 } finally {
3908 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003909 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003910 }
Jake Hambye994d462014-02-03 13:10:13 -08003911
Evan Charltonc66da362014-05-16 14:06:40 -07003912 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003913 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
3914 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003915 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3916 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003917 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003918 if (DBG) {
3919 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
3920 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
3921 }
3922 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
3923 cla, command, p1, p2, p3, data);
3924 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003925
Jordan Liu4c733742019-02-28 12:03:40 -08003926 @Override
3927 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
3928 int command, int p1, int p2, int p3, String data) {
3929 enforceModifyPermission();
3930 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3931 if (DBG) {
3932 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
3933 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
3934 + " data=" + data);
3935 }
3936
3937 return iccTransmitApduBasicChannelWithPermission(
3938 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
3939 p2, p3, data);
3940 }
3941
3942 // open APDU basic channel assuming the caller has sufficient permissions
3943 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
3944 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003945 final long identity = Binder.clearCallingIdentity();
3946 try {
3947 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
3948 && TextUtils.equals(ISDR_AID, data)) {
3949 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003950 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3951 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003952 if (bestComponent == null
3953 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3954 loge("The calling package is not allowed to select ISD-R.");
3955 throw new SecurityException(
3956 "The calling package is not allowed to select ISD-R.");
3957 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003958 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003959
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003960 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003961 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
3962 null /* workSource */);
3963 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003964
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003965 // Append the returned status code to the end of the response payload.
3966 String s = Integer.toHexString(
3967 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3968 if (response.payload != null) {
3969 s = IccUtils.bytesToHexString(response.payload) + s;
3970 }
3971 return s;
3972 } finally {
3973 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003974 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003975 }
3976
3977 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003978 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003979 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003980 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3981 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003982
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003983 final long identity = Binder.clearCallingIdentity();
3984 try {
3985 if (DBG) {
3986 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
3987 + p1 + " " + p2 + " " + p3 + ":" + filePath);
3988 }
3989
3990 IccIoResult response =
3991 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
3992 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
3993 subId);
3994
3995 if (DBG) {
3996 log("Exchange SIM_IO [R]" + response);
3997 }
3998
3999 byte[] result = null;
4000 int length = 2;
4001 if (response.payload != null) {
4002 length = 2 + response.payload.length;
4003 result = new byte[length];
4004 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
4005 } else {
4006 result = new byte[length];
4007 }
4008
4009 result[length - 1] = (byte) response.sw2;
4010 result[length - 2] = (byte) response.sw1;
4011 return result;
4012 } finally {
4013 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004014 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004015 }
4016
Nathan Haroldb3014052017-01-25 15:57:32 -08004017 /**
4018 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4019 * on a particular subscription
4020 */
sqianb6e41952018-03-12 14:54:01 -07004021 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
4022 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4023 mApp, subId, callingPackage, "getForbiddenPlmns")) {
4024 return null;
4025 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004026
4027 final long identity = Binder.clearCallingIdentity();
4028 try {
4029 if (appType != TelephonyManager.APPTYPE_USIM
4030 && appType != TelephonyManager.APPTYPE_SIM) {
4031 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4032 return null;
4033 }
4034 Object response = sendRequest(
4035 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4036 if (response instanceof String[]) {
4037 return (String[]) response;
4038 }
4039 // Response is an Exception of some kind,
4040 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004041 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004042 } finally {
4043 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004044 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004045 }
4046
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004047 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004048 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004049 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4050 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004051
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004052 final long identity = Binder.clearCallingIdentity();
4053 try {
4054 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4055 if (response.payload == null) {
4056 return "";
4057 }
Evan Charltonc66da362014-05-16 14:06:40 -07004058
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004059 // Append the returned status code to the end of the response payload.
4060 String s = Integer.toHexString(
4061 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4062 s = IccUtils.bytesToHexString(response.payload) + s;
4063 return s;
4064 } finally {
4065 Binder.restoreCallingIdentity(identity);
4066 }
Evan Charltonc66da362014-05-16 14:06:40 -07004067 }
4068
Jake Hambye994d462014-02-03 13:10:13 -08004069 /**
4070 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4071 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4072 *
4073 * @param itemID the ID of the item to read
4074 * @return the NV item as a String, or null on error.
4075 */
4076 @Override
4077 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004078 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004079 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4080 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004081
4082 final long identity = Binder.clearCallingIdentity();
4083 try {
4084 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004085 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004086 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4087 return value;
4088 } finally {
4089 Binder.restoreCallingIdentity(identity);
4090 }
Jake Hambye994d462014-02-03 13:10:13 -08004091 }
4092
4093 /**
4094 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4095 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4096 *
4097 * @param itemID the ID of the item to read
4098 * @param itemValue the value to write, as a String
4099 * @return true on success; false on any failure
4100 */
4101 @Override
4102 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004103 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004104 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4105 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004106
4107 final long identity = Binder.clearCallingIdentity();
4108 try {
4109 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4110 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004111 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004112 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4113 return success;
4114 } finally {
4115 Binder.restoreCallingIdentity(identity);
4116 }
Jake Hambye994d462014-02-03 13:10:13 -08004117 }
4118
4119 /**
4120 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4121 * Used for device configuration by some CDMA operators.
4122 *
4123 * @param preferredRoamingList byte array containing the new PRL
4124 * @return true on success; false on any failure
4125 */
4126 @Override
4127 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004128 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4129 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004130
4131 final long identity = Binder.clearCallingIdentity();
4132 try {
4133 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4134 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4135 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4136 return success;
4137 } finally {
4138 Binder.restoreCallingIdentity(identity);
4139 }
Jake Hambye994d462014-02-03 13:10:13 -08004140 }
4141
4142 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004143 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004144 * Used for device configuration by some CDMA operators.
4145 *
chen xu6dac5ab2018-10-26 17:39:23 -07004146 * @param slotIndex - device slot.
4147 *
Jake Hambye994d462014-02-03 13:10:13 -08004148 * @return true on success; false on any failure
4149 */
4150 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004151 public boolean resetModemConfig(int slotIndex) {
4152 Phone phone = PhoneFactory.getPhone(slotIndex);
4153 if (phone != null) {
4154 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4155 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004156
chen xu6dac5ab2018-10-26 17:39:23 -07004157 final long identity = Binder.clearCallingIdentity();
4158 try {
4159 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4160 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4161 return success;
4162 } finally {
4163 Binder.restoreCallingIdentity(identity);
4164 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004165 }
chen xu6dac5ab2018-10-26 17:39:23 -07004166 return false;
4167 }
4168
4169 /**
4170 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4171 *
4172 * @param slotIndex - device slot.
4173 *
4174 * @return true on success; false on any failure
4175 */
4176 @Override
4177 public boolean rebootModem(int slotIndex) {
4178 Phone phone = PhoneFactory.getPhone(slotIndex);
4179 if (phone != null) {
4180 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4181 mApp, phone.getSubId(), "rebootModem");
4182
4183 final long identity = Binder.clearCallingIdentity();
4184 try {
4185 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4186 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4187 return success;
4188 } finally {
4189 Binder.restoreCallingIdentity(identity);
4190 }
4191 }
4192 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004193 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004194
Svet Ganovb320e182015-04-16 12:30:10 -07004195 public String[] getPcscfAddress(String apnType, String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004196 final Phone defaultPhone = getDefaultPhone();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004197 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004198 mApp, defaultPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004199 return new String[0];
4200 }
4201
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004202 final long identity = Binder.clearCallingIdentity();
4203 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004204 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004205 } finally {
4206 Binder.restoreCallingIdentity(identity);
4207 }
Wink Saville36469e72014-06-11 15:17:00 -07004208 }
4209
Brad Ebinger51f743a2017-01-23 13:50:20 -08004210 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004211 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4212 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004213 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004214 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004215 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004216
4217 final long identity = Binder.clearCallingIdentity();
4218 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004219 ImsResolver resolver = PhoneFactory.getImsResolver();
4220 if (resolver == null) {
4221 // may happen if the device does not support IMS.
4222 return;
4223 }
4224 resolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004225 } finally {
4226 Binder.restoreCallingIdentity(identity);
4227 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004228 }
4229
4230 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004231 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4232 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004233 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004234 public void disableIms(int slotId) {
4235 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004236
4237 final long identity = Binder.clearCallingIdentity();
4238 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004239 ImsResolver resolver = PhoneFactory.getImsResolver();
4240 if (resolver == null) {
4241 // may happen if the device does not support IMS.
4242 return;
4243 }
4244 resolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004245 } finally {
4246 Binder.restoreCallingIdentity(identity);
4247 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004248 }
4249
4250 /**
4251 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4252 * feature or {@link null} if the service is not available. If the feature is available, the
4253 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4254 */
4255 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004256 IImsServiceFeatureCallback callback) {
4257 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004258
4259 final long identity = Binder.clearCallingIdentity();
4260 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004261 ImsResolver resolver = PhoneFactory.getImsResolver();
4262 if (resolver == null) {
4263 // may happen if the device does not support IMS.
4264 return null;
4265 }
4266 return resolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004267 } finally {
4268 Binder.restoreCallingIdentity(identity);
4269 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004270 }
4271
4272 /**
4273 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4274 * feature during emergency calling or {@link null} if the service is not available. If the
4275 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4276 * listener for feature updates.
4277 */
4278 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4279 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004280
4281 final long identity = Binder.clearCallingIdentity();
4282 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004283 ImsResolver resolver = PhoneFactory.getImsResolver();
4284 if (resolver == null) {
4285 // may happen if the device does not support IMS.
4286 return null;
4287 }
4288 return resolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004289 } finally {
4290 Binder.restoreCallingIdentity(identity);
4291 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004292 }
4293
Brad Ebinger5f64b052017-12-14 14:26:15 -08004294 /**
4295 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004296 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004297 */
4298 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4299 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004300
4301 final long identity = Binder.clearCallingIdentity();
4302 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004303 ImsResolver resolver = PhoneFactory.getImsResolver();
4304 if (resolver == null) {
4305 // may happen if the device does not support IMS.
4306 return null;
4307 }
4308 return resolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004309 } finally {
4310 Binder.restoreCallingIdentity(identity);
4311 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004312 }
4313
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004314 /**
4315 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004316 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004317 */
4318 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4319 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004320
4321 final long identity = Binder.clearCallingIdentity();
4322 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004323 ImsResolver resolver = PhoneFactory.getImsResolver();
4324 if (resolver == null) {
4325 // may happen if the device does not support IMS.
4326 return null;
4327 }
4328 return resolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004329 } finally {
4330 Binder.restoreCallingIdentity(identity);
4331 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004332 }
4333
Brad Ebinger884c07b2018-02-15 16:17:40 -08004334 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004335 * Sets the ImsService Package Name that Telephony will bind to.
4336 *
4337 * @param slotId the slot ID that the ImsService should bind for.
4338 * @param isCarrierImsService true if the ImsService is the carrier override, false if the
4339 * ImsService is the device default ImsService.
4340 * @param packageName The package name of the application that contains the ImsService to bind
4341 * to.
4342 * @return true if setting the ImsService to bind to succeeded, false if it did not.
4343 * @hide
4344 */
4345 public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004346 int[] subIds = SubscriptionManager.getSubId(slotId);
4347 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4348 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4349 "setImsService");
4350
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004351 final long identity = Binder.clearCallingIdentity();
4352 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004353 ImsResolver resolver = PhoneFactory.getImsResolver();
4354 if (resolver == null) {
4355 // may happen if the device does not support IMS.
4356 return false;
4357 }
4358 return resolver.overrideImsServiceConfiguration(slotId, isCarrierImsService,
4359 packageName);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004360 } finally {
4361 Binder.restoreCallingIdentity(identity);
4362 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004363 }
4364
4365 /**
4366 * Return the ImsService configuration.
4367 *
4368 * @param slotId The slot that the ImsService is associated with.
4369 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4370 * the device default.
4371 * @return the package name of the ImsService configuration.
4372 */
4373 public String getImsService(int slotId, boolean isCarrierImsService) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004374 int[] subIds = SubscriptionManager.getSubId(slotId);
4375 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4376 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4377 "getImsService");
4378
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004379 final long identity = Binder.clearCallingIdentity();
4380 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004381 ImsResolver resolver = PhoneFactory.getImsResolver();
4382 if (resolver == null) {
4383 // may happen if the device does not support IMS.
4384 return "";
4385 }
4386 return resolver.getImsServiceConfiguration(slotId, isCarrierImsService);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004387 } finally {
4388 Binder.restoreCallingIdentity(identity);
4389 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004390 }
4391
Wink Saville36469e72014-06-11 15:17:00 -07004392 public void setImsRegistrationState(boolean registered) {
4393 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004394
4395 final long identity = Binder.clearCallingIdentity();
4396 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004397 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004398 } finally {
4399 Binder.restoreCallingIdentity(identity);
4400 }
Wink Saville36469e72014-06-11 15:17:00 -07004401 }
4402
4403 /**
Stuart Scott54788802015-03-30 13:18:01 -07004404 * Set the network selection mode to automatic.
4405 *
4406 */
4407 @Override
4408 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004409 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4410 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004411
Pengquan Menge92a50d2018-09-21 15:54:48 -07004412 if (!isActiveSubscription(subId)) {
4413 return;
4414 }
4415
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004416 final long identity = Binder.clearCallingIdentity();
4417 try {
4418 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4419 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4420 } finally {
4421 Binder.restoreCallingIdentity(identity);
4422 }
Stuart Scott54788802015-03-30 13:18:01 -07004423 }
4424
Pengquan Mengea84e042018-09-20 14:57:26 -07004425 /**
4426 * Ask the radio to connect to the input network and change selection mode to manual.
4427 *
4428 * @param subId the id of the subscription.
4429 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4430 * the operator to attach to.
4431 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4432 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4433 * normal network selection next time.
4434 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004435 */
4436 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07004437 public boolean setNetworkSelectionModeManual(
4438 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004439 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4440 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07004441
4442 if (!isActiveSubscription(subId)) {
4443 return false;
4444 }
4445
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004446 final long identity = Binder.clearCallingIdentity();
4447 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07004448 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004449 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07004450 if (DBG) {
4451 log("setNetworkSelectionModeManual: subId: " + subId
4452 + " operator: " + operatorInfo);
4453 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004454 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4455 } finally {
4456 Binder.restoreCallingIdentity(identity);
4457 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004458 }
4459
4460 /**
4461 * Scans for available networks.
4462 */
4463 @Override
Hall Liuf19c44f2018-11-27 14:38:17 -08004464 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004465 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4466 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08004467 LocationAccessPolicy.LocationPermissionResult locationResult =
4468 LocationAccessPolicy.checkLocationPermission(mApp,
4469 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4470 .setCallingPackage(callingPackage)
4471 .setCallingPid(Binder.getCallingPid())
4472 .setCallingUid(Binder.getCallingUid())
4473 .setMethod("getCellNetworkScanResults")
4474 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4475 .build());
4476 switch (locationResult) {
4477 case DENIED_HARD:
4478 throw new SecurityException("Not allowed to access scan results -- location");
4479 case DENIED_SOFT:
4480 return null;
4481 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004482
Pengquan Menga1bb6272018-09-06 09:59:22 -07004483 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004484 try {
4485 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07004486 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004487 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004488 } finally {
4489 Binder.restoreCallingIdentity(identity);
4490 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004491 }
4492
4493 /**
yinxub1bed742017-04-17 11:45:04 -07004494 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07004495 *
yinxub1bed742017-04-17 11:45:04 -07004496 * @param subId id of the subscription
4497 * @param request contains the radio access networks with bands/channels to scan
4498 * @param messenger callback messenger for scan results or errors
4499 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07004500 * @return the id of the requested scan which can be used to stop the scan.
4501 */
4502 @Override
4503 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Hall Liuf19c44f2018-11-27 14:38:17 -08004504 IBinder binder, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004505 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4506 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08004507 LocationAccessPolicy.LocationPermissionResult locationResult =
4508 LocationAccessPolicy.checkLocationPermission(mApp,
4509 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4510 .setCallingPackage(callingPackage)
4511 .setCallingPid(Binder.getCallingPid())
4512 .setCallingUid(Binder.getCallingUid())
4513 .setMethod("requestNetworkScan")
4514 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4515 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004516 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Hall Liu558027f2019-05-15 19:14:05 -07004517 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request, subId);
Hall Liub2ac8ef2019-02-28 15:56:23 -08004518 if (e != null) {
4519 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
4520 throw e;
4521 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07004522 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004523 return TelephonyScanManager.INVALID_SCAN_ID;
4524 }
4525 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004526 }
Hall Liu912dfd32019-04-25 14:02:26 -07004527 int callingUid = Binder.getCallingUid();
4528 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07004529 final long identity = Binder.clearCallingIdentity();
4530 try {
4531 return mNetworkScanRequestTracker.startNetworkScan(
4532 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07004533 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07004534 } finally {
4535 Binder.restoreCallingIdentity(identity);
4536 }
yinxu504e1392017-04-12 16:03:22 -07004537 }
4538
Hall Liub2ac8ef2019-02-28 15:56:23 -08004539 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Hall Liu558027f2019-05-15 19:14:05 -07004540 NetworkScanRequest request, int subId) {
4541 boolean hasCarrierPriv = getCarrierPrivilegeStatusForUid(subId, Binder.getCallingUid())
4542 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4543 boolean hasNetworkScanPermission =
4544 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
4545 == PERMISSION_GRANTED;
4546
4547 if (!hasCarrierPriv && !hasNetworkScanPermission) {
4548 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
4549 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08004550 }
4551
4552 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
4553 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08004554 if (ras.getChannels() != null && ras.getChannels().length > 0) {
4555 return new SecurityException("Specific channels must not be"
4556 + " scanned without location access.");
4557 }
4558 }
4559 }
4560
Hall Liub2ac8ef2019-02-28 15:56:23 -08004561 return null;
4562 }
4563
yinxu504e1392017-04-12 16:03:22 -07004564 /**
4565 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07004566 *
4567 * @param subId id of the subscription
4568 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07004569 */
4570 @Override
4571 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004572 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4573 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004574
Hall Liu912dfd32019-04-25 14:02:26 -07004575 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004576 final long identity = Binder.clearCallingIdentity();
4577 try {
Hall Liu912dfd32019-04-25 14:02:26 -07004578 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004579 } finally {
4580 Binder.restoreCallingIdentity(identity);
4581 }
yinxu504e1392017-04-12 16:03:22 -07004582 }
4583
4584 /**
Junda Liu84d15a22014-07-02 11:21:04 -07004585 * Get the calculated preferred network type.
4586 * Used for debugging incorrect network type.
4587 *
4588 * @return the preferred network type, defined in RILConstants.java.
4589 */
4590 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004591 public int getCalculatedPreferredNetworkType(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004592 final Phone defaultPhone = getDefaultPhone();
4593 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4594 callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004595 return RILConstants.PREFERRED_NETWORK_MODE;
4596 }
4597
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004598 final long identity = Binder.clearCallingIdentity();
4599 try {
4600 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004601 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004602 } finally {
4603 Binder.restoreCallingIdentity(identity);
4604 }
Junda Liu84d15a22014-07-02 11:21:04 -07004605 }
4606
4607 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08004608 * Get the preferred network type.
4609 * Used for device configuration by some CDMA operators.
4610 *
4611 * @return the preferred network type, defined in RILConstants.java.
4612 */
4613 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004614 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08004615 TelephonyPermissions
4616 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4617 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004618
4619 final long identity = Binder.clearCallingIdentity();
4620 try {
4621 if (DBG) log("getPreferredNetworkType");
4622 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
4623 int networkType = (result != null ? result[0] : -1);
4624 if (DBG) log("getPreferredNetworkType: " + networkType);
4625 return networkType;
4626 } finally {
4627 Binder.restoreCallingIdentity(identity);
4628 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004629 }
4630
4631 /**
4632 * Set the preferred network type.
4633 * Used for device configuration by some CDMA operators.
4634 *
4635 * @param networkType the preferred network type, defined in RILConstants.java.
4636 * @return true on success; false on any failure.
4637 */
4638 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004639 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004640 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4641 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004642
4643 final long identity = Binder.clearCallingIdentity();
4644 try {
4645 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
4646 Boolean success = (Boolean) sendRequest(
4647 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
4648 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
4649 if (success) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004650 Settings.Global.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004651 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
4652 }
4653 return success;
4654 } finally {
4655 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07004656 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004657 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004658
4659 /**
Miaoa84611c2019-03-15 09:21:10 +08004660 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08004661 *
Miaoa84611c2019-03-15 09:21:10 +08004662 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07004663 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08004664 * @hide
4665 */
4666 @Override
Miaoa84611c2019-03-15 09:21:10 +08004667 public boolean getTetherApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004668 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004669 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08004670 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004671 try {
Miaoa84611c2019-03-15 09:21:10 +08004672 if (phone != null) {
4673 return phone.hasMatchedTetherApnSetting();
4674 } else {
4675 return false;
4676 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004677 } finally {
4678 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08004679 }
Junda Liu475951f2014-11-07 16:45:03 -08004680 }
4681
4682 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07004683 * Set mobile data enabled
4684 * Used by the user through settings etc to turn on/off mobile data
4685 *
4686 * @param enable {@code true} turn turn data on, else {@code false}
4687 */
4688 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004689 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004690 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4691 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004692
4693 final long identity = Binder.clearCallingIdentity();
4694 try {
4695 int phoneId = mSubscriptionController.getPhoneId(subId);
4696 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4697 Phone phone = PhoneFactory.getPhone(phoneId);
4698 if (phone != null) {
4699 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yud79fba22018-12-13 11:51:28 -08004700 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004701 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004702 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004703 }
4704 } finally {
4705 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004706 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004707 }
4708
4709 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08004710 * Get the user enabled state of Mobile Data.
4711 *
4712 * TODO: remove and use isUserDataEnabled.
4713 * This can't be removed now because some vendor codes
4714 * calls through ITelephony directly while they should
4715 * use TelephonyManager.
4716 *
4717 * @return true on enabled
4718 */
4719 @Override
4720 public boolean getDataEnabled(int subId) {
4721 return isUserDataEnabled(subId);
4722 }
4723
4724 /**
4725 * Get whether mobile data is enabled per user setting.
4726 *
4727 * There are other factors deciding whether mobile data is actually enabled, but they are
4728 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07004729 *
Jeff Davidsona1920712016-11-18 17:05:56 -08004730 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07004731 *
4732 * @return {@code true} if data is enabled else {@code false}
4733 */
4734 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004735 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07004736 try {
4737 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4738 null);
4739 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004740 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4741 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07004742 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004743
4744 final long identity = Binder.clearCallingIdentity();
4745 try {
4746 int phoneId = mSubscriptionController.getPhoneId(subId);
4747 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4748 Phone phone = PhoneFactory.getPhone(phoneId);
4749 if (phone != null) {
4750 boolean retVal = phone.isUserDataEnabled();
4751 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
4752 return retVal;
4753 } else {
4754 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
4755 return false;
4756 }
4757 } finally {
4758 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08004759 }
4760 }
4761
4762 /**
4763 * Get whether mobile data is enabled.
4764 *
4765 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
4766 * whether mobile data is actually enabled.
4767 *
4768 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
4769 *
4770 * @return {@code true} if data is enabled else {@code false}
4771 */
4772 @Override
4773 public boolean isDataEnabled(int subId) {
4774 try {
4775 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4776 null);
4777 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004778 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4779 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08004780 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004781
4782 final long identity = Binder.clearCallingIdentity();
4783 try {
4784 int phoneId = mSubscriptionController.getPhoneId(subId);
4785 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4786 Phone phone = PhoneFactory.getPhone(phoneId);
4787 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08004788 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004789 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
4790 return retVal;
4791 } else {
4792 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
4793 return false;
4794 }
4795 } finally {
4796 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004797 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004798 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004799
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004800 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim,
4801 Phone phone) {
4802 //load access rules from carrier configs, and check those as well: b/139133814
4803 SubscriptionController subController = SubscriptionController.getInstance();
4804 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4805 || subController == null) return privilegeFromSim;
4806
4807 int uid = Binder.getCallingUid();
4808 PackageManager pkgMgr = phone.getContext().getPackageManager();
4809 String[] packages = pkgMgr.getPackagesForUid(uid);
4810
4811 final long identity = Binder.clearCallingIdentity();
4812 try {
4813 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4814 SubscriptionManager subManager = (SubscriptionManager)
4815 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4816 for (String pkg : packages) {
4817 if (subManager.canManageSubscription(subInfo, pkg)) {
4818 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4819 }
4820 }
4821 return privilegeFromSim;
4822 } finally {
4823 Binder.restoreCallingIdentity(identity);
4824 }
4825 }
4826
4827 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
4828 String pkgName) {
4829 //load access rules from carrier configs, and check those as well: b/139133814
4830 SubscriptionController subController = SubscriptionController.getInstance();
4831 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4832 || subController == null) return privilegeFromSim;
4833
4834 final long identity = Binder.clearCallingIdentity();
4835 try {
4836 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4837 SubscriptionManager subManager = (SubscriptionManager)
4838 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4839 return subManager.canManageSubscription(subInfo, pkgName)
4840 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
4841 } finally {
4842 Binder.restoreCallingIdentity(identity);
4843 }
4844 }
4845
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004846 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004847 public int getCarrierPrivilegeStatus(int subId) {
4848 final Phone phone = getPhone(subId);
4849 if (phone == null) {
4850 loge("getCarrierPrivilegeStatus: Invalid subId");
4851 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4852 }
4853 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004854 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08004855 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004856 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4857 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004858
4859 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4860 card.getCarrierPrivilegeStatusForCurrentTransaction(
4861 phone.getContext().getPackageManager()), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004862 }
Junda Liu29340342014-07-10 15:23:27 -07004863
4864 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08004865 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
4866 final Phone phone = getPhone(subId);
4867 if (phone == null) {
4868 loge("getCarrierPrivilegeStatus: Invalid subId");
4869 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4870 }
4871 UiccProfile profile =
4872 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
4873 if (profile == null) {
4874 loge("getCarrierPrivilegeStatus: No UICC");
4875 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4876 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004877 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4878 profile.getCarrierPrivilegeStatusForUid(
4879 phone.getContext().getPackageManager(), uid), phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08004880 }
4881
4882 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004883 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
4884 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08004885 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07004886 }
4887
4888 int phoneId = SubscriptionManager.getPhoneId(subId);
4889 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004890 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07004891 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004892 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4893 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004894 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4895 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4896 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004897 }
4898
4899 @Override
4900 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08004901 if (TextUtils.isEmpty(pkgName))
4902 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07004903 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4904 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4905 UiccCard card = UiccController.getInstance().getUiccCard(i);
4906 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07004907 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07004908 continue;
4909 }
4910
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004911 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
4912 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4913 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004914 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
4915 break;
4916 }
4917 }
4918
4919 return result;
Junda Liu29340342014-07-10 15:23:27 -07004920 }
Derek Tan89e89d42014-07-08 17:00:10 -07004921
4922 @Override
Junda Liue64de782015-04-16 17:19:16 -07004923 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
4924 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
4925 loge("phoneId " + phoneId + " is not valid.");
4926 return null;
4927 }
4928 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004929 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07004930 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004931 return null ;
4932 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004933 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004934 }
4935
Amith Yamasani6e118872016-02-19 12:53:51 -08004936 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004937 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004938 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08004939 List<String> privilegedPackages = new ArrayList<>();
4940 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07004941 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
4942 // has UICC in that slot.
4943 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004944 if (card.hasCarrierPrivilegeRules()) {
4945 if (packages == null) {
4946 // Only check packages in user 0 for now
4947 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004948 PackageManager.MATCH_DISABLED_COMPONENTS
4949 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09004950 | PackageManager.GET_SIGNING_CERTIFICATES,
4951 UserHandle.USER_SYSTEM);
Amith Yamasani6e118872016-02-19 12:53:51 -08004952 }
4953 for (int p = packages.size() - 1; p >= 0; p--) {
4954 PackageInfo pkgInfo = packages.get(p);
4955 if (pkgInfo != null && pkgInfo.packageName != null
4956 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07004957 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004958 privilegedPackages.add(pkgInfo.packageName);
4959 }
4960 }
4961 }
4962 }
4963 return privilegedPackages;
4964 }
4965
chen xuf7e9fe82019-05-09 19:31:02 -07004966 @Override
4967 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
4968 List<String> privilegedPackages = new ArrayList<>();
4969 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4970 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
4971 }
4972 return privilegedPackages;
4973 }
4974
Wink Savilleb564aae2014-10-23 10:18:09 -07004975 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07004976 final Phone phone = getPhone(subId);
4977 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07004978 if (card == null) {
4979 loge("getIccId: No UICC");
4980 return null;
4981 }
4982 String iccId = card.getIccId();
4983 if (TextUtils.isEmpty(iccId)) {
4984 loge("getIccId: ICC ID is null or empty.");
4985 return null;
4986 }
4987 return iccId;
4988 }
4989
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004990 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08004991 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
4992 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004993 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4994 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07004995
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004996 final long identity = Binder.clearCallingIdentity();
4997 try {
4998 final String iccId = getIccId(subId);
4999 final Phone phone = getPhone(subId);
5000 if (phone == null) {
5001 return false;
5002 }
5003 final String subscriberId = phone.getSubscriberId();
5004
5005 if (DBG_MERGE) {
5006 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
5007 + subscriberId + " to " + number);
5008 }
5009
5010 if (TextUtils.isEmpty(iccId)) {
5011 return false;
5012 }
5013
5014 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
5015
5016 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5017 if (alphaTag == null) {
5018 editor.remove(alphaTagPrefKey);
5019 } else {
5020 editor.putString(alphaTagPrefKey, alphaTag);
5021 }
5022
5023 // Record both the line number and IMSI for this ICCID, since we need to
5024 // track all merged IMSIs based on line number
5025 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5026 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5027 if (number == null) {
5028 editor.remove(numberPrefKey);
5029 editor.remove(subscriberPrefKey);
5030 } else {
5031 editor.putString(numberPrefKey, number);
5032 editor.putString(subscriberPrefKey, subscriberId);
5033 }
5034
5035 editor.commit();
5036 return true;
5037 } finally {
5038 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005039 }
Derek Tan7226c842014-07-02 17:42:23 -07005040 }
5041
5042 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005043 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07005044 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005045 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08005046 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08005047 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07005048 return null;
5049 }
Derek Tan97ebb422014-09-05 16:55:38 -07005050
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005051 final long identity = Binder.clearCallingIdentity();
5052 try {
5053 String iccId = getIccId(subId);
5054 if (iccId != null) {
5055 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5056 if (DBG_MERGE) {
5057 log("getLine1NumberForDisplay returning "
5058 + mTelephonySharedPreferences.getString(numberPrefKey, null));
5059 }
5060 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08005061 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005062 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
5063 return null;
5064 } finally {
5065 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005066 }
Derek Tan7226c842014-07-02 17:42:23 -07005067 }
5068
5069 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005070 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005071 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005072 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005073 return null;
5074 }
Derek Tan97ebb422014-09-05 16:55:38 -07005075
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005076 final long identity = Binder.clearCallingIdentity();
5077 try {
5078 String iccId = getIccId(subId);
5079 if (iccId != null) {
5080 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5081 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
5082 }
5083 return null;
5084 } finally {
5085 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005086 }
Derek Tan7226c842014-07-02 17:42:23 -07005087 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005088
5089 @Override
Jordan Liub49b04b2019-05-06 14:45:15 -07005090 public String[] getMergedSubscriberIds(int subId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005091 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
5092 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005093 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005094 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
5095 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005096 return null;
5097 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005098
Jordan Liub49b04b2019-05-06 14:45:15 -07005099 // Clear calling identity, when calling TelephonyManager, because callerUid must be
5100 // the process, where TelephonyManager was instantiated.
5101 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005102 final long identity = Binder.clearCallingIdentity();
5103 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005104 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005105 final TelephonyManager tele = TelephonyManager.from(context);
5106 final SubscriptionManager sub = SubscriptionManager.from(context);
5107
5108 // Figure out what subscribers are currently active
5109 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005110
Jordan Liub49b04b2019-05-06 14:45:15 -07005111 // Only consider subs which match the current subId
5112 // This logic can be simplified. See b/131189269 for progress.
5113 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005114 activeSubscriberIds.add(tele.getSubscriberId(subId));
5115 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005116
5117 // First pass, find a number override for an active subscriber
5118 String mergeNumber = null;
5119 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
5120 for (String key : prefs.keySet()) {
5121 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
5122 final String subscriberId = (String) prefs.get(key);
5123 if (activeSubscriberIds.contains(subscriberId)) {
5124 final String iccId = key.substring(
5125 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
5126 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5127 mergeNumber = (String) prefs.get(numberKey);
5128 if (DBG_MERGE) {
5129 Slog.d(LOG_TAG, "Found line number " + mergeNumber
5130 + " for active subscriber " + subscriberId);
5131 }
5132 if (!TextUtils.isEmpty(mergeNumber)) {
5133 break;
5134 }
5135 }
5136 }
5137 }
5138
5139 // Shortcut when no active merged subscribers
5140 if (TextUtils.isEmpty(mergeNumber)) {
5141 return null;
5142 }
5143
5144 // Second pass, find all subscribers under that line override
5145 final ArraySet<String> result = new ArraySet<>();
5146 for (String key : prefs.keySet()) {
5147 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5148 final String number = (String) prefs.get(key);
5149 if (mergeNumber.equals(number)) {
5150 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5151 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5152 final String subscriberId = (String) prefs.get(subscriberKey);
5153 if (!TextUtils.isEmpty(subscriberId)) {
5154 result.add(subscriberId);
5155 }
5156 }
5157 }
5158 }
5159
5160 final String[] resultArray = result.toArray(new String[result.size()]);
5161 Arrays.sort(resultArray);
5162 if (DBG_MERGE) {
5163 Slog.d(LOG_TAG,
5164 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5165 }
5166 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005167 } finally {
5168 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005169 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005170 }
5171
5172 @Override
Malcolm Chen6ca97372019-07-01 16:28:21 -07005173 public String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage) {
5174 enforceReadPrivilegedPermission("getMergedSubscriberIdsFromGroup");
5175
5176 final long identity = Binder.clearCallingIdentity();
5177 try {
5178 final TelephonyManager telephonyManager = mApp.getSystemService(
5179 TelephonyManager.class);
5180 String subscriberId = telephonyManager.getSubscriberId(subId);
5181 if (subscriberId == null) {
5182 if (DBG) {
5183 log("getMergedSubscriberIdsFromGroup can't find subscriberId for subId "
5184 + subId);
5185 }
5186 return null;
5187 }
5188
5189 final SubscriptionInfo info = SubscriptionController.getInstance()
5190 .getSubscriptionInfo(subId);
5191 final ParcelUuid groupUuid = info.getGroupUuid();
5192 // If it doesn't belong to any group, return just subscriberId of itself.
5193 if (groupUuid == null) {
5194 return new String[]{subscriberId};
5195 }
5196
5197 // Get all subscriberIds from the group.
5198 final List<String> mergedSubscriberIds = new ArrayList<>();
5199 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
5200 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName());
5201 for (SubscriptionInfo subInfo : groupInfos) {
5202 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
5203 if (subscriberId != null) {
5204 mergedSubscriberIds.add(subscriberId);
5205 }
5206 }
5207
5208 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
5209 } finally {
5210 Binder.restoreCallingIdentity(identity);
5211
5212 }
5213 }
5214
5215 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005216 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005217 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5218 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005219
5220 final long identity = Binder.clearCallingIdentity();
5221 try {
5222 final Phone phone = getPhone(subId);
5223 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5224 } finally {
5225 Binder.restoreCallingIdentity(identity);
5226 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005227 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005228
5229 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005230 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005231 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5232 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005233 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005234
5235 final long identity = Binder.clearCallingIdentity();
5236 try {
5237 final Phone phone = getPhone(subId);
5238 if (phone == null) {
5239 return false;
5240 }
5241 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5242 cdmaNonRoamingList);
5243 } finally {
5244 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005245 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005246 }
5247
5248 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005249 @Deprecated
5250 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5251 enforceModifyPermission();
5252
5253 int returnValue = 0;
5254 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07005255 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005256 if(result.exception == null) {
5257 if (result.result != null) {
5258 byte[] responseData = (byte[])(result.result);
5259 if(responseData.length > oemResp.length) {
5260 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5261 responseData.length + "bytes. Buffer Size is " +
5262 oemResp.length + "bytes.");
5263 }
5264 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5265 returnValue = responseData.length;
5266 }
5267 } else {
5268 CommandException ex = (CommandException) result.exception;
5269 returnValue = ex.getCommandError().ordinal();
5270 if(returnValue > 0) returnValue *= -1;
5271 }
5272 } catch (RuntimeException e) {
5273 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5274 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5275 if(returnValue > 0) returnValue *= -1;
5276 }
5277
5278 return returnValue;
5279 }
5280
5281 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005282 public void setRadioCapability(RadioAccessFamily[] rafs) {
5283 try {
5284 ProxyController.getInstance().setRadioCapability(rafs);
5285 } catch (RuntimeException e) {
5286 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5287 }
5288 }
5289
5290 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005291 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005292 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07005293 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005294 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07005295 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005296 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005297 final long identity = Binder.clearCallingIdentity();
5298 try {
chen xub97461a2018-10-26 14:17:57 -07005299 TelephonyPermissions
5300 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5301 mApp, phone.getSubId(), "getRadioAccessFamily");
5302 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005303 } finally {
5304 Binder.restoreCallingIdentity(identity);
5305 }
chen xub97461a2018-10-26 14:17:57 -07005306 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005307 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005308
5309 @Override
5310 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005311 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005312 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005313
5314 final long identity = Binder.clearCallingIdentity();
5315 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005316 ImsManager.getInstance(defaultPhone.getContext(),
5317 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005318 } finally {
5319 Binder.restoreCallingIdentity(identity);
5320 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005321 }
5322
5323 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005324 public boolean isVideoCallingEnabled(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005325 final Phone defaultPhone = getDefaultPhone();
Amit Mahajan578e53d2018-03-20 16:18:38 +00005326 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005327 mApp, defaultPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005328 return false;
5329 }
Svet Ganovb320e182015-04-16 12:30:10 -07005330
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005331 final long identity = Binder.clearCallingIdentity();
5332 try {
5333 // Check the user preference and the system-level IMS setting. Even if the user has
5334 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5335 // In the long run, we may instead need to check if there exists a connection service
5336 // which can support video calling.
5337 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005338 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005339 return imsManager.isVtEnabledByPlatform()
5340 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5341 && imsManager.isVtEnabledByUser();
5342 } finally {
5343 Binder.restoreCallingIdentity(identity);
5344 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005345 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005346
Andrew Leea1239f22015-03-02 17:44:07 -08005347 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005348 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
5349 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5350 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5351 return false;
5352 }
5353
5354 final long identity = Binder.clearCallingIdentity();
5355 try {
5356 CarrierConfigManager configManager =
5357 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005358 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005359 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5360 } finally {
5361 Binder.restoreCallingIdentity(identity);
5362 }
Andrew Leea1239f22015-03-02 17:44:07 -08005363 }
5364
5365 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005366 public boolean isWorldPhone(int subId, String callingPackage) {
5367 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5368 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5369 return false;
5370 }
5371
5372 final long identity = Binder.clearCallingIdentity();
5373 try {
5374 CarrierConfigManager configManager =
5375 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005376 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005377 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
5378 } finally {
5379 Binder.restoreCallingIdentity(identity);
5380 }
Andrew Leea1239f22015-03-02 17:44:07 -08005381 }
5382
Andrew Lee9431b832015-03-09 18:46:45 -07005383 @Override
5384 public boolean isTtyModeSupported() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005385 TelecomManager telecomManager = TelecomManager.from(mApp);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08005386 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07005387 }
5388
5389 @Override
5390 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005391 final long identity = Binder.clearCallingIdentity();
5392 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005393 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005394 } finally {
5395 Binder.restoreCallingIdentity(identity);
5396 }
Andrew Lee9431b832015-03-09 18:46:45 -07005397 }
5398
Hall Liuf6668912018-10-31 17:05:23 -07005399 /**
5400 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
5401 * support for the feature and device firmware support.
5402 *
5403 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
5404 */
5405 @Override
5406 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005407 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005408 final Phone phone = getPhone(subscriptionId);
5409 if (phone == null) {
5410 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
5411 return false;
5412 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005413 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005414 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005415 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
5416 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005417 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005418 return isCarrierSupported && isDeviceSupported;
5419 } finally {
5420 Binder.restoreCallingIdentity(identity);
5421 }
Hall Liu98187582018-01-22 19:15:32 -08005422 }
5423
Hall Liuf6668912018-10-31 17:05:23 -07005424 /**
Hall Liuf2daa022019-07-23 18:39:00 -07005425 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
5426 * RTT setting, will return true if the device and carrier both support RTT.
5427 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07005428 */
5429 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005430 final long identity = Binder.clearCallingIdentity();
5431 try {
Hall Liuf2daa022019-07-23 18:39:00 -07005432 boolean isRttSupported = isRttSupported(subscriptionId);
5433 boolean isUserRttSettingOn = Settings.Secure.getInt(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005434 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
Hall Liuf2daa022019-07-23 18:39:00 -07005435 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
5436 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
5437 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005438 } finally {
5439 Binder.restoreCallingIdentity(identity);
5440 }
Hall Liu3ad5f012018-04-06 16:23:39 -07005441 }
5442
Sanket Padawe7310cc72015-01-14 09:53:20 -08005443 /**
5444 * Returns the unique device ID of phone, for example, the IMEI for
5445 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
5446 *
5447 * <p>Requires Permission:
5448 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5449 */
5450 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005451 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005452 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08005453 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005454 return null;
5455 }
Jeff Davidson913390f2018-02-23 17:11:49 -08005456 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07005457 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
5458 callingPackage, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005459 return null;
5460 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005461
5462 final long identity = Binder.clearCallingIdentity();
5463 try {
5464 return phone.getDeviceId();
5465 } finally {
5466 Binder.restoreCallingIdentity(identity);
5467 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08005468 }
5469
Ping Sunc67b7c22016-03-02 19:16:45 +08005470 /**
5471 * {@hide}
5472 * Returns the IMS Registration Status on a particular subid
5473 *
5474 * @param subId
5475 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005476 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08005477 Phone phone = getPhone(subId);
5478 if (phone != null) {
5479 return phone.isImsRegistered();
5480 } else {
5481 return false;
5482 }
5483 }
5484
Santos Cordon7a1885b2015-02-03 11:15:19 -08005485 @Override
5486 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005487 final long identity = Binder.clearCallingIdentity();
5488 try {
5489 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
5490 } finally {
5491 Binder.restoreCallingIdentity(identity);
5492 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08005493 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07005494
Tyler Gunnf70ed162019-04-03 15:28:53 -07005495 @Override
5496 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
5497 final long identity = Binder.clearCallingIdentity();
5498 try {
5499 Phone phone = getPhone(subscriptionId);
5500 if (phone == null) {
5501 return null;
5502 }
5503 return PhoneUtils.makePstnPhoneAccountHandle(phone);
5504 } finally {
5505 Binder.restoreCallingIdentity(identity);
5506 }
5507 }
5508
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005509 /**
5510 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07005511 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005512 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005513 final long identity = Binder.clearCallingIdentity();
5514 try {
5515 Phone phone = getPhone(subId);
5516 if (phone != null) {
5517 return phone.isWifiCallingEnabled();
5518 } else {
5519 return false;
5520 }
5521 } finally {
5522 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005523 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07005524 }
5525
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005526 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005527 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005528 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005529 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005530 final long identity = Binder.clearCallingIdentity();
5531 try {
5532 Phone phone = getPhone(subId);
5533 if (phone != null) {
5534 return phone.isVideoEnabled();
5535 } else {
5536 return false;
5537 }
5538 } finally {
5539 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005540 }
5541 }
5542
5543 /**
5544 * @return the IMS registration technology for the MMTEL feature. Valid return values are
5545 * defined in {@link ImsRegistrationImplBase}.
5546 */
5547 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005548 final long identity = Binder.clearCallingIdentity();
5549 try {
5550 Phone phone = getPhone(subId);
5551 if (phone != null) {
5552 return phone.getImsRegistrationTech();
5553 } else {
5554 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
5555 }
5556 } finally {
5557 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005558 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005559 }
5560
Stuart Scott8eef64f2015-04-08 15:13:54 -07005561 @Override
5562 public void factoryReset(int subId) {
5563 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07005564 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
5565 return;
5566 }
5567
Svet Ganovcc087f82015-05-12 20:35:54 -07005568 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005569
Svet Ganovcc087f82015-05-12 20:35:54 -07005570 try {
Stuart Scott981d8582015-04-21 14:09:50 -07005571 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
5572 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07005573 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07005574 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07005575 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005576 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
5577 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07005578 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005579 // There has been issues when Sms raw table somehow stores orphan
5580 // fragments. They lead to garbled message when new fragments come
5581 // in and combined with those stale ones. In case this happens again,
5582 // user can reset all network settings which will clean up this table.
5583 cleanUpSmsRawTable(getDefaultPhone().getContext());
Naina Nalluri1264a9f2019-09-17 14:10:30 -07005584
5585 // Erase modem config if erase modem on network setting is enabled.
5586 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
5587 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
5588 if (configValue != null && Boolean.parseBoolean(configValue)) {
5589 sendEraseModemConfig(getDefaultPhone());
5590 }
Svet Ganovcc087f82015-05-12 20:35:54 -07005591 } finally {
5592 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07005593 }
5594 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005595
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005596 private void cleanUpSmsRawTable(Context context) {
5597 ContentResolver resolver = context.getContentResolver();
5598 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
5599 resolver.delete(uri, null, null);
5600 }
5601
Narayan Kamath1c496c22015-04-16 14:40:19 +01005602 @Override
chen xu5d3637b2019-01-21 23:31:38 -08005603 public String getSimLocaleForSubscriber(int subId) {
5604 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
5605 final Phone phone = getPhone(subId);
5606 if (phone == null) {
5607 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08005608 return null;
chen xu5d3637b2019-01-21 23:31:38 -08005609 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005610 final long identity = Binder.clearCallingIdentity();
5611 try {
chen xu5d3637b2019-01-21 23:31:38 -08005612 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
5613 phone.getContext().getOpPackageName());
chen xu6291c472019-02-04 12:55:53 -08005614 if (info == null) {
5615 log("getSimLocaleForSubscriber, inactive subId: " + subId);
5616 return null;
5617 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005618 // Try and fetch the locale from the carrier properties or from the SIM language
5619 // preferences (EF-PL and EF-LI)...
5620 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005621 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08005622 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
5623 if (localeFromDefaultSim != null) {
5624 if (!localeFromDefaultSim.getCountry().isEmpty()) {
5625 if (DBG) log("Using locale from subId: " + subId + " locale: "
5626 + localeFromDefaultSim);
5627 return localeFromDefaultSim.toLanguageTag();
5628 } else {
5629 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005630 }
5631 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005632
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005633 // The SIM language preferences only store a language (e.g. fr = French), not an
5634 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
5635 // the SIM and carrier preferences does not include a country we add the country
5636 // determined from the SIM MCC to provide an exact locale.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005637 final Locale mccLocale = MccTable.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005638 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08005639 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005640 return mccLocale.toLanguageTag();
5641 }
5642
5643 if (DBG) log("No locale found - returning null");
5644 return null;
5645 } finally {
5646 Binder.restoreCallingIdentity(identity);
5647 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005648 }
5649
5650 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005651 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005652 }
5653
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005654 /**
5655 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
5656 */
5657 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005658 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005659 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005660
Chenjie Yu1ba97252018-01-11 18:16:20 -08005661 private final ModemActivityInfo mLastModemActivityInfo =
5662 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
5663
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005664 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07005665 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
5666 * representing the state of the modem.
5667 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08005668 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
5669 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07005670 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005671 */
5672 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07005673 public void requestModemActivityInfo(ResultReceiver result) {
5674 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07005675 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005676
5677 final long identity = Binder.clearCallingIdentity();
5678 try {
5679 ModemActivityInfo ret = null;
5680 synchronized (mLastModemActivityInfo) {
5681 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
5682 CMD_GET_MODEM_ACTIVITY_INFO,
vagdeviaf9a5b92018-08-15 16:01:53 -07005683 null, workSource);
Siddharth Rayb8114062018-06-17 15:02:38 -07005684 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005685 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
5686 for (int i = 0; i < mergedTxTimeMs.length; i++) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005687 mergedTxTimeMs[i] = info.getTxTimeMillis()[i]
5688 + mLastModemActivityInfo.getTxTimeMillis()[i];
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005689 }
5690 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005691 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
5692 + mLastModemActivityInfo.getSleepTimeMillis());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005693 mLastModemActivityInfo.setIdleTimeMillis(
5694 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
5695 mLastModemActivityInfo.setTxTimeMillis(mergedTxTimeMs);
5696 mLastModemActivityInfo.setRxTimeMillis(
5697 info.getRxTimeMillis() + mLastModemActivityInfo.getRxTimeMillis());
5698 mLastModemActivityInfo.setEnergyUsed(
5699 info.getEnergyUsed() + mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005700 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005701 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
5702 mLastModemActivityInfo.getSleepTimeMillis(),
5703 mLastModemActivityInfo.getIdleTimeMillis(),
5704 mLastModemActivityInfo.getTxTimeMillis(),
5705 mLastModemActivityInfo.getRxTimeMillis(),
5706 mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005707 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005708 Bundle bundle = new Bundle();
5709 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
5710 result.send(0, bundle);
5711 } finally {
5712 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08005713 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005714 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005715
Siddharth Rayb8114062018-06-17 15:02:38 -07005716 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
5717 // less than total activity duration.
5718 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
5719 if (info == null) {
5720 return false;
5721 }
5722 int activityDurationMs =
5723 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
5724 int totalTxTimeMs = 0;
5725 for (int i = 0; i < info.getTxTimeMillis().length; i++) {
5726 totalTxTimeMs += info.getTxTimeMillis()[i];
5727 }
5728 return (info.isValid()
5729 && (info.getSleepTimeMillis() <= activityDurationMs)
5730 && (info.getIdleTimeMillis() <= activityDurationMs)
5731 && (info.getRxTimeMillis() <= activityDurationMs)
5732 && (totalTxTimeMs <= activityDurationMs));
5733 }
5734
Jack Yu85bd38a2015-11-09 11:34:32 -08005735 /**
5736 * {@hide}
5737 * Returns the service state information on specified subscription.
5738 */
5739 @Override
5740 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005741 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005742 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08005743 return null;
5744 }
5745
Hall Liuf19c44f2018-11-27 14:38:17 -08005746 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
5747 LocationAccessPolicy.checkLocationPermission(mApp,
5748 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5749 .setCallingPackage(callingPackage)
5750 .setCallingPid(Binder.getCallingPid())
5751 .setCallingUid(Binder.getCallingUid())
5752 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005753 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005754 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5755 .build());
5756
5757 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
5758 LocationAccessPolicy.checkLocationPermission(mApp,
5759 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5760 .setCallingPackage(callingPackage)
5761 .setCallingPid(Binder.getCallingPid())
5762 .setCallingUid(Binder.getCallingUid())
5763 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005764 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005765 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5766 .build());
5767 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
5768 boolean hasFinePermission =
5769 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5770 boolean hasCoarsePermission =
5771 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5772
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005773 final long identity = Binder.clearCallingIdentity();
5774 try {
5775 final Phone phone = getPhone(subId);
5776 if (phone == null) {
5777 return null;
5778 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005779
Hall Liuf19c44f2018-11-27 14:38:17 -08005780 ServiceState ss = phone.getServiceState();
5781
5782 // Scrub out the location info in ServiceState depending on what level of access
5783 // the caller has.
5784 if (hasFinePermission) return ss;
5785 if (hasCoarsePermission) return ss.sanitizeLocationInfo(false);
5786 return ss.sanitizeLocationInfo(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005787 } finally {
5788 Binder.restoreCallingIdentity(identity);
5789 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005790 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005791
5792 /**
5793 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
5794 *
5795 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5796 * voicemail ringtone.
5797 * @return The URI for the ringtone to play when receiving a voicemail from a specific
5798 * PhoneAccount.
5799 */
5800 @Override
5801 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005802 final long identity = Binder.clearCallingIdentity();
5803 try {
5804 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5805 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005806 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005807 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005808
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005809 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
5810 } finally {
5811 Binder.restoreCallingIdentity(identity);
5812 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005813 }
5814
5815 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005816 * Sets the per-account voicemail ringtone.
5817 *
5818 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5819 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5820 *
5821 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5822 * voicemail ringtone.
5823 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
5824 * PhoneAccount.
5825 */
5826 @Override
5827 public void setVoicemailRingtoneUri(String callingPackage,
5828 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005829 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005830 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5831 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005832 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005833 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5834 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5835 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005836 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005837
5838 final long identity = Binder.clearCallingIdentity();
5839 try {
5840 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5841 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005842 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005843 }
5844 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
5845 } finally {
5846 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005847 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005848 }
5849
5850 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08005851 * Returns whether vibration is set for voicemail notification in Phone settings.
5852 *
5853 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5854 * voicemail vibration setting.
5855 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
5856 */
5857 @Override
5858 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005859 final long identity = Binder.clearCallingIdentity();
5860 try {
5861 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5862 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005863 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005864 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005865
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005866 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
5867 } finally {
5868 Binder.restoreCallingIdentity(identity);
5869 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005870 }
5871
Youhan Wange64578a2016-05-02 15:32:42 -07005872 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005873 * Sets the per-account voicemail vibration.
5874 *
5875 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5876 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5877 *
5878 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5879 * voicemail vibration setting.
5880 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
5881 * specific PhoneAccount.
5882 */
5883 @Override
5884 public void setVoicemailVibrationEnabled(String callingPackage,
5885 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005886 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005887 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5888 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005889 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005890 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5891 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5892 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005893 }
5894
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005895 final long identity = Binder.clearCallingIdentity();
5896 try {
5897 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5898 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005899 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005900 }
5901 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
5902 } finally {
5903 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005904 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005905 }
5906
5907 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005908 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
5909 *
5910 * @throws SecurityException if the caller does not have the required permission
5911 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005912 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07005913 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07005914 message);
Youhan Wange64578a2016-05-02 15:32:42 -07005915 }
5916
5917 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005918 * Make sure either called from same process as self (phone) or IPC caller has send SMS
5919 * permission.
5920 *
5921 * @throws SecurityException if the caller does not have the required permission
5922 */
5923 private void enforceSendSmsPermission() {
5924 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
5925 }
5926
5927 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005928 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005929 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005930 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005931 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005932 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005933 final long identity = Binder.clearCallingIdentity();
5934 try {
5935 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005936 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005937 if (componentName == null) {
5938 throw new SecurityException(
5939 "Caller not current active visual voicemail package[null]");
5940 }
5941 String vvmPackage = componentName.getPackageName();
5942 if (!callingPackage.equals(vvmPackage)) {
5943 throw new SecurityException("Caller not current active visual voicemail package["
5944 + vvmPackage + "]");
5945 }
5946 } finally {
5947 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005948 }
5949 }
5950
5951 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005952 * Return the application ID for the app type.
5953 *
5954 * @param subId the subscription ID that this request applies to.
5955 * @param appType the uicc app type.
5956 * @return Application ID for specificied app type, or null if no uicc.
5957 */
5958 @Override
5959 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005960 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07005961 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005962
5963 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07005964 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005965 if (phone == null) {
5966 return null;
5967 }
5968 String aid = null;
5969 try {
5970 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
5971 .getApplicationByType(appType).getAid();
5972 } catch (Exception e) {
5973 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
5974 }
5975 return aid;
5976 } finally {
5977 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07005978 }
Youhan Wange64578a2016-05-02 15:32:42 -07005979 }
5980
Youhan Wang4001d252016-05-11 10:29:41 -07005981 /**
5982 * Return the Electronic Serial Number.
5983 *
5984 * @param subId the subscription ID that this request applies to.
5985 * @return ESN or null if error.
5986 */
5987 @Override
5988 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005989 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07005990 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005991
5992 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07005993 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005994 if (phone == null) {
5995 return null;
5996 }
5997 String esn = null;
5998 try {
5999 esn = phone.getEsn();
6000 } catch (Exception e) {
6001 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
6002 }
6003 return esn;
6004 } finally {
6005 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07006006 }
Youhan Wang4001d252016-05-11 10:29:41 -07006007 }
6008
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006009 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07006010 * Return the Preferred Roaming List Version.
6011 *
6012 * @param subId the subscription ID that this request applies to.
6013 * @return PRLVersion or null if error.
6014 */
6015 @Override
6016 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006017 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07006018 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006019
6020 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07006021 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006022 if (phone == null) {
6023 return null;
6024 }
6025 String cdmaPrlVersion = null;
6026 try {
6027 cdmaPrlVersion = phone.getCdmaPrlVersion();
6028 } catch (Exception e) {
6029 Log.e(LOG_TAG, "Not getting PRLVersion", e);
6030 }
6031 return cdmaPrlVersion;
6032 } finally {
6033 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07006034 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07006035 }
6036
6037 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006038 * Get snapshot of Telephony histograms
6039 * @return List of Telephony histograms
6040 * @hide
6041 */
6042 @Override
6043 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006044 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6045 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006046
6047 final long identity = Binder.clearCallingIdentity();
6048 try {
6049 return RIL.getTelephonyRILTimingHistograms();
6050 } finally {
6051 Binder.restoreCallingIdentity(identity);
6052 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006053 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006054
6055 /**
6056 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006057 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
6058 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006059 * Require system privileges. In the future we may add this to carrier APIs.
6060 *
Michele Berionne482f8202018-11-27 18:57:59 -08006061 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006062 */
6063 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006064 @TelephonyManager.SetCarrierRestrictionResult
6065 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07006066 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006067 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006068
Michele Berionne482f8202018-11-27 18:57:59 -08006069 if (carrierRestrictionRules == null) {
6070 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08006071 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006072
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006073 final long identity = Binder.clearCallingIdentity();
6074 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006075 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07006076 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006077 } finally {
6078 Binder.restoreCallingIdentity(identity);
6079 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006080 }
6081
6082 /**
6083 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006084 * Get the allowed carrier list and the excluded carrier list, including the priority between
6085 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006086 * Require system privileges. In the future we may add this to carrier APIs.
6087 *
Michele Berionne482f8202018-11-27 18:57:59 -08006088 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07006089 */
6090 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006091 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006092 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07006093 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006094
6095 final long identity = Binder.clearCallingIdentity();
6096 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006097 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
6098 if (response instanceof CarrierRestrictionRules) {
6099 return (CarrierRestrictionRules) response;
6100 }
6101 // Response is an Exception of some kind,
6102 // which is signalled to the user as a NULL retval
6103 return null;
6104 } catch (Exception e) {
6105 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
6106 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006107 } finally {
6108 Binder.restoreCallingIdentity(identity);
6109 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006110 }
6111
fionaxu59545b42016-05-25 15:53:37 -07006112 /**
6113 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
6114 * @param subId the subscription ID that this action applies to.
6115 * @param enabled control enable or disable metered apns.
6116 * {@hide}
6117 */
6118 @Override
6119 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
6120 enforceModifyPermission();
6121 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006122
6123 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006124 if (phone == null) {
6125 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
6126 return;
6127 }
6128 try {
6129 phone.carrierActionSetMeteredApnsEnabled(enabled);
6130 } catch (Exception e) {
6131 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006132 } finally {
6133 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006134 }
6135 }
6136
6137 /**
6138 * Action set from carrier signalling broadcast receivers to enable/disable radio
6139 * @param subId the subscription ID that this action applies to.
6140 * @param enabled control enable or disable radio.
6141 * {@hide}
6142 */
6143 @Override
6144 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6145 enforceModifyPermission();
6146 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006147
6148 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006149 if (phone == null) {
6150 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
6151 return;
6152 }
6153 try {
6154 phone.carrierActionSetRadioEnabled(enabled);
6155 } catch (Exception e) {
6156 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006157 } finally {
6158 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006159 }
6160 }
6161
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006162 /**
fionaxu8da9cb12017-05-23 15:02:46 -07006163 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
6164 * network status based on which carrier apps could apply actions accordingly,
6165 * enable/disable default url handler for example.
6166 *
6167 * @param subId the subscription ID that this action applies to.
6168 * @param report control start/stop reporting the default network status.
6169 * {@hide}
6170 */
6171 @Override
6172 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
6173 enforceModifyPermission();
6174 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006175
6176 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07006177 if (phone == null) {
6178 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
6179 return;
6180 }
6181 try {
6182 phone.carrierActionReportDefaultNetworkStatus(report);
6183 } catch (Exception e) {
6184 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006185 } finally {
6186 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07006187 }
6188 }
6189
6190 /**
fionaxud9622282017-07-17 17:51:30 -07006191 * Action set from carrier signalling broadcast receivers to reset all carrier actions
6192 * @param subId the subscription ID that this action applies to.
6193 * {@hide}
6194 */
6195 @Override
6196 public void carrierActionResetAll(int subId) {
6197 enforceModifyPermission();
6198 final Phone phone = getPhone(subId);
6199 if (phone == null) {
6200 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
6201 return;
6202 }
6203 try {
6204 phone.carrierActionResetAll();
6205 } catch (Exception e) {
6206 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
6207 }
6208 }
6209
6210 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006211 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
6212 * bug report is being generated.
6213 */
6214 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07006215 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006216 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
6217 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07006218 writer.println("Permission Denial: can't dump Phone from pid="
6219 + Binder.getCallingPid()
6220 + ", uid=" + Binder.getCallingUid()
6221 + "without permission "
6222 + android.Manifest.permission.DUMP);
6223 return;
6224 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006225 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006226 }
Jack Yueb89b242016-06-22 13:27:47 -07006227
Brad Ebingerdac2f002018-04-03 15:17:52 -07006228 @Override
6229 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6230 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6231 throws RemoteException {
6232 (new TelephonyShellCommand(this)).exec(this, in, out, err, args, callback, resultReceiver);
6233 }
6234
Jack Yueb89b242016-06-22 13:27:47 -07006235 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006236 * Get aggregated video call data usage since boot.
6237 *
6238 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6239 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006240 * {@hide}
6241 */
6242 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006243 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006244 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6245 null);
6246
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006247 final long identity = Binder.clearCallingIdentity();
6248 try {
6249 // NetworkStatsService keeps tracking the active network interface and identity. It
6250 // records the delta with the corresponding network identity.
6251 // We just return the total video call data usage snapshot since boot.
6252 Phone phone = getPhone(subId);
6253 if (phone != null) {
6254 return phone.getVtDataUsage(perUidStats);
6255 }
6256 return null;
6257 } finally {
6258 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006259 }
Jack Yueb89b242016-06-22 13:27:47 -07006260 }
Jack Yu75ab2952016-07-08 14:29:33 -07006261
6262 /**
6263 * Policy control of data connection. Usually used when data limit is passed.
6264 * @param enabled True if enabling the data, otherwise disabling.
6265 * @param subId Subscription index
6266 * {@hide}
6267 */
6268 @Override
6269 public void setPolicyDataEnabled(boolean enabled, int subId) {
6270 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006271
6272 final long identity = Binder.clearCallingIdentity();
6273 try {
6274 Phone phone = getPhone(subId);
6275 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006276 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006277 }
6278 } finally {
6279 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006280 }
6281 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006282
6283 /**
6284 * Get Client request stats
6285 * @return List of Client Request Stats
6286 * @hide
6287 */
6288 @Override
6289 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006290 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006291 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006292 return null;
6293 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006294 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006295
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006296 final long identity = Binder.clearCallingIdentity();
6297 try {
6298 if (phone != null) {
6299 return phone.getClientRequestStats();
6300 }
6301
6302 return null;
6303 } finally {
6304 Binder.restoreCallingIdentity(identity);
6305 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006306 }
6307
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006308 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006309 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006310 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006311 }
Jack Yueb4124c2017-02-16 15:32:43 -08006312
6313 /**
Grace Chen70990072017-03-24 17:21:30 -07006314 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006315 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006316 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006317 * @param state State of SIM (power down, power up, pass through)
6318 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6319 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6320 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006321 *
6322 **/
6323 @Override
Grace Chen70990072017-03-24 17:21:30 -07006324 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006325 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006326 Phone phone = PhoneFactory.getPhone(slotIndex);
6327
vagdeviaf9a5b92018-08-15 16:01:53 -07006328 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6329
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006330 final long identity = Binder.clearCallingIdentity();
6331 try {
6332 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006333 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006334 }
6335 } finally {
6336 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08006337 }
6338 }
Shuo Qiandd210312017-04-12 22:11:33 +00006339
Tyler Gunn65d45c22017-06-05 11:22:26 -07006340 private boolean isUssdApiAllowed(int subId) {
6341 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006342 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07006343 if (configManager == null) {
6344 return false;
6345 }
6346 PersistableBundle pb = configManager.getConfigForSubId(subId);
6347 if (pb == null) {
6348 return false;
6349 }
6350 return pb.getBoolean(
6351 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
6352 }
6353
Shuo Qiandd210312017-04-12 22:11:33 +00006354 /**
6355 * Check if phone is in emergency callback mode
6356 * @return true if phone is in emergency callback mode
6357 * @param subId sub id
6358 */
goneil9c5f4872017-12-05 14:07:56 -08006359 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00006360 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006361 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00006362 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006363
6364 final long identity = Binder.clearCallingIdentity();
6365 try {
6366 if (phone != null) {
6367 return phone.isInEcm();
6368 } else {
6369 return false;
6370 }
6371 } finally {
6372 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00006373 }
6374 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006375
6376 /**
6377 * Get the current signal strength information for the given subscription.
6378 * Because this information is not updated when the device is in a low power state
6379 * it should not be relied-upon to be current.
6380 * @param subId Subscription index
6381 * @return the most recent cached signal strength info from the modem
6382 */
6383 @Override
6384 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006385 final long identity = Binder.clearCallingIdentity();
6386 try {
6387 Phone p = getPhone(subId);
6388 if (p == null) {
6389 return null;
6390 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006391
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006392 return p.getSignalStrength();
6393 } finally {
6394 Binder.restoreCallingIdentity(identity);
6395 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006396 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006397
Pengquan Meng77b7f132018-08-22 14:49:57 -07006398 /**
Chen Xuf792fd62018-10-17 17:54:36 +00006399 * Get the current modem radio state for the given slot.
6400 * @param slotIndex slot index.
6401 * @param callingPackage the name of the package making the call.
6402 * @return the current radio power state from the modem
6403 */
6404 @Override
6405 public int getRadioPowerState(int slotIndex, String callingPackage) {
6406 Phone phone = PhoneFactory.getPhone(slotIndex);
6407 if (phone != null) {
6408 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6409 mApp, phone.getSubId(), callingPackage, "getRadioPowerState")) {
6410 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6411 }
6412
6413 final long identity = Binder.clearCallingIdentity();
6414 try {
6415 return phone.getRadioPowerState();
6416 } finally {
6417 Binder.restoreCallingIdentity(identity);
6418 }
6419 }
6420 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6421 }
6422
6423 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07006424 * Checks if data roaming is enabled on the subscription with id {@code subId}.
6425 *
6426 * <p>Requires one of the following permissions:
6427 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
6428 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
6429 * privileges.
6430 *
6431 * @param subId subscription id
6432 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
6433 * {@code false}.
6434 */
6435 @Override
6436 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006437 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6438 null /* message */);
6439
Pengquan Menga1bb6272018-09-06 09:59:22 -07006440 boolean isEnabled = false;
6441 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07006442 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006443 Phone phone = getPhone(subId);
6444 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006445 } catch (Exception e) {
6446 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6447 mApp, subId, "isDataRoamingEnabled");
Pengquan Menga1bb6272018-09-06 09:59:22 -07006448 } finally {
6449 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006450 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006451 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006452 }
6453
6454
6455 /**
6456 * Enables/Disables the data roaming on the subscription with id {@code subId}.
6457 *
6458 * <p> Requires permission:
6459 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
6460 * privileges.
6461 *
6462 * @param subId subscription id
6463 * @param isEnabled {@code true} means enable, {@code false} means disable.
6464 */
6465 @Override
6466 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006467 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6468 mApp, subId, "setDataRoamingEnabled");
6469
Pengquan Menga1bb6272018-09-06 09:59:22 -07006470 final long identity = Binder.clearCallingIdentity();
6471 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006472 Phone phone = getPhone(subId);
6473 if (phone != null) {
6474 phone.setDataRoamingEnabled(isEnabled);
6475 }
6476 } finally {
6477 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006478 }
6479 }
6480
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006481 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006482 public boolean isManualNetworkSelectionAllowed(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006483 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6484 mApp, subId, "isManualNetworkSelectionAllowed");
6485
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006486 boolean isAllowed = true;
6487 final long identity = Binder.clearCallingIdentity();
6488 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006489 Phone phone = getPhone(subId);
6490 if (phone != null) {
6491 isAllowed = phone.isCspPlmnEnabled();
6492 }
6493 } finally {
6494 Binder.restoreCallingIdentity(identity);
6495 }
6496 return isAllowed;
6497 }
6498
6499 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08006500 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liucfdfe3a2020-03-23 11:55:07 -07006501 // Verify that tha callingPackage belongs to the calling UID
6502 mApp.getSystemService(AppOpsManager.class)
6503 .checkPackage(Binder.getCallingUid(), callingPackage);
6504
Jordan Liu1e142fc2019-04-22 15:10:43 -07006505 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08006506 try {
6507 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07006508 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08006509 } catch (SecurityException e) {
6510 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
6511 // has carrier privileges on an active UICC
6512 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
6513 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006514 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08006515 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08006516 }
Jordan Liu5aa07002018-12-18 15:44:48 -08006517
6518 final long identity = Binder.clearCallingIdentity();
6519 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08006520 UiccController uiccController = UiccController.getInstance();
6521 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07006522 if (hasReadPermission) {
6523 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08006524 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07006525
6526 // Remove private info if the caller doesn't have access
6527 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
6528 for (UiccCardInfo cardInfo : cardInfos) {
6529 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
6530 // is available
6531 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
6532 if (card == null || card.getUiccProfile() == null) {
6533 // assume no access if the card or profile is unavailable
6534 filteredInfos.add(cardInfo.getUnprivileged());
6535 continue;
6536 }
6537 UiccProfile profile = card.getUiccProfile();
6538 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
6539 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6540 filteredInfos.add(cardInfo);
6541 } else {
6542 filteredInfos.add(cardInfo.getUnprivileged());
6543 }
6544 }
6545 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08006546 } finally {
6547 Binder.restoreCallingIdentity(identity);
6548 }
6549 }
6550
6551 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006552 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006553 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006554
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006555 final long identity = Binder.clearCallingIdentity();
6556 try {
6557 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
6558 if (slots == null) {
6559 Rlog.i(LOG_TAG, "slots is null.");
6560 return null;
6561 }
6562
6563 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
6564 for (int i = 0; i < slots.length; i++) {
6565 UiccSlot slot = slots[i];
6566 if (slot == null) {
6567 continue;
6568 }
6569
Jordan Liu7be7e652019-05-06 18:55:02 +00006570 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006571 UiccCard card = slot.getUiccCard();
6572 if (card != null) {
6573 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00006574 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07006575 cardId = slot.getEid();
6576 if (TextUtils.isEmpty(cardId)) {
6577 cardId = slot.getIccId();
6578 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006579 }
6580
6581 int cardState = 0;
6582 switch (slot.getCardState()) {
6583 case CARDSTATE_ABSENT:
6584 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
6585 break;
6586 case CARDSTATE_PRESENT:
6587 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
6588 break;
6589 case CARDSTATE_ERROR:
6590 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
6591 break;
6592 case CARDSTATE_RESTRICTED:
6593 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
6594 break;
6595 default:
6596 break;
6597
6598 }
6599
6600 infos[i] = new UiccSlotInfo(
6601 slot.isActive(),
6602 slot.isEuicc(),
6603 cardId,
6604 cardState,
6605 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08006606 slot.isExtendedApduSupported(),
6607 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006608 }
6609 return infos;
6610 } finally {
6611 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07006612 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006613 }
6614
6615 @Override
6616 public boolean switchSlots(int[] physicalSlots) {
6617 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006618
6619 final long identity = Binder.clearCallingIdentity();
6620 try {
6621 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
6622 } finally {
6623 Binder.restoreCallingIdentity(identity);
6624 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006625 }
Jack Yu4c988042018-02-27 15:30:01 -08006626
6627 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08006628 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08006629 final long identity = Binder.clearCallingIdentity();
6630 try {
6631 return UiccController.getInstance().getCardIdForDefaultEuicc();
6632 } finally {
6633 Binder.restoreCallingIdentity(identity);
6634 }
6635 }
6636
6637 @Override
Jack Yu4c988042018-02-27 15:30:01 -08006638 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
6639 enforceModifyPermission();
6640 final Phone phone = getPhone(subId);
6641 if (phone == null) {
6642 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
6643 return;
6644 }
6645
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006646 final long identity = Binder.clearCallingIdentity();
6647 try {
6648 phone.setRadioIndicationUpdateMode(filters, mode);
6649 } finally {
6650 Binder.restoreCallingIdentity(identity);
6651 }
Jack Yu4c988042018-02-27 15:30:01 -08006652 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07006653
6654 /**
goneil47ffb6e2018-04-06 15:40:58 -07006655 * A test API to reload the UICC profile.
6656 *
6657 * <p>Requires that the calling app has permission
6658 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6659 * @hide
6660 */
6661 @Override
6662 public void refreshUiccProfile(int subId) {
6663 enforceModifyPermission();
6664
6665 final long identity = Binder.clearCallingIdentity();
6666 try {
6667 Phone phone = getPhone(subId);
6668 if (phone == null) {
6669 return;
6670 }
6671 UiccCard uiccCard = phone.getUiccCard();
6672 if (uiccCard == null) {
6673 return;
6674 }
6675 UiccProfile uiccProfile = uiccCard.getUiccProfile();
6676 if (uiccProfile == null) {
6677 return;
6678 }
6679 uiccProfile.refresh();
6680 } finally {
6681 Binder.restoreCallingIdentity(identity);
6682 }
6683 }
6684
6685 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07006686 * Returns false if the mobile data is disabled by default, otherwise return true.
6687 */
6688 private boolean getDefaultDataEnabled() {
6689 return "true".equalsIgnoreCase(
6690 SystemProperties.get(DEFAULT_MOBILE_DATA_PROPERTY_NAME, "true"));
6691 }
6692
6693 /**
6694 * Returns true if the data roaming is enabled by default, i.e the system property
6695 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
6696 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
6697 */
6698 private boolean getDefaultDataRoamingEnabled(int subId) {
6699 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006700 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006701 boolean isDataRoamingEnabled = "true".equalsIgnoreCase(
6702 SystemProperties.get(DEFAULT_DATA_ROAMING_PROPERTY_NAME, "false"));
6703 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
6704 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
6705 return isDataRoamingEnabled;
6706 }
6707
6708 /**
6709 * Returns the default network type for the given {@code subId}, if the default network type is
6710 * not set, return {@link Phone#PREFERRED_NT_MODE}.
6711 */
6712 private int getDefaultNetworkType(int subId) {
6713 return Integer.parseInt(
6714 TelephonyManager.getTelephonyProperty(
6715 mSubscriptionController.getPhoneId(subId),
6716 DEFAULT_NETWORK_MODE_PROPERTY_NAME,
6717 String.valueOf(Phone.PREFERRED_NT_MODE)));
6718 }
fionaxua13278b2018-03-21 00:08:13 -07006719
6720 @Override
6721 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07006722 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07006723 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006724
6725 final long identity = Binder.clearCallingIdentity();
6726 try {
6727 final Phone phone = getPhone(subId);
6728 if (phone == null) {
6729 loge("setCarrierTestOverride fails with invalid subId: " + subId);
6730 return;
6731 }
chen xueaba88a2019-03-15 13:15:10 -07006732 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
6733 carrierPrivilegeRules, apn);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006734 } finally {
6735 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006736 }
fionaxua13278b2018-03-21 00:08:13 -07006737 }
6738
6739 @Override
6740 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006741 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006742
6743 final long identity = Binder.clearCallingIdentity();
6744 try {
6745 final Phone phone = getPhone(subId);
6746 if (phone == null) {
6747 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
6748 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
6749 }
6750 return phone.getCarrierIdListVersion();
6751 } finally {
6752 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006753 }
fionaxua13278b2018-03-21 00:08:13 -07006754 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07006755
6756 @Override
6757 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
6758 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6759 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
6760 return -1;
6761 }
6762
6763 final long identity = Binder.clearCallingIdentity();
6764 try {
6765 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
6766 } finally {
6767 Binder.restoreCallingIdentity(identity);
6768 }
6769 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006770
6771 @Override
6772 public int getCdmaRoamingMode(int subId) {
6773 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6774 mApp, subId, "getCdmaRoamingMode");
6775
6776 final long identity = Binder.clearCallingIdentity();
6777 try {
6778 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
6779 } finally {
6780 Binder.restoreCallingIdentity(identity);
6781 }
6782 }
6783
6784 @Override
6785 public boolean setCdmaRoamingMode(int subId, int mode) {
6786 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6787 mApp, subId, "setCdmaRoamingMode");
6788
6789 final long identity = Binder.clearCallingIdentity();
6790 try {
6791 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
6792 } finally {
6793 Binder.restoreCallingIdentity(identity);
6794 }
6795 }
6796
6797 @Override
6798 public boolean setCdmaSubscriptionMode(int subId, int mode) {
6799 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6800 mApp, subId, "setCdmaSubscriptionMode");
6801
6802 final long identity = Binder.clearCallingIdentity();
6803 try {
6804 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
6805 } finally {
6806 Binder.restoreCallingIdentity(identity);
6807 }
6808 }
Makoto Onukida3bf792018-09-18 16:06:29 -07006809
6810 private void ensureUserRunning(int userId) {
6811 if (!mUserManager.isUserRunning(userId)) {
6812 throw new IllegalStateException("User " + userId + " does not exist or not running");
6813 }
6814 }
6815
6816 /**
6817 * Returns a list of SMS apps on a given user.
6818 *
6819 * Only the shell user (UID 2000 or 0) can call it.
6820 * Target user must be running.
6821 */
6822 @Override
6823 public String[] getSmsApps(int userId) {
6824 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getSmsApps");
6825 ensureUserRunning(userId);
6826
6827 final Collection<SmsApplicationData> apps =
6828 SmsApplication.getApplicationCollectionAsUser(mApp, userId);
6829
6830 String[] ret = new String[apps.size()];
6831 int i = 0;
6832 for (SmsApplicationData app : apps) {
6833 ret[i++] = app.mPackageName;
6834 }
6835 return ret;
6836 }
6837
6838 /**
6839 * Returns the default SMS app package name on a given user.
6840 *
6841 * Only the shell user (UID 2000 or 0) can call it.
6842 * Target user must be running.
6843 */
6844 @Override
6845 public String getDefaultSmsApp(int userId) {
6846 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDefaultSmsApp");
6847 ensureUserRunning(userId);
6848
6849 final ComponentName cn = SmsApplication.getDefaultSmsApplicationAsUser(mApp,
6850 /* updateIfNeeded= */ true, userId);
6851 return cn == null ? null : cn.getPackageName();
6852 }
6853
6854 /**
6855 * Set a package as the default SMS app on a given user.
6856 *
6857 * Only the shell user (UID 2000 or 0) can call it.
6858 * Target user must be running.
6859 */
6860 @Override
6861 public void setDefaultSmsApp(int userId, String packageName) {
6862 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDefaultSmsApp");
6863 ensureUserRunning(userId);
6864
6865 boolean found = false;
6866 for (String pkg : getSmsApps(userId)) {
6867 if (TextUtils.equals(packageName, pkg)) {
6868 found = true;
6869 break;
6870 }
6871 }
6872 if (!found) {
6873 throw new IllegalArgumentException("Package " + packageName + " is not an SMS app");
6874 }
6875
6876 SmsApplication.setDefaultApplicationAsUser(packageName, mApp, userId);
6877 }
sqianc5eccab2018-10-19 18:46:41 -07006878
6879 @Override
sqian8c685422019-02-22 15:55:18 -08006880 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
sqianc5eccab2018-10-19 18:46:41 -07006881 String callingPackage) {
sqian11b7a0e2018-12-05 18:48:28 -08006882 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
sqian8c685422019-02-22 15:55:18 -08006883 mApp, getDefaultSubscription(), callingPackage, "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08006884 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6885 }
6886 final long identity = Binder.clearCallingIdentity();
6887 try {
sqian854d44b2018-12-12 16:48:18 -08006888 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
6889 for (Phone phone: PhoneFactory.getPhones()) {
6890 if (phone.getEmergencyNumberTracker() != null
6891 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
6892 emergencyNumberListInternal.put(
6893 phone.getSubId(),
6894 phone.getEmergencyNumberTracker().getEmergencyNumberList());
6895 }
sqian11b7a0e2018-12-05 18:48:28 -08006896 }
sqian854d44b2018-12-12 16:48:18 -08006897 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08006898 } finally {
6899 Binder.restoreCallingIdentity(identity);
6900 }
sqianc5eccab2018-10-19 18:46:41 -07006901 }
6902
6903 @Override
sqian8c685422019-02-22 15:55:18 -08006904 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006905 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08006906 if (!exactMatch) {
6907 TelephonyPermissions
6908 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08006909 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08006910 }
6911 final long identity = Binder.clearCallingIdentity();
6912 try {
sqian854d44b2018-12-12 16:48:18 -08006913 for (Phone phone: PhoneFactory.getPhones()) {
6914 if (phone.getEmergencyNumberTracker() != null
6915 && phone.getEmergencyNumberTracker() != null) {
6916 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
6917 number, exactMatch)) {
6918 return true;
sqian11b7a0e2018-12-05 18:48:28 -08006919 }
6920 }
sqian11b7a0e2018-12-05 18:48:28 -08006921 }
6922 return false;
6923 } finally {
6924 Binder.restoreCallingIdentity(identity);
6925 }
6926 }
6927
sqianf4ca7ed2019-01-15 18:32:07 -08006928 /**
6929 * Update emergency number list for test mode.
6930 */
6931 @Override
6932 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
6933 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6934 "updateEmergencyNumberListTestMode");
6935
6936 final long identity = Binder.clearCallingIdentity();
6937 try {
6938 for (Phone phone: PhoneFactory.getPhones()) {
6939 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6940 if (tracker != null) {
6941 tracker.executeEmergencyNumberTestModeCommand(action, num);
6942 }
6943 }
6944 } finally {
6945 Binder.restoreCallingIdentity(identity);
6946 }
6947 }
6948
6949 /**
6950 * Get the full emergency number list for test mode.
6951 */
6952 @Override
6953 public List<String> getEmergencyNumberListTestMode() {
6954 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6955 "getEmergencyNumberListTestMode");
6956
6957 final long identity = Binder.clearCallingIdentity();
6958 try {
6959 Set<String> emergencyNumbers = new HashSet<>();
6960 for (Phone phone: PhoneFactory.getPhones()) {
6961 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6962 if (tracker != null) {
6963 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
6964 emergencyNumbers.add(num.getNumber());
6965 }
6966 }
6967 }
6968 return new ArrayList<>(emergencyNumbers);
6969 } finally {
6970 Binder.restoreCallingIdentity(identity);
6971 }
6972 }
6973
chen xud6b45bd2018-10-30 22:27:10 -07006974 @Override
6975 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
6976 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
6977 Phone phone = getPhone(subId);
6978 if (phone == null) {
6979 return null;
6980 }
6981 final long identity = Binder.clearCallingIdentity();
6982 try {
6983 UiccProfile profile = UiccController.getInstance()
6984 .getUiccProfileForPhone(phone.getPhoneId());
6985 if (profile != null) {
6986 return profile.getCertsFromCarrierPrivilegeAccessRules();
6987 }
6988 } finally {
6989 Binder.restoreCallingIdentity(identity);
6990 }
6991 return null;
6992 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08006993
6994 /**
6995 * Enable or disable a modem stack.
6996 */
6997 @Override
6998 public boolean enableModemForSlot(int slotIndex, boolean enable) {
6999 enforceModifyPermission();
7000
7001 final long identity = Binder.clearCallingIdentity();
7002 try {
7003 Phone phone = PhoneFactory.getPhone(slotIndex);
7004 if (phone == null) {
7005 return false;
7006 } else {
7007 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
7008 }
7009 } finally {
7010 Binder.restoreCallingIdentity(identity);
7011 }
7012 }
Michelecea4cf22018-12-21 15:00:11 -08007013
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007014 /**
7015 * Whether a modem stack is enabled or not.
7016 */
7017 @Override
7018 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage) {
7019 Phone phone = PhoneFactory.getPhone(slotIndex);
7020 if (phone == null) return false;
7021
7022 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7023 mApp, phone.getSubId(), callingPackage, "isModemEnabledForSlot")) {
7024 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7025 }
7026
7027 final long identity = Binder.clearCallingIdentity();
7028 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07007029 try {
7030 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
7031 } catch (NoSuchElementException ex) {
7032 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
7033 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007034 } finally {
7035 Binder.restoreCallingIdentity(identity);
7036 }
7037 }
7038
Michelecea4cf22018-12-21 15:00:11 -08007039 @Override
Michele0ea7d782019-03-19 14:58:42 -07007040 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08007041 enforceModifyPermission();
7042
7043 final long identity = Binder.clearCallingIdentity();
7044 try {
7045 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07007046 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08007047 .commit();
7048 } finally {
7049 Binder.restoreCallingIdentity(identity);
7050 }
7051 }
7052
7053 @Override
Michele0ea7d782019-03-19 14:58:42 -07007054 @TelephonyManager.IsMultiSimSupportedResult
7055 public int isMultiSimSupported(String callingPackage) {
Michele4245e952019-02-04 11:36:23 -08007056 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Michele0ea7d782019-03-19 14:58:42 -07007057 getDefaultPhone().getSubId(), callingPackage, "isMultiSimSupported")) {
7058 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08007059 }
Michelecea4cf22018-12-21 15:00:11 -08007060
7061 final long identity = Binder.clearCallingIdentity();
7062 try {
Michele0ea7d782019-03-19 14:58:42 -07007063 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08007064 } finally {
7065 Binder.restoreCallingIdentity(identity);
7066 }
7067 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007068
Michele0ea7d782019-03-19 14:58:42 -07007069 @TelephonyManager.IsMultiSimSupportedResult
7070 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08007071 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
7072 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
7073 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007074 loge("isMultiSimSupportedInternal: requires at least 2 cards");
7075 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007076 }
7077 // Check if the hardware supports multisim functionality. If usage of multisim is not
7078 // supported by the modem, indicate that it is restricted.
7079 PhoneCapability staticCapability =
7080 mPhoneConfigurationManager.getStaticPhoneCapability();
7081 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07007082 loge("isMultiSimSupportedInternal: no static configuration available");
7083 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007084 }
7085 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007086 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
7087 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007088 }
7089 // Check if support of multiple SIMs is restricted by carrier
7090 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07007091 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08007092 }
7093
Michele0ea7d782019-03-19 14:58:42 -07007094 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08007095 }
7096
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007097 /**
7098 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007099 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
7100 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
7101 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007102 * @param numOfSims number of active sims we want to switch to
7103 */
7104 @Override
7105 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007106 if (numOfSims == 1) {
7107 enforceModifyPermission();
7108 } else {
7109 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7110 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
7111 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007112 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08007113
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007114 try {
Michele30b57b22019-03-01 12:01:14 -08007115 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07007116 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08007117 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
7118 return;
7119 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007120 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
7121 } finally {
7122 Binder.restoreCallingIdentity(identity);
7123 }
7124 }
7125
7126 /**
chen xub4baa772019-04-03 10:23:41 -07007127 * Get whether making changes to modem configurations will trigger reboot.
7128 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007129 */
7130 @Override
chen xub4baa772019-04-03 10:23:41 -07007131 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage) {
7132 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7133 mApp, subId, callingPackage, "doesSwitchMultiSimConfigTriggerReboot")) {
7134 return false;
7135 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007136 final long identity = Binder.clearCallingIdentity();
7137 try {
7138 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
7139 } finally {
7140 Binder.restoreCallingIdentity(identity);
7141 }
7142 }
7143
Nathan Harold29f5f052019-02-15 13:41:57 -08007144 private void updateModemStateMetrics() {
7145 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
7146 // TODO: check the state for each modem if the api is ready.
7147 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
7148 }
7149
Pengquan Meng3889a572019-01-23 11:16:29 -08007150 @Override
7151 public int[] getSlotsMapping() {
7152 enforceReadPrivilegedPermission("getSlotsMapping");
7153
7154 final long identity = Binder.clearCallingIdentity();
7155 try {
7156 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
7157 // All logical slots should have a mapping to a physical slot.
7158 int[] logicalSlotsMapping = new int[phoneCount];
7159 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
7160 for (int i = 0; i < slotInfos.length; i++) {
7161 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
7162 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
7163 }
7164 }
7165 return logicalSlotsMapping;
7166 } finally {
7167 Binder.restoreCallingIdentity(identity);
7168 }
7169 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08007170
7171 /**
7172 * Get the IRadio HAL Version
7173 */
7174 @Override
7175 public int getRadioHalVersion() {
7176 Phone phone = getDefaultPhone();
7177 if (phone == null) return -1;
7178 HalVersion hv = phone.getHalVersion();
7179 if (hv.equals(HalVersion.UNKNOWN)) return -1;
7180 return hv.major * 100 + hv.minor;
7181 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007182
7183 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07007184 * Return whether data is enabled for certain APN type. This will tell if framework will accept
7185 * corresponding network requests on a subId.
7186 *
7187 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007188 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07007189 * 2) APN is un-metered for this subscription, or
7190 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
7191 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
7192 *
7193 * @return whether data is allowed for a apn type.
7194 *
7195 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007196 */
7197 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07007198 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007199 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Malcolm Chene5ad5792019-04-18 13:51:02 -07007200 mApp, subId, callingPackage, "isDataEnabledForApn")) {
7201 throw new SecurityException("Needs READ_PHONE_STATE for isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007202 }
7203
7204 // Now that all security checks passes, perform the operation as ourselves.
7205 final long identity = Binder.clearCallingIdentity();
7206 try {
7207 Phone phone = getPhone(subId);
7208 if (phone == null) return false;
7209
Jack Yu41407ee2019-05-13 16:54:09 -07007210 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07007211 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
7212 } finally {
7213 Binder.restoreCallingIdentity(identity);
7214 }
7215 }
7216
7217 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07007218 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07007219 enforceReadPrivilegedPermission("isApnMetered");
7220
7221 // Now that all security checks passes, perform the operation as ourselves.
7222 final long identity = Binder.clearCallingIdentity();
7223 try {
7224 Phone phone = getPhone(subId);
7225 if (phone == null) return true; // By default return true.
7226
Jack Yu41407ee2019-05-13 16:54:09 -07007227 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007228 } finally {
7229 Binder.restoreCallingIdentity(identity);
7230 }
7231 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007232
7233 @Override
7234 public void enqueueSmsPickResult(String callingPackage, IIntegerConsumer pendingSubIdResult) {
7235 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
7236 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
7237 if (!permissions.checkCallingCanSendSms(callingPackage, "Sending message")) {
7238 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
7239 }
7240 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
7241 Intent intent = new Intent();
7242 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
7243 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7244 // Bring up choose default SMS subscription dialog right now
7245 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
7246 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
7247 mApp.startActivity(intent);
7248 }
chen xud5ca2d52019-05-28 15:20:57 -07007249
7250 @Override
7251 public String getMmsUAProfUrl(int subId) {
7252 //TODO investigate if this API should require proper permission check in R b/133791609
7253 final long identity = Binder.clearCallingIdentity();
7254 try {
7255 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7256 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
7257 } finally {
7258 Binder.restoreCallingIdentity(identity);
7259 }
7260 }
7261
7262 @Override
7263 public String getMmsUserAgent(int subId) {
7264 //TODO investigate if this API should require proper permission check in R b/133791609
7265 final long identity = Binder.clearCallingIdentity();
7266 try {
7267 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7268 .getString(com.android.internal.R.string.config_mms_user_agent);
7269 } finally {
7270 Binder.restoreCallingIdentity(identity);
7271 }
7272 }
Jack Yub07d4972019-05-28 16:12:25 -07007273
7274 @Override
7275 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
7276 enforceModifyPermission();
7277
7278 // Now that all security checks passes, perform the operation as ourselves.
7279 final long identity = Binder.clearCallingIdentity();
7280 try {
7281 Phone phone = getPhone(subId);
7282 if (phone == null) return false;
7283
7284 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
7285 } finally {
7286 Binder.restoreCallingIdentity(identity);
7287 }
7288 }
7289
7290 @Override
7291 public boolean isDataAllowedInVoiceCall(int subId) {
7292 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
7293
7294 // Now that all security checks passes, perform the operation as ourselves.
7295 final long identity = Binder.clearCallingIdentity();
7296 try {
7297 Phone phone = getPhone(subId);
7298 if (phone == null) return false;
7299
7300 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
7301 } finally {
7302 Binder.restoreCallingIdentity(identity);
7303 }
7304 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07007305}