blob: 31eea789b119733ed1b940f47be1aabc4d4c432c [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;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800112import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800113import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800114
Brad Ebinger35c841c2018-10-01 10:40:55 -0700115import com.android.ims.ImsException;
Andrew Lee312e8172014-10-23 17:01:36 -0700116import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800117import com.android.ims.internal.IImsServiceFeatureCallback;
Jordan Liua39e6c12020-03-04 13:59:23 -0800118import com.android.internal.annotations.VisibleForTesting;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700119import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700120import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700121import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800122import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700123import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700124import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800126import com.android.internal.telephony.HalVersion;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700127import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800128import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700129import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800130import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700131import com.android.internal.telephony.LocaleTracker;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100132import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -0700133import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700134import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700135import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700136import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800137import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700138import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700139import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700140import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700141import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700142import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700143import com.android.internal.telephony.ServiceStateTracker;
Makoto Onukida3bf792018-09-18 16:06:29 -0700144import com.android.internal.telephony.SmsApplication;
145import com.android.internal.telephony.SmsApplication.SmsApplicationData;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700146import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700147import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800148import com.android.internal.telephony.SubscriptionController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800149import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700150import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800151import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700152import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800153import com.android.internal.telephony.ims.ImsResolver;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800154import com.android.internal.telephony.metrics.TelephonyMetrics;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155import com.android.internal.telephony.uicc.IccIoResult;
156import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800157import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700158import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800159import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700160import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800161import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000162import com.android.internal.telephony.uicc.UiccSlot;
fionaxu7ed723d2017-05-30 18:58:54 -0700163import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800164import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700165import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700166import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800167import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700168import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700169import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800170
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700171import java.io.FileDescriptor;
172import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800174import java.util.Arrays;
Makoto Onukida3bf792018-09-18 16:06:29 -0700175import java.util.Collection;
sqian11b7a0e2018-12-05 18:48:28 -0800176import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800177import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800178import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100179import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800180import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700181import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800182import java.util.Set;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183
184/**
185 * Implementation of the ITelephony interface.
186 */
Santos Cordon117fee72014-05-16 17:56:12 -0700187public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 private static final String LOG_TAG = "PhoneInterfaceManager";
189 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
190 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800191 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192
193 // Message codes used with mMainThreadHandler
194 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700195 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
196 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700197 private static final int CMD_OPEN_CHANNEL = 9;
198 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
199 private static final int CMD_CLOSE_CHANNEL = 11;
200 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800201 private static final int CMD_NV_READ_ITEM = 13;
202 private static final int EVENT_NV_READ_ITEM_DONE = 14;
203 private static final int CMD_NV_WRITE_ITEM = 15;
204 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
205 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
206 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700207 private static final int CMD_RESET_MODEM_CONFIG = 19;
208 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800209 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
210 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
211 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
212 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800213 private static final int CMD_SEND_ENVELOPE = 25;
214 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000215 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
216 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700217 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
218 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
219 private static final int CMD_EXCHANGE_SIM_IO = 31;
220 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800221 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
222 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700223 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
224 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700225 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
226 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700227 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
228 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
229 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
230 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700231 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
232 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
233 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
234 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700235 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800236 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
237 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000238 private static final int CMD_SWITCH_SLOTS = 50;
239 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700240 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
241 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
242 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
243 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
244 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
245 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
246 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
247 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700248 private static final int CMD_GET_ALL_CELL_INFO = 60;
249 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
250 private static final int CMD_GET_CELL_LOCATION = 62;
251 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700252 private static final int CMD_MODEM_REBOOT = 64;
253 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700254 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
255 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800256 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
257 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700258 private static final int CMD_GET_MODEM_STATUS = 70;
259 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
Naina Nalluri1264a9f2019-09-17 14:10:30 -0700260 private static final int CMD_ERASE_MODEM_CONFIG = 72;
261 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 73;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800263 // Parameters of select command.
264 private static final int SELECT_COMMAND = 0xA4;
265 private static final int SELECT_P1 = 0x04;
266 private static final int SELECT_P2 = 0;
267 private static final int SELECT_P3 = 0x10;
268
Pengquan Meng85728fb2018-03-12 16:31:21 -0700269 private static final String DEFAULT_NETWORK_MODE_PROPERTY_NAME = "ro.telephony.default_network";
270 private static final String DEFAULT_DATA_ROAMING_PROPERTY_NAME = "ro.com.android.dataroaming";
271 private static final String DEFAULT_MOBILE_DATA_PROPERTY_NAME = "ro.com.android.mobiledata";
272
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 /** The singleton instance. */
274 private static PhoneInterfaceManager sInstance;
275
Wink Saville3ab207e2014-11-20 13:07:20 -0800276 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800277 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700278 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800279 private AppOpsManager mAppOps;
280 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800281 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800282 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700283 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284
Derek Tan97ebb422014-09-05 16:55:38 -0700285 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
286 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800287 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800288 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700289
Michelecea4cf22018-12-21 15:00:11 -0800290 // String to store multi SIM allowed
291 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
292
Derek Tan740e1672017-06-27 14:56:27 -0700293 // The AID of ISD-R.
294 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
295
yinxub1bed742017-04-17 11:45:04 -0700296 private NetworkScanRequestTracker mNetworkScanRequestTracker;
297
David Kelly5e06a7f2018-03-12 14:10:59 +0000298 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
299 private static final int MANUFACTURER_CODE_LENGTH = 8;
300
Derek Tan89e89d42014-07-08 17:00:10 -0700301 /**
Naina Nalluri1264a9f2019-09-17 14:10:30 -0700302 * Experiment flag to enable erase modem config on reset network, default value is false
303 */
304 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
305 "reset_network_erase_modem_config_enabled";
306
307 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700308 * A request object to use for transmitting data to an ICC.
309 */
310 private static final class IccAPDUArgument {
311 public int channel, cla, command, p1, p2, p3;
312 public String data;
313
314 public IccAPDUArgument(int channel, int cla, int command,
315 int p1, int p2, int p3, String data) {
316 this.channel = channel;
317 this.cla = cla;
318 this.command = command;
319 this.p1 = p1;
320 this.p2 = p2;
321 this.p3 = p3;
322 this.data = data;
323 }
324 }
325
326 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700327 * A request object to use for transmitting data to an ICC.
328 */
329 private static final class ManualNetworkSelectionArgument {
330 public OperatorInfo operatorInfo;
331 public boolean persistSelection;
332
333 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
334 this.operatorInfo = operatorInfo;
335 this.persistSelection = persistSelection;
336 }
337 }
338
339 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700340 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
341 * request after sending. The main thread will notify the request when it is complete.
342 */
343 private static final class MainThreadRequest {
344 /** The argument to use for the request */
345 public Object argument;
346 /** The result of the request that is run on the main thread */
347 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800348 // The subscriber id that this request applies to. Defaults to
349 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
350 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351
Nathan Harold92bed182018-10-12 18:16:49 -0700352 // In cases where subId is unavailable, the caller needs to specify the phone.
353 public Phone phone;
354
vagdeviaf9a5b92018-08-15 16:01:53 -0700355 public WorkSource workSource;
356
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 public MainThreadRequest(Object argument) {
358 this.argument = argument;
359 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800360
Nathan Harold92bed182018-10-12 18:16:49 -0700361 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
362 this.argument = argument;
363 if (phone != null) {
364 this.phone = phone;
365 }
366 this.workSource = workSource;
367 }
368
vagdeviaf9a5b92018-08-15 16:01:53 -0700369 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800370 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800371 if (subId != null) {
372 this.subId = subId;
373 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700374 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800375 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376 }
377
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800378 private static final class IncomingThirdPartyCallArgs {
379 public final ComponentName component;
380 public final String callId;
381 public final String callerDisplayName;
382
383 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
384 String callerDisplayName) {
385 this.component = component;
386 this.callId = callId;
387 this.callerDisplayName = callerDisplayName;
388 }
389 }
390
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700391 /**
392 * A handler that processes messages on the main thread in the phone process. Since many
393 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
394 * inbound binder threads to the main thread in the phone process. The Binder thread
395 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
396 * on, which will be notified when the operation completes and will contain the result of the
397 * request.
398 *
399 * <p>If a MainThreadRequest object is provided in the msg.obj field,
400 * note that request.result must be set to something non-null for the calling thread to
401 * unblock.
402 */
403 private final class MainThreadHandler extends Handler {
404 @Override
405 public void handleMessage(Message msg) {
406 MainThreadRequest request;
407 Message onCompleted;
408 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800409 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700410 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800411 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700412
413 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700414 case CMD_HANDLE_USSD_REQUEST: {
415 request = (MainThreadRequest) msg.obj;
416 final Phone phone = getPhoneFromRequest(request);
417 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
418 String ussdRequest = ussdObject.first;
419 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700420
Pengquan Menga1bb6272018-09-06 09:59:22 -0700421 if (!isUssdApiAllowed(request.subId)) {
422 // Carrier does not support use of this API, return failure.
423 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
424 UssdResponse response = new UssdResponse(ussdRequest, null);
425 Bundle returnData = new Bundle();
426 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
427 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700428
Pengquan Menga1bb6272018-09-06 09:59:22 -0700429 request.result = true;
430 notifyRequester(request);
431 return;
432 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700433
Pengquan Menga1bb6272018-09-06 09:59:22 -0700434 try {
435 request.result = phone != null
436 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
437 } catch (CallStateException cse) {
438 request.result = false;
439 }
440 // Wake up the requesting thread
441 notifyRequester(request);
442 break;
pkanwar32d516d2016-10-14 19:37:38 -0700443 }
444
Yorke Lee716f67e2015-06-17 15:39:16 -0700445 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700446 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700447 final Phone phone = getPhoneFromRequest(request);
448 request.result = phone != null ?
449 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
450 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700451 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700452 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700454 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700455
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700456 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700457 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700458 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800459 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700460 if (uiccCard == null) {
461 loge("iccTransmitApduLogicalChannel: No UICC");
462 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700463 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700464 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700465 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
466 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700467 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700468 iccArgument.channel, iccArgument.cla, iccArgument.command,
469 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700470 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700471 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700472 break;
473
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700474 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700475 ar = (AsyncResult) msg.obj;
476 request = (MainThreadRequest) ar.userObj;
477 if (ar.exception == null && ar.result != null) {
478 request.result = ar.result;
479 } else {
480 request.result = new IccIoResult(0x6F, 0, (byte[])null);
481 if (ar.result == null) {
482 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800483 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700484 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800485 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700486 } else {
487 loge("iccTransmitApduLogicalChannel: Unknown exception");
488 }
489 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700490 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700491 break;
492
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700493 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
494 request = (MainThreadRequest) msg.obj;
495 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800496 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700497 if (uiccCard == null) {
498 loge("iccTransmitApduBasicChannel: No UICC");
499 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700500 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700501 } else {
502 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
503 request);
504 uiccCard.iccTransmitApduBasicChannel(
505 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
506 iccArgument.p3, iccArgument.data, onCompleted);
507 }
508 break;
509
510 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
511 ar = (AsyncResult) msg.obj;
512 request = (MainThreadRequest) ar.userObj;
513 if (ar.exception == null && ar.result != null) {
514 request.result = ar.result;
515 } else {
516 request.result = new IccIoResult(0x6F, 0, (byte[])null);
517 if (ar.result == null) {
518 loge("iccTransmitApduBasicChannel: Empty response");
519 } else if (ar.exception instanceof CommandException) {
520 loge("iccTransmitApduBasicChannel: CommandException: " +
521 ar.exception);
522 } else {
523 loge("iccTransmitApduBasicChannel: Unknown exception");
524 }
525 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700526 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700527 break;
528
529 case CMD_EXCHANGE_SIM_IO:
530 request = (MainThreadRequest) msg.obj;
531 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800532 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700533 if (uiccCard == null) {
534 loge("iccExchangeSimIO: No UICC");
535 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700536 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700537 } else {
538 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
539 request);
540 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
541 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
542 iccArgument.data, onCompleted);
543 }
544 break;
545
546 case EVENT_EXCHANGE_SIM_IO_DONE:
547 ar = (AsyncResult) msg.obj;
548 request = (MainThreadRequest) ar.userObj;
549 if (ar.exception == null && ar.result != null) {
550 request.result = ar.result;
551 } else {
552 request.result = new IccIoResult(0x6f, 0, (byte[])null);
553 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700554 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700555 break;
556
Derek Tan4d5e5c12014-02-04 11:54:58 -0800557 case CMD_SEND_ENVELOPE:
558 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800559 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700560 if (uiccCard == null) {
561 loge("sendEnvelopeWithStatus: No UICC");
562 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700563 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700564 } else {
565 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
566 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
567 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800568 break;
569
570 case EVENT_SEND_ENVELOPE_DONE:
571 ar = (AsyncResult) msg.obj;
572 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700573 if (ar.exception == null && ar.result != null) {
574 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800575 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700576 request.result = new IccIoResult(0x6F, 0, (byte[])null);
577 if (ar.result == null) {
578 loge("sendEnvelopeWithStatus: Empty response");
579 } else if (ar.exception instanceof CommandException) {
580 loge("sendEnvelopeWithStatus: CommandException: " +
581 ar.exception);
582 } else {
583 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
584 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800585 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700586 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800587 break;
588
Shishir Agrawal566b7612013-10-28 14:41:00 -0700589 case CMD_OPEN_CHANNEL:
590 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800591 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800592 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700593 if (uiccCard == null) {
594 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800595 request.result = new IccOpenLogicalChannelResponse(-1,
596 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700597 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700598 } else {
599 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800600 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
601 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700602 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700603 break;
604
605 case EVENT_OPEN_CHANNEL_DONE:
606 ar = (AsyncResult) msg.obj;
607 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700608 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700610 int[] result = (int[]) ar.result;
611 int channelId = result[0];
612 byte[] selectResponse = null;
613 if (result.length > 1) {
614 selectResponse = new byte[result.length - 1];
615 for (int i = 1; i < result.length; ++i) {
616 selectResponse[i - 1] = (byte) result[i];
617 }
618 }
619 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700620 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700621 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 if (ar.result == null) {
623 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700624 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700625 if (ar.exception != null) {
626 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
627 }
628
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700629 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700630 if (ar.exception instanceof CommandException) {
631 CommandException.Error error =
632 ((CommandException) (ar.exception)).getCommandError();
633 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700634 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700635 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700636 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700637 }
638 }
639 openChannelResp = new IccOpenLogicalChannelResponse(
640 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700641 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700642 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700643 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700644 break;
645
646 case CMD_CLOSE_CHANNEL:
647 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800648 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700649 if (uiccCard == null) {
650 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900651 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700652 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700653 } else {
654 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
655 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
656 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700657 break;
658
659 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800660 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
661 break;
662
663 case CMD_NV_READ_ITEM:
664 request = (MainThreadRequest) msg.obj;
665 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800666 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
667 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800668 break;
669
670 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700671 ar = (AsyncResult) msg.obj;
672 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800673 if (ar.exception == null && ar.result != null) {
674 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700675 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800676 request.result = "";
677 if (ar.result == null) {
678 loge("nvReadItem: Empty response");
679 } else if (ar.exception instanceof CommandException) {
680 loge("nvReadItem: CommandException: " +
681 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700682 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800683 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700684 }
685 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700686 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700687 break;
688
Jake Hambye994d462014-02-03 13:10:13 -0800689 case CMD_NV_WRITE_ITEM:
690 request = (MainThreadRequest) msg.obj;
691 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
692 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800693 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700694 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800695 break;
696
697 case EVENT_NV_WRITE_ITEM_DONE:
698 handleNullReturnEvent(msg, "nvWriteItem");
699 break;
700
701 case CMD_NV_WRITE_CDMA_PRL:
702 request = (MainThreadRequest) msg.obj;
703 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800704 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800705 break;
706
707 case EVENT_NV_WRITE_CDMA_PRL_DONE:
708 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
709 break;
710
chen xu6dac5ab2018-10-26 17:39:23 -0700711 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800712 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700713 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800714 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800715 break;
716
chen xu6dac5ab2018-10-26 17:39:23 -0700717 case EVENT_RESET_MODEM_CONFIG_DONE:
718 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800719 break;
720
Jake Hamby7c27be32014-03-03 13:25:59 -0800721 case CMD_GET_PREFERRED_NETWORK_TYPE:
722 request = (MainThreadRequest) msg.obj;
723 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700724 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800725 break;
726
727 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
728 ar = (AsyncResult) msg.obj;
729 request = (MainThreadRequest) ar.userObj;
730 if (ar.exception == null && ar.result != null) {
731 request.result = ar.result; // Integer
732 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800733 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800734 if (ar.result == null) {
735 loge("getPreferredNetworkType: Empty response");
736 } else if (ar.exception instanceof CommandException) {
737 loge("getPreferredNetworkType: CommandException: " +
738 ar.exception);
739 } else {
740 loge("getPreferredNetworkType: Unknown exception");
741 }
742 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700743 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800744 break;
745
746 case CMD_SET_PREFERRED_NETWORK_TYPE:
747 request = (MainThreadRequest) msg.obj;
748 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
749 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700750 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800751 break;
752
753 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
754 handleNullReturnEvent(msg, "setPreferredNetworkType");
755 break;
756
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000757 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
758 request = (MainThreadRequest)msg.obj;
759 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800760 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000761 break;
762
763 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
764 ar = (AsyncResult)msg.obj;
765 request = (MainThreadRequest)ar.userObj;
766 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700767 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000768 break;
769
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800770 case CMD_SET_VOICEMAIL_NUMBER:
771 request = (MainThreadRequest) msg.obj;
772 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
773 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800774 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
775 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800776 break;
777
778 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
779 handleNullReturnEvent(msg, "setVoicemailNumber");
780 break;
781
Stuart Scott54788802015-03-30 13:18:01 -0700782 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
783 request = (MainThreadRequest) msg.obj;
784 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
785 request);
786 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
787 break;
788
789 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
790 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
791 break;
792
Shishir Agrawal302c8692015-06-19 13:49:39 -0700793 case CMD_PERFORM_NETWORK_SCAN:
794 request = (MainThreadRequest) msg.obj;
795 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
796 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
797 break;
798
799 case EVENT_PERFORM_NETWORK_SCAN_DONE:
800 ar = (AsyncResult) msg.obj;
801 request = (MainThreadRequest) ar.userObj;
802 CellNetworkScanResult cellScanResult;
803 if (ar.exception == null && ar.result != null) {
804 cellScanResult = new CellNetworkScanResult(
805 CellNetworkScanResult.STATUS_SUCCESS,
806 (List<OperatorInfo>) ar.result);
807 } else {
808 if (ar.result == null) {
809 loge("getCellNetworkScanResults: Empty response");
810 }
811 if (ar.exception != null) {
812 loge("getCellNetworkScanResults: Exception: " + ar.exception);
813 }
814 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
815 if (ar.exception instanceof CommandException) {
816 CommandException.Error error =
817 ((CommandException) (ar.exception)).getCommandError();
818 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
819 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
820 } else if (error == CommandException.Error.GENERIC_FAILURE) {
821 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
822 }
823 }
824 cellScanResult = new CellNetworkScanResult(errorCode, null);
825 }
826 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700827 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700828 break;
829
830 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
831 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700832 ManualNetworkSelectionArgument selArg =
833 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700834 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
835 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700836 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
837 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700838 break;
839
840 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -0700841 ar = (AsyncResult) msg.obj;
842 request = (MainThreadRequest) ar.userObj;
843 if (ar.exception == null) {
844 request.result = true;
845 } else {
846 request.result = false;
847 loge("setNetworkSelectionModeManual " + ar.exception);
848 }
849 notifyRequester(request);
850 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700851 break;
852
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700853 case CMD_GET_MODEM_ACTIVITY_INFO:
854 request = (MainThreadRequest) msg.obj;
855 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -0700856 if (defaultPhone != null) {
857 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
858 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700859 break;
860
861 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
862 ar = (AsyncResult) msg.obj;
863 request = (MainThreadRequest) ar.userObj;
864 if (ar.exception == null && ar.result != null) {
865 request.result = ar.result;
866 } else {
867 if (ar.result == null) {
868 loge("queryModemActivityInfo: Empty response");
869 } else if (ar.exception instanceof CommandException) {
870 loge("queryModemActivityInfo: CommandException: " +
871 ar.exception);
872 } else {
873 loge("queryModemActivityInfo: Unknown exception");
874 }
875 }
Amit Mahajand4766222016-01-28 15:28:28 -0800876 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
877 if (request.result == null) {
878 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
879 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700880 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700881 break;
882
Meng Wang1a7c35a2016-05-05 20:56:15 -0700883 case CMD_SET_ALLOWED_CARRIERS:
884 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -0800885 CarrierRestrictionRules argument =
886 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700887 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -0800888 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700889 break;
890
891 case EVENT_SET_ALLOWED_CARRIERS_DONE:
892 ar = (AsyncResult) msg.obj;
893 request = (MainThreadRequest) ar.userObj;
894 if (ar.exception == null && ar.result != null) {
895 request.result = ar.result;
896 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800897 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
898 if (ar.exception instanceof CommandException) {
899 loge("setAllowedCarriers: CommandException: " + ar.exception);
900 CommandException.Error error =
901 ((CommandException) (ar.exception)).getCommandError();
902 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
903 request.result =
904 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
905 }
Meng Wang1a7c35a2016-05-05 20:56:15 -0700906 } else {
907 loge("setAllowedCarriers: Unknown exception");
908 }
909 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700910 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700911 break;
912
913 case CMD_GET_ALLOWED_CARRIERS:
914 request = (MainThreadRequest) msg.obj;
915 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800916 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700917 break;
918
919 case EVENT_GET_ALLOWED_CARRIERS_DONE:
920 ar = (AsyncResult) msg.obj;
921 request = (MainThreadRequest) ar.userObj;
922 if (ar.exception == null && ar.result != null) {
923 request.result = ar.result;
924 } else {
Michele Berionne482f8202018-11-27 18:57:59 -0800925 request.result = new IllegalStateException(
926 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -0700927 if (ar.result == null) {
928 loge("getAllowedCarriers: Empty response");
929 } else if (ar.exception instanceof CommandException) {
930 loge("getAllowedCarriers: CommandException: " +
931 ar.exception);
932 } else {
933 loge("getAllowedCarriers: Unknown exception");
934 }
935 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700936 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -0700937 break;
938
Nathan Haroldb3014052017-01-25 15:57:32 -0800939 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
940 ar = (AsyncResult) msg.obj;
941 request = (MainThreadRequest) ar.userObj;
942 if (ar.exception == null && ar.result != null) {
943 request.result = ar.result;
944 } else {
945 request.result = new IllegalArgumentException(
946 "Failed to retrieve Forbidden Plmns");
947 if (ar.result == null) {
948 loge("getForbiddenPlmns: Empty response");
949 } else {
950 loge("getForbiddenPlmns: Unknown exception");
951 }
952 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700953 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800954 break;
955
956 case CMD_GET_FORBIDDEN_PLMNS:
957 request = (MainThreadRequest) msg.obj;
958 uiccCard = getUiccCardFromRequest(request);
959 if (uiccCard == null) {
960 loge("getForbiddenPlmns() UiccCard is null");
961 request.result = new IllegalArgumentException(
962 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700963 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800964 break;
965 }
966 Integer appType = (Integer) request.argument;
967 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
968 if (uiccApp == null) {
969 loge("getForbiddenPlmns() no app with specified type -- "
970 + appType);
971 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -0700972 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -0800973 break;
974 } else {
975 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
976 + " specified type -- " + appType);
977 }
978 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
979 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
980 onCompleted);
981 break;
982
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000983 case CMD_SWITCH_SLOTS:
984 request = (MainThreadRequest) msg.obj;
985 int[] physicalSlots = (int[]) request.argument;
986 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
987 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
988 break;
989
990 case EVENT_SWITCH_SLOTS_DONE:
991 ar = (AsyncResult) msg.obj;
992 request = (MainThreadRequest) ar.userObj;
993 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700994 notifyRequester(request);
995 break;
996 case CMD_GET_NETWORK_SELECTION_MODE:
997 request = (MainThreadRequest) msg.obj;
998 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
999 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1000 break;
1001
1002 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1003 ar = (AsyncResult) msg.obj;
1004 request = (MainThreadRequest) ar.userObj;
1005 if (ar.exception != null) {
1006 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1007 } else {
1008 int mode = ((int[]) ar.result)[0];
1009 if (mode == 0) {
1010 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1011 } else {
1012 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1013 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001014 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001015 notifyRequester(request);
1016 break;
1017 case CMD_GET_CDMA_ROAMING_MODE:
1018 request = (MainThreadRequest) msg.obj;
1019 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1020 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1021 break;
1022 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1023 ar = (AsyncResult) msg.obj;
1024 request = (MainThreadRequest) ar.userObj;
1025 if (ar.exception != null) {
1026 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1027 } else {
1028 request.result = ((int[]) ar.result)[0];
1029 }
1030 notifyRequester(request);
1031 break;
1032 case CMD_SET_CDMA_ROAMING_MODE:
1033 request = (MainThreadRequest) msg.obj;
1034 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1035 int mode = (int) request.argument;
1036 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1037 break;
1038 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1039 ar = (AsyncResult) msg.obj;
1040 request = (MainThreadRequest) ar.userObj;
1041 request.result = ar.exception == null;
1042 notifyRequester(request);
1043 break;
1044 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1045 request = (MainThreadRequest) msg.obj;
1046 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1047 int subscriptionMode = (int) request.argument;
1048 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1049 break;
1050 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1051 ar = (AsyncResult) msg.obj;
1052 request = (MainThreadRequest) ar.userObj;
1053 request.result = ar.exception == null;
1054 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001055 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001056 case CMD_GET_ALL_CELL_INFO:
1057 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001058 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001059 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001060 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001061 case EVENT_GET_ALL_CELL_INFO_DONE:
1062 ar = (AsyncResult) msg.obj;
1063 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001064 // If a timeout occurs, the response will be null
1065 request.result = (ar.exception == null && ar.result != null)
1066 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001067 synchronized (request) {
1068 request.notifyAll();
1069 }
1070 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001071 case CMD_REQUEST_CELL_INFO_UPDATE:
1072 request = (MainThreadRequest) msg.obj;
1073 request.phone.requestCellInfoUpdate(request.workSource,
1074 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1075 break;
1076 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1077 ar = (AsyncResult) msg.obj;
1078 request = (MainThreadRequest) ar.userObj;
1079 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1080 try {
1081 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001082 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Nathan Harolde82c4b82018-12-18 19:40:37 -08001083 cb.onError(TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1084 new android.os.ParcelableException(ar.exception));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001085 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001086 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Nathan Harolde82c4b82018-12-18 19:40:37 -08001087 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001088 } else {
1089 // use the result as returned
1090 cb.onCellInfo((List<CellInfo>) ar.result);
1091 }
1092 } catch (RemoteException re) {
1093 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1094 }
1095 break;
1096 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001097 request = (MainThreadRequest) msg.obj;
1098 WorkSource ws = (WorkSource) request.argument;
1099 Phone phone = getPhoneFromRequest(request);
1100 phone.getCellLocation(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
1101 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001102 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001103 ar = (AsyncResult) msg.obj;
1104 request = (MainThreadRequest) ar.userObj;
1105 if (ar.exception == null) {
1106 request.result = ar.result;
1107 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001108 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001109 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
1110 ? new CdmaCellLocation() : new GsmCellLocation();
1111 }
1112
1113 synchronized (request) {
1114 request.notifyAll();
1115 }
1116 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001117 case CMD_MODEM_REBOOT:
1118 request = (MainThreadRequest) msg.obj;
1119 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001120 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001121 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001122 case EVENT_CMD_MODEM_REBOOT_DONE:
1123 handleNullReturnEvent(msg, "rebootModem");
1124 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001125 case CMD_REQUEST_ENABLE_MODEM:
1126 request = (MainThreadRequest) msg.obj;
1127 boolean enable = (boolean) request.argument;
1128 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001129 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001130 PhoneConfigurationManager.getInstance()
1131 .enablePhone(request.phone, enable, onCompleted);
1132 break;
1133 case EVENT_ENABLE_MODEM_DONE:
1134 ar = (AsyncResult) msg.obj;
1135 request = (MainThreadRequest) ar.userObj;
1136 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001137 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001138 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001139 if ((boolean) request.result) {
1140 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1141 updateModemStateMetrics();
1142 } else {
1143 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1144 + ar.exception);
1145 }
1146 notifyRequester(request);
1147 break;
1148 case CMD_GET_MODEM_STATUS:
1149 request = (MainThreadRequest) msg.obj;
1150 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1151 PhoneConfigurationManager.getInstance()
1152 .getPhoneStatusFromModem(request.phone, onCompleted);
1153 break;
1154 case EVENT_GET_MODEM_STATUS_DONE:
1155 ar = (AsyncResult) msg.obj;
1156 request = (MainThreadRequest) ar.userObj;
1157 int id = request.phone.getPhoneId();
1158 if (ar.exception == null && ar.result != null) {
1159 request.result = ar.result;
1160 //update the cache as modem status has changed
1161 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1162 (boolean) request.result);
1163 } else {
1164 // Return true if modem status cannot be retrieved. For most cases,
1165 // modem status is on. And for older version modems, GET_MODEM_STATUS
1166 // and disable modem are not supported. Modem is always on.
1167 // TODO: this should be fixed in R to support a third
1168 // status UNKNOWN b/131631629
1169 request.result = true;
1170 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1171 + ar.exception);
1172 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001173 notifyRequester(request);
1174 break;
Naina Nalluri1264a9f2019-09-17 14:10:30 -07001175 case CMD_ERASE_MODEM_CONFIG:
1176 request = (MainThreadRequest) msg.obj;
1177 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1178 defaultPhone.eraseModemConfig(onCompleted);
1179 break;
1180 case EVENT_ERASE_MODEM_CONFIG_DONE:
1181 handleNullReturnEvent(msg, "eraseModemConfig");
1182 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183 default:
1184 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1185 break;
1186 }
1187 }
Jake Hambye994d462014-02-03 13:10:13 -08001188
Pengquan Menga1bb6272018-09-06 09:59:22 -07001189 private void notifyRequester(MainThreadRequest request) {
1190 synchronized (request) {
1191 request.notifyAll();
1192 }
1193 }
1194
Jake Hambye994d462014-02-03 13:10:13 -08001195 private void handleNullReturnEvent(Message msg, String command) {
1196 AsyncResult ar = (AsyncResult) msg.obj;
1197 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1198 if (ar.exception == null) {
1199 request.result = true;
1200 } else {
1201 request.result = false;
1202 if (ar.exception instanceof CommandException) {
1203 loge(command + ": CommandException: " + ar.exception);
1204 } else {
1205 loge(command + ": Unknown exception");
1206 }
1207 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001208 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001209 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001210 }
1211
1212 /**
1213 * Posts the specified command to be executed on the main thread,
1214 * waits for the request to complete, and returns the result.
1215 * @see #sendRequestAsync
1216 */
1217 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001218 return sendRequest(
1219 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001220 }
1221
1222 /**
1223 * Posts the specified command to be executed on the main thread,
1224 * waits for the request to complete, and returns the result.
1225 * @see #sendRequestAsync
1226 */
1227 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1228 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001229 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001230 }
1231
1232 /**
1233 * Posts the specified command to be executed on the main thread,
1234 * waits for the request to complete, and returns the result.
1235 * @see #sendRequestAsync
1236 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001237 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001238 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001239 }
1240
1241 /**
1242 * Posts the specified command to be executed on the main thread,
1243 * waits for the request to complete, and returns the result.
1244 * @see #sendRequestAsync
1245 */
Nathan Harold92bed182018-10-12 18:16:49 -07001246 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1247 return sendRequest(command, argument, subId, null, workSource);
1248 }
1249
1250 /**
1251 * Posts the specified command to be executed on the main thread,
1252 * waits for the request to complete, and returns the result.
1253 * @see #sendRequestAsync
1254 */
1255 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1256 return sendRequest(
1257 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1258 }
1259
1260 /**
1261 * Posts the specified command to be executed on the main thread,
1262 * waits for the request to complete, and returns the result.
1263 * @see #sendRequestAsync
1264 */
1265 private Object sendRequest(
1266 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001267 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1268 throw new RuntimeException("This method will deadlock if called from the main thread.");
1269 }
1270
Nathan Harold92bed182018-10-12 18:16:49 -07001271 MainThreadRequest request = null;
1272 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1273 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1274 } else if (phone != null) {
1275 request = new MainThreadRequest(argument, phone, workSource);
1276 } else {
1277 request = new MainThreadRequest(argument, subId, workSource);
1278 }
1279
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280 Message msg = mMainThreadHandler.obtainMessage(command, request);
1281 msg.sendToTarget();
1282
1283 // Wait for the request to complete
1284 synchronized (request) {
1285 while (request.result == null) {
1286 try {
1287 request.wait();
1288 } catch (InterruptedException e) {
1289 // Do nothing, go back and wait until the request is complete
1290 }
1291 }
1292 }
1293 return request.result;
1294 }
1295
1296 /**
1297 * Asynchronous ("fire and forget") version of sendRequest():
1298 * Posts the specified command to be executed on the main thread, and
1299 * returns immediately.
1300 * @see #sendRequest
1301 */
1302 private void sendRequestAsync(int command) {
1303 mMainThreadHandler.sendEmptyMessage(command);
1304 }
1305
1306 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001307 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001308 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001309 */
1310 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001311 sendRequestAsync(command, argument, null, null);
1312 }
1313
1314 /**
1315 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1316 * @see {@link #sendRequest(int,Object)}
1317 */
1318 private void sendRequestAsync(
1319 int command, Object argument, Phone phone, WorkSource workSource) {
1320 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001321 Message msg = mMainThreadHandler.obtainMessage(command, request);
1322 msg.sendToTarget();
1323 }
1324
1325 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 * Initialize the singleton PhoneInterfaceManager instance.
1327 * This is only done once, at startup, from PhoneApp.onCreate().
1328 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001329 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001330 synchronized (PhoneInterfaceManager.class) {
1331 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001332 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 } else {
1334 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1335 }
1336 return sInstance;
1337 }
1338 }
1339
1340 /** Private constructor; @see init() */
Jordan Liua39e6c12020-03-04 13:59:23 -08001341 @VisibleForTesting
1342 /* package */ PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001345 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1347 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001348 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001349 mTelephonySharedPreferences =
1350 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001351 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001352 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001353
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 publish();
1355 }
1356
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001357 private Phone getDefaultPhone() {
1358 Phone thePhone = getPhone(getDefaultSubscription());
1359 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1360 }
1361
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 private void publish() {
1363 if (DBG) log("publish: " + this);
1364
1365 ServiceManager.addService("phone", this);
1366 }
1367
Stuart Scott584921c2015-01-15 17:10:34 -08001368 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001369 if (request.phone != null) {
1370 return request.phone;
1371 } else {
1372 return getPhoneFromSubId(request.subId);
1373 }
1374 }
1375
1376 private Phone getPhoneFromSubId(int subId) {
1377 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1378 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001379 }
1380
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001381 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1382 Phone phone = getPhoneFromRequest(request);
1383 return phone == null ? null :
1384 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1385 }
1386
Wink Saville36469e72014-06-11 15:17:00 -07001387 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001388 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001389 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001390 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391
Naina Nalluri1264a9f2019-09-17 14:10:30 -07001392 private void sendEraseModemConfig(Phone phone) {
1393 if (phone != null) {
1394 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1395 mApp, phone.getSubId(), "eraseModemConfig");
1396 final long identity = Binder.clearCallingIdentity();
1397 try {
1398 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1399 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1400 } finally {
1401 Binder.restoreCallingIdentity(identity);
1402 }
1403 }
1404 }
1405
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001407 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001408 }
1409
Wink Savilleb564aae2014-10-23 10:18:09 -07001410 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 if (DBG) log("dial: " + number);
1412 // No permission check needed here: This is just a wrapper around the
1413 // ACTION_DIAL intent, which is available to any app since it puts up
1414 // the UI before it does anything.
1415
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001416 final long identity = Binder.clearCallingIdentity();
1417 try {
1418 String url = createTelUrl(number);
1419 if (url == null) {
1420 return;
1421 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001423 // PENDING: should we just silently fail if phone is offhook or ringing?
1424 PhoneConstants.State state = mCM.getState(subId);
1425 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1426 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1427 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1428 mApp.startActivity(intent);
1429 }
1430 } finally {
1431 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
1433 }
1434
1435 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001436 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001437 }
1438
Wink Savilleb564aae2014-10-23 10:18:09 -07001439 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 if (DBG) log("call: " + number);
1441
1442 // This is just a wrapper around the ACTION_CALL intent, but we still
1443 // need to do a permission check since we're calling startActivity()
1444 // from the context of the phone app.
1445 enforceCallPermission();
1446
1447 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1448 != AppOpsManager.MODE_ALLOWED) {
1449 return;
1450 }
1451
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001452 final long identity = Binder.clearCallingIdentity();
1453 try {
1454 String url = createTelUrl(number);
1455 if (url == null) {
1456 return;
1457 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001459 boolean isValid = false;
1460 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1461 if (slist != null) {
1462 for (SubscriptionInfo subInfoRecord : slist) {
1463 if (subInfoRecord.getSubscriptionId() == subId) {
1464 isValid = true;
1465 break;
1466 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001467 }
Wink Saville08874612014-08-31 19:19:58 -07001468 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001469 if (!isValid) {
1470 return;
1471 }
Wink Saville08874612014-08-31 19:19:58 -07001472
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001473 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1474 intent.putExtra(SUBSCRIPTION_KEY, subId);
1475 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1476 mApp.startActivity(intent);
1477 } finally {
1478 Binder.restoreCallingIdentity(identity);
1479 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 }
1481
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001483 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001484 }
1485
Wink Savilleb564aae2014-10-23 10:18:09 -07001486 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001487 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001488 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1489 }
1490
1491 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001492 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001493 }
1494
Wink Savilleb564aae2014-10-23 10:18:09 -07001495 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001496 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001497 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1498 }
1499
1500 /** {@hide} */
1501 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001502 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001503 }
1504
Wink Savilleb564aae2014-10-23 10:18:09 -07001505 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001507
1508 final long identity = Binder.clearCallingIdentity();
1509 try {
1510 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1511 checkSimPin.start();
1512 return checkSimPin.unlockSim(null, pin);
1513 } finally {
1514 Binder.restoreCallingIdentity(identity);
1515 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001516 }
1517
Wink Saville9de0f752013-10-22 19:04:03 -07001518 /** {@hide} */
1519 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001520 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001521 }
1522
Wink Savilleb564aae2014-10-23 10:18:09 -07001523 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001524 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001525
1526 final long identity = Binder.clearCallingIdentity();
1527 try {
1528 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1529 checkSimPuk.start();
1530 return checkSimPuk.unlockSim(puk, pin);
1531 } finally {
1532 Binder.restoreCallingIdentity(identity);
1533 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 }
1535
1536 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001537 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 * a synchronous one.
1539 */
1540 private static class UnlockSim extends Thread {
1541
1542 private final IccCard mSimCard;
1543
1544 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001545 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1546 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547
1548 // For replies from SimCard interface
1549 private Handler mHandler;
1550
1551 // For async handler to identify request type
1552 private static final int SUPPLY_PIN_COMPLETE = 100;
1553
1554 public UnlockSim(IccCard simCard) {
1555 mSimCard = simCard;
1556 }
1557
1558 @Override
1559 public void run() {
1560 Looper.prepare();
1561 synchronized (UnlockSim.this) {
1562 mHandler = new Handler() {
1563 @Override
1564 public void handleMessage(Message msg) {
1565 AsyncResult ar = (AsyncResult) msg.obj;
1566 switch (msg.what) {
1567 case SUPPLY_PIN_COMPLETE:
1568 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1569 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001570 mRetryCount = msg.arg1;
1571 if (ar.exception != null) {
1572 if (ar.exception instanceof CommandException &&
1573 ((CommandException)(ar.exception)).getCommandError()
1574 == CommandException.Error.PASSWORD_INCORRECT) {
1575 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1576 } else {
1577 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1578 }
1579 } else {
1580 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1581 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 mDone = true;
1583 UnlockSim.this.notifyAll();
1584 }
1585 break;
1586 }
1587 }
1588 };
1589 UnlockSim.this.notifyAll();
1590 }
1591 Looper.loop();
1592 }
1593
1594 /*
1595 * Use PIN or PUK to unlock SIM card
1596 *
1597 * If PUK is null, unlock SIM card with PIN
1598 *
1599 * If PUK is not null, unlock SIM card with PUK and set PIN code
1600 */
Wink Saville9de0f752013-10-22 19:04:03 -07001601 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001602
1603 while (mHandler == null) {
1604 try {
1605 wait();
1606 } catch (InterruptedException e) {
1607 Thread.currentThread().interrupt();
1608 }
1609 }
1610 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1611
1612 if (puk == null) {
1613 mSimCard.supplyPin(pin, callback);
1614 } else {
1615 mSimCard.supplyPuk(puk, pin, callback);
1616 }
1617
1618 while (!mDone) {
1619 try {
1620 Log.d(LOG_TAG, "wait for done");
1621 wait();
1622 } catch (InterruptedException e) {
1623 // Restore the interrupted status
1624 Thread.currentThread().interrupt();
1625 }
1626 }
1627 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001628 int[] resultArray = new int[2];
1629 resultArray[0] = mResult;
1630 resultArray[1] = mRetryCount;
1631 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001632 }
1633 }
1634
1635 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001636 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001637
1638 }
1639
Wink Savilleb564aae2014-10-23 10:18:09 -07001640 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001641 // No permission check needed here: this call is harmless, and it's
1642 // needed for the ServiceState.requestStateUpdate() call (which is
1643 // already intentionally exposed to 3rd parties.)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001644 final long identity = Binder.clearCallingIdentity();
1645 try {
1646 final Phone phone = getPhone(subId);
1647 if (phone != null) {
1648 phone.updateServiceLocation();
1649 }
1650 } finally {
1651 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001652 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001653 }
1654
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001655 @Override
1656 public boolean isRadioOn(String callingPackage) {
1657 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001658 }
1659
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001660 @Override
1661 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001662 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001663 mApp, subId, callingPackage, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001664 return false;
1665 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001666
1667 final long identity = Binder.clearCallingIdentity();
1668 try {
1669 return isRadioOnForSubscriber(subId);
1670 } finally {
1671 Binder.restoreCallingIdentity(identity);
1672 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001673 }
1674
1675 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001676 final long identity = Binder.clearCallingIdentity();
1677 try {
1678 final Phone phone = getPhone(subId);
1679 if (phone != null) {
1680 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1681 } else {
1682 return false;
1683 }
1684 } finally {
1685 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001686 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 }
1688
1689 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001690 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001691 }
Wink Saville36469e72014-06-11 15:17:00 -07001692
Wink Savilleb564aae2014-10-23 10:18:09 -07001693 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001694 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001695
1696 final long identity = Binder.clearCallingIdentity();
1697 try {
1698 final Phone phone = getPhone(subId);
1699 if (phone != null) {
1700 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1701 }
1702 } finally {
1703 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001704 }
Wink Saville36469e72014-06-11 15:17:00 -07001705 }
1706
1707 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001708 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001709 }
1710
Wink Savilleb564aae2014-10-23 10:18:09 -07001711 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001712 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001713
1714 final long identity = Binder.clearCallingIdentity();
1715 try {
1716 final Phone phone = getPhone(subId);
1717 if (phone == null) {
1718 return false;
1719 }
1720 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1721 toggleRadioOnOffForSubscriber(subId);
1722 }
1723 return true;
1724 } finally {
1725 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001726 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001727 }
Wink Saville36469e72014-06-11 15:17:00 -07001728
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001729 public boolean needMobileRadioShutdown() {
1730 /*
1731 * If any of the Radios are available, it will need to be
1732 * shutdown. So return true if any Radio is available.
1733 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001734 final long identity = Binder.clearCallingIdentity();
1735 try {
1736 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1737 Phone phone = PhoneFactory.getPhone(i);
1738 if (phone != null && phone.isRadioAvailable()) return true;
1739 }
1740 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1741 return false;
1742 } finally {
1743 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001744 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001745 }
1746
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001747 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001748 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001749 enforceModifyPermission();
1750
1751 final long identity = Binder.clearCallingIdentity();
1752 try {
1753 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1754 logv("Shutting down Phone " + i);
1755 shutdownRadioUsingPhoneId(i);
1756 }
1757 } finally {
1758 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001759 }
1760 }
1761
1762 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001763 Phone phone = PhoneFactory.getPhone(phoneId);
1764 if (phone != null && phone.isRadioAvailable()) {
1765 phone.shutdownRadio();
1766 }
1767 }
1768
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001769 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001770 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001771
1772 final long identity = Binder.clearCallingIdentity();
1773 try {
1774 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1775 if (defaultPhone != null) {
1776 defaultPhone.setRadioPower(turnOn);
1777 return true;
1778 } else {
1779 loge("There's no default phone.");
1780 return false;
1781 }
1782 } finally {
1783 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001784 }
Wink Saville36469e72014-06-11 15:17:00 -07001785 }
1786
Wink Savilleb564aae2014-10-23 10:18:09 -07001787 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001788 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001789
1790 final long identity = Binder.clearCallingIdentity();
1791 try {
1792 final Phone phone = getPhone(subId);
1793 if (phone != null) {
1794 phone.setRadioPower(turnOn);
1795 return true;
1796 } else {
1797 return false;
1798 }
1799 } finally {
1800 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001801 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001802 }
1803
Wink Saville36469e72014-06-11 15:17:00 -07001804 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001805 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 public boolean enableDataConnectivity() {
1807 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001808
1809 final long identity = Binder.clearCallingIdentity();
1810 try {
1811 int subId = mSubscriptionController.getDefaultDataSubId();
1812 final Phone phone = getPhone(subId);
1813 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001814 phone.getDataEnabledSettings().setUserDataEnabled(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001815 return true;
1816 } else {
1817 return false;
1818 }
1819 } finally {
1820 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001821 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001822 }
1823
Wink Saville36469e72014-06-11 15:17:00 -07001824 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001825 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001826 public boolean disableDataConnectivity() {
1827 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001828
1829 final long identity = Binder.clearCallingIdentity();
1830 try {
1831 int subId = mSubscriptionController.getDefaultDataSubId();
1832 final Phone phone = getPhone(subId);
1833 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08001834 phone.getDataEnabledSettings().setUserDataEnabled(false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001835 return true;
1836 } else {
1837 return false;
1838 }
1839 } finally {
1840 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001841 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001842 }
1843
Sanket Padawe356d7632015-06-22 14:03:32 -07001844 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001845 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001846 final long identity = Binder.clearCallingIdentity();
1847 try {
1848 final Phone phone = getPhone(subId);
1849 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08001850 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001851 } else {
1852 return false;
1853 }
1854 } finally {
1855 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001856 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001857 }
1858
1859 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001860 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001861 }
1862
pkanwarae03a6b2016-11-06 20:37:09 -08001863 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001864 enforceCallPermission();
1865
1866 final long identity = Binder.clearCallingIdentity();
1867 try {
1868 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1869 return;
1870 }
1871 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1872 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1873 } finally {
1874 Binder.restoreCallingIdentity(identity);
1875 }
pkanwar32d516d2016-10-14 19:37:38 -07001876 };
1877
Wink Savilleb564aae2014-10-23 10:18:09 -07001878 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001879 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001880
1881 final long identity = Binder.clearCallingIdentity();
1882 try {
1883 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1884 return false;
1885 }
1886 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1887 } finally {
1888 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001889 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001890 }
1891
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001892 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001893 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001894 }
1895
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001896 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001897 final long identity = Binder.clearCallingIdentity();
1898 try {
1899 Phone phone = PhoneFactory.getPhone(slotIndex);
1900 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1901 PhoneConstantConversions.convertCallState(phone.getState());
1902 } finally {
1903 Binder.restoreCallingIdentity(identity);
1904 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001905 }
1906
Sanket Padawe356d7632015-06-22 14:03:32 -07001907 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001908 public int getDataState() {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001909 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
1910 }
1911
1912 @Override
1913 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001914 final long identity = Binder.clearCallingIdentity();
1915 try {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001916 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001917 if (phone != null) {
1918 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1919 } else {
1920 return PhoneConstantConversions.convertDataState(
1921 PhoneConstants.DataState.DISCONNECTED);
1922 }
1923 } finally {
1924 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001926 }
1927
Sanket Padawe356d7632015-06-22 14:03:32 -07001928 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001929 public int getDataActivity() {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001930 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
1931 }
1932
1933 @Override
1934 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001935 final long identity = Binder.clearCallingIdentity();
1936 try {
Nathan Haroldcbce7262019-06-14 16:58:30 -07001937 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001938 if (phone != null) {
1939 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1940 } else {
1941 return TelephonyManager.DATA_ACTIVITY_NONE;
1942 }
1943 } finally {
1944 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001945 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001946 }
1947
1948 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001949 public Bundle getCellLocation(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001950 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08001951 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08001952
1953 LocationAccessPolicy.LocationPermissionResult locationResult =
1954 LocationAccessPolicy.checkLocationPermission(mApp,
1955 new LocationAccessPolicy.LocationPermissionQuery.Builder()
1956 .setCallingPackage(callingPackage)
1957 .setCallingPid(Binder.getCallingPid())
1958 .setCallingUid(Binder.getCallingUid())
1959 .setMethod("getCellLocation")
Hall Liuc3f8eb62020-01-24 18:07:12 -08001960 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08001961 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
1962 .build());
1963 switch (locationResult) {
1964 case DENIED_HARD:
1965 throw new SecurityException("Not allowed to access cell location");
1966 case DENIED_SOFT:
1967 return new Bundle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001968 }
1969
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001970 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001971 final long identity = Binder.clearCallingIdentity();
1972 try {
1973 if (DBG_LOC) log("getCellLocation: is active user");
1974 Bundle data = new Bundle();
Nathan Harold3ff88932018-08-14 10:19:49 -07001975 int subId = mSubscriptionController.getDefaultDataSubId();
1976 CellLocation cl = (CellLocation) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
1977 cl.fillInNotifierBundle(data);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001978 return data;
1979 } finally {
1980 Binder.restoreCallingIdentity(identity);
1981 }
Svetoslav64fad262015-04-14 14:35:21 -07001982 }
1983
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001984 @Override
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001985 public String getNetworkCountryIsoForPhone(int phoneId) {
1986 // Reporting the correct network country is ambiguous when IWLAN could conflict with
1987 // registered cell info, so return a NULL country instead.
1988 final long identity = Binder.clearCallingIdentity();
1989 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07001990 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
1991 // Get default phone in this case.
1992 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
1993 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001994 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07001995 // Todo: fix this when we can get the actual cellular network info when the device
1996 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001997 if (TelephonyManager.NETWORK_TYPE_IWLAN
1998 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
1999 return "";
2000 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002001 Phone phone = PhoneFactory.getPhone(phoneId);
2002 if (phone != null) {
2003 ServiceStateTracker sst = phone.getServiceStateTracker();
sqianb9d961a2019-07-31 20:23:45 -07002004 EmergencyNumberTracker emergencyNumberTracker = phone.getEmergencyNumberTracker();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002005 if (sst != null) {
2006 LocaleTracker lt = sst.getLocaleTracker();
2007 if (lt != null) {
sqianb9d961a2019-07-31 20:23:45 -07002008 if (!TextUtils.isEmpty(lt.getCurrentCountry())) {
2009 return lt.getCurrentCountry();
2010 } else if (emergencyNumberTracker != null) {
2011 return emergencyNumberTracker.getEmergencyCountryIso();
2012 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002013 }
2014 }
2015 }
2016 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002017 } finally {
2018 Binder.restoreCallingIdentity(identity);
2019 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002020 }
2021
2022 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002023 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002024 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002025 }
2026
Sanket Padawe356d7632015-06-22 14:03:32 -07002027 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002028 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002029 mApp.enforceCallingOrSelfPermission(
2030 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002031
2032 final long identity = Binder.clearCallingIdentity();
2033 try {
2034 final Phone phone = getPhone(subId);
2035 if (phone != null) {
2036 phone.enableLocationUpdates();
2037 }
2038 } finally {
2039 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002040 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002041 }
2042
2043 @Override
2044 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002045 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002046 }
2047
Sanket Padawe356d7632015-06-22 14:03:32 -07002048 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002049 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002050 mApp.enforceCallingOrSelfPermission(
2051 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002052
2053 final long identity = Binder.clearCallingIdentity();
2054 try {
2055 final Phone phone = getPhone(subId);
2056 if (phone != null) {
2057 phone.disableLocationUpdates();
2058 }
2059 } finally {
2060 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002061 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002062 }
2063
Nathan Harold31d7ff32018-10-15 20:20:30 -07002064 /**
2065 * Returns the target SDK version number for a given package name.
2066 *
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002067 * This call MUST be invoked before clearing the calling UID.
2068 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002069 * @return target SDK if the package is found or INT_MAX.
2070 */
2071 private int getTargetSdk(String packageName) {
2072 try {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002073 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
2074 packageName, 0, UserHandle.getUserId(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002075 if (ai != null) return ai.targetSdkVersion;
2076 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldba61eeb2019-07-10 17:04:16 -07002077 loge("Failed to get package info for pkg="
2078 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002079 }
2080 return Integer.MAX_VALUE;
2081 }
2082
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002083 @Override
2084 @SuppressWarnings("unchecked")
Nathan Harold31d7ff32018-10-15 20:20:30 -07002085 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
2086 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002087 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2088 throw new SecurityException(
2089 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2090 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002091
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002092 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
2093 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2094 return null;
2095 }
Svetoslav64fad262015-04-14 14:35:21 -07002096
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002097 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002098
Nathan Haroldf180aac2018-06-01 18:43:55 -07002099 List<CellInfo> info = getAllCellInfo(callingPackage);
2100 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002101
Nathan Haroldf180aac2018-06-01 18:43:55 -07002102 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2103 for (CellInfo ci : info) {
2104 if (ci instanceof CellInfoGsm) {
2105 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2106 } else if (ci instanceof CellInfoWcdma) {
2107 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2108 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002109 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002110 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002111 }
2112
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002113 private List<CellInfo> getCachedCellInfo() {
2114 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2115 for (Phone phone : PhoneFactory.getPhones()) {
2116 List<CellInfo> info = phone.getAllCellInfo();
2117 if (info != null) cellInfos.addAll(info);
2118 }
2119 return cellInfos;
2120 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002121
2122 @Override
Svetoslav64fad262015-04-14 14:35:21 -07002123 public List<CellInfo> getAllCellInfo(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002124 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002125 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002126
2127 LocationAccessPolicy.LocationPermissionResult locationResult =
2128 LocationAccessPolicy.checkLocationPermission(mApp,
2129 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2130 .setCallingPackage(callingPackage)
2131 .setCallingPid(Binder.getCallingPid())
2132 .setCallingUid(Binder.getCallingUid())
2133 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002134 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002135 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2136 .build());
2137 switch (locationResult) {
2138 case DENIED_HARD:
2139 throw new SecurityException("Not allowed to access cell info");
2140 case DENIED_SOFT:
2141 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002142 }
2143
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002144 final int targetSdk = getTargetSdk(callingPackage);
2145 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2146 return getCachedCellInfo();
2147 }
2148
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002149 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002150 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002151 final long identity = Binder.clearCallingIdentity();
2152 try {
2153 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2154 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002155 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002156 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002157 if (info != null) cellInfos.addAll(info);
2158 }
2159 return cellInfos;
2160 } finally {
2161 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002162 }
2163 }
2164
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002165 @Override
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002166 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage) {
2167 requestCellInfoUpdateInternal(
2168 subId, cb, callingPackage, getWorkSource(Binder.getCallingUid()));
2169 }
2170
2171 @Override
2172 public void requestCellInfoUpdateWithWorkSource(
2173 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
2174 enforceModifyPermission();
2175 requestCellInfoUpdateInternal(subId, cb, callingPackage, workSource);
2176 }
2177
2178 private void requestCellInfoUpdateInternal(
2179 int subId, ICellInfoCallback cb, String callingPackage, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002180 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002181 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002182
2183 LocationAccessPolicy.LocationPermissionResult locationResult =
2184 LocationAccessPolicy.checkLocationPermission(mApp,
2185 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2186 .setCallingPackage(callingPackage)
2187 .setCallingPid(Binder.getCallingPid())
2188 .setCallingUid(Binder.getCallingUid())
2189 .setMethod("requestCellInfoUpdate")
2190 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2191 .build());
2192 switch (locationResult) {
2193 case DENIED_HARD:
2194 throw new SecurityException("Not allowed to access cell info");
2195 case DENIED_SOFT:
Nathan Haroldb3c5da42019-05-09 10:26:08 -07002196 try {
2197 cb.onCellInfo(new ArrayList<CellInfo>());
2198 } catch (RemoteException re) {
2199 // Drop without consequences
2200 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002201 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002202 }
2203
Nathan Harold32e84c42019-05-09 10:13:47 -07002204
2205 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002206 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2207
2208 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2209 }
2210
2211 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002212 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002213 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002214 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002215
2216 final long identity = Binder.clearCallingIdentity();
2217 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002218 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002219 } finally {
2220 Binder.restoreCallingIdentity(identity);
2221 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002222 }
2223
Shishir Agrawala9f32182016-04-12 12:00:16 -07002224 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002225 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002226 Phone phone = PhoneFactory.getPhone(slotIndex);
2227 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002228 return null;
2229 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002230 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002231 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2232 callingPackage, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002233 return null;
2234 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002235
2236 final long identity = Binder.clearCallingIdentity();
2237 try {
2238 return phone.getImei();
2239 } finally {
2240 Binder.restoreCallingIdentity(identity);
2241 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002242 }
2243
2244 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002245 public String getTypeAllocationCodeForSlot(int slotIndex) {
2246 Phone phone = PhoneFactory.getPhone(slotIndex);
2247 String tac = null;
2248 if (phone != null) {
2249 String imei = phone.getImei();
2250 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2251 }
2252 return tac;
2253 }
2254
2255 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002256 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002257 Phone phone = PhoneFactory.getPhone(slotIndex);
2258 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002259 return null;
2260 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002261
Jeff Davidson913390f2018-02-23 17:11:49 -08002262 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002263 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
2264 callingPackage, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002265 return null;
2266 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002267
2268 final long identity = Binder.clearCallingIdentity();
2269 try {
2270 return phone.getMeid();
2271 } finally {
2272 Binder.restoreCallingIdentity(identity);
2273 }
Jack Yu2af8d712017-03-15 17:14:14 -07002274 }
2275
2276 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002277 public String getManufacturerCodeForSlot(int slotIndex) {
2278 Phone phone = PhoneFactory.getPhone(slotIndex);
2279 String manufacturerCode = null;
2280 if (phone != null) {
2281 String meid = phone.getMeid();
2282 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2283 }
2284 return manufacturerCode;
2285 }
2286
2287 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002288 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002289 Phone phone = PhoneFactory.getPhone(slotIndex);
2290 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002291 return null;
2292 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002293 int subId = phone.getSubId();
2294 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2295 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2296 return null;
2297 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002298
2299 final long identity = Binder.clearCallingIdentity();
2300 try {
2301 return phone.getDeviceSvn();
2302 } finally {
2303 Binder.restoreCallingIdentity(identity);
2304 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002305 }
2306
fionaxu43304da2017-11-27 22:51:16 -08002307 @Override
2308 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002309 final long identity = Binder.clearCallingIdentity();
2310 try {
2311 final Phone phone = getPhone(subId);
2312 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2313 } finally {
2314 Binder.restoreCallingIdentity(identity);
2315 }
fionaxu43304da2017-11-27 22:51:16 -08002316 }
2317
2318 @Override
2319 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002320 final long identity = Binder.clearCallingIdentity();
2321 try {
2322 final Phone phone = getPhone(subId);
2323 return phone == null ? null : phone.getCarrierName();
2324 } finally {
2325 Binder.restoreCallingIdentity(identity);
2326 }
fionaxu43304da2017-11-27 22:51:16 -08002327 }
2328
calvinpanffe225e2018-11-01 19:43:06 +08002329 @Override
chen xu0026ca62019-03-06 15:28:50 -08002330 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002331 final long identity = Binder.clearCallingIdentity();
2332 try {
2333 final Phone phone = getPhone(subId);
2334 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002335 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002336 } finally {
2337 Binder.restoreCallingIdentity(identity);
2338 }
2339 }
2340
2341 @Override
chen xu0026ca62019-03-06 15:28:50 -08002342 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002343 final long identity = Binder.clearCallingIdentity();
2344 try {
2345 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002346 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002347 } finally {
2348 Binder.restoreCallingIdentity(identity);
2349 }
2350 }
2351
chen xu651eec72018-11-11 19:03:44 -08002352 @Override
chen xu864e11c2018-12-06 22:10:03 -08002353 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2354 if (!isSubscriptionMccMnc) {
2355 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2356 }
chen xu651eec72018-11-11 19:03:44 -08002357 final Phone phone = PhoneFactory.getPhone(slotIndex);
2358 if (phone == null) {
2359 return TelephonyManager.UNKNOWN_CARRIER_ID;
2360 }
2361 final long identity = Binder.clearCallingIdentity();
2362 try {
2363 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2364 } finally {
2365 Binder.restoreCallingIdentity(identity);
2366 }
2367 }
2368
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002369 //
2370 // Internal helper methods.
2371 //
2372
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002373 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002374 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2375 *
2376 * @throws SecurityException if the caller does not have the required permission
2377 */
2378 private void enforceModifyPermission() {
2379 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2380 }
2381
2382 /**
2383 * Make sure the caller has the CALL_PHONE permission.
2384 *
2385 * @throws SecurityException if the caller does not have the required permission
2386 */
2387 private void enforceCallPermission() {
2388 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2389 }
2390
Stuart Scott8eef64f2015-04-08 15:13:54 -07002391 private void enforceConnectivityInternalPermission() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002392 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL,
Stuart Scott8eef64f2015-04-08 15:13:54 -07002393 "ConnectivityService");
2394 }
2395
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002396 private String createTelUrl(String number) {
2397 if (TextUtils.isEmpty(number)) {
2398 return null;
2399 }
2400
Jake Hambye994d462014-02-03 13:10:13 -08002401 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002402 }
2403
Ihab Awadf9e92732013-12-05 18:02:52 -08002404 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002405 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2406 }
2407
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002408 private static void logv(String msg) {
2409 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2410 }
2411
Ihab Awadf9e92732013-12-05 18:02:52 -08002412 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002413 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2414 }
2415
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002416 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002417 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002418 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002419 }
2420
Sanket Padawe356d7632015-06-22 14:03:32 -07002421 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002422 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002423 final long identity = Binder.clearCallingIdentity();
2424 try {
2425 final Phone phone = PhoneFactory.getPhone(slotIndex);
2426 if (phone == null) {
2427 return PhoneConstants.PHONE_TYPE_NONE;
2428 } else {
2429 return phone.getPhoneType();
2430 }
2431 } finally {
2432 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002433 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002434 }
2435
2436 /**
2437 * Returns the CDMA ERI icon index to display
2438 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002439 @Override
2440 public int getCdmaEriIconIndex(String callingPackage) {
2441 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002442 }
2443
Sanket Padawe356d7632015-06-22 14:03:32 -07002444 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002445 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002446 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002447 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002448 return -1;
2449 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002450
2451 final long identity = Binder.clearCallingIdentity();
2452 try {
2453 final Phone phone = getPhone(subId);
2454 if (phone != null) {
2455 return phone.getCdmaEriIconIndex();
2456 } else {
2457 return -1;
2458 }
2459 } finally {
2460 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002461 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002462 }
2463
2464 /**
2465 * Returns the CDMA ERI icon mode,
2466 * 0 - ON
2467 * 1 - FLASHING
2468 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002469 @Override
2470 public int getCdmaEriIconMode(String callingPackage) {
2471 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002472 }
2473
Sanket Padawe356d7632015-06-22 14:03:32 -07002474 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002475 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002476 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002477 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002478 return -1;
2479 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002480
2481 final long identity = Binder.clearCallingIdentity();
2482 try {
2483 final Phone phone = getPhone(subId);
2484 if (phone != null) {
2485 return phone.getCdmaEriIconMode();
2486 } else {
2487 return -1;
2488 }
2489 } finally {
2490 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002491 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002492 }
2493
2494 /**
2495 * Returns the CDMA ERI text,
2496 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002497 @Override
2498 public String getCdmaEriText(String callingPackage) {
2499 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002500 }
2501
Sanket Padawe356d7632015-06-22 14:03:32 -07002502 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002503 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002504 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002505 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002506 return null;
2507 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002508
2509 final long identity = Binder.clearCallingIdentity();
2510 try {
2511 final Phone phone = getPhone(subId);
2512 if (phone != null) {
2513 return phone.getCdmaEriText();
2514 } else {
2515 return null;
2516 }
2517 } finally {
2518 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002519 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002520 }
2521
2522 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002523 * Returns the CDMA MDN.
2524 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002525 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002526 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002527 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2528 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002529
2530 final long identity = Binder.clearCallingIdentity();
2531 try {
2532 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002533 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002534 return phone.getLine1Number();
2535 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002536 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002537 return null;
2538 }
2539 } finally {
2540 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002541 }
2542 }
2543
2544 /**
2545 * Returns the CDMA MIN.
2546 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002547 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002548 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002549 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2550 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002551
2552 final long identity = Binder.clearCallingIdentity();
2553 try {
2554 final Phone phone = getPhone(subId);
2555 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2556 return phone.getCdmaMin();
2557 } else {
2558 return null;
2559 }
2560 } finally {
2561 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002562 }
2563 }
2564
Hall Liud892bec2018-11-30 14:51:45 -08002565 @Override
2566 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2567 INumberVerificationCallback callback, String callingPackage) {
2568 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2569 != PERMISSION_GRANTED) {
2570 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2571 }
2572 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2573
2574 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2575 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2576 throw new SecurityException("Calling package must be configured in the device config");
2577 }
2578
2579 if (range == null) {
2580 throw new NullPointerException("Range must be non-null");
2581 }
2582
2583 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002584 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002585
2586 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
2587 }
2588
Junda Liuca05d5d2014-08-14 22:36:34 -07002589 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002590 * Returns true if CDMA provisioning needs to run.
2591 */
2592 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002593 final long identity = Binder.clearCallingIdentity();
2594 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002595 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002596 } finally {
2597 Binder.restoreCallingIdentity(identity);
2598 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002599 }
2600
2601 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002602 * Sets the voice mail number of a given subId.
2603 */
2604 @Override
2605 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002606 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002607
2608 final long identity = Binder.clearCallingIdentity();
2609 try {
2610 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2611 new Pair<String, String>(alphaTag, number), new Integer(subId));
2612 return success;
2613 } finally {
2614 Binder.restoreCallingIdentity(identity);
2615 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002616 }
2617
Ta-wei Yen87c49842016-05-13 21:19:52 -07002618 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002619 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2620 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002621 String systemDialer = TelecomManager.from(mApp).getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002622 if (!TextUtils.equals(callingPackage, systemDialer)) {
2623 throw new SecurityException("caller must be system dialer");
2624 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002625
2626 final long identity = Binder.clearCallingIdentity();
2627 try {
2628 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2629 if (phoneAccountHandle == null) {
2630 return null;
2631 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002632 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002633 } finally {
2634 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002635 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002636 }
2637
2638 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002639 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002640 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002641 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002642 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002643 return null;
2644 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002645
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002646 final long identity = Binder.clearCallingIdentity();
2647 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002648 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002649 } finally {
2650 Binder.restoreCallingIdentity(identity);
2651 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002652 }
2653
2654 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002655 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2656 VisualVoicemailSmsFilterSettings settings) {
2657 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002658
2659 final long identity = Binder.clearCallingIdentity();
2660 try {
2661 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002662 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002663 } finally {
2664 Binder.restoreCallingIdentity(identity);
2665 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002666 }
2667
2668 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002669 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2670 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002671
2672 final long identity = Binder.clearCallingIdentity();
2673 try {
2674 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002675 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002676 } finally {
2677 Binder.restoreCallingIdentity(identity);
2678 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002679 }
2680
2681 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002682 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2683 String callingPackage, int subId) {
2684 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002685
2686 final long identity = Binder.clearCallingIdentity();
2687 try {
2688 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002689 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002690 } finally {
2691 Binder.restoreCallingIdentity(identity);
2692 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002693 }
2694
2695 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002696 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002697 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002698
2699 final long identity = Binder.clearCallingIdentity();
2700 try {
2701 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002702 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002703 } finally {
2704 Binder.restoreCallingIdentity(identity);
2705 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002706 }
2707
2708 @Override
2709 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2710 String number, int port, String text, PendingIntent sentIntent) {
2711 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002712 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002713 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07002714 SmsController smsController = PhoneFactory.getSmsController();
2715 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, subId, number, port, text,
2716 sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002717 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07002718
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002719 /**
fionaxu0152e512016-11-14 13:36:14 -08002720 * Sets the voice activation state of a given subId.
2721 */
2722 @Override
2723 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002724 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2725 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002726
2727 final long identity = Binder.clearCallingIdentity();
2728 try {
2729 final Phone phone = getPhone(subId);
2730 if (phone != null) {
2731 phone.setVoiceActivationState(activationState);
2732 } else {
2733 loge("setVoiceActivationState fails with invalid subId: " + subId);
2734 }
2735 } finally {
2736 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002737 }
2738 }
2739
2740 /**
2741 * Sets the data activation state of a given subId.
2742 */
2743 @Override
2744 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002745 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2746 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002747
2748 final long identity = Binder.clearCallingIdentity();
2749 try {
2750 final Phone phone = getPhone(subId);
2751 if (phone != null) {
2752 phone.setDataActivationState(activationState);
2753 } else {
2754 loge("setVoiceActivationState fails with invalid subId: " + subId);
2755 }
2756 } finally {
2757 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002758 }
2759 }
2760
2761 /**
2762 * Returns the voice activation state of a given subId.
2763 */
2764 @Override
2765 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002766 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002767
fionaxu0152e512016-11-14 13:36:14 -08002768 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002769 final long identity = Binder.clearCallingIdentity();
2770 try {
2771 if (phone != null) {
2772 return phone.getVoiceActivationState();
2773 } else {
2774 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2775 }
2776 } finally {
2777 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002778 }
2779 }
2780
2781 /**
2782 * Returns the data activation state of a given subId.
2783 */
2784 @Override
2785 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07002786 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002787
fionaxu0152e512016-11-14 13:36:14 -08002788 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002789 final long identity = Binder.clearCallingIdentity();
2790 try {
2791 if (phone != null) {
2792 return phone.getDataActivationState();
2793 } else {
2794 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2795 }
2796 } finally {
2797 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002798 }
2799 }
2800
2801 /**
Wink Saville36469e72014-06-11 15:17:00 -07002802 * Returns the unread count of voicemails for a subId
2803 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002804 @Override
Brad Ebingerf7664ba2018-11-29 12:43:38 -08002805 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage) {
2806 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2807 mApp, subId, callingPackage, "getVoiceMessageCountForSubscriber")) {
2808 return 0;
2809 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002810 final long identity = Binder.clearCallingIdentity();
2811 try {
2812 final Phone phone = getPhone(subId);
2813 if (phone != null) {
2814 return phone.getVoiceMessageCount();
2815 } else {
2816 return 0;
2817 }
2818 } finally {
2819 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002820 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002821 }
2822
2823 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002824 * returns true, if the device is in a state where both voice and data
2825 * are supported simultaneously. This can change based on location or network condition.
2826 */
2827 @Override
2828 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002829 final long identity = Binder.clearCallingIdentity();
2830 try {
2831 final Phone phone = getPhone(subId);
2832 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2833 } finally {
2834 Binder.restoreCallingIdentity(identity);
2835 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002836 }
2837
2838 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002839 * Send the dialer code if called from the current default dialer or the caller has
2840 * carrier privilege.
2841 * @param inputCode The dialer code to send
2842 */
2843 @Override
2844 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002845 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08002846 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002847 String defaultDialer = TelecomManager.from(defaultPhone.getContext())
2848 .getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08002849 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002850 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2851 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002852 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002853
2854 final long identity = Binder.clearCallingIdentity();
2855 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002856 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002857 } finally {
2858 Binder.restoreCallingIdentity(identity);
2859 }
fionaxu235cc5e2017-03-06 22:25:57 -08002860 }
2861
Pengquan Menga1bb6272018-09-06 09:59:22 -07002862 @Override
2863 public int getNetworkSelectionMode(int subId) {
Pengquan Menge92a50d2018-09-21 15:54:48 -07002864 if (!isActiveSubscription(subId)) {
2865 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
2866 }
2867
Pengquan Menga1bb6272018-09-06 09:59:22 -07002868 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
2869 }
2870
Brad Ebinger35c841c2018-10-01 10:40:55 -07002871 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07002872 public boolean isInEmergencySmsMode() {
2873 enforceReadPrivilegedPermission("isInEmergencySmsMode");
2874 final long identity = Binder.clearCallingIdentity();
2875 try {
2876 for (Phone phone : PhoneFactory.getPhones()) {
2877 if (phone.isInEmergencySmsMode()) {
2878 return true;
2879 }
2880 }
2881 } finally {
2882 Binder.restoreCallingIdentity(identity);
2883 }
2884 return false;
2885 }
2886
2887 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002888 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
2889 throws RemoteException {
2890 enforceReadPrivilegedPermission("registerImsRegistrationCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002891 final long token = Binder.clearCallingIdentity();
2892 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002893 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002894 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002895 .addRegistrationCallbackForSubscription(c, subId);
2896 } finally {
2897 Binder.restoreCallingIdentity(token);
2898 }
2899 }
2900
2901 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002902 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
2903 enforceReadPrivilegedPermission("unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002904 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2905 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2906 }
2907 Binder.withCleanCallingIdentity(() -> {
2908 try {
2909 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002910 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002911 .removeRegistrationCallbackForSubscription(c, subId);
2912 } catch (IllegalArgumentException e) {
2913 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
2914 + "is inactive, ignoring unregister.");
2915 // If the subscription is no longer active, just return, since the callback
2916 // will already have been removed internally.
2917 }
2918 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002919 }
2920
2921 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002922 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
2923 throws RemoteException {
2924 enforceReadPrivilegedPermission("registerMmTelCapabilityCallback");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002925 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2926 final long token = Binder.clearCallingIdentity();
2927 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002928 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07002929 .addCapabilitiesCallbackForSubscription(c, subId);
2930 } finally {
2931 Binder.restoreCallingIdentity(token);
2932 }
2933 }
2934
2935 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002936 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
2937 enforceReadPrivilegedPermission("unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002938
2939 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2940 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
2941 }
2942 Binder.withCleanCallingIdentity(() -> {
2943 try {
2944 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002945 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08002946 .removeCapabilitiesCallbackForSubscription(c, subId);
2947 } catch (IllegalArgumentException e) {
2948 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
2949 + "is inactive, ignoring unregister.");
2950 // If the subscription is no longer active, just return, since the callback
2951 // will already have been removed internally.
2952 }
2953 });
Brad Ebinger35c841c2018-10-01 10:40:55 -07002954 }
2955
2956 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002957 public boolean isCapable(int subId, int capability, int regTech) {
2958 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002959 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2960 final long token = Binder.clearCallingIdentity();
2961 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002962 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002963 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
2964 } catch (ImsException e) {
2965 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
2966 return false;
Brad Ebinger6b5ac222019-02-04 14:36:52 -08002967 } catch (IllegalArgumentException e) {
2968 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
2969 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07002970 } finally {
2971 Binder.restoreCallingIdentity(token);
2972 }
2973 }
2974
2975 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08002976 public boolean isAvailable(int subId, int capability, int regTech) {
2977 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07002978 final long token = Binder.clearCallingIdentity();
2979 try {
2980 Phone phone = getPhone(subId);
2981 if (phone == null) return false;
2982 return phone.isImsCapabilityAvailable(capability, regTech);
2983 } finally {
2984 Binder.restoreCallingIdentity(token);
2985 }
2986 }
2987
2988 @Override
2989 public boolean isAdvancedCallingSettingEnabled(int subId) {
2990 enforceReadPrivilegedPermission("enforceReadPrivilegedPermission");
2991 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
2992 final long token = Binder.clearCallingIdentity();
2993 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002994 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07002995 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
2996 } finally {
2997 Binder.restoreCallingIdentity(token);
2998 }
2999 }
3000
3001 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003002 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003003 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003004 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003005 final long identity = Binder.clearCallingIdentity();
3006 try {
3007 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003008 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003009 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
3010 } finally {
3011 Binder.restoreCallingIdentity(identity);
3012 }
3013 }
3014
3015 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003016 public boolean isVtSettingEnabled(int subId) {
3017 enforceReadPrivilegedPermission("isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003018 final long identity = Binder.clearCallingIdentity();
3019 try {
3020 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003021 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003022 getSlotIndexOrException(subId)).isVtEnabledByUser();
3023 } finally {
3024 Binder.restoreCallingIdentity(identity);
3025 }
3026 }
3027
3028 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003029 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003030 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003031 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003032 final long identity = Binder.clearCallingIdentity();
3033 try {
3034 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003035 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003036 } finally {
3037 Binder.restoreCallingIdentity(identity);
3038 }
3039 }
3040
3041 @Override
3042 public boolean isVoWiFiSettingEnabled(int subId) {
3043 enforceReadPrivilegedPermission("isVoWiFiSettingEnabled");
3044 final long identity = Binder.clearCallingIdentity();
3045 try {
3046 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003047 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003048 getSlotIndexOrException(subId)).isWfcEnabledByUser();
3049 } finally {
3050 Binder.restoreCallingIdentity(identity);
3051 }
3052 }
3053
3054 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003055 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003056 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003057 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003058 final long identity = Binder.clearCallingIdentity();
3059 try {
3060 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003061 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003062 } finally {
3063 Binder.restoreCallingIdentity(identity);
3064 }
3065 }
3066
3067 @Override
3068 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
3069 enforceReadPrivilegedPermission("isVoWiFiRoamingSettingEnabled");
3070 final long identity = Binder.clearCallingIdentity();
3071 try {
3072 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003073 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003074 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
3075 } finally {
3076 Binder.restoreCallingIdentity(identity);
3077 }
3078 }
3079
3080 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003081 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003082 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003083 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003084 final long identity = Binder.clearCallingIdentity();
3085 try {
3086 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003087 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003088 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
3089 } finally {
3090 Binder.restoreCallingIdentity(identity);
3091 }
3092 }
3093
3094 @Override
3095 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3096 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3097 "setVoWiFiNonPersistent");
3098 final long identity = Binder.clearCallingIdentity();
3099 try {
3100 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003101 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003102 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003103 } finally {
3104 Binder.restoreCallingIdentity(identity);
3105 }
3106 }
3107
3108 @Override
3109 public int getVoWiFiModeSetting(int subId) {
3110 enforceReadPrivilegedPermission("getVoWiFiModeSetting");
3111 final long identity = Binder.clearCallingIdentity();
3112 try {
3113 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003114 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003115 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
3116 } finally {
3117 Binder.restoreCallingIdentity(identity);
3118 }
3119 }
3120
3121 @Override
3122 public void setVoWiFiModeSetting(int subId, int mode) {
3123 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3124 "setVoWiFiModeSetting");
3125 final long identity = Binder.clearCallingIdentity();
3126 try {
3127 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003128 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003129 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
3130 } finally {
3131 Binder.restoreCallingIdentity(identity);
3132 }
3133 }
3134
3135 @Override
3136 public int getVoWiFiRoamingModeSetting(int subId) {
3137 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3138 final long identity = Binder.clearCallingIdentity();
3139 try {
3140 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003141 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003142 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
3143 } finally {
3144 Binder.restoreCallingIdentity(identity);
3145 }
3146 }
3147
3148 @Override
3149 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3150 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3151 "setVoWiFiRoamingModeSetting");
3152 final long identity = Binder.clearCallingIdentity();
3153 try {
3154 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003155 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003156 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
3157 } finally {
3158 Binder.restoreCallingIdentity(identity);
3159 }
3160 }
3161
3162 @Override
3163 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3164 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3165 "setRttCapabilityEnabled");
3166 final long identity = Binder.clearCallingIdentity();
3167 try {
3168 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003169 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003170 getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3171 } finally {
3172 Binder.restoreCallingIdentity(identity);
3173 }
3174 }
3175
3176 @Override
3177 public boolean isTtyOverVolteEnabled(int subId) {
3178 enforceReadPrivilegedPermission("isTtyOverVolteEnabled");
3179 final long identity = Binder.clearCallingIdentity();
3180 try {
3181 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003182 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003183 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
3184 } finally {
3185 Binder.restoreCallingIdentity(identity);
3186 }
3187 }
3188
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003189 @Override
3190 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3191 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3192 final long identity = Binder.clearCallingIdentity();
3193 try {
3194 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003195 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003196 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003197 } finally {
3198 Binder.restoreCallingIdentity(identity);
3199 }
3200 }
3201
3202 @Override
3203 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3204 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3205 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003206 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3207 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3208 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003209 try {
3210 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003211 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003212 .removeProvisioningCallbackForSubscription(callback, subId);
3213 } catch (IllegalArgumentException e) {
3214 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3215 + "is inactive, ignoring unregister.");
3216 // If the subscription is no longer active, just return, since the callback will already
3217 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003218 } finally {
3219 Binder.restoreCallingIdentity(identity);
3220 }
3221 }
3222
3223 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003224 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3225 boolean isProvisioned) {
3226 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3227 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3228 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3229 }
3230 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3231 "setProvisioningStatusForCapability");
3232 final long identity = Binder.clearCallingIdentity();
3233 try {
3234 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3235 Phone phone = getPhone(subId);
3236 if (phone == null) {
3237 loge("setImsProvisioningStatusForCapability: phone instance null for subid "
3238 + subId);
3239 return;
3240 }
3241 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3242 return;
3243 }
3244
3245 // this capability requires provisioning, route to the correct API.
3246 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3247 switch (capability) {
3248 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3249 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3250 ims.setVolteProvisioned(isProvisioned);
3251 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3252 ims.setWfcProvisioned(isProvisioned);
3253 }
3254 break;
3255 }
3256 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3257 // There is currently no difference in VT provisioning type.
3258 ims.setVtProvisioned(isProvisioned);
3259 break;
3260 }
3261 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3262 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3263 // change the capability of the feature instead if needed.
3264 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3265 == isProvisioned) {
3266 // No change in provisioning.
3267 return;
3268 }
3269 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3270 try {
3271 ims.changeMmTelCapability(capability, tech, isProvisioned);
3272 } catch (ImsException e) {
3273 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3274 + ", Exception" + e.getMessage());
3275 }
3276 break;
3277 }
3278 default: {
3279 throw new IllegalArgumentException("Tried to set provisioning for capability '"
3280 + capability + "', which does not require provisioning.");
3281 }
3282 }
3283
3284 } finally {
3285 Binder.restoreCallingIdentity(identity);
3286 }
3287 }
3288
3289 @Override
3290 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
3291 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3292 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3293 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3294 }
3295 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
3296 final long identity = Binder.clearCallingIdentity();
3297 try {
3298 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3299 Phone phone = getPhone(subId);
3300 if (phone == null) {
3301 loge("getImsProvisioningStatusForCapability: phone instance null for subid "
3302 + subId);
3303 // We will fail with "true" as the provisioning status because this is the default
3304 // if we do not require provisioning.
3305 return true;
3306 }
3307
3308 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3309 return true;
3310 }
3311
3312 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3313 switch (capability) {
3314 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3315 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3316 return ims.isVolteProvisionedOnDevice();
3317 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3318 return ims.isWfcProvisionedOnDevice();
3319 }
3320 // This should never happen, since we are checking tech above to make sure it
3321 // is either LTE or IWLAN.
3322 throw new IllegalArgumentException("Invalid radio technology for voice "
3323 + "capability.");
3324 }
3325 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3326 // There is currently no difference in VT provisioning type.
3327 return ims.isVtProvisionedOnDevice();
3328 }
3329 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3330 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
3331 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
3332 }
3333 default: {
3334 throw new IllegalArgumentException("Tried to get provisioning for capability '"
3335 + capability + "', which does not require provisioning.");
3336 }
3337 }
3338
3339 } finally {
3340 Binder.restoreCallingIdentity(identity);
3341 }
3342 }
3343
3344 @Override
3345 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
3346 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3347 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3348 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3349 }
3350 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
3351 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3352 return (provisionedBits & capability) > 0;
3353 }
3354
3355 @Override
3356 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
3357 boolean isProvisioned) {
3358 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3359 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3360 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3361 }
3362 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3363 "setProvisioningStatusForCapability");
3364 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
3365 // If the current provisioning status for capability already matches isProvisioned,
3366 // do nothing.
3367 if (((provisionedBits & capability) > 0) == isProvisioned) {
3368 return;
3369 }
3370 if (isProvisioned) {
3371 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
3372 } else {
3373 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
3374 }
3375 }
3376
3377 /**
3378 * @return the bitfield containing the MmTel provisioning for the provided subscription and
3379 * technology. The bitfield should mirror the bitfield defined by
3380 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
3381 */
3382 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
3383 String key = getMmTelProvisioningKey(subId, tech);
3384 // Default is no capabilities are provisioned.
3385 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
3386 }
3387
3388 /**
3389 * Sets the MmTel capability provisioning bitfield (defined by
3390 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
3391 * technology specified.
3392 *
3393 * Note: This is a synchronous command and should not be called on UI thread.
3394 */
3395 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
3396 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3397 String key = getMmTelProvisioningKey(subId, tech);
3398 editor.putInt(key, newField);
3399 editor.commit();
3400 }
3401
3402 private static String getMmTelProvisioningKey(int subId, int tech) {
3403 // resulting key is provision_ims_mmtel_{subId}_{tech}
3404 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
3405 }
3406
3407 /**
3408 * Query CarrierConfig to see if the specified capability requires provisioning for the
3409 * carrier associated with the subscription id.
3410 */
3411 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
3412 int capability) {
3413 CarrierConfigManager configManager = new CarrierConfigManager(context);
3414 PersistableBundle c = configManager.getConfigForSubId(subId);
3415 boolean requireUtProvisioning = c.getBoolean(
3416 // By default, this config is true (even if there is no SIM). We also check to make
3417 // sure the subscription needs provisioning here, so we do not need to check for
3418 // the no-SIM case, where we would normally shortcut this to false.
3419 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, true)
3420 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
3421 false);
3422 boolean requireVoiceVtProvisioning = c.getBoolean(
3423 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
3424
3425 // First check to make sure that the capability requires provisioning.
3426 switch (capability) {
3427 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
3428 // intentional fallthrough
3429 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3430 if (requireVoiceVtProvisioning) {
3431 // Voice and Video requires provisioning
3432 return true;
3433 }
3434 break;
3435 }
3436 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3437 if (requireUtProvisioning) {
3438 // UT requires provisioning
3439 return true;
3440 }
3441 break;
3442 }
3443 }
3444 return false;
3445 }
3446
3447 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003448 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003449 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3450 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3451 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003452 enforceReadPrivilegedPermission("getImsProvisioningInt");
3453 final long identity = Binder.clearCallingIdentity();
3454 try {
3455 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003456 int slotId = getSlotIndex(subId);
3457 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3458 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
3459 + subId + "' for key:" + key);
3460 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
3461 }
3462 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003463 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003464 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
3465 + subId + "' for key:" + key);
3466 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003467 } finally {
3468 Binder.restoreCallingIdentity(identity);
3469 }
3470 }
3471
3472 @Override
3473 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003474 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3475 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3476 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003477 enforceReadPrivilegedPermission("getImsProvisioningString");
3478 final long identity = Binder.clearCallingIdentity();
3479 try {
3480 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003481 int slotId = getSlotIndex(subId);
3482 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3483 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
3484 + subId + "' for key:" + key);
3485 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
3486 }
3487 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003488 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003489 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
3490 + subId + "' for key:" + key);
3491 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003492 } finally {
3493 Binder.restoreCallingIdentity(identity);
3494 }
3495 }
3496
3497 @Override
3498 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003499 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3500 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3501 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003502 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3503 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003504 final long identity = Binder.clearCallingIdentity();
3505 try {
3506 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003507 int slotId = getSlotIndex(subId);
3508 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3509 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
3510 + subId + "' for key:" + key);
3511 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3512 }
3513 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003514 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003515 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
3516 + "' for key:" + key);
3517 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003518 } finally {
3519 Binder.restoreCallingIdentity(identity);
3520 }
3521 }
3522
3523 @Override
3524 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003525 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3526 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
3527 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08003528 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3529 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003530 final long identity = Binder.clearCallingIdentity();
3531 try {
3532 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003533 int slotId = getSlotIndex(subId);
3534 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3535 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
3536 + subId + "' for key:" + key);
3537 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
3538 }
3539 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003540 } catch (ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003541 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
3542 + "' for key:" + key);
3543 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003544 } finally {
3545 Binder.restoreCallingIdentity(identity);
3546 }
3547 }
3548
Brad Ebinger35c841c2018-10-01 10:40:55 -07003549 private int getSlotIndexOrException(int subId) throws IllegalArgumentException {
3550 int slotId = SubscriptionManager.getSlotIndex(subId);
3551 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003552 throw new IllegalArgumentException("Invalid Subscription Id, subId=" + subId);
Brad Ebinger35c841c2018-10-01 10:40:55 -07003553 }
3554 return slotId;
3555 }
3556
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003557 private int getSlotIndex(int subId) {
3558 int slotId = SubscriptionManager.getSlotIndex(subId);
3559 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
3560 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
3561 }
3562 return slotId;
3563 }
3564
Wink Saville36469e72014-06-11 15:17:00 -07003565 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07003566 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07003567 */
3568 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003569 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Nathan Harold9042f0b2019-05-21 15:51:27 -07003570 if (getTargetSdk(callingPackage) >= android.os.Build.VERSION_CODES.Q
3571 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
3572 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003573 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3574 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003575
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003576 final long identity = Binder.clearCallingIdentity();
3577 try {
3578 final Phone phone = getPhone(subId);
3579 if (phone != null) {
3580 return phone.getServiceState().getDataNetworkType();
3581 } else {
3582 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3583 }
3584 } finally {
3585 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003586 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003587 }
3588
3589 /**
3590 * Returns the data network type
3591 */
3592 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003593 public int getDataNetworkType(String callingPackage) {
3594 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003595 }
3596
3597 /**
3598 * Returns the data network type for a subId
3599 */
3600 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003601 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003602 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003603 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003604 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3605 }
3606
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003607 final long identity = Binder.clearCallingIdentity();
3608 try {
3609 final Phone phone = getPhone(subId);
3610 if (phone != null) {
3611 return phone.getServiceState().getDataNetworkType();
3612 } else {
3613 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3614 }
3615 } finally {
3616 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003617 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003618 }
3619
3620 /**
Wink Saville36469e72014-06-11 15:17:00 -07003621 * Returns the Voice network type for a subId
3622 */
3623 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003624 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003625 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003626 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07003627 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3628 }
3629
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003630 final long identity = Binder.clearCallingIdentity();
3631 try {
3632 final Phone phone = getPhone(subId);
3633 if (phone != null) {
3634 return phone.getServiceState().getVoiceNetworkType();
3635 } else {
3636 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
3637 }
3638 } finally {
3639 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003640 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003641 }
3642
3643 /**
3644 * @return true if a ICC card is present
3645 */
3646 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07003647 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003648 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
3649 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07003650 }
3651
3652 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003653 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07003654 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003655 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003656 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003657 final long identity = Binder.clearCallingIdentity();
3658 try {
3659 final Phone phone = PhoneFactory.getPhone(slotIndex);
3660 if (phone != null) {
3661 return phone.getIccCard().hasIccCard();
3662 } else {
3663 return false;
3664 }
3665 } finally {
3666 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08003667 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003668 }
3669
3670 /**
3671 * Return if the current radio is LTE on CDMA. This
3672 * is a tri-state return value as for a period of time
3673 * the mode may be unknown.
3674 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003675 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003676 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08003677 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003678 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003679 @Override
3680 public int getLteOnCdmaMode(String callingPackage) {
3681 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07003682 }
3683
Sanket Padawe356d7632015-06-22 14:03:32 -07003684 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003685 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003686 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003687 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003688 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3689 }
3690
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003691 final long identity = Binder.clearCallingIdentity();
3692 try {
3693 final Phone phone = getPhone(subId);
3694 if (phone == null) {
3695 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
3696 } else {
3697 return phone.getLteOnCdmaMode();
3698 }
3699 } finally {
3700 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003701 }
Wink Saville36469e72014-06-11 15:17:00 -07003702 }
3703
Wink Saville36469e72014-06-11 15:17:00 -07003704 /**
3705 * {@hide}
3706 * Returns Default subId, 0 in the case of single standby.
3707 */
Wink Savilleb564aae2014-10-23 10:18:09 -07003708 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003709 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07003710 }
3711
Shishir Agrawala9f32182016-04-12 12:00:16 -07003712 private int getSlotForDefaultSubscription() {
3713 return mSubscriptionController.getPhoneId(getDefaultSubscription());
3714 }
3715
Wink Savilleb564aae2014-10-23 10:18:09 -07003716 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08003717 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003718 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003719
Pengquan Menge92a50d2018-09-21 15:54:48 -07003720 private boolean isActiveSubscription(int subId) {
3721 return mSubscriptionController.isActiveSubId(subId);
3722 }
3723
Ihab Awadf2177b72013-11-25 13:33:23 -08003724 /**
3725 * @see android.telephony.TelephonyManager.WifiCallingChoices
3726 */
3727 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003728 final long identity = Binder.clearCallingIdentity();
3729 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003730 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003731 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
3732 getWhenToMakeWifiCallsDefaultPreference());
3733 } finally {
3734 Binder.restoreCallingIdentity(identity);
3735 }
Ihab Awadf2177b72013-11-25 13:33:23 -08003736 }
3737
3738 /**
3739 * @see android.telephony.TelephonyManager.WifiCallingChoices
3740 */
3741 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003742 final long identity = Binder.clearCallingIdentity();
3743 try {
3744 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003745 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003746 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
3747 } finally {
3748 Binder.restoreCallingIdentity(identity);
3749 }
Ihab Awadf9e92732013-12-05 18:02:52 -08003750 }
3751
Sailesh Nepald1e68152013-12-12 19:08:02 -08003752 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07003753 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08003754 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08003755 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08003756
Jordan Liu4c733742019-02-28 12:03:40 -08003757 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
3758 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
3759 if (phoneId == -1) {
3760 throw new IllegalArgumentException("Given slot index: " + slotIndex
3761 + " does not correspond to an active phone");
3762 }
3763 return PhoneFactory.getPhone(phoneId);
3764 }
3765
Shishir Agrawal566b7612013-10-28 14:41:00 -07003766 @Override
Derek Tan740e1672017-06-27 14:56:27 -07003767 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
3768 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003769 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3770 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003771 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003772 if (DBG) {
3773 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
3774 }
3775 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
3776 p2);
3777 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003778
Jordan Liu4c733742019-02-28 12:03:40 -08003779
3780 @Override
3781 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
3782 int slotIndex, String callingPackage, String aid, int p2) {
3783 enforceModifyPermission();
3784 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3785 if (DBG) {
3786 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
3787 }
3788 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3789 callingPackage, aid, p2);
3790 }
3791
3792 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
3793 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003794 final long identity = Binder.clearCallingIdentity();
3795 try {
3796 if (TextUtils.equals(ISDR_AID, aid)) {
3797 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003798 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3799 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003800 if (bestComponent == null
3801 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3802 loge("The calling package is not allowed to access ISD-R.");
3803 throw new SecurityException(
3804 "The calling package is not allowed to access ISD-R.");
3805 }
Derek Tan740e1672017-06-27 14:56:27 -07003806 }
Derek Tan740e1672017-06-27 14:56:27 -07003807
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003808 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08003809 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
3810 null /* workSource */);
3811 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003812 return response;
3813 } finally {
3814 Binder.restoreCallingIdentity(identity);
3815 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003816 }
3817
3818 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003819 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003820 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3821 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003822 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
3823 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
3824 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003825
Jordan Liu4c733742019-02-28 12:03:40 -08003826 @Override
3827 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
3828 enforceModifyPermission();
3829 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
3830 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
3831 channel);
3832 }
3833
3834 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003835 final long identity = Binder.clearCallingIdentity();
3836 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003837 if (channel < 0) {
3838 return false;
3839 }
Jordan Liu4c733742019-02-28 12:03:40 -08003840 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
3841 null /* workSource */);
3842 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003843 return success;
3844 } finally {
3845 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003846 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003847 }
3848
3849 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003850 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07003851 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003852 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3853 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08003854 if (DBG) {
3855 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
3856 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3857 + p3 + " data=" + data);
3858 }
3859 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
3860 command, p1, p2, p3, data);
3861 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003862
Jordan Liu4c733742019-02-28 12:03:40 -08003863 @Override
3864 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
3865 int command, int p1, int p2, int p3, String data) {
3866 enforceModifyPermission();
3867 if (DBG) {
3868 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
3869 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
3870 + p3 + " data=" + data);
3871 }
3872 return iccTransmitApduLogicalChannelWithPermission(
3873 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
3874 data);
3875 }
3876
3877 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
3878 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003879 final long identity = Binder.clearCallingIdentity();
3880 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07003881 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003882 return "";
3883 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003884
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003885 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003886 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
3887 null /* workSource */);
3888 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07003889
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003890 // Append the returned status code to the end of the response payload.
3891 String s = Integer.toHexString(
3892 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3893 if (response.payload != null) {
3894 s = IccUtils.bytesToHexString(response.payload) + s;
3895 }
3896 return s;
3897 } finally {
3898 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003899 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07003900 }
Jake Hambye994d462014-02-03 13:10:13 -08003901
Evan Charltonc66da362014-05-16 14:06:40 -07003902 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003903 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
3904 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003905 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3906 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003907 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08003908 if (DBG) {
3909 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
3910 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
3911 }
3912 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
3913 cla, command, p1, p2, p3, data);
3914 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003915
Jordan Liu4c733742019-02-28 12:03:40 -08003916 @Override
3917 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
3918 int command, int p1, int p2, int p3, String data) {
3919 enforceModifyPermission();
3920 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3921 if (DBG) {
3922 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
3923 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
3924 + " data=" + data);
3925 }
3926
3927 return iccTransmitApduBasicChannelWithPermission(
3928 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
3929 p2, p3, data);
3930 }
3931
3932 // open APDU basic channel assuming the caller has sufficient permissions
3933 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
3934 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003935 final long identity = Binder.clearCallingIdentity();
3936 try {
3937 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
3938 && TextUtils.equals(ISDR_AID, data)) {
3939 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003940 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
3941 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003942 if (bestComponent == null
3943 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
3944 loge("The calling package is not allowed to select ISD-R.");
3945 throw new SecurityException(
3946 "The calling package is not allowed to select ISD-R.");
3947 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003948 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08003949
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003950 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08003951 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
3952 null /* workSource */);
3953 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003954
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003955 // Append the returned status code to the end of the response payload.
3956 String s = Integer.toHexString(
3957 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3958 if (response.payload != null) {
3959 s = IccUtils.bytesToHexString(response.payload) + s;
3960 }
3961 return s;
3962 } finally {
3963 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07003964 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003965 }
3966
3967 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003968 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003969 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003970 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3971 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003972
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003973 final long identity = Binder.clearCallingIdentity();
3974 try {
3975 if (DBG) {
3976 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
3977 + p1 + " " + p2 + " " + p3 + ":" + filePath);
3978 }
3979
3980 IccIoResult response =
3981 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
3982 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
3983 subId);
3984
3985 if (DBG) {
3986 log("Exchange SIM_IO [R]" + response);
3987 }
3988
3989 byte[] result = null;
3990 int length = 2;
3991 if (response.payload != null) {
3992 length = 2 + response.payload.length;
3993 result = new byte[length];
3994 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
3995 } else {
3996 result = new byte[length];
3997 }
3998
3999 result[length - 1] = (byte) response.sw2;
4000 result[length - 2] = (byte) response.sw1;
4001 return result;
4002 } finally {
4003 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004004 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004005 }
4006
Nathan Haroldb3014052017-01-25 15:57:32 -08004007 /**
4008 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4009 * on a particular subscription
4010 */
sqianb6e41952018-03-12 14:54:01 -07004011 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
4012 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4013 mApp, subId, callingPackage, "getForbiddenPlmns")) {
4014 return null;
4015 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004016
4017 final long identity = Binder.clearCallingIdentity();
4018 try {
4019 if (appType != TelephonyManager.APPTYPE_USIM
4020 && appType != TelephonyManager.APPTYPE_SIM) {
4021 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4022 return null;
4023 }
4024 Object response = sendRequest(
4025 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4026 if (response instanceof String[]) {
4027 return (String[]) response;
4028 }
4029 // Response is an Exception of some kind,
4030 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004031 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004032 } finally {
4033 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004034 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004035 }
4036
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004037 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004038 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004039 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4040 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004041
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004042 final long identity = Binder.clearCallingIdentity();
4043 try {
4044 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4045 if (response.payload == null) {
4046 return "";
4047 }
Evan Charltonc66da362014-05-16 14:06:40 -07004048
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004049 // Append the returned status code to the end of the response payload.
4050 String s = Integer.toHexString(
4051 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4052 s = IccUtils.bytesToHexString(response.payload) + s;
4053 return s;
4054 } finally {
4055 Binder.restoreCallingIdentity(identity);
4056 }
Evan Charltonc66da362014-05-16 14:06:40 -07004057 }
4058
Jake Hambye994d462014-02-03 13:10:13 -08004059 /**
4060 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4061 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4062 *
4063 * @param itemID the ID of the item to read
4064 * @return the NV item as a String, or null on error.
4065 */
4066 @Override
4067 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004068 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004069 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4070 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004071
4072 final long identity = Binder.clearCallingIdentity();
4073 try {
4074 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004075 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004076 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4077 return value;
4078 } finally {
4079 Binder.restoreCallingIdentity(identity);
4080 }
Jake Hambye994d462014-02-03 13:10:13 -08004081 }
4082
4083 /**
4084 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4085 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4086 *
4087 * @param itemID the ID of the item to read
4088 * @param itemValue the value to write, as a String
4089 * @return true on success; false on any failure
4090 */
4091 @Override
4092 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004093 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004094 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4095 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004096
4097 final long identity = Binder.clearCallingIdentity();
4098 try {
4099 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4100 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004101 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004102 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4103 return success;
4104 } finally {
4105 Binder.restoreCallingIdentity(identity);
4106 }
Jake Hambye994d462014-02-03 13:10:13 -08004107 }
4108
4109 /**
4110 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4111 * Used for device configuration by some CDMA operators.
4112 *
4113 * @param preferredRoamingList byte array containing the new PRL
4114 * @return true on success; false on any failure
4115 */
4116 @Override
4117 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004118 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4119 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004120
4121 final long identity = Binder.clearCallingIdentity();
4122 try {
4123 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4124 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4125 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4126 return success;
4127 } finally {
4128 Binder.restoreCallingIdentity(identity);
4129 }
Jake Hambye994d462014-02-03 13:10:13 -08004130 }
4131
4132 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004133 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004134 * Used for device configuration by some CDMA operators.
4135 *
chen xu6dac5ab2018-10-26 17:39:23 -07004136 * @param slotIndex - device slot.
4137 *
Jake Hambye994d462014-02-03 13:10:13 -08004138 * @return true on success; false on any failure
4139 */
4140 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004141 public boolean resetModemConfig(int slotIndex) {
4142 Phone phone = PhoneFactory.getPhone(slotIndex);
4143 if (phone != null) {
4144 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4145 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004146
chen xu6dac5ab2018-10-26 17:39:23 -07004147 final long identity = Binder.clearCallingIdentity();
4148 try {
4149 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4150 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4151 return success;
4152 } finally {
4153 Binder.restoreCallingIdentity(identity);
4154 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004155 }
chen xu6dac5ab2018-10-26 17:39:23 -07004156 return false;
4157 }
4158
4159 /**
4160 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4161 *
4162 * @param slotIndex - device slot.
4163 *
4164 * @return true on success; false on any failure
4165 */
4166 @Override
4167 public boolean rebootModem(int slotIndex) {
4168 Phone phone = PhoneFactory.getPhone(slotIndex);
4169 if (phone != null) {
4170 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4171 mApp, phone.getSubId(), "rebootModem");
4172
4173 final long identity = Binder.clearCallingIdentity();
4174 try {
4175 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4176 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4177 return success;
4178 } finally {
4179 Binder.restoreCallingIdentity(identity);
4180 }
4181 }
4182 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004183 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004184
Svet Ganovb320e182015-04-16 12:30:10 -07004185 public String[] getPcscfAddress(String apnType, String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004186 final Phone defaultPhone = getDefaultPhone();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004187 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004188 mApp, defaultPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004189 return new String[0];
4190 }
4191
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004192 final long identity = Binder.clearCallingIdentity();
4193 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004194 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004195 } finally {
4196 Binder.restoreCallingIdentity(identity);
4197 }
Wink Saville36469e72014-06-11 15:17:00 -07004198 }
4199
Brad Ebinger51f743a2017-01-23 13:50:20 -08004200 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004201 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
4202 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08004203 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004204 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08004205 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004206
4207 final long identity = Binder.clearCallingIdentity();
4208 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004209 ImsResolver resolver = PhoneFactory.getImsResolver();
4210 if (resolver == null) {
4211 // may happen if the device does not support IMS.
4212 return;
4213 }
4214 resolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004215 } finally {
4216 Binder.restoreCallingIdentity(identity);
4217 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004218 }
4219
4220 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004221 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
4222 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08004223 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004224 public void disableIms(int slotId) {
4225 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004226
4227 final long identity = Binder.clearCallingIdentity();
4228 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004229 ImsResolver resolver = PhoneFactory.getImsResolver();
4230 if (resolver == null) {
4231 // may happen if the device does not support IMS.
4232 return;
4233 }
4234 resolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004235 } finally {
4236 Binder.restoreCallingIdentity(identity);
4237 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004238 }
4239
4240 /**
4241 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
4242 * feature or {@link null} if the service is not available. If the feature is available, the
4243 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
4244 */
4245 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08004246 IImsServiceFeatureCallback callback) {
4247 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004248
4249 final long identity = Binder.clearCallingIdentity();
4250 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004251 ImsResolver resolver = PhoneFactory.getImsResolver();
4252 if (resolver == null) {
4253 // may happen if the device does not support IMS.
4254 return null;
4255 }
4256 return resolver.getMmTelFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004257 } finally {
4258 Binder.restoreCallingIdentity(identity);
4259 }
Brad Ebinger34bef922017-11-09 10:27:08 -08004260 }
4261
4262 /**
4263 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
4264 * feature during emergency calling or {@link null} if the service is not available. If the
4265 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
4266 * listener for feature updates.
4267 */
4268 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
4269 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004270
4271 final long identity = Binder.clearCallingIdentity();
4272 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004273 ImsResolver resolver = PhoneFactory.getImsResolver();
4274 if (resolver == null) {
4275 // may happen if the device does not support IMS.
4276 return null;
4277 }
4278 return resolver.getRcsFeatureAndListen(slotId, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004279 } finally {
4280 Binder.restoreCallingIdentity(identity);
4281 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08004282 }
4283
Brad Ebinger5f64b052017-12-14 14:26:15 -08004284 /**
4285 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004286 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08004287 */
4288 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
4289 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004290
4291 final long identity = Binder.clearCallingIdentity();
4292 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004293 ImsResolver resolver = PhoneFactory.getImsResolver();
4294 if (resolver == null) {
4295 // may happen if the device does not support IMS.
4296 return null;
4297 }
4298 return resolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004299 } finally {
4300 Binder.restoreCallingIdentity(identity);
4301 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08004302 }
4303
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004304 /**
4305 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004306 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004307 */
4308 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
4309 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004310
4311 final long identity = Binder.clearCallingIdentity();
4312 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004313 ImsResolver resolver = PhoneFactory.getImsResolver();
4314 if (resolver == null) {
4315 // may happen if the device does not support IMS.
4316 return null;
4317 }
4318 return resolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004319 } finally {
4320 Binder.restoreCallingIdentity(identity);
4321 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08004322 }
4323
Brad Ebinger884c07b2018-02-15 16:17:40 -08004324 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07004325 * Sets the ImsService Package Name that Telephony will bind to.
4326 *
4327 * @param slotId the slot ID that the ImsService should bind for.
4328 * @param isCarrierImsService true if the ImsService is the carrier override, false if the
4329 * ImsService is the device default ImsService.
4330 * @param packageName The package name of the application that contains the ImsService to bind
4331 * to.
4332 * @return true if setting the ImsService to bind to succeeded, false if it did not.
4333 * @hide
4334 */
4335 public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004336 int[] subIds = SubscriptionManager.getSubId(slotId);
4337 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4338 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4339 "setImsService");
4340
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004341 final long identity = Binder.clearCallingIdentity();
4342 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004343 ImsResolver resolver = PhoneFactory.getImsResolver();
4344 if (resolver == null) {
4345 // may happen if the device does not support IMS.
4346 return false;
4347 }
4348 return resolver.overrideImsServiceConfiguration(slotId, isCarrierImsService,
4349 packageName);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004350 } finally {
4351 Binder.restoreCallingIdentity(identity);
4352 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004353 }
4354
4355 /**
4356 * Return the ImsService configuration.
4357 *
4358 * @param slotId The slot that the ImsService is associated with.
4359 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
4360 * the device default.
4361 * @return the package name of the ImsService configuration.
4362 */
4363 public String getImsService(int slotId, boolean isCarrierImsService) {
Brad Ebingerde696de2018-04-06 09:56:40 -07004364 int[] subIds = SubscriptionManager.getSubId(slotId);
4365 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
4366 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
4367 "getImsService");
4368
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004369 final long identity = Binder.clearCallingIdentity();
4370 try {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08004371 ImsResolver resolver = PhoneFactory.getImsResolver();
4372 if (resolver == null) {
4373 // may happen if the device does not support IMS.
4374 return "";
4375 }
4376 return resolver.getImsServiceConfiguration(slotId, isCarrierImsService);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004377 } finally {
4378 Binder.restoreCallingIdentity(identity);
4379 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07004380 }
4381
Wink Saville36469e72014-06-11 15:17:00 -07004382 public void setImsRegistrationState(boolean registered) {
4383 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004384
4385 final long identity = Binder.clearCallingIdentity();
4386 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004387 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004388 } finally {
4389 Binder.restoreCallingIdentity(identity);
4390 }
Wink Saville36469e72014-06-11 15:17:00 -07004391 }
4392
4393 /**
Stuart Scott54788802015-03-30 13:18:01 -07004394 * Set the network selection mode to automatic.
4395 *
4396 */
4397 @Override
4398 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004399 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4400 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004401
Pengquan Menge92a50d2018-09-21 15:54:48 -07004402 if (!isActiveSubscription(subId)) {
4403 return;
4404 }
4405
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004406 final long identity = Binder.clearCallingIdentity();
4407 try {
4408 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
4409 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
4410 } finally {
4411 Binder.restoreCallingIdentity(identity);
4412 }
Stuart Scott54788802015-03-30 13:18:01 -07004413 }
4414
Pengquan Mengea84e042018-09-20 14:57:26 -07004415 /**
4416 * Ask the radio to connect to the input network and change selection mode to manual.
4417 *
4418 * @param subId the id of the subscription.
4419 * @param operatorInfo the operator information, included the PLMN, long name and short name of
4420 * the operator to attach to.
4421 * @param persistSelection whether the selection will persist until reboot. If true, only allows
4422 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
4423 * normal network selection next time.
4424 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07004425 */
4426 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07004427 public boolean setNetworkSelectionModeManual(
4428 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004429 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4430 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07004431
4432 if (!isActiveSubscription(subId)) {
4433 return false;
4434 }
4435
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004436 final long identity = Binder.clearCallingIdentity();
4437 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07004438 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004439 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07004440 if (DBG) {
4441 log("setNetworkSelectionModeManual: subId: " + subId
4442 + " operator: " + operatorInfo);
4443 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004444 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
4445 } finally {
4446 Binder.restoreCallingIdentity(identity);
4447 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004448 }
4449
4450 /**
4451 * Scans for available networks.
4452 */
4453 @Override
Hall Liuf19c44f2018-11-27 14:38:17 -08004454 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004455 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4456 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08004457 LocationAccessPolicy.LocationPermissionResult locationResult =
4458 LocationAccessPolicy.checkLocationPermission(mApp,
4459 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4460 .setCallingPackage(callingPackage)
4461 .setCallingPid(Binder.getCallingPid())
4462 .setCallingUid(Binder.getCallingUid())
4463 .setMethod("getCellNetworkScanResults")
4464 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4465 .build());
4466 switch (locationResult) {
4467 case DENIED_HARD:
4468 throw new SecurityException("Not allowed to access scan results -- location");
4469 case DENIED_SOFT:
4470 return null;
4471 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004472
Pengquan Menga1bb6272018-09-06 09:59:22 -07004473 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004474 try {
4475 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07004476 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004477 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004478 } finally {
4479 Binder.restoreCallingIdentity(identity);
4480 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07004481 }
4482
4483 /**
yinxub1bed742017-04-17 11:45:04 -07004484 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07004485 *
yinxub1bed742017-04-17 11:45:04 -07004486 * @param subId id of the subscription
4487 * @param request contains the radio access networks with bands/channels to scan
4488 * @param messenger callback messenger for scan results or errors
4489 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07004490 * @return the id of the requested scan which can be used to stop the scan.
4491 */
4492 @Override
4493 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Hall Liuf19c44f2018-11-27 14:38:17 -08004494 IBinder binder, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004495 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4496 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08004497 LocationAccessPolicy.LocationPermissionResult locationResult =
4498 LocationAccessPolicy.checkLocationPermission(mApp,
4499 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4500 .setCallingPackage(callingPackage)
4501 .setCallingPid(Binder.getCallingPid())
4502 .setCallingUid(Binder.getCallingUid())
4503 .setMethod("requestNetworkScan")
4504 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
4505 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004506 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Hall Liu558027f2019-05-15 19:14:05 -07004507 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(request, subId);
Hall Liub2ac8ef2019-02-28 15:56:23 -08004508 if (e != null) {
4509 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
4510 throw e;
4511 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07004512 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08004513 return TelephonyScanManager.INVALID_SCAN_ID;
4514 }
4515 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004516 }
Hall Liu912dfd32019-04-25 14:02:26 -07004517 int callingUid = Binder.getCallingUid();
4518 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07004519 final long identity = Binder.clearCallingIdentity();
4520 try {
4521 return mNetworkScanRequestTracker.startNetworkScan(
4522 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07004523 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07004524 } finally {
4525 Binder.restoreCallingIdentity(identity);
4526 }
yinxu504e1392017-04-12 16:03:22 -07004527 }
4528
Hall Liub2ac8ef2019-02-28 15:56:23 -08004529 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Hall Liu558027f2019-05-15 19:14:05 -07004530 NetworkScanRequest request, int subId) {
4531 boolean hasCarrierPriv = getCarrierPrivilegeStatusForUid(subId, Binder.getCallingUid())
4532 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4533 boolean hasNetworkScanPermission =
4534 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
4535 == PERMISSION_GRANTED;
4536
4537 if (!hasCarrierPriv && !hasNetworkScanPermission) {
4538 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
4539 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08004540 }
4541
4542 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
4543 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08004544 if (ras.getChannels() != null && ras.getChannels().length > 0) {
4545 return new SecurityException("Specific channels must not be"
4546 + " scanned without location access.");
4547 }
4548 }
4549 }
4550
Hall Liub2ac8ef2019-02-28 15:56:23 -08004551 return null;
4552 }
4553
yinxu504e1392017-04-12 16:03:22 -07004554 /**
4555 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07004556 *
4557 * @param subId id of the subscription
4558 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07004559 */
4560 @Override
4561 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004562 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4563 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004564
Hall Liu912dfd32019-04-25 14:02:26 -07004565 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004566 final long identity = Binder.clearCallingIdentity();
4567 try {
Hall Liu912dfd32019-04-25 14:02:26 -07004568 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004569 } finally {
4570 Binder.restoreCallingIdentity(identity);
4571 }
yinxu504e1392017-04-12 16:03:22 -07004572 }
4573
4574 /**
Junda Liu84d15a22014-07-02 11:21:04 -07004575 * Get the calculated preferred network type.
4576 * Used for debugging incorrect network type.
4577 *
4578 * @return the preferred network type, defined in RILConstants.java.
4579 */
4580 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004581 public int getCalculatedPreferredNetworkType(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004582 final Phone defaultPhone = getDefaultPhone();
4583 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4584 callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004585 return RILConstants.PREFERRED_NETWORK_MODE;
4586 }
4587
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004588 final long identity = Binder.clearCallingIdentity();
4589 try {
4590 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004591 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004592 } finally {
4593 Binder.restoreCallingIdentity(identity);
4594 }
Junda Liu84d15a22014-07-02 11:21:04 -07004595 }
4596
4597 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08004598 * Get the preferred network type.
4599 * Used for device configuration by some CDMA operators.
4600 *
4601 * @return the preferred network type, defined in RILConstants.java.
4602 */
4603 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004604 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08004605 TelephonyPermissions
4606 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
4607 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004608
4609 final long identity = Binder.clearCallingIdentity();
4610 try {
4611 if (DBG) log("getPreferredNetworkType");
4612 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
4613 int networkType = (result != null ? result[0] : -1);
4614 if (DBG) log("getPreferredNetworkType: " + networkType);
4615 return networkType;
4616 } finally {
4617 Binder.restoreCallingIdentity(identity);
4618 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004619 }
4620
4621 /**
4622 * Set the preferred network type.
4623 * Used for device configuration by some CDMA operators.
4624 *
4625 * @param networkType the preferred network type, defined in RILConstants.java.
4626 * @return true on success; false on any failure.
4627 */
4628 @Override
Stuart Scott54788802015-03-30 13:18:01 -07004629 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004630 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4631 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004632
4633 final long identity = Binder.clearCallingIdentity();
4634 try {
4635 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
4636 Boolean success = (Boolean) sendRequest(
4637 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
4638 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
4639 if (success) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004640 Settings.Global.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004641 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
4642 }
4643 return success;
4644 } finally {
4645 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07004646 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004647 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004648
4649 /**
Miaoa84611c2019-03-15 09:21:10 +08004650 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08004651 *
Miaoa84611c2019-03-15 09:21:10 +08004652 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07004653 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08004654 * @hide
4655 */
4656 @Override
Miaoa84611c2019-03-15 09:21:10 +08004657 public boolean getTetherApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004658 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004659 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08004660 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004661 try {
Miaoa84611c2019-03-15 09:21:10 +08004662 if (phone != null) {
4663 return phone.hasMatchedTetherApnSetting();
4664 } else {
4665 return false;
4666 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004667 } finally {
4668 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08004669 }
Junda Liu475951f2014-11-07 16:45:03 -08004670 }
4671
4672 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07004673 * Set mobile data enabled
4674 * Used by the user through settings etc to turn on/off mobile data
4675 *
4676 * @param enable {@code true} turn turn data on, else {@code false}
4677 */
4678 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004679 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004680 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4681 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004682
4683 final long identity = Binder.clearCallingIdentity();
4684 try {
4685 int phoneId = mSubscriptionController.getPhoneId(subId);
4686 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4687 Phone phone = PhoneFactory.getPhone(phoneId);
4688 if (phone != null) {
4689 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
Jack Yud79fba22018-12-13 11:51:28 -08004690 phone.getDataEnabledSettings().setUserDataEnabled(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004691 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004692 loge("setUserDataEnabled: no phone found. Invalid subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004693 }
4694 } finally {
4695 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004696 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004697 }
4698
4699 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08004700 * Get the user enabled state of Mobile Data.
4701 *
4702 * TODO: remove and use isUserDataEnabled.
4703 * This can't be removed now because some vendor codes
4704 * calls through ITelephony directly while they should
4705 * use TelephonyManager.
4706 *
4707 * @return true on enabled
4708 */
4709 @Override
4710 public boolean getDataEnabled(int subId) {
4711 return isUserDataEnabled(subId);
4712 }
4713
4714 /**
4715 * Get whether mobile data is enabled per user setting.
4716 *
4717 * There are other factors deciding whether mobile data is actually enabled, but they are
4718 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07004719 *
Jeff Davidsona1920712016-11-18 17:05:56 -08004720 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07004721 *
4722 * @return {@code true} if data is enabled else {@code false}
4723 */
4724 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08004725 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07004726 try {
4727 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4728 null);
4729 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004730 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4731 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07004732 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004733
4734 final long identity = Binder.clearCallingIdentity();
4735 try {
4736 int phoneId = mSubscriptionController.getPhoneId(subId);
4737 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4738 Phone phone = PhoneFactory.getPhone(phoneId);
4739 if (phone != null) {
4740 boolean retVal = phone.isUserDataEnabled();
4741 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
4742 return retVal;
4743 } else {
4744 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
4745 return false;
4746 }
4747 } finally {
4748 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08004749 }
4750 }
4751
4752 /**
4753 * Get whether mobile data is enabled.
4754 *
4755 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
4756 * whether mobile data is actually enabled.
4757 *
4758 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
4759 *
4760 * @return {@code true} if data is enabled else {@code false}
4761 */
4762 @Override
4763 public boolean isDataEnabled(int subId) {
4764 try {
4765 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
4766 null);
4767 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004768 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4769 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08004770 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004771
4772 final long identity = Binder.clearCallingIdentity();
4773 try {
4774 int phoneId = mSubscriptionController.getPhoneId(subId);
4775 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
4776 Phone phone = PhoneFactory.getPhone(phoneId);
4777 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08004778 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004779 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
4780 return retVal;
4781 } else {
4782 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
4783 return false;
4784 }
4785 } finally {
4786 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08004787 }
Robert Greenwalted86e582014-05-21 20:03:20 -07004788 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004789
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004790 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim,
4791 Phone phone) {
4792 //load access rules from carrier configs, and check those as well: b/139133814
4793 SubscriptionController subController = SubscriptionController.getInstance();
4794 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4795 || subController == null) return privilegeFromSim;
4796
4797 int uid = Binder.getCallingUid();
4798 PackageManager pkgMgr = phone.getContext().getPackageManager();
4799 String[] packages = pkgMgr.getPackagesForUid(uid);
4800
4801 final long identity = Binder.clearCallingIdentity();
4802 try {
4803 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4804 SubscriptionManager subManager = (SubscriptionManager)
4805 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4806 for (String pkg : packages) {
4807 if (subManager.canManageSubscription(subInfo, pkg)) {
4808 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
4809 }
4810 }
4811 return privilegeFromSim;
4812 } finally {
4813 Binder.restoreCallingIdentity(identity);
4814 }
4815 }
4816
4817 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
4818 String pkgName) {
4819 //load access rules from carrier configs, and check those as well: b/139133814
4820 SubscriptionController subController = SubscriptionController.getInstance();
4821 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
4822 || subController == null) return privilegeFromSim;
4823
4824 final long identity = Binder.clearCallingIdentity();
4825 try {
4826 SubscriptionInfo subInfo = subController.getSubscriptionInfo(phone.getSubId());
4827 SubscriptionManager subManager = (SubscriptionManager)
4828 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
4829 return subManager.canManageSubscription(subInfo, pkgName)
4830 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
4831 } finally {
4832 Binder.restoreCallingIdentity(identity);
4833 }
4834 }
4835
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004836 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004837 public int getCarrierPrivilegeStatus(int subId) {
4838 final Phone phone = getPhone(subId);
4839 if (phone == null) {
4840 loge("getCarrierPrivilegeStatus: Invalid subId");
4841 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4842 }
4843 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004844 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08004845 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004846 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4847 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004848
4849 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4850 card.getCarrierPrivilegeStatusForCurrentTransaction(
4851 phone.getContext().getPackageManager()), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07004852 }
Junda Liu29340342014-07-10 15:23:27 -07004853
4854 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08004855 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
4856 final Phone phone = getPhone(subId);
4857 if (phone == null) {
4858 loge("getCarrierPrivilegeStatus: Invalid subId");
4859 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
4860 }
4861 UiccProfile profile =
4862 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
4863 if (profile == null) {
4864 loge("getCarrierPrivilegeStatus: No UICC");
4865 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4866 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004867 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4868 profile.getCarrierPrivilegeStatusForUid(
4869 phone.getContext().getPackageManager(), uid), phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08004870 }
4871
4872 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004873 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
4874 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08004875 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07004876 }
4877
4878 int phoneId = SubscriptionManager.getPhoneId(subId);
4879 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004880 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07004881 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07004882 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4883 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004884 return getCarrierPrivilegeStatusFromCarrierConfigRules(
4885 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4886 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004887 }
4888
4889 @Override
4890 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08004891 if (TextUtils.isEmpty(pkgName))
4892 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07004893 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
4894 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4895 UiccCard card = UiccController.getInstance().getUiccCard(i);
4896 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07004897 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07004898 continue;
4899 }
4900
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004901 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
4902 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
4903 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07004904 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
4905 break;
4906 }
4907 }
4908
4909 return result;
Junda Liu29340342014-07-10 15:23:27 -07004910 }
Derek Tan89e89d42014-07-08 17:00:10 -07004911
4912 @Override
Junda Liue64de782015-04-16 17:19:16 -07004913 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
4914 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
4915 loge("phoneId " + phoneId + " is not valid.");
4916 return null;
4917 }
4918 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004919 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07004920 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004921 return null ;
4922 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004923 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004924 }
4925
Amith Yamasani6e118872016-02-19 12:53:51 -08004926 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07004927 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004928 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08004929 List<String> privilegedPackages = new ArrayList<>();
4930 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07004931 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
4932 // has UICC in that slot.
4933 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004934 if (card.hasCarrierPrivilegeRules()) {
4935 if (packages == null) {
4936 // Only check packages in user 0 for now
4937 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07004938 PackageManager.MATCH_DISABLED_COMPONENTS
4939 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09004940 | PackageManager.GET_SIGNING_CERTIFICATES,
4941 UserHandle.USER_SYSTEM);
Amith Yamasani6e118872016-02-19 12:53:51 -08004942 }
4943 for (int p = packages.size() - 1; p >= 0; p--) {
4944 PackageInfo pkgInfo = packages.get(p);
4945 if (pkgInfo != null && pkgInfo.packageName != null
4946 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07004947 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08004948 privilegedPackages.add(pkgInfo.packageName);
4949 }
4950 }
4951 }
4952 }
4953 return privilegedPackages;
4954 }
4955
chen xuf7e9fe82019-05-09 19:31:02 -07004956 @Override
4957 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
4958 List<String> privilegedPackages = new ArrayList<>();
4959 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
4960 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
4961 }
4962 return privilegedPackages;
4963 }
4964
Wink Savilleb564aae2014-10-23 10:18:09 -07004965 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07004966 final Phone phone = getPhone(subId);
4967 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07004968 if (card == null) {
4969 loge("getIccId: No UICC");
4970 return null;
4971 }
4972 String iccId = card.getIccId();
4973 if (TextUtils.isEmpty(iccId)) {
4974 loge("getIccId: ICC ID is null or empty.");
4975 return null;
4976 }
4977 return iccId;
4978 }
4979
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07004980 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08004981 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
4982 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004983 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4984 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07004985
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004986 final long identity = Binder.clearCallingIdentity();
4987 try {
4988 final String iccId = getIccId(subId);
4989 final Phone phone = getPhone(subId);
4990 if (phone == null) {
4991 return false;
4992 }
4993 final String subscriberId = phone.getSubscriberId();
4994
4995 if (DBG_MERGE) {
4996 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
4997 + subscriberId + " to " + number);
4998 }
4999
5000 if (TextUtils.isEmpty(iccId)) {
5001 return false;
5002 }
5003
5004 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
5005
5006 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5007 if (alphaTag == null) {
5008 editor.remove(alphaTagPrefKey);
5009 } else {
5010 editor.putString(alphaTagPrefKey, alphaTag);
5011 }
5012
5013 // Record both the line number and IMSI for this ICCID, since we need to
5014 // track all merged IMSIs based on line number
5015 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5016 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5017 if (number == null) {
5018 editor.remove(numberPrefKey);
5019 editor.remove(subscriberPrefKey);
5020 } else {
5021 editor.putString(numberPrefKey, number);
5022 editor.putString(subscriberPrefKey, subscriberId);
5023 }
5024
5025 editor.commit();
5026 return true;
5027 } finally {
5028 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005029 }
Derek Tan7226c842014-07-02 17:42:23 -07005030 }
5031
5032 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005033 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07005034 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005035 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08005036 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08005037 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07005038 return null;
5039 }
Derek Tan97ebb422014-09-05 16:55:38 -07005040
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005041 final long identity = Binder.clearCallingIdentity();
5042 try {
5043 String iccId = getIccId(subId);
5044 if (iccId != null) {
5045 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5046 if (DBG_MERGE) {
5047 log("getLine1NumberForDisplay returning "
5048 + mTelephonySharedPreferences.getString(numberPrefKey, null));
5049 }
5050 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08005051 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005052 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
5053 return null;
5054 } finally {
5055 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005056 }
Derek Tan7226c842014-07-02 17:42:23 -07005057 }
5058
5059 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005060 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005061 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005062 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005063 return null;
5064 }
Derek Tan97ebb422014-09-05 16:55:38 -07005065
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005066 final long identity = Binder.clearCallingIdentity();
5067 try {
5068 String iccId = getIccId(subId);
5069 if (iccId != null) {
5070 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
5071 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
5072 }
5073 return null;
5074 } finally {
5075 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07005076 }
Derek Tan7226c842014-07-02 17:42:23 -07005077 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005078
5079 @Override
Jordan Liub49b04b2019-05-06 14:45:15 -07005080 public String[] getMergedSubscriberIds(int subId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005081 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
5082 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08005083 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005084 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
5085 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005086 return null;
5087 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005088
Jordan Liub49b04b2019-05-06 14:45:15 -07005089 // Clear calling identity, when calling TelephonyManager, because callerUid must be
5090 // the process, where TelephonyManager was instantiated.
5091 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005092 final long identity = Binder.clearCallingIdentity();
5093 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005094 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005095 final TelephonyManager tele = TelephonyManager.from(context);
5096 final SubscriptionManager sub = SubscriptionManager.from(context);
5097
5098 // Figure out what subscribers are currently active
5099 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005100
Jordan Liub49b04b2019-05-06 14:45:15 -07005101 // Only consider subs which match the current subId
5102 // This logic can be simplified. See b/131189269 for progress.
5103 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005104 activeSubscriberIds.add(tele.getSubscriberId(subId));
5105 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005106
5107 // First pass, find a number override for an active subscriber
5108 String mergeNumber = null;
5109 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
5110 for (String key : prefs.keySet()) {
5111 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
5112 final String subscriberId = (String) prefs.get(key);
5113 if (activeSubscriberIds.contains(subscriberId)) {
5114 final String iccId = key.substring(
5115 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
5116 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
5117 mergeNumber = (String) prefs.get(numberKey);
5118 if (DBG_MERGE) {
5119 Slog.d(LOG_TAG, "Found line number " + mergeNumber
5120 + " for active subscriber " + subscriberId);
5121 }
5122 if (!TextUtils.isEmpty(mergeNumber)) {
5123 break;
5124 }
5125 }
5126 }
5127 }
5128
5129 // Shortcut when no active merged subscribers
5130 if (TextUtils.isEmpty(mergeNumber)) {
5131 return null;
5132 }
5133
5134 // Second pass, find all subscribers under that line override
5135 final ArraySet<String> result = new ArraySet<>();
5136 for (String key : prefs.keySet()) {
5137 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
5138 final String number = (String) prefs.get(key);
5139 if (mergeNumber.equals(number)) {
5140 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
5141 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
5142 final String subscriberId = (String) prefs.get(subscriberKey);
5143 if (!TextUtils.isEmpty(subscriberId)) {
5144 result.add(subscriberId);
5145 }
5146 }
5147 }
5148 }
5149
5150 final String[] resultArray = result.toArray(new String[result.size()]);
5151 Arrays.sort(resultArray);
5152 if (DBG_MERGE) {
5153 Slog.d(LOG_TAG,
5154 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
5155 }
5156 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07005157 } finally {
5158 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08005159 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08005160 }
5161
5162 @Override
Malcolm Chen6ca97372019-07-01 16:28:21 -07005163 public String[] getMergedSubscriberIdsFromGroup(int subId, String callingPackage) {
5164 enforceReadPrivilegedPermission("getMergedSubscriberIdsFromGroup");
5165
5166 final long identity = Binder.clearCallingIdentity();
5167 try {
5168 final TelephonyManager telephonyManager = mApp.getSystemService(
5169 TelephonyManager.class);
5170 String subscriberId = telephonyManager.getSubscriberId(subId);
5171 if (subscriberId == null) {
5172 if (DBG) {
5173 log("getMergedSubscriberIdsFromGroup can't find subscriberId for subId "
5174 + subId);
5175 }
5176 return null;
5177 }
5178
5179 final SubscriptionInfo info = SubscriptionController.getInstance()
5180 .getSubscriptionInfo(subId);
5181 final ParcelUuid groupUuid = info.getGroupUuid();
5182 // If it doesn't belong to any group, return just subscriberId of itself.
5183 if (groupUuid == null) {
5184 return new String[]{subscriberId};
5185 }
5186
5187 // Get all subscriberIds from the group.
5188 final List<String> mergedSubscriberIds = new ArrayList<>();
5189 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
5190 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName());
5191 for (SubscriptionInfo subInfo : groupInfos) {
5192 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
5193 if (subscriberId != null) {
5194 mergedSubscriberIds.add(subscriberId);
5195 }
5196 }
5197
5198 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
5199 } finally {
5200 Binder.restoreCallingIdentity(identity);
5201
5202 }
5203 }
5204
5205 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005206 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005207 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
5208 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005209
5210 final long identity = Binder.clearCallingIdentity();
5211 try {
5212 final Phone phone = getPhone(subId);
5213 return phone == null ? false : phone.setOperatorBrandOverride(brand);
5214 } finally {
5215 Binder.restoreCallingIdentity(identity);
5216 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07005217 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05005218
5219 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005220 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005221 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
5222 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005223 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005224
5225 final long identity = Binder.clearCallingIdentity();
5226 try {
5227 final Phone phone = getPhone(subId);
5228 if (phone == null) {
5229 return false;
5230 }
5231 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
5232 cdmaNonRoamingList);
5233 } finally {
5234 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005235 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08005236 }
5237
5238 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005239 @Deprecated
5240 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
5241 enforceModifyPermission();
5242
5243 int returnValue = 0;
5244 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07005245 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00005246 if(result.exception == null) {
5247 if (result.result != null) {
5248 byte[] responseData = (byte[])(result.result);
5249 if(responseData.length > oemResp.length) {
5250 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
5251 responseData.length + "bytes. Buffer Size is " +
5252 oemResp.length + "bytes.");
5253 }
5254 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
5255 returnValue = responseData.length;
5256 }
5257 } else {
5258 CommandException ex = (CommandException) result.exception;
5259 returnValue = ex.getCommandError().ordinal();
5260 if(returnValue > 0) returnValue *= -1;
5261 }
5262 } catch (RuntimeException e) {
5263 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
5264 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
5265 if(returnValue > 0) returnValue *= -1;
5266 }
5267
5268 return returnValue;
5269 }
5270
5271 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07005272 public void setRadioCapability(RadioAccessFamily[] rafs) {
5273 try {
5274 ProxyController.getInstance().setRadioCapability(rafs);
5275 } catch (RuntimeException e) {
5276 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
5277 }
5278 }
5279
5280 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005281 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005282 Phone phone = PhoneFactory.getPhone(phoneId);
chen xub97461a2018-10-26 14:17:57 -07005283 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08005284 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07005285 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08005286 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005287 final long identity = Binder.clearCallingIdentity();
5288 try {
chen xub97461a2018-10-26 14:17:57 -07005289 TelephonyPermissions
5290 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5291 mApp, phone.getSubId(), "getRadioAccessFamily");
5292 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005293 } finally {
5294 Binder.restoreCallingIdentity(identity);
5295 }
chen xub97461a2018-10-26 14:17:57 -07005296 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07005297 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005298
5299 @Override
5300 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005301 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07005302 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005303
5304 final long identity = Binder.clearCallingIdentity();
5305 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005306 ImsManager.getInstance(defaultPhone.getContext(),
5307 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005308 } finally {
5309 Binder.restoreCallingIdentity(identity);
5310 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005311 }
5312
5313 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005314 public boolean isVideoCallingEnabled(String callingPackage) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005315 final Phone defaultPhone = getDefaultPhone();
Amit Mahajan578e53d2018-03-20 16:18:38 +00005316 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005317 mApp, defaultPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00005318 return false;
5319 }
Svet Ganovb320e182015-04-16 12:30:10 -07005320
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005321 final long identity = Binder.clearCallingIdentity();
5322 try {
5323 // Check the user preference and the system-level IMS setting. Even if the user has
5324 // enabled video calling, if IMS is disabled we aren't able to support video calling.
5325 // In the long run, we may instead need to check if there exists a connection service
5326 // which can support video calling.
5327 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005328 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005329 return imsManager.isVtEnabledByPlatform()
5330 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
5331 && imsManager.isVtEnabledByUser();
5332 } finally {
5333 Binder.restoreCallingIdentity(identity);
5334 }
Andrew Leedf14ead2014-10-17 14:22:52 -07005335 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06005336
Andrew Leea1239f22015-03-02 17:44:07 -08005337 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005338 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
5339 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5340 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5341 return false;
5342 }
5343
5344 final long identity = Binder.clearCallingIdentity();
5345 try {
5346 CarrierConfigManager configManager =
5347 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005348 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005349 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
5350 } finally {
5351 Binder.restoreCallingIdentity(identity);
5352 }
Andrew Leea1239f22015-03-02 17:44:07 -08005353 }
5354
5355 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005356 public boolean isWorldPhone(int subId, String callingPackage) {
5357 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5358 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
5359 return false;
5360 }
5361
5362 final long identity = Binder.clearCallingIdentity();
5363 try {
5364 CarrierConfigManager configManager =
5365 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005366 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005367 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
5368 } finally {
5369 Binder.restoreCallingIdentity(identity);
5370 }
Andrew Leea1239f22015-03-02 17:44:07 -08005371 }
5372
Andrew Lee9431b832015-03-09 18:46:45 -07005373 @Override
5374 public boolean isTtyModeSupported() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005375 TelecomManager telecomManager = TelecomManager.from(mApp);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08005376 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07005377 }
5378
5379 @Override
5380 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005381 final long identity = Binder.clearCallingIdentity();
5382 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005383 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005384 } finally {
5385 Binder.restoreCallingIdentity(identity);
5386 }
Andrew Lee9431b832015-03-09 18:46:45 -07005387 }
5388
Hall Liuf6668912018-10-31 17:05:23 -07005389 /**
5390 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
5391 * support for the feature and device firmware support.
5392 *
5393 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
5394 */
5395 @Override
5396 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005397 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005398 final Phone phone = getPhone(subscriptionId);
5399 if (phone == null) {
5400 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
5401 return false;
5402 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005403 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005404 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005405 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
5406 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005407 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005408 return isCarrierSupported && isDeviceSupported;
5409 } finally {
5410 Binder.restoreCallingIdentity(identity);
5411 }
Hall Liu98187582018-01-22 19:15:32 -08005412 }
5413
Hall Liuf6668912018-10-31 17:05:23 -07005414 /**
Hall Liuf2daa022019-07-23 18:39:00 -07005415 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
5416 * RTT setting, will return true if the device and carrier both support RTT.
5417 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07005418 */
5419 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005420 final long identity = Binder.clearCallingIdentity();
5421 try {
Hall Liuf2daa022019-07-23 18:39:00 -07005422 boolean isRttSupported = isRttSupported(subscriptionId);
5423 boolean isUserRttSettingOn = Settings.Secure.getInt(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005424 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
Hall Liuf2daa022019-07-23 18:39:00 -07005425 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
5426 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
5427 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005428 } finally {
5429 Binder.restoreCallingIdentity(identity);
5430 }
Hall Liu3ad5f012018-04-06 16:23:39 -07005431 }
5432
Sanket Padawe7310cc72015-01-14 09:53:20 -08005433 /**
5434 * Returns the unique device ID of phone, for example, the IMEI for
5435 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
5436 *
5437 * <p>Requires Permission:
5438 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
5439 */
5440 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07005441 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005442 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08005443 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08005444 return null;
5445 }
Jeff Davidson913390f2018-02-23 17:11:49 -08005446 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07005447 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
5448 callingPackage, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08005449 return null;
5450 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005451
5452 final long identity = Binder.clearCallingIdentity();
5453 try {
5454 return phone.getDeviceId();
5455 } finally {
5456 Binder.restoreCallingIdentity(identity);
5457 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08005458 }
5459
Ping Sunc67b7c22016-03-02 19:16:45 +08005460 /**
5461 * {@hide}
5462 * Returns the IMS Registration Status on a particular subid
5463 *
5464 * @param subId
5465 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005466 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08005467 Phone phone = getPhone(subId);
5468 if (phone != null) {
5469 return phone.isImsRegistered();
5470 } else {
5471 return false;
5472 }
5473 }
5474
Santos Cordon7a1885b2015-02-03 11:15:19 -08005475 @Override
5476 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005477 final long identity = Binder.clearCallingIdentity();
5478 try {
5479 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
5480 } finally {
5481 Binder.restoreCallingIdentity(identity);
5482 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08005483 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07005484
Tyler Gunnf70ed162019-04-03 15:28:53 -07005485 @Override
5486 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
5487 final long identity = Binder.clearCallingIdentity();
5488 try {
5489 Phone phone = getPhone(subscriptionId);
5490 if (phone == null) {
5491 return null;
5492 }
5493 return PhoneUtils.makePstnPhoneAccountHandle(phone);
5494 } finally {
5495 Binder.restoreCallingIdentity(identity);
5496 }
5497 }
5498
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005499 /**
5500 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07005501 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005502 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005503 final long identity = Binder.clearCallingIdentity();
5504 try {
5505 Phone phone = getPhone(subId);
5506 if (phone != null) {
5507 return phone.isWifiCallingEnabled();
5508 } else {
5509 return false;
5510 }
5511 } finally {
5512 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005513 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07005514 }
5515
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005516 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005517 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005518 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005519 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005520 final long identity = Binder.clearCallingIdentity();
5521 try {
5522 Phone phone = getPhone(subId);
5523 if (phone != null) {
5524 return phone.isVideoEnabled();
5525 } else {
5526 return false;
5527 }
5528 } finally {
5529 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005530 }
5531 }
5532
5533 /**
5534 * @return the IMS registration technology for the MMTEL feature. Valid return values are
5535 * defined in {@link ImsRegistrationImplBase}.
5536 */
5537 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005538 final long identity = Binder.clearCallingIdentity();
5539 try {
5540 Phone phone = getPhone(subId);
5541 if (phone != null) {
5542 return phone.getImsRegistrationTech();
5543 } else {
5544 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
5545 }
5546 } finally {
5547 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08005548 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07005549 }
5550
Stuart Scott8eef64f2015-04-08 15:13:54 -07005551 @Override
5552 public void factoryReset(int subId) {
5553 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07005554 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
5555 return;
5556 }
5557
Svet Ganovcc087f82015-05-12 20:35:54 -07005558 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005559
Svet Ganovcc087f82015-05-12 20:35:54 -07005560 try {
Stuart Scott981d8582015-04-21 14:09:50 -07005561 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
5562 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07005563 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07005564 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07005565 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005566 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
5567 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07005568 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005569 // There has been issues when Sms raw table somehow stores orphan
5570 // fragments. They lead to garbled message when new fragments come
5571 // in and combined with those stale ones. In case this happens again,
5572 // user can reset all network settings which will clean up this table.
5573 cleanUpSmsRawTable(getDefaultPhone().getContext());
Naina Nalluri1264a9f2019-09-17 14:10:30 -07005574
5575 // Erase modem config if erase modem on network setting is enabled.
5576 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
5577 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
5578 if (configValue != null && Boolean.parseBoolean(configValue)) {
5579 sendEraseModemConfig(getDefaultPhone());
5580 }
Svet Ganovcc087f82015-05-12 20:35:54 -07005581 } finally {
5582 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07005583 }
5584 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005585
Amit Mahajan7dbbd822019-03-13 17:33:47 -07005586 private void cleanUpSmsRawTable(Context context) {
5587 ContentResolver resolver = context.getContentResolver();
5588 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
5589 resolver.delete(uri, null, null);
5590 }
5591
Narayan Kamath1c496c22015-04-16 14:40:19 +01005592 @Override
chen xu5d3637b2019-01-21 23:31:38 -08005593 public String getSimLocaleForSubscriber(int subId) {
5594 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
5595 final Phone phone = getPhone(subId);
5596 if (phone == null) {
5597 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08005598 return null;
chen xu5d3637b2019-01-21 23:31:38 -08005599 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005600 final long identity = Binder.clearCallingIdentity();
5601 try {
chen xu5d3637b2019-01-21 23:31:38 -08005602 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
5603 phone.getContext().getOpPackageName());
chen xu6291c472019-02-04 12:55:53 -08005604 if (info == null) {
5605 log("getSimLocaleForSubscriber, inactive subId: " + subId);
5606 return null;
5607 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005608 // Try and fetch the locale from the carrier properties or from the SIM language
5609 // preferences (EF-PL and EF-LI)...
5610 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005611 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08005612 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
5613 if (localeFromDefaultSim != null) {
5614 if (!localeFromDefaultSim.getCountry().isEmpty()) {
5615 if (DBG) log("Using locale from subId: " + subId + " locale: "
5616 + localeFromDefaultSim);
5617 return localeFromDefaultSim.toLanguageTag();
5618 } else {
5619 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005620 }
5621 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005622
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005623 // The SIM language preferences only store a language (e.g. fr = French), not an
5624 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
5625 // the SIM and carrier preferences does not include a country we add the country
5626 // determined from the SIM MCC to provide an exact locale.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005627 final Locale mccLocale = MccTable.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005628 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08005629 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005630 return mccLocale.toLanguageTag();
5631 }
5632
5633 if (DBG) log("No locale found - returning null");
5634 return null;
5635 } finally {
5636 Binder.restoreCallingIdentity(identity);
5637 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01005638 }
5639
5640 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005641 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005642 }
5643
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005644 /**
5645 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
5646 */
5647 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005648 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01005649 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005650
Chenjie Yu1ba97252018-01-11 18:16:20 -08005651 private final ModemActivityInfo mLastModemActivityInfo =
5652 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
5653
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005654 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07005655 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
5656 * representing the state of the modem.
5657 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08005658 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
5659 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07005660 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005661 */
5662 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07005663 public void requestModemActivityInfo(ResultReceiver result) {
5664 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07005665 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005666
5667 final long identity = Binder.clearCallingIdentity();
5668 try {
5669 ModemActivityInfo ret = null;
5670 synchronized (mLastModemActivityInfo) {
5671 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
5672 CMD_GET_MODEM_ACTIVITY_INFO,
vagdeviaf9a5b92018-08-15 16:01:53 -07005673 null, workSource);
Siddharth Rayb8114062018-06-17 15:02:38 -07005674 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005675 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
5676 for (int i = 0; i < mergedTxTimeMs.length; i++) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005677 mergedTxTimeMs[i] = info.getTxTimeMillis()[i]
5678 + mLastModemActivityInfo.getTxTimeMillis()[i];
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005679 }
5680 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005681 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
5682 + mLastModemActivityInfo.getSleepTimeMillis());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005683 mLastModemActivityInfo.setIdleTimeMillis(
5684 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
5685 mLastModemActivityInfo.setTxTimeMillis(mergedTxTimeMs);
5686 mLastModemActivityInfo.setRxTimeMillis(
5687 info.getRxTimeMillis() + mLastModemActivityInfo.getRxTimeMillis());
5688 mLastModemActivityInfo.setEnergyUsed(
5689 info.getEnergyUsed() + mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005690 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005691 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
5692 mLastModemActivityInfo.getSleepTimeMillis(),
5693 mLastModemActivityInfo.getIdleTimeMillis(),
5694 mLastModemActivityInfo.getTxTimeMillis(),
5695 mLastModemActivityInfo.getRxTimeMillis(),
5696 mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08005697 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005698 Bundle bundle = new Bundle();
5699 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
5700 result.send(0, bundle);
5701 } finally {
5702 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08005703 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07005704 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005705
Siddharth Rayb8114062018-06-17 15:02:38 -07005706 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
5707 // less than total activity duration.
5708 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
5709 if (info == null) {
5710 return false;
5711 }
5712 int activityDurationMs =
5713 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
5714 int totalTxTimeMs = 0;
5715 for (int i = 0; i < info.getTxTimeMillis().length; i++) {
5716 totalTxTimeMs += info.getTxTimeMillis()[i];
5717 }
5718 return (info.isValid()
5719 && (info.getSleepTimeMillis() <= activityDurationMs)
5720 && (info.getIdleTimeMillis() <= activityDurationMs)
5721 && (info.getRxTimeMillis() <= activityDurationMs)
5722 && (totalTxTimeMs <= activityDurationMs));
5723 }
5724
Jack Yu85bd38a2015-11-09 11:34:32 -08005725 /**
5726 * {@hide}
5727 * Returns the service state information on specified subscription.
5728 */
5729 @Override
5730 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005731 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08005732 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08005733 return null;
5734 }
5735
Hall Liuf19c44f2018-11-27 14:38:17 -08005736 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
5737 LocationAccessPolicy.checkLocationPermission(mApp,
5738 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5739 .setCallingPackage(callingPackage)
5740 .setCallingPid(Binder.getCallingPid())
5741 .setCallingUid(Binder.getCallingUid())
5742 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005743 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005744 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
5745 .build());
5746
5747 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
5748 LocationAccessPolicy.checkLocationPermission(mApp,
5749 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5750 .setCallingPackage(callingPackage)
5751 .setCallingPid(Binder.getCallingPid())
5752 .setCallingUid(Binder.getCallingUid())
5753 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07005754 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08005755 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5756 .build());
5757 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
5758 boolean hasFinePermission =
5759 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5760 boolean hasCoarsePermission =
5761 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
5762
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005763 final long identity = Binder.clearCallingIdentity();
5764 try {
5765 final Phone phone = getPhone(subId);
5766 if (phone == null) {
5767 return null;
5768 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005769
Hall Liuf19c44f2018-11-27 14:38:17 -08005770 ServiceState ss = phone.getServiceState();
5771
5772 // Scrub out the location info in ServiceState depending on what level of access
5773 // the caller has.
5774 if (hasFinePermission) return ss;
5775 if (hasCoarsePermission) return ss.sanitizeLocationInfo(false);
5776 return ss.sanitizeLocationInfo(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005777 } finally {
5778 Binder.restoreCallingIdentity(identity);
5779 }
Jack Yu85bd38a2015-11-09 11:34:32 -08005780 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005781
5782 /**
5783 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
5784 *
5785 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5786 * voicemail ringtone.
5787 * @return The URI for the ringtone to play when receiving a voicemail from a specific
5788 * PhoneAccount.
5789 */
5790 @Override
5791 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005792 final long identity = Binder.clearCallingIdentity();
5793 try {
5794 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5795 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005796 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005797 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005798
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005799 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
5800 } finally {
5801 Binder.restoreCallingIdentity(identity);
5802 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005803 }
5804
5805 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005806 * Sets the per-account voicemail ringtone.
5807 *
5808 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5809 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5810 *
5811 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5812 * voicemail ringtone.
5813 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
5814 * PhoneAccount.
5815 */
5816 @Override
5817 public void setVoicemailRingtoneUri(String callingPackage,
5818 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005819 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005820 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5821 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005822 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005823 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5824 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5825 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005826 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005827
5828 final long identity = Binder.clearCallingIdentity();
5829 try {
5830 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5831 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005832 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005833 }
5834 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
5835 } finally {
5836 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005837 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005838 }
5839
5840 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08005841 * Returns whether vibration is set for voicemail notification in Phone settings.
5842 *
5843 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
5844 * voicemail vibration setting.
5845 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
5846 */
5847 @Override
5848 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005849 final long identity = Binder.clearCallingIdentity();
5850 try {
5851 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
5852 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005853 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005854 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005855
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005856 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
5857 } finally {
5858 Binder.restoreCallingIdentity(identity);
5859 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08005860 }
5861
Youhan Wange64578a2016-05-02 15:32:42 -07005862 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005863 * Sets the per-account voicemail vibration.
5864 *
5865 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
5866 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5867 *
5868 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
5869 * voicemail vibration setting.
5870 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
5871 * specific PhoneAccount.
5872 */
5873 @Override
5874 public void setVoicemailVibrationEnabled(String callingPackage,
5875 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005876 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005877 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5878 if (!TextUtils.equals(callingPackage,
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005879 TelecomManager.from(defaultPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005880 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5881 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
5882 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005883 }
5884
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005885 final long identity = Binder.clearCallingIdentity();
5886 try {
5887 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
5888 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005889 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005890 }
5891 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
5892 } finally {
5893 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005894 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08005895 }
5896
5897 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005898 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
5899 *
5900 * @throws SecurityException if the caller does not have the required permission
5901 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005902 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07005903 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07005904 message);
Youhan Wange64578a2016-05-02 15:32:42 -07005905 }
5906
5907 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005908 * Make sure either called from same process as self (phone) or IPC caller has send SMS
5909 * permission.
5910 *
5911 * @throws SecurityException if the caller does not have the required permission
5912 */
5913 private void enforceSendSmsPermission() {
5914 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
5915 }
5916
5917 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005918 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005919 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005920 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005921 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08005922 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005923 final long identity = Binder.clearCallingIdentity();
5924 try {
5925 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005926 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005927 if (componentName == null) {
5928 throw new SecurityException(
5929 "Caller not current active visual voicemail package[null]");
5930 }
5931 String vvmPackage = componentName.getPackageName();
5932 if (!callingPackage.equals(vvmPackage)) {
5933 throw new SecurityException("Caller not current active visual voicemail package["
5934 + vvmPackage + "]");
5935 }
5936 } finally {
5937 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08005938 }
5939 }
5940
5941 /**
Youhan Wange64578a2016-05-02 15:32:42 -07005942 * Return the application ID for the app type.
5943 *
5944 * @param subId the subscription ID that this request applies to.
5945 * @param appType the uicc app type.
5946 * @return Application ID for specificied app type, or null if no uicc.
5947 */
5948 @Override
5949 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005950 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07005951 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005952
5953 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07005954 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005955 if (phone == null) {
5956 return null;
5957 }
5958 String aid = null;
5959 try {
5960 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
5961 .getApplicationByType(appType).getAid();
5962 } catch (Exception e) {
5963 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
5964 }
5965 return aid;
5966 } finally {
5967 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07005968 }
Youhan Wange64578a2016-05-02 15:32:42 -07005969 }
5970
Youhan Wang4001d252016-05-11 10:29:41 -07005971 /**
5972 * Return the Electronic Serial Number.
5973 *
5974 * @param subId the subscription ID that this request applies to.
5975 * @return ESN or null if error.
5976 */
5977 @Override
5978 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005979 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07005980 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005981
5982 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07005983 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005984 if (phone == null) {
5985 return null;
5986 }
5987 String esn = null;
5988 try {
5989 esn = phone.getEsn();
5990 } catch (Exception e) {
5991 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
5992 }
5993 return esn;
5994 } finally {
5995 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07005996 }
Youhan Wang4001d252016-05-11 10:29:41 -07005997 }
5998
Sanket Padawe99ef1e32016-05-18 16:12:33 -07005999 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07006000 * Return the Preferred Roaming List Version.
6001 *
6002 * @param subId the subscription ID that this request applies to.
6003 * @return PRLVersion or null if error.
6004 */
6005 @Override
6006 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006007 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07006008 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006009
6010 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07006011 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006012 if (phone == null) {
6013 return null;
6014 }
6015 String cdmaPrlVersion = null;
6016 try {
6017 cdmaPrlVersion = phone.getCdmaPrlVersion();
6018 } catch (Exception e) {
6019 Log.e(LOG_TAG, "Not getting PRLVersion", e);
6020 }
6021 return cdmaPrlVersion;
6022 } finally {
6023 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07006024 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07006025 }
6026
6027 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006028 * Get snapshot of Telephony histograms
6029 * @return List of Telephony histograms
6030 * @hide
6031 */
6032 @Override
6033 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006034 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6035 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006036
6037 final long identity = Binder.clearCallingIdentity();
6038 try {
6039 return RIL.getTelephonyRILTimingHistograms();
6040 } finally {
6041 Binder.restoreCallingIdentity(identity);
6042 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07006043 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006044
6045 /**
6046 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006047 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
6048 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006049 * Require system privileges. In the future we may add this to carrier APIs.
6050 *
Michele Berionne482f8202018-11-27 18:57:59 -08006051 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006052 */
6053 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006054 @TelephonyManager.SetCarrierRestrictionResult
6055 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07006056 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006057 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006058
Michele Berionne482f8202018-11-27 18:57:59 -08006059 if (carrierRestrictionRules == null) {
6060 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08006061 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006062
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006063 final long identity = Binder.clearCallingIdentity();
6064 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006065 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07006066 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006067 } finally {
6068 Binder.restoreCallingIdentity(identity);
6069 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006070 }
6071
6072 /**
6073 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08006074 * Get the allowed carrier list and the excluded carrier list, including the priority between
6075 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07006076 * Require system privileges. In the future we may add this to carrier APIs.
6077 *
Michele Berionne482f8202018-11-27 18:57:59 -08006078 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07006079 */
6080 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08006081 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006082 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07006083 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006084
6085 final long identity = Binder.clearCallingIdentity();
6086 try {
Michele Berionne482f8202018-11-27 18:57:59 -08006087 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
6088 if (response instanceof CarrierRestrictionRules) {
6089 return (CarrierRestrictionRules) response;
6090 }
6091 // Response is an Exception of some kind,
6092 // which is signalled to the user as a NULL retval
6093 return null;
6094 } catch (Exception e) {
6095 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
6096 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006097 } finally {
6098 Binder.restoreCallingIdentity(identity);
6099 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07006100 }
6101
fionaxu59545b42016-05-25 15:53:37 -07006102 /**
6103 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
6104 * @param subId the subscription ID that this action applies to.
6105 * @param enabled control enable or disable metered apns.
6106 * {@hide}
6107 */
6108 @Override
6109 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
6110 enforceModifyPermission();
6111 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006112
6113 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006114 if (phone == null) {
6115 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
6116 return;
6117 }
6118 try {
6119 phone.carrierActionSetMeteredApnsEnabled(enabled);
6120 } catch (Exception e) {
6121 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006122 } finally {
6123 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006124 }
6125 }
6126
6127 /**
6128 * Action set from carrier signalling broadcast receivers to enable/disable radio
6129 * @param subId the subscription ID that this action applies to.
6130 * @param enabled control enable or disable radio.
6131 * {@hide}
6132 */
6133 @Override
6134 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
6135 enforceModifyPermission();
6136 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006137
6138 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07006139 if (phone == null) {
6140 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
6141 return;
6142 }
6143 try {
6144 phone.carrierActionSetRadioEnabled(enabled);
6145 } catch (Exception e) {
6146 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006147 } finally {
6148 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07006149 }
6150 }
6151
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006152 /**
fionaxu8da9cb12017-05-23 15:02:46 -07006153 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
6154 * network status based on which carrier apps could apply actions accordingly,
6155 * enable/disable default url handler for example.
6156 *
6157 * @param subId the subscription ID that this action applies to.
6158 * @param report control start/stop reporting the default network status.
6159 * {@hide}
6160 */
6161 @Override
6162 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
6163 enforceModifyPermission();
6164 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006165
6166 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07006167 if (phone == null) {
6168 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
6169 return;
6170 }
6171 try {
6172 phone.carrierActionReportDefaultNetworkStatus(report);
6173 } catch (Exception e) {
6174 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006175 } finally {
6176 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07006177 }
6178 }
6179
6180 /**
fionaxud9622282017-07-17 17:51:30 -07006181 * Action set from carrier signalling broadcast receivers to reset all carrier actions
6182 * @param subId the subscription ID that this action applies to.
6183 * {@hide}
6184 */
6185 @Override
6186 public void carrierActionResetAll(int subId) {
6187 enforceModifyPermission();
6188 final Phone phone = getPhone(subId);
6189 if (phone == null) {
6190 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
6191 return;
6192 }
6193 try {
6194 phone.carrierActionResetAll();
6195 } catch (Exception e) {
6196 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
6197 }
6198 }
6199
6200 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006201 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
6202 * bug report is being generated.
6203 */
6204 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07006205 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006206 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
6207 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07006208 writer.println("Permission Denial: can't dump Phone from pid="
6209 + Binder.getCallingPid()
6210 + ", uid=" + Binder.getCallingUid()
6211 + "without permission "
6212 + android.Manifest.permission.DUMP);
6213 return;
6214 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006215 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07006216 }
Jack Yueb89b242016-06-22 13:27:47 -07006217
Brad Ebingerdac2f002018-04-03 15:17:52 -07006218 @Override
6219 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
6220 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
6221 throws RemoteException {
6222 (new TelephonyShellCommand(this)).exec(this, in, out, err, args, callback, resultReceiver);
6223 }
6224
Jack Yueb89b242016-06-22 13:27:47 -07006225 /**
Jack Yu84291ec2017-05-26 16:07:50 -07006226 * Get aggregated video call data usage since boot.
6227 *
6228 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
6229 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07006230 * {@hide}
6231 */
6232 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07006233 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07006234 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
6235 null);
6236
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006237 final long identity = Binder.clearCallingIdentity();
6238 try {
6239 // NetworkStatsService keeps tracking the active network interface and identity. It
6240 // records the delta with the corresponding network identity.
6241 // We just return the total video call data usage snapshot since boot.
6242 Phone phone = getPhone(subId);
6243 if (phone != null) {
6244 return phone.getVtDataUsage(perUidStats);
6245 }
6246 return null;
6247 } finally {
6248 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07006249 }
Jack Yueb89b242016-06-22 13:27:47 -07006250 }
Jack Yu75ab2952016-07-08 14:29:33 -07006251
6252 /**
6253 * Policy control of data connection. Usually used when data limit is passed.
6254 * @param enabled True if enabling the data, otherwise disabling.
6255 * @param subId Subscription index
6256 * {@hide}
6257 */
6258 @Override
6259 public void setPolicyDataEnabled(boolean enabled, int subId) {
6260 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006261
6262 final long identity = Binder.clearCallingIdentity();
6263 try {
6264 Phone phone = getPhone(subId);
6265 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08006266 phone.getDataEnabledSettings().setPolicyDataEnabled(enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006267 }
6268 } finally {
6269 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07006270 }
6271 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006272
6273 /**
6274 * Get Client request stats
6275 * @return List of Client Request Stats
6276 * @hide
6277 */
6278 @Override
6279 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006280 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006281 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006282 return null;
6283 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006284 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006285
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006286 final long identity = Binder.clearCallingIdentity();
6287 try {
6288 if (phone != null) {
6289 return phone.getClientRequestStats();
6290 }
6291
6292 return null;
6293 } finally {
6294 Binder.restoreCallingIdentity(identity);
6295 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006296 }
6297
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006298 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006299 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00006300 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07006301 }
Jack Yueb4124c2017-02-16 15:32:43 -08006302
6303 /**
Grace Chen70990072017-03-24 17:21:30 -07006304 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08006305 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006306 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07006307 * @param state State of SIM (power down, power up, pass through)
6308 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
6309 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
6310 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08006311 *
6312 **/
6313 @Override
Grace Chen70990072017-03-24 17:21:30 -07006314 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08006315 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07006316 Phone phone = PhoneFactory.getPhone(slotIndex);
6317
vagdeviaf9a5b92018-08-15 16:01:53 -07006318 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6319
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006320 final long identity = Binder.clearCallingIdentity();
6321 try {
6322 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006323 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006324 }
6325 } finally {
6326 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08006327 }
6328 }
Shuo Qiandd210312017-04-12 22:11:33 +00006329
Tyler Gunn65d45c22017-06-05 11:22:26 -07006330 private boolean isUssdApiAllowed(int subId) {
6331 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006332 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07006333 if (configManager == null) {
6334 return false;
6335 }
6336 PersistableBundle pb = configManager.getConfigForSubId(subId);
6337 if (pb == null) {
6338 return false;
6339 }
6340 return pb.getBoolean(
6341 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
6342 }
6343
Shuo Qiandd210312017-04-12 22:11:33 +00006344 /**
6345 * Check if phone is in emergency callback mode
6346 * @return true if phone is in emergency callback mode
6347 * @param subId sub id
6348 */
goneil9c5f4872017-12-05 14:07:56 -08006349 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00006350 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006351 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00006352 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006353
6354 final long identity = Binder.clearCallingIdentity();
6355 try {
6356 if (phone != null) {
6357 return phone.isInEcm();
6358 } else {
6359 return false;
6360 }
6361 } finally {
6362 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00006363 }
6364 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006365
6366 /**
6367 * Get the current signal strength information for the given subscription.
6368 * Because this information is not updated when the device is in a low power state
6369 * it should not be relied-upon to be current.
6370 * @param subId Subscription index
6371 * @return the most recent cached signal strength info from the modem
6372 */
6373 @Override
6374 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006375 final long identity = Binder.clearCallingIdentity();
6376 try {
6377 Phone p = getPhone(subId);
6378 if (p == null) {
6379 return null;
6380 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006381
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006382 return p.getSignalStrength();
6383 } finally {
6384 Binder.restoreCallingIdentity(identity);
6385 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08006386 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006387
Pengquan Meng77b7f132018-08-22 14:49:57 -07006388 /**
Chen Xuf792fd62018-10-17 17:54:36 +00006389 * Get the current modem radio state for the given slot.
6390 * @param slotIndex slot index.
6391 * @param callingPackage the name of the package making the call.
6392 * @return the current radio power state from the modem
6393 */
6394 @Override
6395 public int getRadioPowerState(int slotIndex, String callingPackage) {
6396 Phone phone = PhoneFactory.getPhone(slotIndex);
6397 if (phone != null) {
6398 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6399 mApp, phone.getSubId(), callingPackage, "getRadioPowerState")) {
6400 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6401 }
6402
6403 final long identity = Binder.clearCallingIdentity();
6404 try {
6405 return phone.getRadioPowerState();
6406 } finally {
6407 Binder.restoreCallingIdentity(identity);
6408 }
6409 }
6410 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
6411 }
6412
6413 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07006414 * Checks if data roaming is enabled on the subscription with id {@code subId}.
6415 *
6416 * <p>Requires one of the following permissions:
6417 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
6418 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
6419 * privileges.
6420 *
6421 * @param subId subscription id
6422 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
6423 * {@code false}.
6424 */
6425 @Override
6426 public boolean isDataRoamingEnabled(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006427 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
6428 null /* message */);
6429
Pengquan Menga1bb6272018-09-06 09:59:22 -07006430 boolean isEnabled = false;
6431 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07006432 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006433 Phone phone = getPhone(subId);
6434 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006435 } catch (Exception e) {
6436 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6437 mApp, subId, "isDataRoamingEnabled");
Pengquan Menga1bb6272018-09-06 09:59:22 -07006438 } finally {
6439 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006440 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006441 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07006442 }
6443
6444
6445 /**
6446 * Enables/Disables the data roaming on the subscription with id {@code subId}.
6447 *
6448 * <p> Requires permission:
6449 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
6450 * privileges.
6451 *
6452 * @param subId subscription id
6453 * @param isEnabled {@code true} means enable, {@code false} means disable.
6454 */
6455 @Override
6456 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006457 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6458 mApp, subId, "setDataRoamingEnabled");
6459
Pengquan Menga1bb6272018-09-06 09:59:22 -07006460 final long identity = Binder.clearCallingIdentity();
6461 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07006462 Phone phone = getPhone(subId);
6463 if (phone != null) {
6464 phone.setDataRoamingEnabled(isEnabled);
6465 }
6466 } finally {
6467 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07006468 }
6469 }
6470
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006471 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006472 public boolean isManualNetworkSelectionAllowed(int subId) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07006473 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
6474 mApp, subId, "isManualNetworkSelectionAllowed");
6475
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006476 boolean isAllowed = true;
6477 final long identity = Binder.clearCallingIdentity();
6478 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07006479 Phone phone = getPhone(subId);
6480 if (phone != null) {
6481 isAllowed = phone.isCspPlmnEnabled();
6482 }
6483 } finally {
6484 Binder.restoreCallingIdentity(identity);
6485 }
6486 return isAllowed;
6487 }
6488
6489 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08006490 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liucfdfe3a2020-03-23 11:55:07 -07006491 // Verify that tha callingPackage belongs to the calling UID
6492 mApp.getSystemService(AppOpsManager.class)
6493 .checkPackage(Binder.getCallingUid(), callingPackage);
6494
Jordan Liu1e142fc2019-04-22 15:10:43 -07006495 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08006496 try {
6497 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07006498 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08006499 } catch (SecurityException e) {
6500 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
6501 // has carrier privileges on an active UICC
6502 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
6503 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07006504 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08006505 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08006506 }
Jordan Liu5aa07002018-12-18 15:44:48 -08006507
6508 final long identity = Binder.clearCallingIdentity();
6509 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08006510 UiccController uiccController = UiccController.getInstance();
6511 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07006512 if (hasReadPermission) {
6513 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08006514 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07006515
6516 // Remove private info if the caller doesn't have access
6517 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
6518 for (UiccCardInfo cardInfo : cardInfos) {
6519 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
6520 // is available
6521 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
6522 if (card == null || card.getUiccProfile() == null) {
6523 // assume no access if the card or profile is unavailable
6524 filteredInfos.add(cardInfo.getUnprivileged());
6525 continue;
6526 }
6527 UiccProfile profile = card.getUiccProfile();
6528 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
6529 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6530 filteredInfos.add(cardInfo);
6531 } else {
6532 filteredInfos.add(cardInfo.getUnprivileged());
6533 }
6534 }
6535 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08006536 } finally {
6537 Binder.restoreCallingIdentity(identity);
6538 }
6539 }
6540
6541 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006542 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006543 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006544
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006545 final long identity = Binder.clearCallingIdentity();
6546 try {
6547 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
6548 if (slots == null) {
6549 Rlog.i(LOG_TAG, "slots is null.");
6550 return null;
6551 }
6552
6553 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
6554 for (int i = 0; i < slots.length; i++) {
6555 UiccSlot slot = slots[i];
6556 if (slot == null) {
6557 continue;
6558 }
6559
Jordan Liu7be7e652019-05-06 18:55:02 +00006560 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006561 UiccCard card = slot.getUiccCard();
6562 if (card != null) {
6563 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00006564 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07006565 cardId = slot.getEid();
6566 if (TextUtils.isEmpty(cardId)) {
6567 cardId = slot.getIccId();
6568 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006569 }
6570
6571 int cardState = 0;
6572 switch (slot.getCardState()) {
6573 case CARDSTATE_ABSENT:
6574 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
6575 break;
6576 case CARDSTATE_PRESENT:
6577 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
6578 break;
6579 case CARDSTATE_ERROR:
6580 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
6581 break;
6582 case CARDSTATE_RESTRICTED:
6583 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
6584 break;
6585 default:
6586 break;
6587
6588 }
6589
6590 infos[i] = new UiccSlotInfo(
6591 slot.isActive(),
6592 slot.isEuicc(),
6593 cardId,
6594 cardState,
6595 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08006596 slot.isExtendedApduSupported(),
6597 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006598 }
6599 return infos;
6600 } finally {
6601 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07006602 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006603 }
6604
6605 @Override
6606 public boolean switchSlots(int[] physicalSlots) {
6607 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006608
6609 final long identity = Binder.clearCallingIdentity();
6610 try {
6611 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
6612 } finally {
6613 Binder.restoreCallingIdentity(identity);
6614 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00006615 }
Jack Yu4c988042018-02-27 15:30:01 -08006616
6617 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08006618 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08006619 final long identity = Binder.clearCallingIdentity();
6620 try {
6621 return UiccController.getInstance().getCardIdForDefaultEuicc();
6622 } finally {
6623 Binder.restoreCallingIdentity(identity);
6624 }
6625 }
6626
6627 @Override
Jack Yu4c988042018-02-27 15:30:01 -08006628 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
6629 enforceModifyPermission();
6630 final Phone phone = getPhone(subId);
6631 if (phone == null) {
6632 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
6633 return;
6634 }
6635
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006636 final long identity = Binder.clearCallingIdentity();
6637 try {
6638 phone.setRadioIndicationUpdateMode(filters, mode);
6639 } finally {
6640 Binder.restoreCallingIdentity(identity);
6641 }
Jack Yu4c988042018-02-27 15:30:01 -08006642 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07006643
6644 /**
goneil47ffb6e2018-04-06 15:40:58 -07006645 * A test API to reload the UICC profile.
6646 *
6647 * <p>Requires that the calling app has permission
6648 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
6649 * @hide
6650 */
6651 @Override
6652 public void refreshUiccProfile(int subId) {
6653 enforceModifyPermission();
6654
6655 final long identity = Binder.clearCallingIdentity();
6656 try {
6657 Phone phone = getPhone(subId);
6658 if (phone == null) {
6659 return;
6660 }
6661 UiccCard uiccCard = phone.getUiccCard();
6662 if (uiccCard == null) {
6663 return;
6664 }
6665 UiccProfile uiccProfile = uiccCard.getUiccProfile();
6666 if (uiccProfile == null) {
6667 return;
6668 }
6669 uiccProfile.refresh();
6670 } finally {
6671 Binder.restoreCallingIdentity(identity);
6672 }
6673 }
6674
6675 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07006676 * Returns false if the mobile data is disabled by default, otherwise return true.
6677 */
6678 private boolean getDefaultDataEnabled() {
6679 return "true".equalsIgnoreCase(
6680 SystemProperties.get(DEFAULT_MOBILE_DATA_PROPERTY_NAME, "true"));
6681 }
6682
6683 /**
6684 * Returns true if the data roaming is enabled by default, i.e the system property
6685 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
6686 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
6687 */
6688 private boolean getDefaultDataRoamingEnabled(int subId) {
6689 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006690 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006691 boolean isDataRoamingEnabled = "true".equalsIgnoreCase(
6692 SystemProperties.get(DEFAULT_DATA_ROAMING_PROPERTY_NAME, "false"));
6693 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
6694 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
6695 return isDataRoamingEnabled;
6696 }
6697
6698 /**
6699 * Returns the default network type for the given {@code subId}, if the default network type is
6700 * not set, return {@link Phone#PREFERRED_NT_MODE}.
6701 */
6702 private int getDefaultNetworkType(int subId) {
6703 return Integer.parseInt(
6704 TelephonyManager.getTelephonyProperty(
6705 mSubscriptionController.getPhoneId(subId),
6706 DEFAULT_NETWORK_MODE_PROPERTY_NAME,
6707 String.valueOf(Phone.PREFERRED_NT_MODE)));
6708 }
fionaxua13278b2018-03-21 00:08:13 -07006709
6710 @Override
6711 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07006712 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07006713 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006714
6715 final long identity = Binder.clearCallingIdentity();
6716 try {
6717 final Phone phone = getPhone(subId);
6718 if (phone == null) {
6719 loge("setCarrierTestOverride fails with invalid subId: " + subId);
6720 return;
6721 }
chen xueaba88a2019-03-15 13:15:10 -07006722 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
6723 carrierPrivilegeRules, apn);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006724 } finally {
6725 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006726 }
fionaxua13278b2018-03-21 00:08:13 -07006727 }
6728
6729 @Override
6730 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07006731 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006732
6733 final long identity = Binder.clearCallingIdentity();
6734 try {
6735 final Phone phone = getPhone(subId);
6736 if (phone == null) {
6737 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
6738 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
6739 }
6740 return phone.getCarrierIdListVersion();
6741 } finally {
6742 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07006743 }
fionaxua13278b2018-03-21 00:08:13 -07006744 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07006745
6746 @Override
6747 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
6748 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
6749 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
6750 return -1;
6751 }
6752
6753 final long identity = Binder.clearCallingIdentity();
6754 try {
6755 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
6756 } finally {
6757 Binder.restoreCallingIdentity(identity);
6758 }
6759 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07006760
6761 @Override
6762 public int getCdmaRoamingMode(int subId) {
6763 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6764 mApp, subId, "getCdmaRoamingMode");
6765
6766 final long identity = Binder.clearCallingIdentity();
6767 try {
6768 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
6769 } finally {
6770 Binder.restoreCallingIdentity(identity);
6771 }
6772 }
6773
6774 @Override
6775 public boolean setCdmaRoamingMode(int subId, int mode) {
6776 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6777 mApp, subId, "setCdmaRoamingMode");
6778
6779 final long identity = Binder.clearCallingIdentity();
6780 try {
6781 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
6782 } finally {
6783 Binder.restoreCallingIdentity(identity);
6784 }
6785 }
6786
6787 @Override
6788 public boolean setCdmaSubscriptionMode(int subId, int mode) {
6789 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6790 mApp, subId, "setCdmaSubscriptionMode");
6791
6792 final long identity = Binder.clearCallingIdentity();
6793 try {
6794 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
6795 } finally {
6796 Binder.restoreCallingIdentity(identity);
6797 }
6798 }
Makoto Onukida3bf792018-09-18 16:06:29 -07006799
6800 private void ensureUserRunning(int userId) {
6801 if (!mUserManager.isUserRunning(userId)) {
6802 throw new IllegalStateException("User " + userId + " does not exist or not running");
6803 }
6804 }
6805
6806 /**
6807 * Returns a list of SMS apps on a given user.
6808 *
6809 * Only the shell user (UID 2000 or 0) can call it.
6810 * Target user must be running.
6811 */
6812 @Override
6813 public String[] getSmsApps(int userId) {
6814 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getSmsApps");
6815 ensureUserRunning(userId);
6816
6817 final Collection<SmsApplicationData> apps =
6818 SmsApplication.getApplicationCollectionAsUser(mApp, userId);
6819
6820 String[] ret = new String[apps.size()];
6821 int i = 0;
6822 for (SmsApplicationData app : apps) {
6823 ret[i++] = app.mPackageName;
6824 }
6825 return ret;
6826 }
6827
6828 /**
6829 * Returns the default SMS app package name on a given user.
6830 *
6831 * Only the shell user (UID 2000 or 0) can call it.
6832 * Target user must be running.
6833 */
6834 @Override
6835 public String getDefaultSmsApp(int userId) {
6836 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDefaultSmsApp");
6837 ensureUserRunning(userId);
6838
6839 final ComponentName cn = SmsApplication.getDefaultSmsApplicationAsUser(mApp,
6840 /* updateIfNeeded= */ true, userId);
6841 return cn == null ? null : cn.getPackageName();
6842 }
6843
6844 /**
6845 * Set a package as the default SMS app on a given user.
6846 *
6847 * Only the shell user (UID 2000 or 0) can call it.
6848 * Target user must be running.
6849 */
6850 @Override
6851 public void setDefaultSmsApp(int userId, String packageName) {
6852 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDefaultSmsApp");
6853 ensureUserRunning(userId);
6854
6855 boolean found = false;
6856 for (String pkg : getSmsApps(userId)) {
6857 if (TextUtils.equals(packageName, pkg)) {
6858 found = true;
6859 break;
6860 }
6861 }
6862 if (!found) {
6863 throw new IllegalArgumentException("Package " + packageName + " is not an SMS app");
6864 }
6865
6866 SmsApplication.setDefaultApplicationAsUser(packageName, mApp, userId);
6867 }
sqianc5eccab2018-10-19 18:46:41 -07006868
6869 @Override
sqian8c685422019-02-22 15:55:18 -08006870 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
sqianc5eccab2018-10-19 18:46:41 -07006871 String callingPackage) {
sqian11b7a0e2018-12-05 18:48:28 -08006872 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
sqian8c685422019-02-22 15:55:18 -08006873 mApp, getDefaultSubscription(), callingPackage, "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08006874 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6875 }
6876 final long identity = Binder.clearCallingIdentity();
6877 try {
sqian854d44b2018-12-12 16:48:18 -08006878 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
6879 for (Phone phone: PhoneFactory.getPhones()) {
6880 if (phone.getEmergencyNumberTracker() != null
6881 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
6882 emergencyNumberListInternal.put(
6883 phone.getSubId(),
6884 phone.getEmergencyNumberTracker().getEmergencyNumberList());
6885 }
sqian11b7a0e2018-12-05 18:48:28 -08006886 }
sqian854d44b2018-12-12 16:48:18 -08006887 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08006888 } finally {
6889 Binder.restoreCallingIdentity(identity);
6890 }
sqianc5eccab2018-10-19 18:46:41 -07006891 }
6892
6893 @Override
sqian8c685422019-02-22 15:55:18 -08006894 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006895 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08006896 if (!exactMatch) {
6897 TelephonyPermissions
6898 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08006899 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08006900 }
6901 final long identity = Binder.clearCallingIdentity();
6902 try {
sqian854d44b2018-12-12 16:48:18 -08006903 for (Phone phone: PhoneFactory.getPhones()) {
6904 if (phone.getEmergencyNumberTracker() != null
6905 && phone.getEmergencyNumberTracker() != null) {
6906 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
6907 number, exactMatch)) {
6908 return true;
sqian11b7a0e2018-12-05 18:48:28 -08006909 }
6910 }
sqian11b7a0e2018-12-05 18:48:28 -08006911 }
6912 return false;
6913 } finally {
6914 Binder.restoreCallingIdentity(identity);
6915 }
6916 }
6917
sqianf4ca7ed2019-01-15 18:32:07 -08006918 /**
6919 * Update emergency number list for test mode.
6920 */
6921 @Override
6922 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
6923 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6924 "updateEmergencyNumberListTestMode");
6925
6926 final long identity = Binder.clearCallingIdentity();
6927 try {
6928 for (Phone phone: PhoneFactory.getPhones()) {
6929 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6930 if (tracker != null) {
6931 tracker.executeEmergencyNumberTestModeCommand(action, num);
6932 }
6933 }
6934 } finally {
6935 Binder.restoreCallingIdentity(identity);
6936 }
6937 }
6938
6939 /**
6940 * Get the full emergency number list for test mode.
6941 */
6942 @Override
6943 public List<String> getEmergencyNumberListTestMode() {
6944 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6945 "getEmergencyNumberListTestMode");
6946
6947 final long identity = Binder.clearCallingIdentity();
6948 try {
6949 Set<String> emergencyNumbers = new HashSet<>();
6950 for (Phone phone: PhoneFactory.getPhones()) {
6951 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
6952 if (tracker != null) {
6953 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
6954 emergencyNumbers.add(num.getNumber());
6955 }
6956 }
6957 }
6958 return new ArrayList<>(emergencyNumbers);
6959 } finally {
6960 Binder.restoreCallingIdentity(identity);
6961 }
6962 }
6963
chen xud6b45bd2018-10-30 22:27:10 -07006964 @Override
6965 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
6966 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
6967 Phone phone = getPhone(subId);
6968 if (phone == null) {
6969 return null;
6970 }
6971 final long identity = Binder.clearCallingIdentity();
6972 try {
6973 UiccProfile profile = UiccController.getInstance()
6974 .getUiccProfileForPhone(phone.getPhoneId());
6975 if (profile != null) {
6976 return profile.getCertsFromCarrierPrivilegeAccessRules();
6977 }
6978 } finally {
6979 Binder.restoreCallingIdentity(identity);
6980 }
6981 return null;
6982 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08006983
6984 /**
6985 * Enable or disable a modem stack.
6986 */
6987 @Override
6988 public boolean enableModemForSlot(int slotIndex, boolean enable) {
6989 enforceModifyPermission();
6990
6991 final long identity = Binder.clearCallingIdentity();
6992 try {
6993 Phone phone = PhoneFactory.getPhone(slotIndex);
6994 if (phone == null) {
6995 return false;
6996 } else {
6997 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
6998 }
6999 } finally {
7000 Binder.restoreCallingIdentity(identity);
7001 }
7002 }
Michelecea4cf22018-12-21 15:00:11 -08007003
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007004 /**
7005 * Whether a modem stack is enabled or not.
7006 */
7007 @Override
7008 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage) {
7009 Phone phone = PhoneFactory.getPhone(slotIndex);
7010 if (phone == null) return false;
7011
7012 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7013 mApp, phone.getSubId(), callingPackage, "isModemEnabledForSlot")) {
7014 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7015 }
7016
7017 final long identity = Binder.clearCallingIdentity();
7018 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07007019 try {
7020 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
7021 } catch (NoSuchElementException ex) {
7022 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
7023 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07007024 } finally {
7025 Binder.restoreCallingIdentity(identity);
7026 }
7027 }
7028
Michelecea4cf22018-12-21 15:00:11 -08007029 @Override
Michele0ea7d782019-03-19 14:58:42 -07007030 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08007031 enforceModifyPermission();
7032
7033 final long identity = Binder.clearCallingIdentity();
7034 try {
7035 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07007036 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08007037 .commit();
7038 } finally {
7039 Binder.restoreCallingIdentity(identity);
7040 }
7041 }
7042
7043 @Override
Michele0ea7d782019-03-19 14:58:42 -07007044 @TelephonyManager.IsMultiSimSupportedResult
7045 public int isMultiSimSupported(String callingPackage) {
Michele4245e952019-02-04 11:36:23 -08007046 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Michele0ea7d782019-03-19 14:58:42 -07007047 getDefaultPhone().getSubId(), callingPackage, "isMultiSimSupported")) {
7048 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08007049 }
Michelecea4cf22018-12-21 15:00:11 -08007050
7051 final long identity = Binder.clearCallingIdentity();
7052 try {
Michele0ea7d782019-03-19 14:58:42 -07007053 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08007054 } finally {
7055 Binder.restoreCallingIdentity(identity);
7056 }
7057 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007058
Michele0ea7d782019-03-19 14:58:42 -07007059 @TelephonyManager.IsMultiSimSupportedResult
7060 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08007061 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
7062 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
7063 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007064 loge("isMultiSimSupportedInternal: requires at least 2 cards");
7065 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007066 }
7067 // Check if the hardware supports multisim functionality. If usage of multisim is not
7068 // supported by the modem, indicate that it is restricted.
7069 PhoneCapability staticCapability =
7070 mPhoneConfigurationManager.getStaticPhoneCapability();
7071 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07007072 loge("isMultiSimSupportedInternal: no static configuration available");
7073 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007074 }
7075 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07007076 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
7077 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08007078 }
7079 // Check if support of multiple SIMs is restricted by carrier
7080 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07007081 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08007082 }
7083
Michele0ea7d782019-03-19 14:58:42 -07007084 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08007085 }
7086
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007087 /**
7088 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007089 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
7090 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
7091 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007092 * @param numOfSims number of active sims we want to switch to
7093 */
7094 @Override
7095 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08007096 if (numOfSims == 1) {
7097 enforceModifyPermission();
7098 } else {
7099 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7100 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
7101 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007102 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08007103
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007104 try {
Michele30b57b22019-03-01 12:01:14 -08007105 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07007106 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08007107 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
7108 return;
7109 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08007110 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
7111 } finally {
7112 Binder.restoreCallingIdentity(identity);
7113 }
7114 }
7115
7116 /**
chen xub4baa772019-04-03 10:23:41 -07007117 * Get whether making changes to modem configurations will trigger reboot.
7118 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007119 */
7120 @Override
chen xub4baa772019-04-03 10:23:41 -07007121 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage) {
7122 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
7123 mApp, subId, callingPackage, "doesSwitchMultiSimConfigTriggerReboot")) {
7124 return false;
7125 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08007126 final long identity = Binder.clearCallingIdentity();
7127 try {
7128 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
7129 } finally {
7130 Binder.restoreCallingIdentity(identity);
7131 }
7132 }
7133
Nathan Harold29f5f052019-02-15 13:41:57 -08007134 private void updateModemStateMetrics() {
7135 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
7136 // TODO: check the state for each modem if the api is ready.
7137 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
7138 }
7139
Pengquan Meng3889a572019-01-23 11:16:29 -08007140 @Override
7141 public int[] getSlotsMapping() {
7142 enforceReadPrivilegedPermission("getSlotsMapping");
7143
7144 final long identity = Binder.clearCallingIdentity();
7145 try {
7146 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
7147 // All logical slots should have a mapping to a physical slot.
7148 int[] logicalSlotsMapping = new int[phoneCount];
7149 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
7150 for (int i = 0; i < slotInfos.length; i++) {
7151 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
7152 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
7153 }
7154 }
7155 return logicalSlotsMapping;
7156 } finally {
7157 Binder.restoreCallingIdentity(identity);
7158 }
7159 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08007160
7161 /**
7162 * Get the IRadio HAL Version
7163 */
7164 @Override
7165 public int getRadioHalVersion() {
7166 Phone phone = getDefaultPhone();
7167 if (phone == null) return -1;
7168 HalVersion hv = phone.getHalVersion();
7169 if (hv.equals(HalVersion.UNKNOWN)) return -1;
7170 return hv.major * 100 + hv.minor;
7171 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007172
7173 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07007174 * Return whether data is enabled for certain APN type. This will tell if framework will accept
7175 * corresponding network requests on a subId.
7176 *
7177 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007178 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07007179 * 2) APN is un-metered for this subscription, or
7180 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
7181 * {@link SubscriptionManager#setAlwaysAllowMmsData} is turned on.
7182 *
7183 * @return whether data is allowed for a apn type.
7184 *
7185 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007186 */
7187 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07007188 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007189 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Malcolm Chene5ad5792019-04-18 13:51:02 -07007190 mApp, subId, callingPackage, "isDataEnabledForApn")) {
7191 throw new SecurityException("Needs READ_PHONE_STATE for isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007192 }
7193
7194 // Now that all security checks passes, perform the operation as ourselves.
7195 final long identity = Binder.clearCallingIdentity();
7196 try {
7197 Phone phone = getPhone(subId);
7198 if (phone == null) return false;
7199
Jack Yu41407ee2019-05-13 16:54:09 -07007200 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07007201 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
7202 } finally {
7203 Binder.restoreCallingIdentity(identity);
7204 }
7205 }
7206
7207 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07007208 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07007209 enforceReadPrivilegedPermission("isApnMetered");
7210
7211 // Now that all security checks passes, perform the operation as ourselves.
7212 final long identity = Binder.clearCallingIdentity();
7213 try {
7214 Phone phone = getPhone(subId);
7215 if (phone == null) return true; // By default return true.
7216
Jack Yu41407ee2019-05-13 16:54:09 -07007217 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07007218 } finally {
7219 Binder.restoreCallingIdentity(identity);
7220 }
7221 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07007222
7223 @Override
7224 public void enqueueSmsPickResult(String callingPackage, IIntegerConsumer pendingSubIdResult) {
7225 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
7226 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
7227 if (!permissions.checkCallingCanSendSms(callingPackage, "Sending message")) {
7228 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
7229 }
7230 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
7231 Intent intent = new Intent();
7232 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
7233 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7234 // Bring up choose default SMS subscription dialog right now
7235 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
7236 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
7237 mApp.startActivity(intent);
7238 }
chen xud5ca2d52019-05-28 15:20:57 -07007239
7240 @Override
7241 public String getMmsUAProfUrl(int subId) {
7242 //TODO investigate if this API should require proper permission check in R b/133791609
7243 final long identity = Binder.clearCallingIdentity();
7244 try {
7245 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7246 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
7247 } finally {
7248 Binder.restoreCallingIdentity(identity);
7249 }
7250 }
7251
7252 @Override
7253 public String getMmsUserAgent(int subId) {
7254 //TODO investigate if this API should require proper permission check in R b/133791609
7255 final long identity = Binder.clearCallingIdentity();
7256 try {
7257 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
7258 .getString(com.android.internal.R.string.config_mms_user_agent);
7259 } finally {
7260 Binder.restoreCallingIdentity(identity);
7261 }
7262 }
Jack Yub07d4972019-05-28 16:12:25 -07007263
7264 @Override
7265 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
7266 enforceModifyPermission();
7267
7268 // Now that all security checks passes, perform the operation as ourselves.
7269 final long identity = Binder.clearCallingIdentity();
7270 try {
7271 Phone phone = getPhone(subId);
7272 if (phone == null) return false;
7273
7274 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
7275 } finally {
7276 Binder.restoreCallingIdentity(identity);
7277 }
7278 }
7279
7280 @Override
7281 public boolean isDataAllowedInVoiceCall(int subId) {
7282 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
7283
7284 // Now that all security checks passes, perform the operation as ourselves.
7285 final long identity = Binder.clearCallingIdentity();
7286 try {
7287 Phone phone = getPhone(subId);
7288 if (phone == null) return false;
7289
7290 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
7291 } finally {
7292 Binder.restoreCallingIdentity(identity);
7293 }
7294 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07007295}