blob: 011d041cc160d682b789711879f8eaaff4fd7cb6 [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;
Gil Cukierman1c0eb932022-12-06 22:28:24 +000020import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
jimsunf9ec1622022-09-13 21:18:43 +080021import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
Hall Liud892bec2018-11-30 14:51:45 -080022
Shuo Qianccbaf742021-02-22 18:32:21 -080023import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA;
24import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_GSM;
Tyler Gunn7bcdc742019-10-04 15:56:59 -070025import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070026import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
joonhunshin3e154242021-09-17 06:33:39 +000027import static com.android.internal.telephony.TelephonyStatsLog.RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT;
Ta-wei Yen87c49842016-05-13 21:19:52 -070028
Brad Ebinger34c09a52021-02-17 23:23:21 +000029import android.Manifest;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080030import android.Manifest.permission;
Hall Liua1548bd2019-12-24 14:14:12 -080031import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070032import android.annotation.Nullable;
sandeepjsb6c87872021-09-27 15:34:44 +000033import android.annotation.RequiresPermission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080035import android.app.PendingIntent;
Tyler Gunn64144d92022-03-17 14:16:41 -070036import android.app.PropertyInvalidatedCache;
Brad Ebinger4f6208e2021-03-23 21:04:45 +000037import android.app.compat.CompatChanges;
Hall Liu82694d52020-12-11 18:22:04 -080038import android.app.role.RoleManager;
sandeepjsb6c87872021-09-27 15:34:44 +000039import android.compat.annotation.ChangeId;
40import android.compat.annotation.EnabledSince;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070041import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070042import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.content.Context;
44import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070046import android.content.pm.ComponentInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070047import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.net.Uri;
49import android.os.AsyncResult;
50import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080051import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.os.Bundle;
53import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070054import android.os.IBinder;
tom hsu0b59d292022-09-29 23:49:21 +080055import android.os.LocaleList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.os.Looper;
57import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070058import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080059import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070060import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070061import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080062import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080063import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070064import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070065import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080066import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070068import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070069import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070070import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070071import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080072import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070073import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090074import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080075import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080076import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070077import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080078import android.telephony.AccessNetworkConstants;
79import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070080import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070081import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080082import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070083import android.telephony.AnomalyReporter;
Shuo Qian4a594052020-01-23 11:59:30 -080084import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070085import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080086import android.telephony.CarrierRestrictionRules;
Hui Wang9b5793a2022-12-05 14:38:06 -060087import android.telephony.CellBroadcastIdRange;
yincheng zhao2737e882019-09-06 17:06:54 -070088import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080089import android.telephony.CellIdentityCdma;
90import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070092import android.telephony.CellInfoGsm;
93import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070094import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080095import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070096import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070097import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070098import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080099import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700100import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -0800101import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -0700102import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -0800103import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800104import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700105import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800106import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800108import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800109import android.telephony.SignalStrengthUpdateRequest;
110import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800111import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800112import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800113import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700114import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700115import android.telephony.TelephonyManager;
Jack Yuf5badd92022-12-08 00:50:53 -0800116import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800117import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800118import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800119import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000120import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000121import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000122import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700123import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700124import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800125import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800126import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700127import android.telephony.gba.GbaAuthRequest;
128import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700129import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800130import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000131import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000132import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700133import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000134import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700135import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800136import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700137import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800138import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700139import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000140import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700141import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800142import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800143import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800145import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700146import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800148import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800149
Andrew Lee312e8172014-10-23 17:01:36 -0700150import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800151import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800152import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800153import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800154import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700156import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700157import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800158import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800159import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700160import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700161import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800162import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700163import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800164import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800165import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800166import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700167import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000168import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700169import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800170import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800172import com.android.internal.telephony.IccCard;
Jack Yuf5badd92022-12-08 00:50:53 -0800173import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800174import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700175import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700176import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700177import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700179import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800180import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700181import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700182import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700183import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700184import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800185import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800186import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700187import com.android.internal.telephony.ServiceStateTracker;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000188import com.android.internal.telephony.SmsApplication;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700189import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700190import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800191import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800192import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800193import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700194import com.android.internal.telephony.data.DataUtils;
Hunsuk Choi42cc62a2022-10-16 06:03:40 +0000195import com.android.internal.telephony.domainselection.DomainSelectionResolver;
sqianf4ca7ed2019-01-15 18:32:07 -0800196import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700197import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800198import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700199import com.android.internal.telephony.imsphone.ImsPhone;
200import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000201import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800202import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yu285100e2022-12-02 22:48:35 -0800203import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
204import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700205import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700206import com.android.internal.telephony.uicc.IccIoResult;
207import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800208import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700209import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800210import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700211import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000212import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800213import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000214import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800215import com.android.internal.telephony.util.LocaleUtils;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000216import com.android.internal.telephony.util.TelephonyUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700217import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700218import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800219import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800220import com.android.phone.callcomposer.CallComposerPictureManager;
221import com.android.phone.callcomposer.CallComposerPictureTransfer;
222import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700223import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin46355ba2022-11-01 23:51:16 -0700224import com.android.phone.slice.SlicePurchaseController;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700225import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800226import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700227import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700228import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800229import com.android.services.telephony.TelecomAccountRegistry;
230import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800231import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800232
Hall Liu82694d52020-12-11 18:22:04 -0800233import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700234import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800235import java.io.IOException;
236import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700237import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800239import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000240import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800241import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800242import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800243import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800244import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100245import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800246import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700247import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800248import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800249import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700250import java.util.UUID;
Hall Liu82694d52020-12-11 18:22:04 -0800251import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800252import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800253import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254
255/**
256 * Implementation of the ITelephony interface.
257 */
Santos Cordon117fee72014-05-16 17:56:12 -0700258public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 private static final String LOG_TAG = "PhoneInterfaceManager";
260 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
261 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800262 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263
264 // Message codes used with mMainThreadHandler
265 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700266 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
267 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700268 private static final int CMD_OPEN_CHANNEL = 9;
269 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
270 private static final int CMD_CLOSE_CHANNEL = 11;
271 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800272 private static final int CMD_NV_READ_ITEM = 13;
273 private static final int EVENT_NV_READ_ITEM_DONE = 14;
274 private static final int CMD_NV_WRITE_ITEM = 15;
275 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
276 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
277 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700278 private static final int CMD_RESET_MODEM_CONFIG = 19;
279 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800280 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
281 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800282 private static final int CMD_SEND_ENVELOPE = 25;
283 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000284 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
285 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700286 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
287 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
288 private static final int CMD_EXCHANGE_SIM_IO = 31;
289 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800290 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
291 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700292 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
293 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700294 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
295 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700296 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
297 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
298 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
299 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700300 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
301 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
302 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
303 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700304 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800305 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
306 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000307 private static final int CMD_SWITCH_SLOTS = 50;
308 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700309 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
310 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
311 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
312 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
313 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
314 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
315 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
316 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700317 private static final int CMD_GET_ALL_CELL_INFO = 60;
318 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
319 private static final int CMD_GET_CELL_LOCATION = 62;
320 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700321 private static final int CMD_MODEM_REBOOT = 64;
322 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700323 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
324 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800325 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
326 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700327 private static final int CMD_GET_MODEM_STATUS = 70;
328 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700329 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
330 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700331 private static final int CMD_ERASE_MODEM_CONFIG = 74;
332 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800333 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
334 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
335 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
336 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800337 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
338 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800339 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800340 private static final int CMD_GET_CALL_FORWARDING = 83;
341 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
342 private static final int CMD_SET_CALL_FORWARDING = 85;
343 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
344 private static final int CMD_GET_CALL_WAITING = 87;
345 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
346 private static final int CMD_SET_CALL_WAITING = 89;
347 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700348 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
349 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
350 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
351 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700352 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
353 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800354 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
355 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800356 private static final int CMD_SET_DATA_THROTTLING = 99;
357 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800358 private static final int CMD_SET_SIM_POWER = 101;
359 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800360 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
361 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
362 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
363 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800364 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
365 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000366 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800367 private static final int CMD_GET_SLICING_CONFIG = 110;
368 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800369 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700370 private static final int CMD_ENABLE_VONR = 113;
371 private static final int EVENT_ENABLE_VONR_DONE = 114;
372 private static final int CMD_IS_VONR_ENABLED = 115;
373 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700374 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
375 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800377 // Parameters of select command.
378 private static final int SELECT_COMMAND = 0xA4;
379 private static final int SELECT_P1 = 0x04;
380 private static final int SELECT_P2 = 0;
381 private static final int SELECT_P3 = 0x10;
382
Gil Cukierman1c0eb932022-12-06 22:28:24 +0000383 // Toggling null cipher and integrity support was added in IRadioNetwork 2.1
384 private static final int MIN_NULL_CIPHER_AND_INTEGRITY_VERSION = 201;
385
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 /** The singleton instance. */
387 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800388 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389
Wink Saville3ab207e2014-11-20 13:07:20 -0800390 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800391 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800392 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700393 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800394 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700395 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800396 private MainThreadHandler mMainThreadHandler;
Jack Yue37dd262022-12-16 11:53:37 -0800397 private final SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800398 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700399 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800400 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Gil Cukierman6dac5eb2022-09-19 16:09:04 +0000401 private final Telephony2gUpdater mTelephony2gUpdater;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402
Peter Wangdafb9ac2020-01-15 14:13:38 -0800403 /** User Activity */
404 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800405 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
406
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700407 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
408
Derek Tan97ebb422014-09-05 16:55:38 -0700409 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
410 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800411 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800412 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700413
Michelecea4cf22018-12-21 15:00:11 -0800414 // String to store multi SIM allowed
415 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
416
Derek Tan740e1672017-06-27 14:56:27 -0700417 // The AID of ISD-R.
418 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
419
yinxub1bed742017-04-17 11:45:04 -0700420 private NetworkScanRequestTracker mNetworkScanRequestTracker;
421
David Kelly5e06a7f2018-03-12 14:10:59 +0000422 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
423 private static final int MANUFACTURER_CODE_LENGTH = 8;
424
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800425 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800426 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800427
Sarah Chin2ec39f62022-08-31 17:03:26 -0700428 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
429 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
430
Derek Tan89e89d42014-07-08 17:00:10 -0700431 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700432 * Experiment flag to enable erase modem config on reset network, default value is false
433 */
434 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
435 "reset_network_erase_modem_config_enabled";
436
Rambo Wang0f050d82021-02-12 11:43:36 -0800437 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800438
Gary Jian76280a42022-12-07 16:18:33 +0800439 private static final int MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS = 50;
440
sandeepjsb6c87872021-09-27 15:34:44 +0000441 /**
442 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
443 * one ICCID active at the same time.
444 * Apps should use below API signatures if targeting SDK is T and beyond.
445 *
446 * @hide
447 */
448 @ChangeId
449 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
450 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800451
Naina Nallurid63128d2019-09-17 14:10:30 -0700452 /**
Chen Xu540470b2021-12-14 17:15:47 -0800453 * Apps targeting on Android T and beyond will get exception whenever icc close channel
454 * operation fails.
455 */
456 @ChangeId
457 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
458 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
459
460 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700461 * A request object to use for transmitting data to an ICC.
462 */
463 private static final class IccAPDUArgument {
464 public int channel, cla, command, p1, p2, p3;
465 public String data;
466
467 public IccAPDUArgument(int channel, int cla, int command,
468 int p1, int p2, int p3, String data) {
469 this.channel = channel;
470 this.cla = cla;
471 this.command = command;
472 this.p1 = p1;
473 this.p2 = p2;
474 this.p3 = p3;
475 this.data = data;
476 }
477 }
478
479 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700480 * A request object to use for transmitting data to an ICC.
481 */
482 private static final class ManualNetworkSelectionArgument {
483 public OperatorInfo operatorInfo;
484 public boolean persistSelection;
485
486 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
487 this.operatorInfo = operatorInfo;
488 this.persistSelection = persistSelection;
489 }
490 }
491
Sarah Chin71b3a852022-09-28 15:54:19 -0700492 private static final class PurchasePremiumCapabilityArgument {
493 public @TelephonyManager.PremiumCapability int capability;
494 public @NonNull String appName;
495 public @NonNull IIntegerConsumer callback;
496
497 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
498 @NonNull String appName, @NonNull IIntegerConsumer callback) {
499 this.capability = capability;
500 this.appName = appName;
501 this.callback = callback;
502 }
503 }
504
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700505 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
507 * request after sending. The main thread will notify the request when it is complete.
508 */
509 private static final class MainThreadRequest {
510 /** The argument to use for the request */
511 public Object argument;
512 /** The result of the request that is run on the main thread */
513 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800514 // The subscriber id that this request applies to. Defaults to
515 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
516 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517
Nathan Harold92bed182018-10-12 18:16:49 -0700518 // In cases where subId is unavailable, the caller needs to specify the phone.
519 public Phone phone;
520
vagdeviaf9a5b92018-08-15 16:01:53 -0700521 public WorkSource workSource;
522
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 public MainThreadRequest(Object argument) {
524 this.argument = argument;
525 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800526
Nathan Harold92bed182018-10-12 18:16:49 -0700527 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
528 this.argument = argument;
529 if (phone != null) {
530 this.phone = phone;
531 }
532 this.workSource = workSource;
533 }
534
vagdeviaf9a5b92018-08-15 16:01:53 -0700535 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800536 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800537 if (subId != null) {
538 this.subId = subId;
539 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700540 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 }
543
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800544 private static final class IncomingThirdPartyCallArgs {
545 public final ComponentName component;
546 public final String callId;
547 public final String callerDisplayName;
548
549 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
550 String callerDisplayName) {
551 this.component = component;
552 this.callId = callId;
553 this.callerDisplayName = callerDisplayName;
554 }
555 }
556
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557 /**
558 * A handler that processes messages on the main thread in the phone process. Since many
559 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
560 * inbound binder threads to the main thread in the phone process. The Binder thread
561 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
562 * on, which will be notified when the operation completes and will contain the result of the
563 * request.
564 *
565 * <p>If a MainThreadRequest object is provided in the msg.obj field,
566 * note that request.result must be set to something non-null for the calling thread to
567 * unblock.
568 */
569 private final class MainThreadHandler extends Handler {
570 @Override
571 public void handleMessage(Message msg) {
572 MainThreadRequest request;
573 Message onCompleted;
574 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000575 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700576 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800577 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578
579 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700580 case CMD_HANDLE_USSD_REQUEST: {
581 request = (MainThreadRequest) msg.obj;
582 final Phone phone = getPhoneFromRequest(request);
583 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800584 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700585 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700586
Pengquan Menga1bb6272018-09-06 09:59:22 -0700587 if (!isUssdApiAllowed(request.subId)) {
588 // Carrier does not support use of this API, return failure.
589 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
590 UssdResponse response = new UssdResponse(ussdRequest, null);
591 Bundle returnData = new Bundle();
592 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
593 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700594
Pengquan Menga1bb6272018-09-06 09:59:22 -0700595 request.result = true;
596 notifyRequester(request);
597 return;
598 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700599
Pengquan Menga1bb6272018-09-06 09:59:22 -0700600 try {
601 request.result = phone != null
602 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
603 } catch (CallStateException cse) {
604 request.result = false;
605 }
606 // Wake up the requesting thread
607 notifyRequester(request);
608 break;
pkanwar32d516d2016-10-14 19:37:38 -0700609 }
610
Yorke Lee716f67e2015-06-17 15:39:16 -0700611 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700613 final Phone phone = getPhoneFromRequest(request);
614 request.result = phone != null ?
615 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
616 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700618 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700620 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700621
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700622 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700623 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700624 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000625 uiccPort = getUiccPortFromRequest(request);
626 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700627 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800628 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700629 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700630 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700631 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800632 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000633 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800634 iccArgument.channel, iccArgument.cla, iccArgument.command,
635 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
636 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700637 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700638 break;
639
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700640 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700641 ar = (AsyncResult) msg.obj;
642 request = (MainThreadRequest) ar.userObj;
643 if (ar.exception == null && ar.result != null) {
644 request.result = ar.result;
645 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800646 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700647 if (ar.result == null) {
648 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800649 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700650 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800651 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700652 } else {
653 loge("iccTransmitApduLogicalChannel: Unknown exception");
654 }
655 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700656 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700657 break;
658
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700659 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
660 request = (MainThreadRequest) msg.obj;
661 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000662 uiccPort = getUiccPortFromRequest(request);
663 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700664 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800665 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700666 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700667 } else {
668 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800669 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000670 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800671 iccArgument.cla, iccArgument.command, iccArgument.p1,
672 iccArgument.p2,
673 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700674 }
675 break;
676
677 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
678 ar = (AsyncResult) msg.obj;
679 request = (MainThreadRequest) ar.userObj;
680 if (ar.exception == null && ar.result != null) {
681 request.result = ar.result;
682 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800683 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700684 if (ar.result == null) {
685 loge("iccTransmitApduBasicChannel: Empty response");
686 } else if (ar.exception instanceof CommandException) {
687 loge("iccTransmitApduBasicChannel: CommandException: " +
688 ar.exception);
689 } else {
690 loge("iccTransmitApduBasicChannel: Unknown exception");
691 }
692 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700693 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700694 break;
695
696 case CMD_EXCHANGE_SIM_IO:
697 request = (MainThreadRequest) msg.obj;
698 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000699 uiccPort = getUiccPortFromRequest(request);
700 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700701 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800702 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700703 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700704 } else {
705 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
706 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000707 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700708 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
709 iccArgument.data, onCompleted);
710 }
711 break;
712
713 case EVENT_EXCHANGE_SIM_IO_DONE:
714 ar = (AsyncResult) msg.obj;
715 request = (MainThreadRequest) ar.userObj;
716 if (ar.exception == null && ar.result != null) {
717 request.result = ar.result;
718 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800719 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700720 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700721 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700722 break;
723
Derek Tan4d5e5c12014-02-04 11:54:58 -0800724 case CMD_SEND_ENVELOPE:
725 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000726 uiccPort = getUiccPortFromRequest(request);
727 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700728 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800729 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700730 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700731 } else {
732 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800733 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700734 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800735 break;
736
737 case EVENT_SEND_ENVELOPE_DONE:
738 ar = (AsyncResult) msg.obj;
739 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700740 if (ar.exception == null && ar.result != null) {
741 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800742 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800743 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700744 if (ar.result == null) {
745 loge("sendEnvelopeWithStatus: Empty response");
746 } else if (ar.exception instanceof CommandException) {
747 loge("sendEnvelopeWithStatus: CommandException: " +
748 ar.exception);
749 } else {
750 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
751 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800752 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700753 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800754 break;
755
Shishir Agrawal566b7612013-10-28 14:41:00 -0700756 case CMD_OPEN_CHANNEL:
757 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000758 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800759 IccLogicalChannelRequest openChannelRequest =
760 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000761 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700762 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800763 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800764 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700765 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700766 } else {
767 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800768 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
769 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700770 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700771 break;
772
773 case EVENT_OPEN_CHANNEL_DONE:
774 ar = (AsyncResult) msg.obj;
775 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700776 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700777 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700778 int[] result = (int[]) ar.result;
779 int channelId = result[0];
780 byte[] selectResponse = null;
781 if (result.length > 1) {
782 selectResponse = new byte[result.length - 1];
783 for (int i = 1; i < result.length; ++i) {
784 selectResponse[i - 1] = (byte) result[i];
785 }
786 }
787 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800788 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800789
790 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700791 if (uiccPort == null) {
792 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
793 } else {
794 IccLogicalChannelRequest channelRequest =
795 (IccLogicalChannelRequest) request.argument;
796 channelRequest.channel = channelId;
797 uiccPort.onLogicalChannelOpened(channelRequest);
798 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700799 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700800 if (ar.result == null) {
801 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700802 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700803 if (ar.exception != null) {
804 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
805 }
806
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700807 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700808 if (ar.exception instanceof CommandException) {
809 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800810 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700811 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700812 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700813 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700814 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700815 }
816 }
817 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800818 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700819 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700820 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700821 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700822 break;
823
824 case CMD_CLOSE_CHANNEL:
825 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000826 uiccPort = getUiccPortFromRequest(request);
827 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700828 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800829 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800830 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800831 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700832 } else {
833 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000834 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700835 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700836 break;
837
838 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800839 ar = (AsyncResult) msg.obj;
840 request = (MainThreadRequest) ar.userObj;
841 if (ar.exception == null) {
842 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800843 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700844 if (uiccPort == null) {
845 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
846 } else {
847 final int channelId = (Integer) request.argument;
848 uiccPort.onLogicalChannelClosed(channelId);
849 }
Chen Xu540470b2021-12-14 17:15:47 -0800850 } else {
851 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800852 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800853 if (ar.exception instanceof CommandException) {
854 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
855 CommandException.Error error =
856 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800857 if (error == CommandException.Error.INVALID_ARGUMENTS) {
858 // should only throw exceptions from the binder threads.
859 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800860 "iccCloseLogicalChannel: invalid argument ");
861 }
862 } else {
863 loge("iccCloseLogicalChannel: Unknown exception");
864 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800865 request.result = (exception != null) ? exception :
866 new IllegalStateException(
867 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800868 }
869 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800870 break;
871
872 case CMD_NV_READ_ITEM:
873 request = (MainThreadRequest) msg.obj;
874 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800875 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
876 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800877 break;
878
879 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700880 ar = (AsyncResult) msg.obj;
881 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800882 if (ar.exception == null && ar.result != null) {
883 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700884 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800885 request.result = "";
886 if (ar.result == null) {
887 loge("nvReadItem: Empty response");
888 } else if (ar.exception instanceof CommandException) {
889 loge("nvReadItem: CommandException: " +
890 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700891 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800892 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700893 }
894 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700895 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700896 break;
897
Jake Hambye994d462014-02-03 13:10:13 -0800898 case CMD_NV_WRITE_ITEM:
899 request = (MainThreadRequest) msg.obj;
900 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
901 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800902 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700903 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800904 break;
905
906 case EVENT_NV_WRITE_ITEM_DONE:
907 handleNullReturnEvent(msg, "nvWriteItem");
908 break;
909
910 case CMD_NV_WRITE_CDMA_PRL:
911 request = (MainThreadRequest) msg.obj;
912 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800913 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800914 break;
915
916 case EVENT_NV_WRITE_CDMA_PRL_DONE:
917 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
918 break;
919
chen xu6dac5ab2018-10-26 17:39:23 -0700920 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800921 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700922 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800923 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800924 break;
925
chen xu6dac5ab2018-10-26 17:39:23 -0700926 case EVENT_RESET_MODEM_CONFIG_DONE:
927 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800928 break;
929
Sooraj Sasindran37444802020-08-11 10:40:43 -0700930 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
931 request = (MainThreadRequest) msg.obj;
932 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
933 request);
934 Phone phone = getPhoneFromRequest(request);
935 if (phone != null) {
936 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
937 } else {
938 loge("isNRDualConnectivityEnabled: No phone object");
939 request.result = false;
940 notifyRequester(request);
941 }
942 break;
943 }
944
945 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
946 ar = (AsyncResult) msg.obj;
947 request = (MainThreadRequest) ar.userObj;
948 if (ar.exception == null && ar.result != null) {
949 request.result = ar.result;
950 } else {
951 // request.result must be set to something non-null
952 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700953 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700954 request.result = ar.result;
955 } else {
956 request.result = false;
957 }
958 if (ar.result == null) {
959 loge("isNRDualConnectivityEnabled: Empty response");
960 } else if (ar.exception instanceof CommandException) {
961 loge("isNRDualConnectivityEnabled: CommandException: "
962 + ar.exception);
963 } else {
964 loge("isNRDualConnectivityEnabled: Unknown exception");
965 }
966 }
967 notifyRequester(request);
968 break;
969
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700970 case CMD_IS_VONR_ENABLED: {
971 request = (MainThreadRequest) msg.obj;
972 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
973 request);
974 Phone phone = getPhoneFromRequest(request);
975 if (phone != null) {
976 phone.isVoNrEnabled(onCompleted, request.workSource);
977 } else {
978 loge("isVoNrEnabled: No phone object");
979 request.result = false;
980 notifyRequester(request);
981 }
982 break;
983 }
984
985 case EVENT_IS_VONR_ENABLED_DONE:
986 ar = (AsyncResult) msg.obj;
987 request = (MainThreadRequest) ar.userObj;
988 if (ar.exception == null && ar.result != null) {
989 request.result = ar.result;
990 } else {
991 // request.result must be set to something non-null
992 // for the calling thread to unblock
993 if (ar.result != null) {
994 request.result = ar.result;
995 } else {
996 request.result = false;
997 }
998 if (ar.result == null) {
999 loge("isVoNrEnabled: Empty response");
1000 } else if (ar.exception instanceof CommandException) {
1001 loge("isVoNrEnabled: CommandException: "
1002 + ar.exception);
1003 } else {
1004 loge("isVoNrEnabled: Unknown exception");
1005 }
1006 }
1007 notifyRequester(request);
1008 break;
1009
Sooraj Sasindran37444802020-08-11 10:40:43 -07001010 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1011 request = (MainThreadRequest) msg.obj;
1012 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1013 Phone phone = getPhoneFromRequest(request);
1014 if (phone != null) {
1015 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1016 request.workSource);
1017 } else {
1018 loge("enableNrDualConnectivity: No phone object");
1019 request.result =
1020 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1021 notifyRequester(request);
1022 }
1023 break;
1024 }
1025
1026 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1027 ar = (AsyncResult) msg.obj;
1028 request = (MainThreadRequest) ar.userObj;
1029 if (ar.exception == null) {
1030 request.result =
1031 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1032 } else {
1033 request.result =
1034 TelephonyManager
1035 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1036 if (ar.exception instanceof CommandException) {
1037 CommandException.Error error =
1038 ((CommandException) (ar.exception)).getCommandError();
1039 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1040 request.result =
1041 TelephonyManager
1042 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001043 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1044 request.result =
1045 TelephonyManager
1046 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001047 }
1048 loge("enableNrDualConnectivity" + ": CommandException: "
1049 + ar.exception);
1050 } else {
1051 loge("enableNrDualConnectivity" + ": Unknown exception");
1052 }
1053 }
1054 notifyRequester(request);
1055 break;
1056 }
1057
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001058 case CMD_ENABLE_VONR: {
1059 request = (MainThreadRequest) msg.obj;
1060 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1061 Phone phone = getPhoneFromRequest(request);
1062 if (phone != null) {
1063 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1064 request.workSource);
1065 } else {
1066 loge("setVoNrEnabled: No phone object");
1067 request.result =
1068 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1069 notifyRequester(request);
1070 }
1071 break;
1072 }
1073
1074 case EVENT_ENABLE_VONR_DONE: {
1075 ar = (AsyncResult) msg.obj;
1076 request = (MainThreadRequest) ar.userObj;
1077 if (ar.exception == null) {
1078 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1079 } else {
1080 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1081 if (ar.exception instanceof CommandException) {
1082 CommandException.Error error =
1083 ((CommandException) (ar.exception)).getCommandError();
1084 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1085 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1086 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1087 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1088 } else {
1089 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1090 }
1091 loge("setVoNrEnabled" + ": CommandException: "
1092 + ar.exception);
1093 } else {
1094 loge("setVoNrEnabled" + ": Unknown exception");
1095 }
1096 }
1097 notifyRequester(request);
1098 break;
1099 }
1100
SongFerngWang3ef3e072020-12-21 16:41:52 +08001101 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001102 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001103 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1104 request);
1105 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001106 break;
1107
SongFerngWang3ef3e072020-12-21 16:41:52 +08001108 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001109 ar = (AsyncResult) msg.obj;
1110 request = (MainThreadRequest) ar.userObj;
1111 if (ar.exception == null && ar.result != null) {
1112 request.result = ar.result; // Integer
1113 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301114 // request.result must be set to something non-null
1115 // for the calling thread to unblock
1116 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001117 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001118 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001119 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001120 loge("getAllowedNetworkTypesBitmask: CommandException: "
1121 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001122 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001123 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001124 }
1125 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001126 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001127 break;
1128
SongFerngWang3ef3e072020-12-21 16:41:52 +08001129 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001130 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001131 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1132 request);
1133 Pair<Integer, Long> reasonWithNetworkTypes =
1134 (Pair<Integer, Long>) request.argument;
1135 getPhoneFromRequest(request).setAllowedNetworkTypes(
1136 reasonWithNetworkTypes.first,
1137 reasonWithNetworkTypes.second,
1138 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001139 break;
1140
SongFerngWang3ef3e072020-12-21 16:41:52 +08001141 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1142 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001143 break;
1144
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001145 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1146 request = (MainThreadRequest)msg.obj;
1147 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001148 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001149 break;
1150
1151 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1152 ar = (AsyncResult)msg.obj;
1153 request = (MainThreadRequest)ar.userObj;
1154 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001155 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001156 break;
1157
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001158 case CMD_SET_VOICEMAIL_NUMBER:
1159 request = (MainThreadRequest) msg.obj;
1160 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1161 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001162 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1163 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001164 break;
1165
1166 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1167 handleNullReturnEvent(msg, "setVoicemailNumber");
1168 break;
1169
Stuart Scott54788802015-03-30 13:18:01 -07001170 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1171 request = (MainThreadRequest) msg.obj;
1172 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1173 request);
1174 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1175 break;
1176
1177 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1178 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1179 break;
1180
Shishir Agrawal302c8692015-06-19 13:49:39 -07001181 case CMD_PERFORM_NETWORK_SCAN:
1182 request = (MainThreadRequest) msg.obj;
1183 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1184 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1185 break;
1186
Hall Liu27d24262020-09-18 19:04:59 -07001187 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001188 request = (MainThreadRequest) msg.obj;
1189 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001190 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1191 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1192 request.argument;
1193 int callForwardingReason = args.first;
1194 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001195 break;
Hall Liu27d24262020-09-18 19:04:59 -07001196 }
1197 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001198 ar = (AsyncResult) msg.obj;
1199 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001200 TelephonyManager.CallForwardingInfoCallback callback =
1201 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1202 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001203 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001204 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001205 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1206 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1207 // Service Class is a bit mask per 3gpp 27.007. Search for
1208 // any service for voice call.
1209 if ((callForwardInfo.serviceClass
1210 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001211 callForwardingInfo = new CallForwardingInfo(
1212 callForwardInfo.status
1213 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001214 callForwardInfo.reason,
1215 callForwardInfo.number,
1216 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001217 break;
1218 }
1219 }
1220 // Didn't find a call forward info for voice call.
1221 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001222 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1223 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001224 }
Hall Liu27d24262020-09-18 19:04:59 -07001225 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001226 } else {
1227 if (ar.result == null) {
1228 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1229 }
1230 if (ar.exception != null) {
1231 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1232 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001233 int errorCode = TelephonyManager
1234 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001235 if (ar.exception instanceof CommandException) {
1236 CommandException.Error error =
1237 ((CommandException) (ar.exception)).getCommandError();
1238 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001239 errorCode = TelephonyManager
1240 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001241 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001242 errorCode = TelephonyManager
1243 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001244 }
1245 }
Hall Liu27d24262020-09-18 19:04:59 -07001246 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001247 }
Shuo Qian4a594052020-01-23 11:59:30 -08001248 break;
Hall Liu27d24262020-09-18 19:04:59 -07001249 }
Shuo Qian4a594052020-01-23 11:59:30 -08001250
Hall Liu27d24262020-09-18 19:04:59 -07001251 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001252 request = (MainThreadRequest) msg.obj;
1253 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001254 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001255 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001256 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1257 request.argument).first;
1258 request.phone.setCallForwardingOption(
1259 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001260 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001261 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001262 callForwardingInfoToSet.getReason(),
1263 callForwardingInfoToSet.getNumber(),
1264 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1265 break;
Hall Liu27d24262020-09-18 19:04:59 -07001266 }
Shuo Qian4a594052020-01-23 11:59:30 -08001267
Hall Liu27d24262020-09-18 19:04:59 -07001268 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001269 ar = (AsyncResult) msg.obj;
1270 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001271 Consumer<Integer> callback =
1272 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1273 request.argument).second;
1274 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001275 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001276 int errorCode = TelephonyManager.CallForwardingInfoCallback
1277 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001278 if (ar.exception instanceof CommandException) {
1279 CommandException.Error error =
1280 ((CommandException) (ar.exception)).getCommandError();
1281 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001282 errorCode = TelephonyManager.CallForwardingInfoCallback
1283 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001284 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001285 errorCode = TelephonyManager.CallForwardingInfoCallback
1286 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001287 }
1288 }
1289 callback.accept(errorCode);
1290 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001291 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001292 }
Shuo Qian4a594052020-01-23 11:59:30 -08001293 break;
Hall Liu27d24262020-09-18 19:04:59 -07001294 }
Shuo Qian4a594052020-01-23 11:59:30 -08001295
Hall Liu27d24262020-09-18 19:04:59 -07001296 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001297 request = (MainThreadRequest) msg.obj;
1298 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1299 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1300 break;
Hall Liu27d24262020-09-18 19:04:59 -07001301 }
Shuo Qian4a594052020-01-23 11:59:30 -08001302
Hall Liu27d24262020-09-18 19:04:59 -07001303 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001304 ar = (AsyncResult) msg.obj;
1305 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001306 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001307 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001308 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001309 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001310 // Service Class is a bit mask per 3gpp 27.007.
1311 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001312 if (callForwardResults.length > 1
1313 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001314 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001315 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001316 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1317 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001318 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001319 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001320 }
1321 } else {
1322 if (ar.result == null) {
1323 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1324 }
1325 if (ar.exception != null) {
1326 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1327 }
1328 if (ar.exception instanceof CommandException) {
1329 CommandException.Error error =
1330 ((CommandException) (ar.exception)).getCommandError();
1331 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001332 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001333 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001334 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1335 callWaitingStatus =
1336 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001337 }
1338 }
1339 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001340 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001341 break;
Hall Liu27d24262020-09-18 19:04:59 -07001342 }
Shuo Qian4a594052020-01-23 11:59:30 -08001343
Hall Liu27d24262020-09-18 19:04:59 -07001344 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001345 request = (MainThreadRequest) msg.obj;
1346 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001347 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1348 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001349 break;
Hall Liu27d24262020-09-18 19:04:59 -07001350 }
Shuo Qian4a594052020-01-23 11:59:30 -08001351
Hall Liu27d24262020-09-18 19:04:59 -07001352 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001353 ar = (AsyncResult) msg.obj;
1354 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001355 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1356 Consumer<Integer> callback =
1357 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1358 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001359 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001360 if (ar.exception instanceof CommandException) {
1361 CommandException.Error error =
1362 ((CommandException) (ar.exception)).getCommandError();
1363 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1364 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001365 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1366 callback.accept(
1367 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001368 } else {
1369 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1370 }
1371 } else {
1372 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1373 }
1374 } else {
1375 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1376 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001377 }
Shuo Qian4a594052020-01-23 11:59:30 -08001378 break;
Hall Liu27d24262020-09-18 19:04:59 -07001379 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001380 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1381 ar = (AsyncResult) msg.obj;
1382 request = (MainThreadRequest) ar.userObj;
1383 CellNetworkScanResult cellScanResult;
1384 if (ar.exception == null && ar.result != null) {
1385 cellScanResult = new CellNetworkScanResult(
1386 CellNetworkScanResult.STATUS_SUCCESS,
1387 (List<OperatorInfo>) ar.result);
1388 } else {
1389 if (ar.result == null) {
1390 loge("getCellNetworkScanResults: Empty response");
1391 }
1392 if (ar.exception != null) {
1393 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1394 }
1395 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1396 if (ar.exception instanceof CommandException) {
1397 CommandException.Error error =
1398 ((CommandException) (ar.exception)).getCommandError();
1399 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1400 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1401 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1402 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1403 }
1404 }
1405 cellScanResult = new CellNetworkScanResult(errorCode, null);
1406 }
1407 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001408 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001409 break;
1410
1411 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1412 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001413 ManualNetworkSelectionArgument selArg =
1414 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001415 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1416 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001417 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1418 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001419 break;
1420
1421 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001422 ar = (AsyncResult) msg.obj;
1423 request = (MainThreadRequest) ar.userObj;
1424 if (ar.exception == null) {
1425 request.result = true;
1426 } else {
1427 request.result = false;
1428 loge("setNetworkSelectionModeManual " + ar.exception);
1429 }
1430 notifyRequester(request);
1431 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001432 break;
1433
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001434 case CMD_GET_MODEM_ACTIVITY_INFO:
1435 request = (MainThreadRequest) msg.obj;
1436 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001437 if (defaultPhone != null) {
1438 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001439 } else {
1440 ResultReceiver result = (ResultReceiver) request.argument;
1441 Bundle bundle = new Bundle();
1442 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001443 new ModemActivityInfo(0, 0, 0,
1444 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001445 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001446 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001447 break;
1448
Hall Liud0f208c2020-10-14 16:54:44 -07001449 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001450 ar = (AsyncResult) msg.obj;
1451 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001452 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001453 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001454 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001455 if (mLastModemActivityInfo == null) {
1456 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1457 mLastModemActivitySpecificInfo[0] =
1458 new ActivityStatsTechSpecificInfo(
1459 0,
1460 0,
1461 new int[ModemActivityInfo.getNumTxPowerLevels()],
1462 0);
1463 mLastModemActivityInfo =
1464 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1465 }
1466
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001467 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001468 // Update the last modem activity info and the result of the request.
1469 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1470 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001471 mergeModemActivityInfo(info);
Gary Jian76280a42022-12-07 16:18:33 +08001472 } else {
1473 loge("queryModemActivityInfo: invalid response");
Shuo Qian8f4750a2020-02-20 17:12:10 -08001474 }
Kai Shi917fdc62022-11-28 14:01:02 -08001475 // This is needed to decouple ret from mLastModemActivityInfo
1476 // We don't want to return mLastModemActivityInfo which is updated
1477 // inside mergeModemActivityInfo()
1478 ret = new ModemActivityInfo(
1479 mLastModemActivityInfo.getTimestampMillis(),
1480 mLastModemActivityInfo.getSleepTimeMillis(),
1481 mLastModemActivityInfo.getIdleTimeMillis(),
1482 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001483
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001484 } else {
1485 if (ar.result == null) {
1486 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001487 error = TelephonyManager.ModemActivityInfoException
1488 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001489 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001490 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001491 error = TelephonyManager.ModemActivityInfoException
1492 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001493 } else {
1494 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001495 error = TelephonyManager.ModemActivityInfoException
1496 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001497 }
1498 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001499 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001500 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001501 bundle.putParcelable(
1502 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001503 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001504 } else {
1505 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1506 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001507 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001508 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001509 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001510 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001511
Meng Wang1a7c35a2016-05-05 20:56:15 -07001512 case CMD_SET_ALLOWED_CARRIERS:
1513 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001514 CarrierRestrictionRules argument =
1515 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001516 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001517 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001518 break;
1519
1520 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1521 ar = (AsyncResult) msg.obj;
1522 request = (MainThreadRequest) ar.userObj;
1523 if (ar.exception == null && ar.result != null) {
1524 request.result = ar.result;
1525 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001526 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1527 if (ar.exception instanceof CommandException) {
1528 loge("setAllowedCarriers: CommandException: " + ar.exception);
1529 CommandException.Error error =
1530 ((CommandException) (ar.exception)).getCommandError();
1531 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1532 request.result =
1533 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1534 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001535 } else {
1536 loge("setAllowedCarriers: Unknown exception");
1537 }
1538 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001539 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001540 break;
1541
1542 case CMD_GET_ALLOWED_CARRIERS:
1543 request = (MainThreadRequest) msg.obj;
1544 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001545 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001546 break;
1547
1548 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1549 ar = (AsyncResult) msg.obj;
1550 request = (MainThreadRequest) ar.userObj;
1551 if (ar.exception == null && ar.result != null) {
1552 request.result = ar.result;
1553 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001554 request.result = new IllegalStateException(
1555 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001556 if (ar.result == null) {
1557 loge("getAllowedCarriers: Empty response");
1558 } else if (ar.exception instanceof CommandException) {
1559 loge("getAllowedCarriers: CommandException: " +
1560 ar.exception);
1561 } else {
1562 loge("getAllowedCarriers: Unknown exception");
1563 }
1564 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001565 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001566 break;
1567
Nathan Haroldb3014052017-01-25 15:57:32 -08001568 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1569 ar = (AsyncResult) msg.obj;
1570 request = (MainThreadRequest) ar.userObj;
1571 if (ar.exception == null && ar.result != null) {
1572 request.result = ar.result;
1573 } else {
1574 request.result = new IllegalArgumentException(
1575 "Failed to retrieve Forbidden Plmns");
1576 if (ar.result == null) {
1577 loge("getForbiddenPlmns: Empty response");
1578 } else {
1579 loge("getForbiddenPlmns: Unknown exception");
1580 }
1581 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001582 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001583 break;
1584
1585 case CMD_GET_FORBIDDEN_PLMNS:
1586 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001587 uiccPort = getUiccPortFromRequest(request);
1588 if (uiccPort == null) {
1589 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001590 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001591 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001592 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001593 break;
1594 }
1595 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001596 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001597 if (uiccApp == null) {
1598 loge("getForbiddenPlmns() no app with specified type -- "
1599 + appType);
1600 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001601 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001602 break;
1603 } else {
1604 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1605 + " specified type -- " + appType);
1606 }
1607 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1608 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1609 onCompleted);
1610 break;
1611
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001612 case CMD_SWITCH_SLOTS:
1613 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001614 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001615 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001616 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001617 break;
1618
1619 case EVENT_SWITCH_SLOTS_DONE:
1620 ar = (AsyncResult) msg.obj;
1621 request = (MainThreadRequest) ar.userObj;
1622 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001623 notifyRequester(request);
1624 break;
1625 case CMD_GET_NETWORK_SELECTION_MODE:
1626 request = (MainThreadRequest) msg.obj;
1627 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1628 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1629 break;
1630
1631 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1632 ar = (AsyncResult) msg.obj;
1633 request = (MainThreadRequest) ar.userObj;
1634 if (ar.exception != null) {
1635 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1636 } else {
1637 int mode = ((int[]) ar.result)[0];
1638 if (mode == 0) {
1639 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1640 } else {
1641 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1642 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001643 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001644 notifyRequester(request);
1645 break;
1646 case CMD_GET_CDMA_ROAMING_MODE:
1647 request = (MainThreadRequest) msg.obj;
1648 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1649 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1650 break;
1651 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1652 ar = (AsyncResult) msg.obj;
1653 request = (MainThreadRequest) ar.userObj;
1654 if (ar.exception != null) {
1655 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1656 } else {
1657 request.result = ((int[]) ar.result)[0];
1658 }
1659 notifyRequester(request);
1660 break;
1661 case CMD_SET_CDMA_ROAMING_MODE:
1662 request = (MainThreadRequest) msg.obj;
1663 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1664 int mode = (int) request.argument;
1665 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1666 break;
1667 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1668 ar = (AsyncResult) msg.obj;
1669 request = (MainThreadRequest) ar.userObj;
1670 request.result = ar.exception == null;
1671 notifyRequester(request);
1672 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001673 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1674 request = (MainThreadRequest) msg.obj;
1675 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1676 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1677 break;
1678 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1679 ar = (AsyncResult) msg.obj;
1680 request = (MainThreadRequest) ar.userObj;
1681 if (ar.exception != null) {
1682 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1683 } else {
1684 request.result = ((int[]) ar.result)[0];
1685 }
1686 notifyRequester(request);
1687 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001688 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1689 request = (MainThreadRequest) msg.obj;
1690 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1691 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001692 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1693 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001694 break;
1695 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1696 ar = (AsyncResult) msg.obj;
1697 request = (MainThreadRequest) ar.userObj;
1698 request.result = ar.exception == null;
1699 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001700 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001701 case CMD_GET_ALL_CELL_INFO:
1702 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001703 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001704 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001705 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001706 case EVENT_GET_ALL_CELL_INFO_DONE:
1707 ar = (AsyncResult) msg.obj;
1708 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001709 // If a timeout occurs, the response will be null
1710 request.result = (ar.exception == null && ar.result != null)
1711 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001712 synchronized (request) {
1713 request.notifyAll();
1714 }
1715 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001716 case CMD_REQUEST_CELL_INFO_UPDATE:
1717 request = (MainThreadRequest) msg.obj;
1718 request.phone.requestCellInfoUpdate(request.workSource,
1719 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1720 break;
1721 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1722 ar = (AsyncResult) msg.obj;
1723 request = (MainThreadRequest) ar.userObj;
1724 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1725 try {
1726 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001727 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001728 cb.onError(
1729 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1730 ar.exception.getClass().getName(),
1731 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001732 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001733 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001734 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001735 } else {
1736 // use the result as returned
1737 cb.onCellInfo((List<CellInfo>) ar.result);
1738 }
1739 } catch (RemoteException re) {
1740 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1741 }
1742 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001743 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001744 request = (MainThreadRequest) msg.obj;
1745 WorkSource ws = (WorkSource) request.argument;
1746 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001747 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001748 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001749 }
1750 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001751 ar = (AsyncResult) msg.obj;
1752 request = (MainThreadRequest) ar.userObj;
1753 if (ar.exception == null) {
1754 request.result = ar.result;
1755 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001756 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001757 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001758 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001759 }
1760
1761 synchronized (request) {
1762 request.notifyAll();
1763 }
1764 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001765 }
chen xu6dac5ab2018-10-26 17:39:23 -07001766 case CMD_MODEM_REBOOT:
1767 request = (MainThreadRequest) msg.obj;
1768 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001769 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001770 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001771 case EVENT_CMD_MODEM_REBOOT_DONE:
1772 handleNullReturnEvent(msg, "rebootModem");
1773 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001774 case CMD_REQUEST_ENABLE_MODEM:
1775 request = (MainThreadRequest) msg.obj;
1776 boolean enable = (boolean) request.argument;
1777 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001778 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001779 PhoneConfigurationManager.getInstance()
1780 .enablePhone(request.phone, enable, onCompleted);
1781 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001782 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001783 ar = (AsyncResult) msg.obj;
1784 request = (MainThreadRequest) ar.userObj;
1785 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001786 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001787 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001788 if ((boolean) request.result) {
1789 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1790 updateModemStateMetrics();
1791 } else {
1792 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1793 + ar.exception);
1794 }
1795 notifyRequester(request);
1796 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001797 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001798 case CMD_GET_MODEM_STATUS:
1799 request = (MainThreadRequest) msg.obj;
1800 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1801 PhoneConfigurationManager.getInstance()
1802 .getPhoneStatusFromModem(request.phone, onCompleted);
1803 break;
1804 case EVENT_GET_MODEM_STATUS_DONE:
1805 ar = (AsyncResult) msg.obj;
1806 request = (MainThreadRequest) ar.userObj;
1807 int id = request.phone.getPhoneId();
1808 if (ar.exception == null && ar.result != null) {
1809 request.result = ar.result;
1810 //update the cache as modem status has changed
1811 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1812 (boolean) request.result);
1813 } else {
1814 // Return true if modem status cannot be retrieved. For most cases,
1815 // modem status is on. And for older version modems, GET_MODEM_STATUS
1816 // and disable modem are not supported. Modem is always on.
1817 // TODO: this should be fixed in R to support a third
1818 // status UNKNOWN b/131631629
1819 request.result = true;
1820 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1821 + ar.exception);
1822 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001823 notifyRequester(request);
1824 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001825 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1826 request = (MainThreadRequest) msg.obj;
1827 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1828 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1829 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1830 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1831 break;
1832 }
1833 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1834 ar = (AsyncResult) msg.obj;
1835 request = (MainThreadRequest) ar.userObj;
1836 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1837 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1838 args.second.accept(ar.exception == null);
1839 notifyRequester(request);
1840 break;
1841 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001842 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1843 request = (MainThreadRequest) msg.obj;
1844 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1845 Phone phone = getPhoneFromRequest(request);
1846 if (phone != null) {
1847 phone.getSystemSelectionChannels(onCompleted);
1848 } else {
1849 loge("getSystemSelectionChannels: No phone object");
1850 request.result = new ArrayList<RadioAccessSpecifier>();
1851 notifyRequester(request);
1852 }
1853 break;
1854 }
1855 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1856 ar = (AsyncResult) msg.obj;
1857 request = (MainThreadRequest) ar.userObj;
1858 if (ar.exception == null && ar.result != null) {
1859 request.result = ar.result;
1860 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001861 request.result = new IllegalStateException(
1862 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001863 if (ar.result == null) {
1864 loge("getSystemSelectionChannels: Empty response");
1865 } else {
1866 loge("getSystemSelectionChannels: Unknown exception");
1867 }
1868 }
1869 notifyRequester(request);
1870 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001871 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1872 ar = (AsyncResult) msg.obj;
1873 request = (MainThreadRequest) ar.userObj;
1874 if (ar.exception == null && ar.result != null) {
1875 request.result = ar.result;
1876 } else {
1877 request.result = -1;
1878 loge("Failed to set Forbidden Plmns");
1879 if (ar.result == null) {
1880 loge("setForbidenPlmns: Empty response");
1881 } else if (ar.exception != null) {
1882 loge("setForbiddenPlmns: Exception: " + ar.exception);
1883 request.result = -1;
1884 } else {
1885 loge("setForbiddenPlmns: Unknown exception");
1886 }
1887 }
1888 notifyRequester(request);
1889 break;
1890 case CMD_SET_FORBIDDEN_PLMNS:
1891 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001892 uiccPort = getUiccPortFromRequest(request);
1893 if (uiccPort == null) {
1894 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001895 request.result = -1;
1896 notifyRequester(request);
1897 break;
1898 }
1899 Pair<Integer, List<String>> setFplmnsArgs =
1900 (Pair<Integer, List<String>>) request.argument;
1901 appType = setFplmnsArgs.first;
1902 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001903 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001904 if (uiccApp == null) {
1905 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1906 request.result = -1;
1907 loge("Failed to get UICC App");
1908 notifyRequester(request);
1909 } else {
1910 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1911 ((SIMRecords) uiccApp.getIccRecords())
1912 .setForbiddenPlmns(onCompleted, fplmns);
1913 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001914 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001915 case CMD_ERASE_MODEM_CONFIG:
1916 request = (MainThreadRequest) msg.obj;
1917 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1918 defaultPhone.eraseModemConfig(onCompleted);
1919 break;
1920 case EVENT_ERASE_MODEM_CONFIG_DONE:
1921 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001922 break;
zoey chene02881a2019-12-30 16:11:23 +08001923
Kai Shif70f46f2021-03-03 13:59:46 -08001924 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1925 request = (MainThreadRequest) msg.obj;
1926 request.result = defaultPhone.eraseDataInSharedPreferences();
1927 notifyRequester(request);
1928 break;
1929
zoey chene02881a2019-12-30 16:11:23 +08001930 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1931 request = (MainThreadRequest) msg.obj;
1932 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1933 Pair<String, String> changed = (Pair<String, String>) request.argument;
1934 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1935 changed.first, changed.second, onCompleted);
1936 break;
1937 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1938 ar = (AsyncResult) msg.obj;
1939 request = (MainThreadRequest) ar.userObj;
1940 if (ar.exception == null) {
1941 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001942 // If the operation is successful, update the PIN storage
1943 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1944 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001945 UiccController.getInstance().getPinStorage()
1946 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001947 } else {
1948 request.result = msg.arg1;
1949 }
1950 notifyRequester(request);
1951 break;
1952
Michele Berionne5e411512020-11-13 02:36:59 +00001953 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001954 request = (MainThreadRequest) msg.obj;
1955 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1956 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1957 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1958 enabled.first, enabled.second, onCompleted);
1959 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001960 }
zoey chene02881a2019-12-30 16:11:23 +08001961 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1962 ar = (AsyncResult) msg.obj;
1963 request = (MainThreadRequest) ar.userObj;
1964 if (ar.exception == null) {
1965 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001966 // If the operation is successful, update the PIN storage
1967 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1968 int phoneId = getPhoneFromRequest(request).getPhoneId();
1969 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001970 UiccController.getInstance().getPinStorage()
1971 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001972 } else {
1973 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1974 }
zoey chene02881a2019-12-30 16:11:23 +08001975 } else {
1976 request.result = msg.arg1;
1977 }
Michele Berionne5e411512020-11-13 02:36:59 +00001978
1979
zoey chene02881a2019-12-30 16:11:23 +08001980 notifyRequester(request);
1981 break;
1982
Peter Wangdafb9ac2020-01-15 14:13:38 -08001983 case MSG_NOTIFY_USER_ACTIVITY:
1984 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001985 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001986 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1987 getDefaultPhone().getContext().sendBroadcastAsUser(
1988 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1989 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001990
1991 case CMD_SET_DATA_THROTTLING: {
1992 request = (MainThreadRequest) msg.obj;
1993 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1994 DataThrottlingRequest dataThrottlingRequest =
1995 (DataThrottlingRequest) request.argument;
1996 Phone phone = getPhoneFromRequest(request);
1997 if (phone != null) {
1998 phone.setDataThrottling(onCompleted,
1999 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
2000 dataThrottlingRequest.getCompletionDurationMillis());
2001 } else {
2002 loge("setDataThrottling: No phone object");
2003 request.result =
2004 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2005 notifyRequester(request);
2006 }
2007
2008 break;
2009 }
2010 case EVENT_SET_DATA_THROTTLING_DONE:
2011 ar = (AsyncResult) msg.obj;
2012 request = (MainThreadRequest) ar.userObj;
2013
2014 if (ar.exception == null) {
2015 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2016 } else if (ar.exception instanceof CommandException) {
2017 loge("setDataThrottling: CommandException: " + ar.exception);
2018 CommandException.Error error =
2019 ((CommandException) (ar.exception)).getCommandError();
2020
2021 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2022 request.result = TelephonyManager
2023 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2024 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2025 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002026 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2027 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002028 } else {
2029 request.result =
2030 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2031 }
2032 } else {
2033 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2034 }
2035 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2036 notifyRequester(request);
2037 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002038
2039 case CMD_SET_SIM_POWER: {
2040 request = (MainThreadRequest) msg.obj;
2041 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2042 request = (MainThreadRequest) msg.obj;
2043 int stateToSet =
2044 ((Pair<Integer, IIntegerConsumer>)
2045 request.argument).first;
2046 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2047 break;
2048 }
2049 case EVENT_SET_SIM_POWER_DONE: {
2050 ar = (AsyncResult) msg.obj;
2051 request = (MainThreadRequest) ar.userObj;
2052 IIntegerConsumer callback =
2053 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2054 if (ar.exception != null) {
2055 loge("setSimPower exception: " + ar.exception);
2056 int errorCode = TelephonyManager.CallForwardingInfoCallback
2057 .RESULT_ERROR_UNKNOWN;
2058 if (ar.exception instanceof CommandException) {
2059 CommandException.Error error =
2060 ((CommandException) (ar.exception)).getCommandError();
2061 if (error == CommandException.Error.SIM_ERR) {
2062 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2063 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2064 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2065 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2066 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2067 } else {
2068 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2069 }
2070 }
2071 try {
2072 callback.accept(errorCode);
2073 } catch (RemoteException e) {
2074 // Ignore if the remote process is no longer available to call back.
2075 Log.w(LOG_TAG, "setSimPower: callback not available.");
2076 }
2077 } else {
2078 try {
2079 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2080 } catch (RemoteException e) {
2081 // Ignore if the remote process is no longer available to call back.
2082 Log.w(LOG_TAG, "setSimPower: callback not available.");
2083 }
2084 }
2085 break;
2086 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002087 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2088 request = (MainThreadRequest) msg.obj;
2089
2090 final Phone phone = getPhoneFromRequest(request);
2091 if (phone == null || phone.getServiceStateTracker() == null) {
2092 request.result = new IllegalStateException("Phone or SST is null");
2093 notifyRequester(request);
2094 break;
2095 }
2096
2097 Pair<Integer, SignalStrengthUpdateRequest> pair =
2098 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2099 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2100 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002101 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002102 request.subId, pair.first /*callingUid*/,
2103 pair.second /*request*/, onCompleted);
2104 break;
2105 }
2106 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2107 ar = (AsyncResult) msg.obj;
2108 request = (MainThreadRequest) ar.userObj;
2109 // request.result will be the exception of ar if present, true otherwise.
2110 // Be cautious not to leave result null which will wait() forever
2111 request.result = ar.exception != null ? ar.exception : true;
2112 notifyRequester(request);
2113 break;
2114 }
2115 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2116 request = (MainThreadRequest) msg.obj;
2117
2118 Phone phone = getPhoneFromRequest(request);
2119 if (phone == null || phone.getServiceStateTracker() == null) {
2120 request.result = new IllegalStateException("Phone or SST is null");
2121 notifyRequester(request);
2122 break;
2123 }
2124
2125 Pair<Integer, SignalStrengthUpdateRequest> pair =
2126 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2127 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2128 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002129 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002130 request.subId, pair.first /*callingUid*/,
2131 pair.second /*request*/, onCompleted);
2132 break;
2133 }
2134 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2135 ar = (AsyncResult) msg.obj;
2136 request = (MainThreadRequest) ar.userObj;
2137 request.result = ar.exception != null ? ar.exception : true;
2138 notifyRequester(request);
2139 break;
2140 }
Jordan Liu109698e2020-11-24 14:50:34 -08002141
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002142 case CMD_GET_SLICING_CONFIG: {
2143 request = (MainThreadRequest) msg.obj;
2144 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2145 request.phone.getSlicingConfig(onCompleted);
2146 break;
2147 }
2148 case EVENT_GET_SLICING_CONFIG_DONE: {
2149 ar = (AsyncResult) msg.obj;
2150 request = (MainThreadRequest) ar.userObj;
2151 ResultReceiver result = (ResultReceiver) request.argument;
2152
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002153 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002154 Bundle bundle = new Bundle();
2155 int resultCode = 0;
2156 if (ar.exception != null) {
2157 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2158 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002159 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002160 } else if (ar.result == null) {
2161 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002162 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002163 } else {
2164 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002165 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2166 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002167 }
2168
2169 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002170 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002171 }
2172 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2173 result.send(resultCode, bundle);
2174 notifyRequester(request);
2175 break;
2176 }
2177
Sarah Chin71b3a852022-09-28 15:54:19 -07002178 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002179 request = (MainThreadRequest) msg.obj;
2180 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002181 PurchasePremiumCapabilityArgument arg =
2182 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002183 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chin71b3a852022-09-28 15:54:19 -07002184 arg.capability, arg.appName, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002185 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002186 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002187
Sarah Chin71b3a852022-09-28 15:54:19 -07002188 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002189 ar = (AsyncResult) msg.obj;
2190 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002191 PurchasePremiumCapabilityArgument arg =
2192 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002193 try {
2194 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002195 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002196 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002197 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002198 + ", result= "
2199 + TelephonyManager.convertPurchaseResultToString(result));
2200 } catch (RemoteException e) {
2201 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002202 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002203 + " failed: " + e;
2204 if (DBG) log(logStr);
2205 AnomalyReporter.reportAnomaly(
2206 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2207 }
2208 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002209 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002210
Michele Berionne5e411512020-11-13 02:36:59 +00002211 case CMD_PREPARE_UNATTENDED_REBOOT:
2212 request = (MainThreadRequest) msg.obj;
2213 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002214 UiccController.getInstance().getPinStorage()
2215 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002216 notifyRequester(request);
2217 break;
2218
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002219 default:
2220 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2221 break;
2222 }
2223 }
Jake Hambye994d462014-02-03 13:10:13 -08002224
Pengquan Menga1bb6272018-09-06 09:59:22 -07002225 private void notifyRequester(MainThreadRequest request) {
2226 synchronized (request) {
2227 request.notifyAll();
2228 }
2229 }
2230
Jake Hambye994d462014-02-03 13:10:13 -08002231 private void handleNullReturnEvent(Message msg, String command) {
2232 AsyncResult ar = (AsyncResult) msg.obj;
2233 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2234 if (ar.exception == null) {
2235 request.result = true;
2236 } else {
2237 request.result = false;
2238 if (ar.exception instanceof CommandException) {
2239 loge(command + ": CommandException: " + ar.exception);
2240 } else {
2241 loge(command + ": Unknown exception");
2242 }
2243 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002244 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002245 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002246 }
2247
2248 /**
2249 * Posts the specified command to be executed on the main thread,
2250 * waits for the request to complete, and returns the result.
2251 * @see #sendRequestAsync
2252 */
2253 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002254 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2255 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002256 }
2257
2258 /**
2259 * Posts the specified command to be executed on the main thread,
2260 * waits for the request to complete, and returns the result.
2261 * @see #sendRequestAsync
2262 */
2263 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2264 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002265 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002266 }
2267
2268 /**
2269 * Posts the specified command to be executed on the main thread,
2270 * waits for the request to complete, and returns the result.
2271 * @see #sendRequestAsync
2272 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002273 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002274 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2275 }
2276
2277 /**
2278 * Posts the specified command to be executed on the main thread,
2279 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2280 * if not timeout or null otherwise.
2281 * @see #sendRequestAsync
2282 */
2283 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2284 long timeoutInMs) {
2285 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002286 }
2287
2288 /**
2289 * Posts the specified command to be executed on the main thread,
2290 * waits for the request to complete, and returns the result.
2291 * @see #sendRequestAsync
2292 */
Nathan Harold92bed182018-10-12 18:16:49 -07002293 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002294 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002295 }
2296
2297 /**
2298 * Posts the specified command to be executed on the main thread,
2299 * waits for the request to complete, and returns the result.
2300 * @see #sendRequestAsync
2301 */
2302 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002303 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2304 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002305 }
2306
2307 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002308 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2309 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2310 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002311 * @see #sendRequestAsync
2312 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002313 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2314 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002315 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2316 throw new RuntimeException("This method will deadlock if called from the main thread.");
2317 }
2318
Nathan Harold92bed182018-10-12 18:16:49 -07002319 MainThreadRequest request = null;
2320 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2321 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2322 } else if (phone != null) {
2323 request = new MainThreadRequest(argument, phone, workSource);
2324 } else {
2325 request = new MainThreadRequest(argument, subId, workSource);
2326 }
2327
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002328 Message msg = mMainThreadHandler.obtainMessage(command, request);
2329 msg.sendToTarget();
2330
Rambo Wang0f050d82021-02-12 11:43:36 -08002331
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002333 if (timeoutInMs >= 0) {
2334 // Wait for at least timeoutInMs before returning null request result
2335 long now = SystemClock.elapsedRealtime();
2336 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002337 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002338 try {
2339 request.wait(deadline - now);
2340 } catch (InterruptedException e) {
2341 // Do nothing, go back and check if request is completed or timeout
2342 } finally {
2343 now = SystemClock.elapsedRealtime();
2344 }
2345 }
2346 } else {
2347 // Wait for the request to complete
2348 while (request.result == null) {
2349 try {
2350 request.wait();
2351 } catch (InterruptedException e) {
2352 // Do nothing, go back and wait until the request is complete
2353 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002354 }
2355 }
2356 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002357 if (request.result == null) {
2358 Log.wtf(LOG_TAG,
2359 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2360 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002361 return request.result;
2362 }
2363
2364 /**
2365 * Asynchronous ("fire and forget") version of sendRequest():
2366 * Posts the specified command to be executed on the main thread, and
2367 * returns immediately.
2368 * @see #sendRequest
2369 */
2370 private void sendRequestAsync(int command) {
2371 mMainThreadHandler.sendEmptyMessage(command);
2372 }
2373
2374 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002375 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002376 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002377 */
2378 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002379 sendRequestAsync(command, argument, null, null);
2380 }
2381
2382 /**
2383 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2384 * @see {@link #sendRequest(int,Object)}
2385 */
2386 private void sendRequestAsync(
2387 int command, Object argument, Phone phone, WorkSource workSource) {
2388 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002389 Message msg = mMainThreadHandler.obtainMessage(command, request);
2390 msg.sendToTarget();
2391 }
2392
2393 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002394 * Initialize the singleton PhoneInterfaceManager instance.
2395 * This is only done once, at startup, from PhoneApp.onCreate().
2396 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002397 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002398 synchronized (PhoneInterfaceManager.class) {
2399 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002400 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002401 } else {
2402 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2403 }
2404 return sInstance;
2405 }
2406 }
2407
2408 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002409 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002410 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002411 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002412 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002413 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002414 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002415 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002416 mMainThreadHandler = new MainThreadHandler();
Jack Yue37dd262022-12-16 11:53:37 -08002417 if (!PhoneFactory.isSubscriptionManagerServiceEnabled()) {
2418 mSubscriptionController = SubscriptionController.getInstance();
2419 } else {
2420 mSubscriptionController = null;
2421 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002422 mTelephonySharedPreferences =
2423 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002424 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002425 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002426 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002427 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002428 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002429 mTelephony2gUpdater = new Telephony2gUpdater(
2430 Executors.newSingleThreadExecutor(), mApp);
2431 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002432 publish();
2433 }
2434
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002435 @VisibleForTesting
2436 public SharedPreferences getSharedPreferences() {
2437 return mTelephonySharedPreferences;
2438 }
2439
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002440 /**
2441 * Get the default phone for this device.
2442 */
2443 @VisibleForTesting
2444 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002445 Phone thePhone = getPhone(getDefaultSubscription());
2446 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2447 }
2448
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002449 private void publish() {
2450 if (DBG) log("publish: " + this);
2451
Peter Wangc035ce42020-01-08 21:00:22 -08002452 TelephonyFrameworkInitializer
2453 .getTelephonyServiceManager()
2454 .getTelephonyServiceRegisterer()
2455 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002456 }
2457
Stuart Scott584921c2015-01-15 17:10:34 -08002458 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002459 if (request.phone != null) {
2460 return request.phone;
2461 } else {
2462 return getPhoneFromSubId(request.subId);
2463 }
2464 }
2465
2466 private Phone getPhoneFromSubId(int subId) {
2467 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2468 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002469 }
2470
Rambo Wange53e07d2022-05-10 13:01:13 -07002471 @Nullable
2472 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002473 Phone phone = getPhoneFromRequest(request);
2474 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002475 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002476 }
2477
Wink Saville36469e72014-06-11 15:17:00 -07002478 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002479 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002480 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002481 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002482
Kai Shif70f46f2021-03-03 13:59:46 -08002483 private void sendEraseModemConfig(@NonNull Phone phone) {
2484 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2485 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2486 }
2487
2488 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2489 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2490 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002491 }
2492
Peter Wang44b186e2020-01-13 23:33:09 -08002493 private boolean isImsAvailableOnDevice() {
2494 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2495 if (pm == null) {
2496 // For some reason package manger is not available.. This will fail internally anyway,
2497 // so do not throw error and allow.
2498 return true;
2499 }
2500 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2501 }
2502
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002503 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002504 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002505 }
2506
Wink Savilleb564aae2014-10-23 10:18:09 -07002507 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002508 if (DBG) log("dial: " + number);
2509 // No permission check needed here: This is just a wrapper around the
2510 // ACTION_DIAL intent, which is available to any app since it puts up
2511 // the UI before it does anything.
2512
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002513 final long identity = Binder.clearCallingIdentity();
2514 try {
2515 String url = createTelUrl(number);
2516 if (url == null) {
2517 return;
2518 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002519
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002520 // PENDING: should we just silently fail if phone is offhook or ringing?
2521 PhoneConstants.State state = mCM.getState(subId);
2522 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2523 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2524 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2525 mApp.startActivity(intent);
2526 }
2527 } finally {
2528 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002529 }
2530 }
2531
2532 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002533 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002534 }
2535
Wink Savilleb564aae2014-10-23 10:18:09 -07002536 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002537 if (DBG) log("call: " + number);
2538
2539 // This is just a wrapper around the ACTION_CALL intent, but we still
2540 // need to do a permission check since we're calling startActivity()
2541 // from the context of the phone app.
2542 enforceCallPermission();
2543
Jordan Liu1617b712019-07-10 15:06:26 -07002544 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002545 != AppOpsManager.MODE_ALLOWED) {
2546 return;
2547 }
2548
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002549 final long identity = Binder.clearCallingIdentity();
2550 try {
2551 String url = createTelUrl(number);
2552 if (url == null) {
2553 return;
2554 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002555
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002556 boolean isValid = false;
2557 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2558 if (slist != null) {
2559 for (SubscriptionInfo subInfoRecord : slist) {
2560 if (subInfoRecord.getSubscriptionId() == subId) {
2561 isValid = true;
2562 break;
2563 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002564 }
Wink Saville08874612014-08-31 19:19:58 -07002565 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002566 if (!isValid) {
2567 return;
2568 }
Wink Saville08874612014-08-31 19:19:58 -07002569
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002570 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2571 intent.putExtra(SUBSCRIPTION_KEY, subId);
2572 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2573 mApp.startActivity(intent);
2574 } finally {
2575 Binder.restoreCallingIdentity(identity);
2576 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002577 }
2578
Wink Savilleb564aae2014-10-23 10:18:09 -07002579 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002580 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002581 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2582 }
2583
Wink Savilleb564aae2014-10-23 10:18:09 -07002584 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002585 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002586 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2587 }
2588
Wink Savilleb564aae2014-10-23 10:18:09 -07002589 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002590 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002591
2592 final long identity = Binder.clearCallingIdentity();
2593 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002594 Phone phone = getPhone(subId);
2595 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002596 checkSimPin.start();
2597 return checkSimPin.unlockSim(null, pin);
2598 } finally {
2599 Binder.restoreCallingIdentity(identity);
2600 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002601 }
2602
Wink Savilleb564aae2014-10-23 10:18:09 -07002603 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002604 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002605
2606 final long identity = Binder.clearCallingIdentity();
2607 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002608 Phone phone = getPhone(subId);
2609 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002610 checkSimPuk.start();
2611 return checkSimPuk.unlockSim(puk, pin);
2612 } finally {
2613 Binder.restoreCallingIdentity(identity);
2614 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002615 }
2616
2617 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002618 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002619 * a synchronous one.
2620 */
2621 private static class UnlockSim extends Thread {
2622
2623 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002624 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002625
2626 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002627 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2628 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002629
2630 // For replies from SimCard interface
2631 private Handler mHandler;
2632
2633 // For async handler to identify request type
2634 private static final int SUPPLY_PIN_COMPLETE = 100;
2635
Michele Berionne5e411512020-11-13 02:36:59 +00002636 UnlockSim(int phoneId, IccCard simCard) {
2637 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002638 mSimCard = simCard;
2639 }
2640
2641 @Override
2642 public void run() {
2643 Looper.prepare();
2644 synchronized (UnlockSim.this) {
2645 mHandler = new Handler() {
2646 @Override
2647 public void handleMessage(Message msg) {
2648 AsyncResult ar = (AsyncResult) msg.obj;
2649 switch (msg.what) {
2650 case SUPPLY_PIN_COMPLETE:
2651 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2652 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002653 mRetryCount = msg.arg1;
2654 if (ar.exception != null) {
2655 if (ar.exception instanceof CommandException &&
2656 ((CommandException)(ar.exception)).getCommandError()
2657 == CommandException.Error.PASSWORD_INCORRECT) {
2658 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002659 } //When UiccCardApp dispose,handle message and return exception
2660 else if (ar.exception instanceof CommandException &&
2661 ((CommandException) (ar.exception)).getCommandError()
2662 == CommandException.Error.ABORTED) {
2663 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002664 } else {
2665 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2666 }
2667 } else {
2668 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2669 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002670 mDone = true;
2671 UnlockSim.this.notifyAll();
2672 }
2673 break;
2674 }
2675 }
2676 };
2677 UnlockSim.this.notifyAll();
2678 }
2679 Looper.loop();
2680 }
2681
2682 /*
2683 * Use PIN or PUK to unlock SIM card
2684 *
2685 * If PUK is null, unlock SIM card with PIN
2686 *
2687 * If PUK is not null, unlock SIM card with PUK and set PIN code
2688 */
Wink Saville9de0f752013-10-22 19:04:03 -07002689 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002690
2691 while (mHandler == null) {
2692 try {
2693 wait();
2694 } catch (InterruptedException e) {
2695 Thread.currentThread().interrupt();
2696 }
2697 }
2698 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2699
2700 if (puk == null) {
2701 mSimCard.supplyPin(pin, callback);
2702 } else {
2703 mSimCard.supplyPuk(puk, pin, callback);
2704 }
2705
2706 while (!mDone) {
2707 try {
2708 Log.d(LOG_TAG, "wait for done");
2709 wait();
2710 } catch (InterruptedException e) {
2711 // Restore the interrupted status
2712 Thread.currentThread().interrupt();
2713 }
2714 }
2715 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002716 int[] resultArray = new int[2];
2717 resultArray[0] = mResult;
2718 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002719
2720 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002721 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002722 }
2723
Wink Saville9de0f752013-10-22 19:04:03 -07002724 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002725 }
2726 }
2727
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002728 /**
2729 * This method has been removed due to privacy and stability concerns.
2730 */
2731 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002732 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002733 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2734 return;
Wink Saville36469e72014-06-11 15:17:00 -07002735 }
2736
Nathan Harold1f889d82020-06-04 17:05:26 -07002737 @Override
2738 public void updateServiceLocationWithPackageName(String callingPackage) {
2739 mApp.getSystemService(AppOpsManager.class)
2740 .checkPackage(Binder.getCallingUid(), callingPackage);
2741
Nathan Haroldf096d982020-11-18 17:18:06 -08002742 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002743 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2744 // Callers targeting S have no business invoking this method.
2745 return;
2746 }
2747
2748 LocationAccessPolicy.LocationPermissionResult locationResult =
2749 LocationAccessPolicy.checkLocationPermission(mApp,
2750 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2751 .setCallingPackage(callingPackage)
2752 .setCallingFeatureId(null)
2753 .setCallingPid(Binder.getCallingPid())
2754 .setCallingUid(Binder.getCallingUid())
2755 .setMethod("updateServiceLocation")
2756 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2757 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2758 .build());
2759 // Apps that lack location permission have no business calling this method;
2760 // however, because no permission was declared in the public API, denials must
2761 // all be "soft".
2762 switch (locationResult) {
2763 case DENIED_HARD: /* fall through */
2764 case DENIED_SOFT:
2765 return;
2766 }
2767
2768 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002769 final long identity = Binder.clearCallingIdentity();
2770 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002771 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002772 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002773 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002774 }
2775 } finally {
2776 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002777 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002778 }
2779
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002780 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002781 @Override
2782 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002783 return isRadioOnWithFeature(callingPackage, null);
2784 }
2785
2786
2787 @Override
2788 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2789 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2790 callingFeatureId);
2791 }
2792
2793 @Deprecated
2794 @Override
2795 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2796 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002797 }
2798
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002799 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002800 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2801 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002802 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002803 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002804 return false;
2805 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002806
2807 final long identity = Binder.clearCallingIdentity();
2808 try {
2809 return isRadioOnForSubscriber(subId);
2810 } finally {
2811 Binder.restoreCallingIdentity(identity);
2812 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002813 }
2814
2815 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002816 final long identity = Binder.clearCallingIdentity();
2817 try {
2818 final Phone phone = getPhone(subId);
2819 if (phone != null) {
2820 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2821 } else {
2822 return false;
2823 }
2824 } finally {
2825 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002826 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002827 }
2828
2829 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002830 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002831 }
Wink Saville36469e72014-06-11 15:17:00 -07002832
Wink Savilleb564aae2014-10-23 10:18:09 -07002833 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002834 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002835
2836 final long identity = Binder.clearCallingIdentity();
2837 try {
2838 final Phone phone = getPhone(subId);
2839 if (phone != null) {
2840 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2841 }
2842 } finally {
2843 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002844 }
Wink Saville36469e72014-06-11 15:17:00 -07002845 }
2846
2847 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002848 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002849 }
2850
Wink Savilleb564aae2014-10-23 10:18:09 -07002851 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002852 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002853
2854 final long identity = Binder.clearCallingIdentity();
2855 try {
2856 final Phone phone = getPhone(subId);
2857 if (phone == null) {
2858 return false;
2859 }
2860 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2861 toggleRadioOnOffForSubscriber(subId);
2862 }
2863 return true;
2864 } finally {
2865 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002866 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002867 }
Wink Saville36469e72014-06-11 15:17:00 -07002868
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002869 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002870 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002871 /*
2872 * If any of the Radios are available, it will need to be
2873 * shutdown. So return true if any Radio is available.
2874 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002875 final long identity = Binder.clearCallingIdentity();
2876 try {
2877 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2878 Phone phone = PhoneFactory.getPhone(i);
2879 if (phone != null && phone.isRadioAvailable()) return true;
2880 }
2881 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2882 return false;
2883 } finally {
2884 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002885 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002886 }
2887
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002888 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002889 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002890 enforceModifyPermission();
2891
2892 final long identity = Binder.clearCallingIdentity();
2893 try {
2894 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2895 logv("Shutting down Phone " + i);
2896 shutdownRadioUsingPhoneId(i);
2897 }
2898 } finally {
2899 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002900 }
2901 }
2902
2903 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002904 Phone phone = PhoneFactory.getPhone(phoneId);
2905 if (phone != null && phone.isRadioAvailable()) {
2906 phone.shutdownRadio();
2907 }
2908 }
2909
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002910 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002911 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002912
2913 final long identity = Binder.clearCallingIdentity();
2914 try {
2915 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2916 if (defaultPhone != null) {
2917 defaultPhone.setRadioPower(turnOn);
2918 return true;
2919 } else {
2920 loge("There's no default phone.");
2921 return false;
2922 }
2923 } finally {
2924 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002925 }
Wink Saville36469e72014-06-11 15:17:00 -07002926 }
2927
Wink Savilleb564aae2014-10-23 10:18:09 -07002928 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002929 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002930
2931 final long identity = Binder.clearCallingIdentity();
2932 try {
2933 final Phone phone = getPhone(subId);
2934 if (phone != null) {
2935 phone.setRadioPower(turnOn);
2936 return true;
2937 } else {
2938 return false;
2939 }
2940 } finally {
2941 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002942 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002943 }
2944
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002945 /**
2946 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
2947 * no reason to power it off. When any of the voters want to power it off, it will be turned
2948 * off. In case of emergency, the radio will be turned on even if there are some reasons for
2949 * powering it off, and these radio off votes will be cleared.
2950 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
2951 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
2952 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
2953 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
2954 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
2955 * check its vote.
2956 *
2957 * @param subId The subscription ID.
2958 * @param reason The reason for powering off radio.
2959 * @return true on success and false on failure.
2960 */
2961 public boolean requestRadioPowerOffForReason(int subId,
2962 @TelephonyManager.RadioPowerReason int reason) {
2963 enforceModifyPermission();
2964
2965 final long identity = Binder.clearCallingIdentity();
2966 try {
2967 final Phone phone = getPhone(subId);
2968 if (phone != null) {
2969 phone.setRadioPowerForReason(false, reason);
2970 return true;
2971 } else {
2972 return false;
2973 }
2974 } finally {
2975 Binder.restoreCallingIdentity(identity);
2976 }
2977 }
2978
2979 /**
2980 * Remove the vote on powering off the radio for a reason, as requested by
2981 * {@link requestRadioPowerOffForReason}.
2982 *
2983 * @param subId The subscription ID.
2984 * @param reason The reason for powering off radio.
2985 * @return true on success and false on failure.
2986 */
2987 public boolean clearRadioPowerOffForReason(int subId,
2988 @TelephonyManager.RadioPowerReason int reason) {
2989 enforceModifyPermission();
2990
2991 final long identity = Binder.clearCallingIdentity();
2992 try {
2993 final Phone phone = getPhone(subId);
2994 if (phone != null) {
2995 phone.setRadioPowerForReason(true, reason);
2996 return true;
2997 } else {
2998 return false;
2999 }
3000 } finally {
3001 Binder.restoreCallingIdentity(identity);
3002 }
3003 }
3004
3005 /**
3006 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3007 *
3008 * @param subId The subscription ID.
3009 * @param callingPackage The package making the call.
3010 * @param callingFeatureId The feature in the package.
3011 * @return List of reasons for powering off radio.
3012 */
3013 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3014 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3015
3016 final long identity = Binder.clearCallingIdentity();
3017 List result = new ArrayList();
3018 try {
3019 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3020 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3021 return result;
3022 }
3023
3024 final Phone phone = getPhone(subId);
3025 if (phone != null) {
3026 result.addAll(phone.getRadioPowerOffReasons());
3027 }
3028 } finally {
3029 Binder.restoreCallingIdentity(identity);
3030 }
3031 return result;
3032 }
3033
Wink Saville36469e72014-06-11 15:17:00 -07003034 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003035 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003036 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003037 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003038
3039 final long identity = Binder.clearCallingIdentity();
3040 try {
Jack Yu285100e2022-12-02 22:48:35 -08003041 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003042 final Phone phone = getPhone(subId);
3043 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003044 phone.getDataSettingsManager().setDataEnabled(
3045 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003046 return true;
3047 } else {
3048 return false;
3049 }
3050 } finally {
3051 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003052 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003053 }
3054
Wink Saville36469e72014-06-11 15:17:00 -07003055 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003056 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003057 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003058 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003059
3060 final long identity = Binder.clearCallingIdentity();
3061 try {
Jack Yu285100e2022-12-02 22:48:35 -08003062 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003063 final Phone phone = getPhone(subId);
3064 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003065 phone.getDataSettingsManager().setDataEnabled(
3066 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003067 return true;
3068 } else {
3069 return false;
3070 }
3071 } finally {
3072 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003073 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003074 }
3075
Sanket Padawe356d7632015-06-22 14:03:32 -07003076 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003077 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003078 final long identity = Binder.clearCallingIdentity();
3079 try {
3080 final Phone phone = getPhone(subId);
3081 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003082 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003083 } else {
3084 return false;
3085 }
3086 } finally {
3087 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003088 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003089 }
3090
3091 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003092 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003093 }
3094
pkanwarae03a6b2016-11-06 20:37:09 -08003095 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003096 enforceCallPermission();
3097
3098 final long identity = Binder.clearCallingIdentity();
3099 try {
3100 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3101 return;
3102 }
3103 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3104 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3105 } finally {
3106 Binder.restoreCallingIdentity(identity);
3107 }
pkanwar32d516d2016-10-14 19:37:38 -07003108 };
3109
Wink Savilleb564aae2014-10-23 10:18:09 -07003110 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003111 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003112
3113 final long identity = Binder.clearCallingIdentity();
3114 try {
3115 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3116 return false;
3117 }
3118 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3119 } finally {
3120 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003121 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003122 }
3123
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003124 /**
3125 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3126 * tag on getCallState Binder call.
3127 */
3128 @Deprecated
3129 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003130 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003131 if (CompatChanges.isChangeEnabled(
3132 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3133 Binder.getCallingUid())) {
3134 // Do not allow this API to be called on API version 31+, it should only be
3135 // called on old apps using this Binder call directly.
3136 throw new SecurityException("This method can only be used for applications "
3137 + "targeting API version 30 or less.");
3138 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003139 final long identity = Binder.clearCallingIdentity();
3140 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003141 Phone phone = getPhone(getDefaultSubscription());
3142 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3143 PhoneConstantConversions.convertCallState(phone.getState());
3144 } finally {
3145 Binder.restoreCallingIdentity(identity);
3146 }
3147 }
3148
3149 @Override
3150 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3151 if (CompatChanges.isChangeEnabled(
3152 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3153 Binder.getCallingUid())) {
3154 // Check READ_PHONE_STATE for API version 31+
3155 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3156 featureId, "getCallStateForSubscription")) {
3157 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3158 + "targeting API level 31+.");
3159 }
3160 }
3161 final long identity = Binder.clearCallingIdentity();
3162 try {
3163 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003164 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3165 PhoneConstantConversions.convertCallState(phone.getState());
3166 } finally {
3167 Binder.restoreCallingIdentity(identity);
3168 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003169 }
3170
Sanket Padawe356d7632015-06-22 14:03:32 -07003171 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003172 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003173 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003174 }
3175
3176 @Override
3177 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003178 final long identity = Binder.clearCallingIdentity();
3179 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003180 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003181 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003182 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003183 } else {
3184 return PhoneConstantConversions.convertDataState(
3185 PhoneConstants.DataState.DISCONNECTED);
3186 }
3187 } finally {
3188 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003189 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003190 }
3191
Sanket Padawe356d7632015-06-22 14:03:32 -07003192 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003193 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003194 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003195 }
3196
3197 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003198 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003199 final long identity = Binder.clearCallingIdentity();
3200 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003201 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003202 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003203 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003204 } else {
3205 return TelephonyManager.DATA_ACTIVITY_NONE;
3206 }
3207 } finally {
3208 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003209 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003210 }
3211
3212 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003213 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003214 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003215 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003216
3217 LocationAccessPolicy.LocationPermissionResult locationResult =
3218 LocationAccessPolicy.checkLocationPermission(mApp,
3219 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3220 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003221 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003222 .setCallingPid(Binder.getCallingPid())
3223 .setCallingUid(Binder.getCallingUid())
3224 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003225 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003226 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3227 .build());
3228 switch (locationResult) {
3229 case DENIED_HARD:
3230 throw new SecurityException("Not allowed to access cell location");
3231 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003232 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3233 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003234 }
3235
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003236 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003237 final long identity = Binder.clearCallingIdentity();
3238 try {
3239 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003240 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003241 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003242 } finally {
3243 Binder.restoreCallingIdentity(identity);
3244 }
Svetoslav64fad262015-04-14 14:35:21 -07003245 }
3246
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003247 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003248 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003249 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3250 // registered cell info, so return a NULL country instead.
3251 final long identity = Binder.clearCallingIdentity();
3252 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003253 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3254 // Get default phone in this case.
3255 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3256 }
Jack Yu285100e2022-12-02 22:48:35 -08003257 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003258 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003259 if (phone == null) return "";
3260 ServiceStateTracker sst = phone.getServiceStateTracker();
3261 if (sst == null) return "";
3262 LocaleTracker lt = sst.getLocaleTracker();
3263 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003264 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003265 } finally {
3266 Binder.restoreCallingIdentity(identity);
3267 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003268 }
3269
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003270 /**
3271 * This method was removed due to potential issues caused by performing partial
3272 * updates of service state, and lack of a credible use case.
3273 *
3274 * This has the ability to break the telephony implementation by disabling notification of
3275 * changes in device connectivity. DO NOT USE THIS!
3276 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003277 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003278 public void enableLocationUpdates() {
3279 mApp.enforceCallingOrSelfPermission(
3280 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003281 }
3282
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003283 /**
3284 * This method was removed due to potential issues caused by performing partial
3285 * updates of service state, and lack of a credible use case.
3286 *
3287 * This has the ability to break the telephony implementation by disabling notification of
3288 * changes in device connectivity. DO NOT USE THIS!
3289 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003290 @Override
3291 public void disableLocationUpdates() {
3292 mApp.enforceCallingOrSelfPermission(
3293 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003294 }
3295
3296 @Override
3297 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003298 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3299 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003300 try {
3301 mApp.getSystemService(AppOpsManager.class)
3302 .checkPackage(Binder.getCallingUid(), callingPackage);
3303 } catch (SecurityException e) {
3304 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3305 throw e;
3306 }
3307
Nathan Haroldf096d982020-11-18 17:18:06 -08003308 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003309 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3310 throw new SecurityException(
3311 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3312 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003313
Jordan Liu1617b712019-07-10 15:06:26 -07003314 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003315 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3316 return null;
3317 }
Svetoslav64fad262015-04-14 14:35:21 -07003318
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003319 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003320
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003321 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003322 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003323
Nathan Haroldf180aac2018-06-01 18:43:55 -07003324 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3325 for (CellInfo ci : info) {
3326 if (ci instanceof CellInfoGsm) {
3327 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3328 } else if (ci instanceof CellInfoWcdma) {
3329 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3330 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003331 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003332 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003333 }
3334
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003335 private List<CellInfo> getCachedCellInfo() {
3336 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3337 for (Phone phone : PhoneFactory.getPhones()) {
3338 List<CellInfo> info = phone.getAllCellInfo();
3339 if (info != null) cellInfos.addAll(info);
3340 }
3341 return cellInfos;
3342 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003343
3344 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003345 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003346 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003347 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003348
3349 LocationAccessPolicy.LocationPermissionResult locationResult =
3350 LocationAccessPolicy.checkLocationPermission(mApp,
3351 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3352 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003353 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003354 .setCallingPid(Binder.getCallingPid())
3355 .setCallingUid(Binder.getCallingUid())
3356 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003357 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003358 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3359 .build());
3360 switch (locationResult) {
3361 case DENIED_HARD:
3362 throw new SecurityException("Not allowed to access cell info");
3363 case DENIED_SOFT:
3364 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003365 }
3366
Nathan Haroldf096d982020-11-18 17:18:06 -08003367 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003368 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3369 return getCachedCellInfo();
3370 }
3371
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003372 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003373 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003374 final long identity = Binder.clearCallingIdentity();
3375 try {
3376 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3377 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003378 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003379 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003380 if (info != null) cellInfos.addAll(info);
3381 }
3382 return cellInfos;
3383 } finally {
3384 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003385 }
3386 }
3387
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003388 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003389 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3390 String callingFeatureId) {
3391 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3392 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003393 }
3394
3395 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003396 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3397 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003398 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003399 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003400 }
3401
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003402 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3403 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003404 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003405 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003406
3407 LocationAccessPolicy.LocationPermissionResult locationResult =
3408 LocationAccessPolicy.checkLocationPermission(mApp,
3409 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3410 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003411 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003412 .setCallingPid(Binder.getCallingPid())
3413 .setCallingUid(Binder.getCallingUid())
3414 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003415 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3416 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003417 .build());
3418 switch (locationResult) {
3419 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003420 if (TelephonyPermissions
3421 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003422 // Safetynet logging for b/154934934
3423 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3424 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003425 throw new SecurityException("Not allowed to access cell info");
3426 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003427 if (TelephonyPermissions
3428 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003429 // Safetynet logging for b/154934934
3430 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3431 }
Nathan Harold5320c422019-05-09 10:26:08 -07003432 try {
3433 cb.onCellInfo(new ArrayList<CellInfo>());
3434 } catch (RemoteException re) {
3435 // Drop without consequences
3436 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003437 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003438 }
3439
Nathan Harolda939a962019-05-09 10:13:47 -07003440
3441 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003442 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3443
3444 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3445 }
3446
3447 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003448 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003449 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003450 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003451
3452 final long identity = Binder.clearCallingIdentity();
3453 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003454 Phone phone = getPhone(subId);
3455 if (phone == null) {
3456 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3457 } else {
3458 phone.setCellInfoListRate(rateInMillis, workSource);
3459 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003460 } finally {
3461 Binder.restoreCallingIdentity(identity);
3462 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003463 }
3464
Shishir Agrawala9f32182016-04-12 12:00:16 -07003465 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003466 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003467 Phone phone = PhoneFactory.getPhone(slotIndex);
3468 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003469 return null;
3470 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003471 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003472 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003473 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003474 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003475 return null;
3476 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003477
3478 final long identity = Binder.clearCallingIdentity();
3479 try {
3480 return phone.getImei();
3481 } finally {
3482 Binder.restoreCallingIdentity(identity);
3483 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003484 }
3485
3486 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003487 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3488 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3489 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3490 callingFeatureId, "getPrimaryImei")) {
3491 throw new SecurityException("Caller does not have permission");
3492 }
3493 final long identity = Binder.clearCallingIdentity();
3494 try {
3495 for (Phone phone : PhoneFactory.getPhones()) {
3496 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3497 return phone.getImei();
3498 }
3499 }
3500 throw new UnsupportedOperationException("Operation not supported");
3501 } finally {
3502 Binder.restoreCallingIdentity(identity);
3503 }
3504 }
3505
3506 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003507 public String getTypeAllocationCodeForSlot(int slotIndex) {
3508 Phone phone = PhoneFactory.getPhone(slotIndex);
3509 String tac = null;
3510 if (phone != null) {
3511 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003512 try {
3513 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3514 } catch (IndexOutOfBoundsException e) {
3515 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3516 return null;
3517 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003518 }
3519 return tac;
3520 }
3521
3522 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003523 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003524 try {
3525 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3526 } catch (SecurityException se) {
3527 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3528 throw new SecurityException("Package " + callingPackage + " does not belong to "
3529 + Binder.getCallingUid());
3530 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003531 Phone phone = PhoneFactory.getPhone(slotIndex);
3532 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003533 return null;
3534 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003535
Jeff Davidson913390f2018-02-23 17:11:49 -08003536 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003537 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003538 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003539 return null;
3540 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003541
3542 final long identity = Binder.clearCallingIdentity();
3543 try {
3544 return phone.getMeid();
3545 } finally {
3546 Binder.restoreCallingIdentity(identity);
3547 }
Jack Yu2af8d712017-03-15 17:14:14 -07003548 }
3549
3550 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003551 public String getManufacturerCodeForSlot(int slotIndex) {
3552 Phone phone = PhoneFactory.getPhone(slotIndex);
3553 String manufacturerCode = null;
3554 if (phone != null) {
3555 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003556 try {
3557 manufacturerCode =
3558 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3559 } catch (IndexOutOfBoundsException e) {
3560 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3561 return null;
3562 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003563 }
3564 return manufacturerCode;
3565 }
3566
3567 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003568 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3569 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003570 Phone phone = PhoneFactory.getPhone(slotIndex);
3571 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003572 return null;
3573 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003574 int subId = phone.getSubId();
3575 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003576 mApp, subId, callingPackage, callingFeatureId,
3577 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003578 return null;
3579 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003580
3581 final long identity = Binder.clearCallingIdentity();
3582 try {
3583 return phone.getDeviceSvn();
3584 } finally {
3585 Binder.restoreCallingIdentity(identity);
3586 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003587 }
3588
fionaxu43304da2017-11-27 22:51:16 -08003589 @Override
3590 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003591 final long identity = Binder.clearCallingIdentity();
3592 try {
3593 final Phone phone = getPhone(subId);
3594 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3595 } finally {
3596 Binder.restoreCallingIdentity(identity);
3597 }
fionaxu43304da2017-11-27 22:51:16 -08003598 }
3599
3600 @Override
3601 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003602 final long identity = Binder.clearCallingIdentity();
3603 try {
3604 final Phone phone = getPhone(subId);
3605 return phone == null ? null : phone.getCarrierName();
3606 } finally {
3607 Binder.restoreCallingIdentity(identity);
3608 }
fionaxu43304da2017-11-27 22:51:16 -08003609 }
3610
calvinpanffe225e2018-11-01 19:43:06 +08003611 @Override
chen xu0026ca62019-03-06 15:28:50 -08003612 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003613 final long identity = Binder.clearCallingIdentity();
3614 try {
3615 final Phone phone = getPhone(subId);
3616 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003617 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003618 } finally {
3619 Binder.restoreCallingIdentity(identity);
3620 }
3621 }
3622
3623 @Override
chen xu0026ca62019-03-06 15:28:50 -08003624 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003625 final long identity = Binder.clearCallingIdentity();
3626 try {
3627 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003628 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003629 } finally {
3630 Binder.restoreCallingIdentity(identity);
3631 }
3632 }
3633
chen xu651eec72018-11-11 19:03:44 -08003634 @Override
chen xu864e11c2018-12-06 22:10:03 -08003635 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3636 if (!isSubscriptionMccMnc) {
3637 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3638 }
chen xu651eec72018-11-11 19:03:44 -08003639 final Phone phone = PhoneFactory.getPhone(slotIndex);
3640 if (phone == null) {
3641 return TelephonyManager.UNKNOWN_CARRIER_ID;
3642 }
3643 final long identity = Binder.clearCallingIdentity();
3644 try {
3645 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3646 } finally {
3647 Binder.restoreCallingIdentity(identity);
3648 }
3649 }
3650
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003651 //
3652 // Internal helper methods.
3653 //
3654
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003655 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003656 * Make sure the caller is the calling package itself
3657 *
3658 * @throws SecurityException if the caller is not the calling package
3659 */
3660 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3661 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003662 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3663 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003664 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003665 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003666 } catch (PackageManager.NameNotFoundException e) {
3667 // packageUid is -1
3668 }
3669 if (packageUid != callingUid) {
3670 throw new SecurityException(message + ": Package " + callingPackage
3671 + " does not belong to " + callingUid);
3672 }
3673 }
3674
3675 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003676 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3677 *
3678 * @throws SecurityException if the caller does not have the required permission
3679 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003680 @VisibleForTesting
3681 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003682 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3683 }
3684
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003685 /**
3686 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3687 *
3688 * @throws SecurityException if the caller does not have the required permission
3689 */
3690 @VisibleForTesting
3691 public void enforceReadPermission() {
3692 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
3693 }
3694
Shuo Qian3b6ee772019-11-13 17:43:31 -08003695 private void enforceActiveEmergencySessionPermission() {
3696 mApp.enforceCallingOrSelfPermission(
3697 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3698 }
3699
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003700 /**
3701 * Make sure the caller has the CALL_PHONE permission.
3702 *
3703 * @throws SecurityException if the caller does not have the required permission
3704 */
3705 private void enforceCallPermission() {
3706 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3707 }
3708
paulhu5a773602019-08-23 19:17:33 +08003709 private void enforceSettingsPermission() {
3710 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003711 }
3712
Michele Berionne5e411512020-11-13 02:36:59 +00003713 private void enforceRebootPermission() {
3714 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3715 }
3716
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003717 private String createTelUrl(String number) {
3718 if (TextUtils.isEmpty(number)) {
3719 return null;
3720 }
3721
Jake Hambye994d462014-02-03 13:10:13 -08003722 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003723 }
3724
Ihab Awadf9e92732013-12-05 18:02:52 -08003725 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003726 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3727 }
3728
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003729 private static void logv(String msg) {
3730 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3731 }
3732
Ihab Awadf9e92732013-12-05 18:02:52 -08003733 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003734 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3735 }
3736
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003737 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003738 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003739 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003740 }
3741
Sanket Padawe356d7632015-06-22 14:03:32 -07003742 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003743 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003744 final long identity = Binder.clearCallingIdentity();
3745 try {
3746 final Phone phone = PhoneFactory.getPhone(slotIndex);
3747 if (phone == null) {
3748 return PhoneConstants.PHONE_TYPE_NONE;
3749 } else {
3750 return phone.getPhoneType();
3751 }
3752 } finally {
3753 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003754 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003755 }
3756
3757 /**
3758 * Returns the CDMA ERI icon index to display
3759 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003760 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003761 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3762 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3763 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003764 }
3765
Sanket Padawe356d7632015-06-22 14:03:32 -07003766 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003767 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3768 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003769 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003770 mApp, subId, callingPackage, callingFeatureId,
3771 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003772 return -1;
3773 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003774
3775 final long identity = Binder.clearCallingIdentity();
3776 try {
3777 final Phone phone = getPhone(subId);
3778 if (phone != null) {
3779 return phone.getCdmaEriIconIndex();
3780 } else {
3781 return -1;
3782 }
3783 } finally {
3784 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003785 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003786 }
3787
3788 /**
3789 * Returns the CDMA ERI icon mode,
3790 * 0 - ON
3791 * 1 - FLASHING
3792 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003793 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003794 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3795 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3796 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003797 }
3798
Sanket Padawe356d7632015-06-22 14:03:32 -07003799 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003800 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3801 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003802 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003803 mApp, subId, callingPackage, callingFeatureId,
3804 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003805 return -1;
3806 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003807
3808 final long identity = Binder.clearCallingIdentity();
3809 try {
3810 final Phone phone = getPhone(subId);
3811 if (phone != null) {
3812 return phone.getCdmaEriIconMode();
3813 } else {
3814 return -1;
3815 }
3816 } finally {
3817 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003818 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003819 }
3820
3821 /**
3822 * Returns the CDMA ERI text,
3823 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003824 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003825 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3826 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3827 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003828 }
3829
Sanket Padawe356d7632015-06-22 14:03:32 -07003830 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003831 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3832 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003833 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003834 mApp, subId, callingPackage, callingFeatureId,
3835 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003836 return null;
3837 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003838
3839 final long identity = Binder.clearCallingIdentity();
3840 try {
3841 final Phone phone = getPhone(subId);
3842 if (phone != null) {
3843 return phone.getCdmaEriText();
3844 } else {
3845 return null;
3846 }
3847 } finally {
3848 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003849 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003850 }
3851
3852 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003853 * Returns the CDMA MDN.
3854 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003855 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003856 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003857 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3858 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003859
3860 final long identity = Binder.clearCallingIdentity();
3861 try {
3862 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003863 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003864 return phone.getLine1Number();
3865 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003866 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003867 return null;
3868 }
3869 } finally {
3870 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003871 }
3872 }
3873
3874 /**
3875 * Returns the CDMA MIN.
3876 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003877 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003878 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003879 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3880 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003881
3882 final long identity = Binder.clearCallingIdentity();
3883 try {
3884 final Phone phone = getPhone(subId);
3885 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3886 return phone.getCdmaMin();
3887 } else {
3888 return null;
3889 }
3890 } finally {
3891 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003892 }
3893 }
3894
Hall Liud892bec2018-11-30 14:51:45 -08003895 @Override
3896 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3897 INumberVerificationCallback callback, String callingPackage) {
3898 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3899 != PERMISSION_GRANTED) {
3900 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3901 }
3902 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3903
3904 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3905 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003906 throw new SecurityException("Calling package must be configured in the device config: "
3907 + "calling package: " + callingPackage
3908 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003909 }
3910
3911 if (range == null) {
3912 throw new NullPointerException("Range must be non-null");
3913 }
3914
3915 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003916 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003917
3918 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3919 }
3920
Junda Liuca05d5d2014-08-14 22:36:34 -07003921 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003922 * Returns true if CDMA provisioning needs to run.
3923 */
3924 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003925 final long identity = Binder.clearCallingIdentity();
3926 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003927 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003928 } finally {
3929 Binder.restoreCallingIdentity(identity);
3930 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003931 }
3932
3933 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003934 * Sets the voice mail number of a given subId.
3935 */
3936 @Override
3937 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003938 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3939 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003940
3941 final long identity = Binder.clearCallingIdentity();
3942 try {
3943 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3944 new Pair<String, String>(alphaTag, number), new Integer(subId));
3945 return success;
3946 } finally {
3947 Binder.restoreCallingIdentity(identity);
3948 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003949 }
3950
Ta-wei Yen87c49842016-05-13 21:19:52 -07003951 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003952 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3953 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003954 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3955 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003956 if (!TextUtils.equals(callingPackage, systemDialer)) {
3957 throw new SecurityException("caller must be system dialer");
3958 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003959
3960 final long identity = Binder.clearCallingIdentity();
3961 try {
3962 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3963 if (phoneAccountHandle == null) {
3964 return null;
3965 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003966 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003967 } finally {
3968 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003969 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003970 }
3971
3972 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003973 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3974 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003975 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003976 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003977 mApp, subId, callingPackage, callingFeatureId,
3978 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003979 return null;
3980 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003981
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003982 final long identity = Binder.clearCallingIdentity();
3983 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003984 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003985 } finally {
3986 Binder.restoreCallingIdentity(identity);
3987 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003988 }
3989
3990 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003991 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3992 VisualVoicemailSmsFilterSettings settings) {
3993 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003994
3995 final long identity = Binder.clearCallingIdentity();
3996 try {
3997 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003998 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003999 } finally {
4000 Binder.restoreCallingIdentity(identity);
4001 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004002 }
4003
4004 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004005 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4006 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004007
4008 final long identity = Binder.clearCallingIdentity();
4009 try {
4010 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004011 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004012 } finally {
4013 Binder.restoreCallingIdentity(identity);
4014 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004015 }
4016
4017 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004018 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4019 String callingPackage, int subId) {
4020 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004021
4022 final long identity = Binder.clearCallingIdentity();
4023 try {
4024 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004025 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004026 } finally {
4027 Binder.restoreCallingIdentity(identity);
4028 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004029 }
4030
4031 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004032 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004033 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004034
4035 final long identity = Binder.clearCallingIdentity();
4036 try {
4037 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004038 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004039 } finally {
4040 Binder.restoreCallingIdentity(identity);
4041 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004042 }
4043
4044 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004045 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4046 String callingAttributionTag, int subId, String number, int port, String text,
4047 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004048 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004049 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004050 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004051 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004052 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4053 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004054 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004055
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004056 /**
fionaxu0152e512016-11-14 13:36:14 -08004057 * Sets the voice activation state of a given subId.
4058 */
4059 @Override
4060 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004061 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4062 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004063
4064 final long identity = Binder.clearCallingIdentity();
4065 try {
4066 final Phone phone = getPhone(subId);
4067 if (phone != null) {
4068 phone.setVoiceActivationState(activationState);
4069 } else {
4070 loge("setVoiceActivationState fails with invalid subId: " + subId);
4071 }
4072 } finally {
4073 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004074 }
4075 }
4076
4077 /**
4078 * Sets the data activation state of a given subId.
4079 */
4080 @Override
4081 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004082 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4083 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004084
4085 final long identity = Binder.clearCallingIdentity();
4086 try {
4087 final Phone phone = getPhone(subId);
4088 if (phone != null) {
4089 phone.setDataActivationState(activationState);
4090 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004091 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004092 }
4093 } finally {
4094 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004095 }
4096 }
4097
4098 /**
4099 * Returns the voice activation state of a given subId.
4100 */
4101 @Override
4102 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004103 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004104
fionaxu0152e512016-11-14 13:36:14 -08004105 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004106 final long identity = Binder.clearCallingIdentity();
4107 try {
4108 if (phone != null) {
4109 return phone.getVoiceActivationState();
4110 } else {
4111 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4112 }
4113 } finally {
4114 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004115 }
4116 }
4117
4118 /**
4119 * Returns the data activation state of a given subId.
4120 */
4121 @Override
4122 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004123 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004124
fionaxu0152e512016-11-14 13:36:14 -08004125 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004126 final long identity = Binder.clearCallingIdentity();
4127 try {
4128 if (phone != null) {
4129 return phone.getDataActivationState();
4130 } else {
4131 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4132 }
4133 } finally {
4134 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004135 }
4136 }
4137
4138 /**
Wink Saville36469e72014-06-11 15:17:00 -07004139 * Returns the unread count of voicemails for a subId
4140 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004141 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004142 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4143 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004144 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004145 mApp, subId, callingPackage, callingFeatureId,
4146 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004147 return 0;
4148 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004149 final long identity = Binder.clearCallingIdentity();
4150 try {
4151 final Phone phone = getPhone(subId);
4152 if (phone != null) {
4153 return phone.getVoiceMessageCount();
4154 } else {
4155 return 0;
4156 }
4157 } finally {
4158 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004159 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004160 }
4161
4162 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004163 * returns true, if the device is in a state where both voice and data
4164 * are supported simultaneously. This can change based on location or network condition.
4165 */
4166 @Override
4167 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004168 final long identity = Binder.clearCallingIdentity();
4169 try {
4170 final Phone phone = getPhone(subId);
4171 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4172 } finally {
4173 Binder.restoreCallingIdentity(identity);
4174 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004175 }
4176
4177 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004178 * Send the dialer code if called from the current default dialer or the caller has
4179 * carrier privilege.
4180 * @param inputCode The dialer code to send
4181 */
4182 @Override
4183 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004184 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004185 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004186 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4187 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004188 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004189 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004190 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004191 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004192
4193 final long identity = Binder.clearCallingIdentity();
4194 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004195 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004196 } finally {
4197 Binder.restoreCallingIdentity(identity);
4198 }
fionaxu235cc5e2017-03-06 22:25:57 -08004199 }
4200
Pengquan Menga1bb6272018-09-06 09:59:22 -07004201 @Override
4202 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004203 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004204 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004205 mApp, subId, "getNetworkSelectionMode");
4206 final long identity = Binder.clearCallingIdentity();
4207 try {
4208 if (!isActiveSubscription(subId)) {
4209 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4210 }
4211 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4212 } finally {
4213 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004214 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004215 }
4216
Brad Ebinger35c841c2018-10-01 10:40:55 -07004217 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004218 public boolean isInEmergencySmsMode() {
4219 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4220 final long identity = Binder.clearCallingIdentity();
4221 try {
4222 for (Phone phone : PhoneFactory.getPhones()) {
4223 if (phone.isInEmergencySmsMode()) {
4224 return true;
4225 }
4226 }
4227 } finally {
4228 Binder.restoreCallingIdentity(identity);
4229 }
4230 return false;
4231 }
4232
shilu366312e2019-12-17 09:28:10 -08004233 /**
4234 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4235 * @param subId The subscription to use to check the configuration.
4236 * @param c The callback that will be used to send the result.
4237 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004238 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004239 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4240 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004241 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004242 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004243
4244 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4245 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4246 "IMS not available on device.");
4247 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004248 final long token = Binder.clearCallingIdentity();
4249 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004250 int slotId = getSlotIndexOrException(subId);
4251 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004252
4253 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4254 if (controller != null) {
4255 ImsManager imsManager = controller.getImsManager(subId);
4256 if (imsManager != null) {
4257 imsManager.addRegistrationCallbackForSubscription(c, subId);
4258 } else {
4259 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4260 }
4261 } else {
4262 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4263 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004264 } catch (ImsException e) {
4265 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004266 } finally {
4267 Binder.restoreCallingIdentity(token);
4268 }
4269 }
4270
shilu366312e2019-12-17 09:28:10 -08004271 /**
4272 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4273 * @param subId The subscription to use to check the configuration.
4274 * @param c The callback that will be used to send the result.
4275 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004276 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004277 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004278 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004279 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004280 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4281 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4282 }
Meng Wangafbc5852019-09-19 17:37:13 -07004283 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004284
Meng Wangafbc5852019-09-19 17:37:13 -07004285 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004286 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4287 if (controller != null) {
4288 ImsManager imsManager = controller.getImsManager(subId);
4289 if (imsManager != null) {
4290 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4291 } else {
4292 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4293 + "is inactive, ignoring unregister.");
4294 // If the ImsManager is not valid, just return, since the callback
4295 // will already have been removed internally.
4296 }
4297 }
Meng Wangafbc5852019-09-19 17:37:13 -07004298 } finally {
4299 Binder.restoreCallingIdentity(token);
4300 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004301 }
4302
Brad Ebingera34a6c22019-10-22 17:36:18 -07004303 /**
4304 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4305 */
4306 @Override
4307 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4308 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4309 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4310 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4311 "IMS not available on device.");
4312 }
4313 final long token = Binder.clearCallingIdentity();
4314 try {
4315 Phone phone = getPhone(subId);
4316 if (phone == null) {
4317 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4318 + subId + "'");
4319 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4320 }
4321 phone.getImsRegistrationState(regState -> {
4322 try {
4323 consumer.accept((regState == null)
4324 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4325 } catch (RemoteException e) {
4326 // Ignore if the remote process is no longer available to call back.
4327 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4328 }
4329 });
4330 } finally {
4331 Binder.restoreCallingIdentity(token);
4332 }
4333 }
4334
4335 /**
4336 * Get the transport type for the IMS service registration state.
4337 */
4338 @Override
4339 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004340 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004341 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004342 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4343 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4344 "IMS not available on device.");
4345 }
4346 final long token = Binder.clearCallingIdentity();
4347 try {
4348 Phone phone = getPhone(subId);
4349 if (phone == null) {
4350 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4351 + subId + "'");
4352 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4353 }
4354 phone.getImsRegistrationTech(regTech -> {
4355 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4356 int regTechConverted = (regTech == null)
4357 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4358 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4359 regTechConverted);
4360 try {
4361 consumer.accept(regTechConverted);
4362 } catch (RemoteException e) {
4363 // Ignore if the remote process is no longer available to call back.
4364 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4365 }
4366 });
4367 } finally {
4368 Binder.restoreCallingIdentity(token);
4369 }
4370 }
4371
shilu366312e2019-12-17 09:28:10 -08004372 /**
4373 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4374 * @param subId The subscription to use to check the configuration.
4375 * @param c The callback that will be used to send the result.
4376 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004377 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004378 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4379 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004380 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004381 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004382 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4383 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4384 "IMS not available on device.");
4385 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004386 final long token = Binder.clearCallingIdentity();
4387 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004388 int slotId = getSlotIndexOrException(subId);
4389 verifyImsMmTelConfiguredOrThrow(slotId);
4390 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004391 } catch (ImsException e) {
4392 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004393 } finally {
4394 Binder.restoreCallingIdentity(token);
4395 }
4396 }
4397
shilu366312e2019-12-17 09:28:10 -08004398 /**
4399 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4400 * @param subId The subscription to use to check the configuration.
4401 * @param c The callback that will be used to send the result.
4402 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004403 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004404 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004405 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004406 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004407 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4408 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4409 }
Meng Wangafbc5852019-09-19 17:37:13 -07004410
4411 final long token = Binder.clearCallingIdentity();
4412 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004413 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004414 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004415 } catch (ImsException e) {
4416 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4417 + "is inactive, ignoring unregister.");
4418 // If the subscription is no longer active, just return, since the callback
4419 // will already have been removed internally.
4420 } finally {
4421 Binder.restoreCallingIdentity(token);
4422 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004423 }
4424
4425 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004426 public boolean isCapable(int subId, int capability, int regTech) {
4427 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004428 final long token = Binder.clearCallingIdentity();
4429 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004430 int slotId = getSlotIndexOrException(subId);
4431 verifyImsMmTelConfiguredOrThrow(slotId);
4432 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4433 } catch (com.android.ims.ImsException e) {
4434 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4435 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004436 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004437 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4438 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004439 } finally {
4440 Binder.restoreCallingIdentity(token);
4441 }
4442 }
4443
4444 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004445 public boolean isAvailable(int subId, int capability, int regTech) {
4446 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004447 final long token = Binder.clearCallingIdentity();
4448 try {
4449 Phone phone = getPhone(subId);
4450 if (phone == null) return false;
4451 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004452 } catch (com.android.ims.ImsException e) {
4453 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4454 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004455 } finally {
4456 Binder.restoreCallingIdentity(token);
4457 }
4458 }
4459
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004460 /**
4461 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4462 * subscription.
4463 * @param subId The subscription to use to check the configuration.
4464 * @param callback The callback that will be used to send the result.
4465 * @param capability The MmTelFeature capability that will be used to send the result.
4466 * @param transportType The transport type of the MmTelFeature capability.
4467 */
4468 @Override
4469 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4470 int transportType) {
4471 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004472 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4473 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4474 "IMS not available on device.");
4475 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004476 final long token = Binder.clearCallingIdentity();
4477 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004478 int slotId = getSlotIndex(subId);
4479 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4480 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4481 + subId + "'");
4482 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4483 }
4484 verifyImsMmTelConfiguredOrThrow(slotId);
4485 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4486 transportType, aBoolean -> {
4487 try {
4488 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4489 } catch (RemoteException e) {
4490 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4491 + "running. Ignore");
4492 }
4493 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004494 } catch (ImsException e) {
4495 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004496 } finally {
4497 Binder.restoreCallingIdentity(token);
4498 }
4499 }
4500
shilu366312e2019-12-17 09:28:10 -08004501 /**
4502 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4503 * @param subId The subscription to use to check the configuration.
4504 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004505 @Override
4506 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004507 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004508 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004509
Brad Ebinger35c841c2018-10-01 10:40:55 -07004510 final long token = Binder.clearCallingIdentity();
4511 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004512 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004513 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004514 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004515 } catch (ImsException e) {
4516 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004517 } finally {
4518 Binder.restoreCallingIdentity(token);
4519 }
4520 }
4521
4522 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004523 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004524 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004525 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004526 final long identity = Binder.clearCallingIdentity();
4527 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004528 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004529 // This setting doesn't require an active ImsService connection, so do not verify. The
4530 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004531 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004532 } catch (ImsException e) {
4533 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004534 } finally {
4535 Binder.restoreCallingIdentity(identity);
4536 }
4537 }
4538
shilu366312e2019-12-17 09:28:10 -08004539 /**
4540 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4541 * @param subId The subscription to use to check the configuration.
4542 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004543 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004544 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004545 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004546 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004547 final long identity = Binder.clearCallingIdentity();
4548 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004549 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004550 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004551 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004552 } catch (ImsException e) {
4553 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004554 } finally {
4555 Binder.restoreCallingIdentity(identity);
4556 }
4557 }
4558
4559 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004560 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004561 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004562 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004563 final long identity = Binder.clearCallingIdentity();
4564 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004565 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004566 // This setting doesn't require an active ImsService connection, so do not verify. The
4567 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004568 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004569 } catch (ImsException e) {
4570 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004571 } finally {
4572 Binder.restoreCallingIdentity(identity);
4573 }
4574 }
4575
shilu366312e2019-12-17 09:28:10 -08004576 /**
4577 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4578 * @param subId The subscription to use to check the configuration.
4579 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004580 @Override
4581 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004582 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004583 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004584 final long identity = Binder.clearCallingIdentity();
4585 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004586 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004587 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004588 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004589 } catch (ImsException e) {
4590 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004591 } finally {
4592 Binder.restoreCallingIdentity(identity);
4593 }
4594 }
4595
4596 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004597 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004598 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004599 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004600 final long identity = Binder.clearCallingIdentity();
4601 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004602 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004603 // This setting doesn't require an active ImsService connection, so do not verify. The
4604 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004605 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004606 } catch (ImsException e) {
4607 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004608 } finally {
4609 Binder.restoreCallingIdentity(identity);
4610 }
4611 }
4612
shilu366312e2019-12-17 09:28:10 -08004613 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004614 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4615 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4616 * @param subId The subscription to use to check the configuration.
4617 */
4618 @Override
4619 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004620 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004621 mApp, subId, "isCrossSimCallingEnabledByUser");
4622 final long identity = Binder.clearCallingIdentity();
4623 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004624 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004625 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004626 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004627 } catch (ImsException e) {
4628 throw new ServiceSpecificException(e.getCode());
4629 } finally {
4630 Binder.restoreCallingIdentity(identity);
4631 }
4632 }
4633
4634 /**
4635 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4636 * Requires MODIFY_PHONE_STATE permission.
4637 * @param subId The subscription to use to check the configuration.
4638 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4639 * false otherwise
4640 */
4641 @Override
4642 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4643 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4644 "setCrossSimCallingEnabled");
4645 final long identity = Binder.clearCallingIdentity();
4646 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004647 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004648 // This setting doesn't require an active ImsService connection, so do not verify. The
4649 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004650 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004651 } catch (ImsException e) {
4652 throw new ServiceSpecificException(e.getCode());
4653 } finally {
4654 Binder.restoreCallingIdentity(identity);
4655 }
4656 }
4657
4658 /**
shilu366312e2019-12-17 09:28:10 -08004659 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4660 * @param subId The subscription to use to check the configuration.
4661 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004662 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004663
Brad Ebinger35c841c2018-10-01 10:40:55 -07004664 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004665 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004666 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004667 final long identity = Binder.clearCallingIdentity();
4668 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004669 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004670 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004671 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004672 } catch (ImsException e) {
4673 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004674 } finally {
4675 Binder.restoreCallingIdentity(identity);
4676 }
4677 }
4678
4679 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004680 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004681 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004682 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004683 final long identity = Binder.clearCallingIdentity();
4684 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004685 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004686 // This setting doesn't require an active ImsService connection, so do not verify. The
4687 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004688 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004689 } catch (ImsException e) {
4690 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004691 } finally {
4692 Binder.restoreCallingIdentity(identity);
4693 }
4694 }
4695
4696 @Override
4697 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4698 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4699 "setVoWiFiNonPersistent");
4700 final long identity = Binder.clearCallingIdentity();
4701 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004702 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004703 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004704 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004705 } catch (ImsException e) {
4706 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004707 } finally {
4708 Binder.restoreCallingIdentity(identity);
4709 }
4710 }
4711
shilu366312e2019-12-17 09:28:10 -08004712 /**
4713 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4714 * @param subId The subscription to use to check the configuration.
4715 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004716 @Override
4717 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004718 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004719 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004720 final long identity = Binder.clearCallingIdentity();
4721 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004722 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004723 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004724 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004725 } catch (ImsException e) {
4726 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004727 } finally {
4728 Binder.restoreCallingIdentity(identity);
4729 }
4730 }
4731
4732 @Override
4733 public void setVoWiFiModeSetting(int subId, int mode) {
4734 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4735 "setVoWiFiModeSetting");
4736 final long identity = Binder.clearCallingIdentity();
4737 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004738 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004739 // This setting doesn't require an active ImsService connection, so do not verify. The
4740 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004741 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004742 } catch (ImsException e) {
4743 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004744 } finally {
4745 Binder.restoreCallingIdentity(identity);
4746 }
4747 }
4748
4749 @Override
4750 public int getVoWiFiRoamingModeSetting(int subId) {
4751 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4752 final long identity = Binder.clearCallingIdentity();
4753 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004754 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004755 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004756 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004757 } catch (ImsException e) {
4758 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004759 } finally {
4760 Binder.restoreCallingIdentity(identity);
4761 }
4762 }
4763
4764 @Override
4765 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4766 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4767 "setVoWiFiRoamingModeSetting");
4768 final long identity = Binder.clearCallingIdentity();
4769 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004770 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004771 // This setting doesn't require an active ImsService connection, so do not verify. The
4772 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004773 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004774 } catch (ImsException e) {
4775 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004776 } finally {
4777 Binder.restoreCallingIdentity(identity);
4778 }
4779 }
4780
4781 @Override
4782 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4783 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4784 "setRttCapabilityEnabled");
4785 final long identity = Binder.clearCallingIdentity();
4786 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004787 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004788 // This setting doesn't require an active ImsService connection, so do not verify. The
4789 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004790 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004791 } catch (ImsException e) {
4792 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004793 } finally {
4794 Binder.restoreCallingIdentity(identity);
4795 }
4796 }
4797
shilu366312e2019-12-17 09:28:10 -08004798 /**
4799 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4800 * @param subId The subscription to use to check the configuration.
4801 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004802 @Override
4803 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004804 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004805 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004806 final long identity = Binder.clearCallingIdentity();
4807 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004808 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004809 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004810 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004811 } catch (ImsException e) {
4812 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004813 } finally {
4814 Binder.restoreCallingIdentity(identity);
4815 }
4816 }
4817
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004818 @Override
4819 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4820 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004821
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004822 final long identity = Binder.clearCallingIdentity();
4823 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004824 if (!isImsAvailableOnDevice()) {
4825 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4826 "IMS not available on device.");
4827 }
4828 int slotId = getSlotIndexOrException(subId);
4829 verifyImsMmTelConfiguredOrThrow(slotId);
4830 ImsManager.getInstance(mApp, slotId)
4831 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004832 } catch (ImsException e) {
4833 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004834 } finally {
4835 Binder.restoreCallingIdentity(identity);
4836 }
4837 }
4838
4839 @Override
4840 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4841 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004842
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004843 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004844 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4845 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4846 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004847 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004848 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004849 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004850 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004851 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4852 + "is inactive, ignoring unregister.");
4853 // If the subscription is no longer active, just return, since the callback will already
4854 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004855 } finally {
4856 Binder.restoreCallingIdentity(identity);
4857 }
4858 }
4859
joonhunshincffb7fc2021-11-28 07:32:01 +00004860 @Override
4861 public void registerFeatureProvisioningChangedCallback(int subId,
4862 IFeatureProvisioningCallback callback) {
4863 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4864 mApp, subId, "registerFeatureProvisioningChangedCallback");
4865
4866 final long identity = Binder.clearCallingIdentity();
4867 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4868 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4869 }
4870
joonhunshin91bc1952022-04-29 08:47:15 +00004871 try {
4872 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4873 if (controller == null) {
4874 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4875 "Device does not support IMS");
4876 }
4877 controller.addFeatureProvisioningChangedCallback(subId, callback);
4878 } finally {
4879 Binder.restoreCallingIdentity(identity);
4880 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004881 }
4882
4883 @Override
4884 public void unregisterFeatureProvisioningChangedCallback(int subId,
4885 IFeatureProvisioningCallback callback) {
4886 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4887 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4888
4889 final long identity = Binder.clearCallingIdentity();
4890 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4891 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4892 }
4893
joonhunshin91bc1952022-04-29 08:47:15 +00004894 try {
4895 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4896 if (controller == null) {
4897 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4898 return;
4899 }
4900 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4901 } finally {
4902 Binder.restoreCallingIdentity(identity);
4903 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004904 }
allenwtsu99c623b2020-01-03 18:24:23 +08004905
4906 private void checkModifyPhoneStatePermission(int subId, String message) {
4907 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4908 message);
4909 }
4910
allenwtsu99c623b2020-01-03 18:24:23 +08004911 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004912 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004913 boolean isProvisioned) {
4914 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4915
4916 final long identity = Binder.clearCallingIdentity();
4917 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004918 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4919 if (controller == null) {
4920 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4921 return;
4922 }
4923 controller.setRcsProvisioningStatusForCapability(
4924 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004925 } finally {
4926 Binder.restoreCallingIdentity(identity);
4927 }
allenwtsu99c623b2020-01-03 18:24:23 +08004928 }
4929
4930
4931 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004932 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4933 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4934 mApp, subId, "getRcsProvisioningStatusForCapability");
4935
allenwtsu99c623b2020-01-03 18:24:23 +08004936 final long identity = Binder.clearCallingIdentity();
4937 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004938 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4939 if (controller == null) {
4940 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4941
4942 // device does not support IMS, this method will return true always.
4943 return true;
4944 }
4945 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004946 } finally {
4947 Binder.restoreCallingIdentity(identity);
4948 }
4949 }
4950
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004951 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004952 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4953 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004954 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004955
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004956 final long identity = Binder.clearCallingIdentity();
4957 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004958 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4959 if (controller == null) {
4960 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4961 return;
4962 }
4963 controller.setImsProvisioningStatusForCapability(
4964 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004965 } finally {
4966 Binder.restoreCallingIdentity(identity);
4967 }
4968 }
4969
4970 @Override
4971 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004972 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4973 mApp, subId, "getProvisioningStatusForCapability");
4974
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004975 final long identity = Binder.clearCallingIdentity();
4976 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004977 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4978 if (controller == null) {
4979 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004980
joonhunshin91bc1952022-04-29 08:47:15 +00004981 // device does not support IMS, this method will return true always.
4982 return true;
4983 }
4984 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004985 } finally {
4986 Binder.restoreCallingIdentity(identity);
4987 }
4988 }
4989
4990 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004991 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4992 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4993 mApp, subId, "isProvisioningRequiredForCapability");
4994
4995 final long identity = Binder.clearCallingIdentity();
4996 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004997 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4998 if (controller == null) {
4999 loge("isProvisioningRequiredForCapability: Device does not support IMS");
5000
5001 // device does not support IMS, this method will return false
5002 return false;
5003 }
5004 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005005 } finally {
5006 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005007 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005008 }
5009
5010 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005011 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5012 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5013 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005014
joonhunshincffb7fc2021-11-28 07:32:01 +00005015 final long identity = Binder.clearCallingIdentity();
5016 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005017 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5018 if (controller == null) {
5019 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5020
5021 // device does not support IMS, this method will return false
5022 return false;
5023 }
5024 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005025 } finally {
5026 Binder.restoreCallingIdentity(identity);
5027 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005028 }
5029
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005030 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005031 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005032 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5033 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5034 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005035 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5036 mApp, subId, "getImsProvisioningInt");
5037
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005038 final long identity = Binder.clearCallingIdentity();
5039 try {
5040 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005041 int slotId = getSlotIndex(subId);
5042 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5043 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5044 + subId + "' for key:" + key);
5045 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5046 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005047
joonhunshin91bc1952022-04-29 08:47:15 +00005048 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5049 if (controller == null) {
5050 loge("getImsProvisioningInt: Device does not support IMS");
5051
5052 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5053 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5054 }
5055 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005056 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5057 return retVal;
5058 }
5059
calvinpanb5a34062021-02-08 19:59:36 +08005060 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005061 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005062 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5063 + subId + "' for key:" + key);
5064 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005065 } finally {
5066 Binder.restoreCallingIdentity(identity);
5067 }
5068 }
5069
5070 @Override
5071 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005072 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5073 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5074 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005075 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5076 mApp, subId, "getImsProvisioningString");
5077
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005078 final long identity = Binder.clearCallingIdentity();
5079 try {
5080 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005081 int slotId = getSlotIndex(subId);
5082 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5083 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5084 + subId + "' for key:" + key);
5085 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5086 }
calvinpanb5a34062021-02-08 19:59:36 +08005087 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005088 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005089 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5090 + subId + "' for key:" + key);
5091 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005092 } finally {
5093 Binder.restoreCallingIdentity(identity);
5094 }
5095 }
5096
5097 @Override
5098 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005099 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5100 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5101 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005102 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5103 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005104
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005105 final long identity = Binder.clearCallingIdentity();
5106 try {
5107 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005108 int slotId = getSlotIndex(subId);
5109 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5110 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5111 + subId + "' for key:" + key);
5112 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5113 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005114
joonhunshin91bc1952022-04-29 08:47:15 +00005115 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5116 if (controller == null) {
5117 loge("setImsProvisioningInt: Device does not support IMS");
5118
5119 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5120 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5121 }
5122 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005123 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5124 return retVal;
5125 }
5126
calvinpanb5a34062021-02-08 19:59:36 +08005127 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5128 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005129 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005130 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005131 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005132 } finally {
5133 Binder.restoreCallingIdentity(identity);
5134 }
5135 }
5136
5137 @Override
5138 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005139 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5140 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5141 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005142 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5143 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005144 final long identity = Binder.clearCallingIdentity();
5145 try {
5146 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005147 int slotId = getSlotIndex(subId);
5148 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5149 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5150 + subId + "' for key:" + key);
5151 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5152 }
calvinpanb5a34062021-02-08 19:59:36 +08005153 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5154 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005155 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005156 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005157 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005158 } finally {
5159 Binder.restoreCallingIdentity(identity);
5160 }
5161 }
5162
Brad Ebinger919631e2021-06-02 17:46:35 -07005163 /**
5164 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5165 * for the given slot ID or no ImsResolver instance has been created.
5166 * @param slotId The slot ID that the IMS service is created for.
5167 * @throws ImsException If there is no ImsService configured for this slot.
5168 */
5169 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5170 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5171 ImsFeature.FEATURE_MMTEL)) {
5172 throw new ImsException("This subscription does not support MMTEL over IMS",
5173 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5174 }
5175 }
5176
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005177 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005178 int slotId = SubscriptionManager.getSlotIndex(subId);
5179 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005180 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5181 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005182 }
5183 return slotId;
5184 }
5185
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005186 private int getSlotIndex(int subId) {
5187 int slotId = SubscriptionManager.getSlotIndex(subId);
5188 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5189 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5190 }
5191 return slotId;
5192 }
5193
Wink Saville36469e72014-06-11 15:17:00 -07005194 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005195 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005196 */
5197 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005198 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5199 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005200 try {
5201 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5202 } catch (SecurityException se) {
5203 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5204 throw new SecurityException("Package " + callingPackage + " does not belong to "
5205 + Binder.getCallingUid());
5206 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005207 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005208 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005209 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005210 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005211 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005212 mApp, subId, callingPackage, callingFeatureId,
5213 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005214 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5215 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005216
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005217 final long identity = Binder.clearCallingIdentity();
5218 try {
5219 final Phone phone = getPhone(subId);
5220 if (phone != null) {
5221 return phone.getServiceState().getDataNetworkType();
5222 } else {
5223 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5224 }
5225 } finally {
5226 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005227 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005228 }
5229
5230 /**
5231 * Returns the data network type
5232 */
5233 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005234 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005235 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005236 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005237 }
5238
5239 /**
5240 * Returns the data network type for a subId
5241 */
5242 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005243 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5244 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005245 String functionName = "getDataNetworkTypeForSubscriber";
5246 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5247 mApp, functionName)) {
5248 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5249 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5250 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5251 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005252 }
5253
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005254 final long identity = Binder.clearCallingIdentity();
5255 try {
5256 final Phone phone = getPhone(subId);
5257 if (phone != null) {
5258 return phone.getServiceState().getDataNetworkType();
5259 } else {
5260 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5261 }
5262 } finally {
5263 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005264 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005265 }
5266
5267 /**
Wink Saville36469e72014-06-11 15:17:00 -07005268 * Returns the Voice network type for a subId
5269 */
5270 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005271 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5272 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005273 String functionName = "getVoiceNetworkTypeForSubscriber";
5274 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5275 mApp, functionName)) {
5276 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5277 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5278 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5279 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005280 }
5281
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005282 final long identity = Binder.clearCallingIdentity();
5283 try {
5284 final Phone phone = getPhone(subId);
5285 if (phone != null) {
5286 return phone.getServiceState().getVoiceNetworkType();
5287 } else {
5288 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5289 }
5290 } finally {
5291 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005292 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005293 }
5294
5295 /**
5296 * @return true if a ICC card is present
5297 */
5298 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005299 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005300 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005301 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005302 }
5303
5304 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005305 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005306 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005307 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005308 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005309 final long identity = Binder.clearCallingIdentity();
5310 try {
5311 final Phone phone = PhoneFactory.getPhone(slotIndex);
5312 if (phone != null) {
5313 return phone.getIccCard().hasIccCard();
5314 } else {
5315 return false;
5316 }
5317 } finally {
5318 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005319 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005320 }
5321
5322 /**
5323 * Return if the current radio is LTE on CDMA. This
5324 * is a tri-state return value as for a period of time
5325 * the mode may be unknown.
5326 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005327 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005328 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005329 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005330 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005331 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005332 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5333 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5334 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005335 }
5336
Sanket Padawe356d7632015-06-22 14:03:32 -07005337 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005338 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5339 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005340 try {
5341 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5342 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005343 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5344 }
5345
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005346 final long identity = Binder.clearCallingIdentity();
5347 try {
5348 final Phone phone = getPhone(subId);
5349 if (phone == null) {
5350 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5351 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005352 return TelephonyProperties.lte_on_cdma_device()
5353 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005354 }
5355 } finally {
5356 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005357 }
Wink Saville36469e72014-06-11 15:17:00 -07005358 }
5359
Wink Saville36469e72014-06-11 15:17:00 -07005360 /**
5361 * {@hide}
5362 * Returns Default subId, 0 in the case of single standby.
5363 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005364 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005365 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005366 }
5367
Shishir Agrawala9f32182016-04-12 12:00:16 -07005368 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005369 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005370 }
5371
Wink Savilleb564aae2014-10-23 10:18:09 -07005372 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005373 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005374 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005375
Pengquan Menge92a50d2018-09-21 15:54:48 -07005376 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005377 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5378 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5379 mApp.getOpPackageName(), mApp.getFeatureId());
5380 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005381 return mSubscriptionController.isActiveSubId(subId);
5382 }
5383
Ihab Awadf2177b72013-11-25 13:33:23 -08005384 /**
5385 * @see android.telephony.TelephonyManager.WifiCallingChoices
5386 */
5387 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005388 final long identity = Binder.clearCallingIdentity();
5389 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005390 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005391 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5392 getWhenToMakeWifiCallsDefaultPreference());
5393 } finally {
5394 Binder.restoreCallingIdentity(identity);
5395 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005396 }
5397
5398 /**
5399 * @see android.telephony.TelephonyManager.WifiCallingChoices
5400 */
5401 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005402 final long identity = Binder.clearCallingIdentity();
5403 try {
5404 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005405 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005406 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5407 } finally {
5408 Binder.restoreCallingIdentity(identity);
5409 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005410 }
5411
Sailesh Nepald1e68152013-12-12 19:08:02 -08005412 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005413 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005414 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005415 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005416
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005417 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5418 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5419 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005420 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005421 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5422 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005423 }
5424 return PhoneFactory.getPhone(phoneId);
5425 }
5426
Shishir Agrawal566b7612013-10-28 14:41:00 -07005427 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005428 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005429 @NonNull IccLogicalChannelRequest request) {
5430 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5431 /*message=*/ "iccOpenLogicalChannel");
5432
5433 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5434 // Verify that the callingPackage in the request belongs to the calling UID
5435 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5436
5437 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005438 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005439
Rambo Wanga1782702021-11-10 20:15:19 -08005440 private Phone getPhoneFromValidIccLogicalChannelRequest(
5441 @NonNull IccLogicalChannelRequest request, String message) {
5442 Phone phone;
5443 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5444 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5445 mApp, request.subId, message);
5446 phone = getPhoneFromSubId(request.subId);
5447 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5448 enforceModifyPermission();
5449 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5450 } else {
5451 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005452 }
Rambo Wanga1782702021-11-10 20:15:19 -08005453 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005454 }
5455
5456 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005457 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005458 final long identity = Binder.clearCallingIdentity();
5459 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005460 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005461 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005462 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5463 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005464 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5465 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005466 loge("The calling package is not allowed to access ISD-R.");
5467 throw new SecurityException(
5468 "The calling package is not allowed to access ISD-R.");
5469 }
Derek Tan740e1672017-06-27 14:56:27 -07005470 }
Derek Tan740e1672017-06-27 14:56:27 -07005471
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005472 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005473 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5474 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005475 return response;
5476 } finally {
5477 Binder.restoreCallingIdentity(identity);
5478 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005479 }
5480
5481 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005482 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5483 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5484 /*message=*/"iccCloseLogicalChannel");
5485
5486 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5487
5488 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005489 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005490
Rambo Wanga1782702021-11-10 20:15:19 -08005491 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5492 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005493 // before this feature is enabled, this API should only return false if
5494 // the operation fails instead of throwing runtime exception for
5495 // backward-compatibility.
5496 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5497 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005498 final long identity = Binder.clearCallingIdentity();
5499 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005500 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005501 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005502 }
Chen Xue9d737e2022-01-01 23:41:31 -08005503 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005504 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005505 Boolean success = false;
5506 if (result instanceof RuntimeException) {
5507 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005508 if (shouldThrowExceptionOnFailure) {
5509 throw (RuntimeException) result;
5510 } else {
5511 return false;
5512 }
Chen Xue9d737e2022-01-01 23:41:31 -08005513 } else if (result instanceof Boolean) {
5514 success = (Boolean) result;
5515 } else {
5516 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5517 }
Rambo Wanga1782702021-11-10 20:15:19 -08005518 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005519 return success;
5520 } finally {
5521 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005522 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005523 }
5524
5525 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005526 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005527 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005528 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5529 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005530 if (DBG) {
5531 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5532 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5533 + p3 + " data=" + data);
5534 }
5535 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5536 command, p1, p2, p3, data);
5537 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005538
Jordan Liu4c733742019-02-28 12:03:40 -08005539 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005540 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5541 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005542 enforceModifyPermission();
5543 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005544 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5545 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5546 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005547 }
5548 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005549 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5550 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005551 }
5552
5553 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5554 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005555 final long identity = Binder.clearCallingIdentity();
5556 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005557 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005558 return "";
5559 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005560
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005561 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005562 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5563 null /* workSource */);
5564 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005565
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005566 // Append the returned status code to the end of the response payload.
5567 String s = Integer.toHexString(
5568 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5569 if (response.payload != null) {
5570 s = IccUtils.bytesToHexString(response.payload) + s;
5571 }
5572 return s;
5573 } finally {
5574 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005575 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005576 }
Jake Hambye994d462014-02-03 13:10:13 -08005577
Evan Charltonc66da362014-05-16 14:06:40 -07005578 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005579 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5580 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005581 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5582 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005583 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005584 if (DBG) {
5585 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5586 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5587 }
5588 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5589 cla, command, p1, p2, p3, data);
5590 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005591
Jordan Liu4c733742019-02-28 12:03:40 -08005592 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005593 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5594 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005595 enforceModifyPermission();
5596 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5597 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005598 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5599 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5600 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005601 }
5602
5603 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005604 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5605 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005606 }
5607
5608 // open APDU basic channel assuming the caller has sufficient permissions
5609 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5610 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005611 final long identity = Binder.clearCallingIdentity();
5612 try {
5613 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5614 && TextUtils.equals(ISDR_AID, data)) {
5615 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005616 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5617 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005618 if (bestComponent == null
5619 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5620 loge("The calling package is not allowed to select ISD-R.");
5621 throw new SecurityException(
5622 "The calling package is not allowed to select ISD-R.");
5623 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005624 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005625
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005626 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005627 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5628 null /* workSource */);
5629 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005630
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005631 // Append the returned status code to the end of the response payload.
5632 String s = Integer.toHexString(
5633 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5634 if (response.payload != null) {
5635 s = IccUtils.bytesToHexString(response.payload) + s;
5636 }
5637 return s;
5638 } finally {
5639 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005640 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005641 }
5642
5643 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005644 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005645 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005646 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5647 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005648
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005649 final long identity = Binder.clearCallingIdentity();
5650 try {
5651 if (DBG) {
5652 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5653 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5654 }
5655
5656 IccIoResult response =
5657 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5658 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5659 subId);
5660
5661 if (DBG) {
5662 log("Exchange SIM_IO [R]" + response);
5663 }
5664
5665 byte[] result = null;
5666 int length = 2;
5667 if (response.payload != null) {
5668 length = 2 + response.payload.length;
5669 result = new byte[length];
5670 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5671 } else {
5672 result = new byte[length];
5673 }
5674
5675 result[length - 1] = (byte) response.sw2;
5676 result[length - 2] = (byte) response.sw1;
5677 return result;
5678 } finally {
5679 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005680 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005681 }
5682
Nathan Haroldb3014052017-01-25 15:57:32 -08005683 /**
5684 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5685 * on a particular subscription
5686 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005687 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5688 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005689 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005690 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005691 return null;
5692 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005693
5694 final long identity = Binder.clearCallingIdentity();
5695 try {
5696 if (appType != TelephonyManager.APPTYPE_USIM
5697 && appType != TelephonyManager.APPTYPE_SIM) {
5698 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5699 return null;
5700 }
5701 Object response = sendRequest(
5702 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5703 if (response instanceof String[]) {
5704 return (String[]) response;
5705 }
yincheng zhao2737e882019-09-06 17:06:54 -07005706 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005707 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005708 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005709 } finally {
5710 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005711 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005712 }
5713
yincheng zhao2737e882019-09-06 17:06:54 -07005714 /**
5715 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5716 * subscription.
5717 *
5718 * @param subId the id of the subscription.
5719 * @param appType the uicc app type, must be USIM or SIM.
5720 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5721 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005722 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005723 * @return number of fplmns that is successfully written to the SIM.
5724 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005725 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5726 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005727 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5728 mApp, subId, "setForbiddenPlmns");
5729
yincheng zhao2737e882019-09-06 17:06:54 -07005730 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5731 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5732 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5733 }
5734 if (fplmns == null) {
5735 throw new IllegalArgumentException("Fplmn List provided is null");
5736 }
5737 for (String fplmn : fplmns) {
5738 if (!CellIdentity.isValidPlmn(fplmn)) {
5739 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5740 }
5741 }
5742 final long identity = Binder.clearCallingIdentity();
5743 try {
5744 Object response = sendRequest(
5745 CMD_SET_FORBIDDEN_PLMNS,
5746 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5747 subId);
5748 return (int) response;
5749 } finally {
5750 Binder.restoreCallingIdentity(identity);
5751 }
5752 }
5753
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005754 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005755 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005756 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5757 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005758
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005759 final long identity = Binder.clearCallingIdentity();
5760 try {
5761 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5762 if (response.payload == null) {
5763 return "";
5764 }
Evan Charltonc66da362014-05-16 14:06:40 -07005765
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005766 // Append the returned status code to the end of the response payload.
5767 String s = Integer.toHexString(
5768 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5769 s = IccUtils.bytesToHexString(response.payload) + s;
5770 return s;
5771 } finally {
5772 Binder.restoreCallingIdentity(identity);
5773 }
Evan Charltonc66da362014-05-16 14:06:40 -07005774 }
5775
Jake Hambye994d462014-02-03 13:10:13 -08005776 /**
5777 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5778 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5779 *
5780 * @param itemID the ID of the item to read
5781 * @return the NV item as a String, or null on error.
5782 */
5783 @Override
5784 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005785 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005786 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5787 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005788
5789 final long identity = Binder.clearCallingIdentity();
5790 try {
5791 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005792 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005793 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5794 return value;
5795 } finally {
5796 Binder.restoreCallingIdentity(identity);
5797 }
Jake Hambye994d462014-02-03 13:10:13 -08005798 }
5799
5800 /**
5801 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5802 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5803 *
5804 * @param itemID the ID of the item to read
5805 * @param itemValue the value to write, as a String
5806 * @return true on success; false on any failure
5807 */
5808 @Override
5809 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005810 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005811 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5812 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005813
5814 final long identity = Binder.clearCallingIdentity();
5815 try {
5816 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5817 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005818 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005819 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5820 return success;
5821 } finally {
5822 Binder.restoreCallingIdentity(identity);
5823 }
Jake Hambye994d462014-02-03 13:10:13 -08005824 }
5825
5826 /**
5827 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5828 * Used for device configuration by some CDMA operators.
5829 *
5830 * @param preferredRoamingList byte array containing the new PRL
5831 * @return true on success; false on any failure
5832 */
5833 @Override
5834 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005835 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5836 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005837
5838 final long identity = Binder.clearCallingIdentity();
5839 try {
5840 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5841 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5842 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5843 return success;
5844 } finally {
5845 Binder.restoreCallingIdentity(identity);
5846 }
Jake Hambye994d462014-02-03 13:10:13 -08005847 }
5848
5849 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005850 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005851 * Used for device configuration by some CDMA operators.
5852 *
chen xu6dac5ab2018-10-26 17:39:23 -07005853 * @param slotIndex - device slot.
5854 *
Jake Hambye994d462014-02-03 13:10:13 -08005855 * @return true on success; false on any failure
5856 */
5857 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005858 public boolean resetModemConfig(int slotIndex) {
5859 Phone phone = PhoneFactory.getPhone(slotIndex);
5860 if (phone != null) {
5861 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5862 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005863
chen xu6dac5ab2018-10-26 17:39:23 -07005864 final long identity = Binder.clearCallingIdentity();
5865 try {
5866 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5867 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5868 return success;
5869 } finally {
5870 Binder.restoreCallingIdentity(identity);
5871 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005872 }
chen xu6dac5ab2018-10-26 17:39:23 -07005873 return false;
5874 }
5875
5876 /**
5877 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5878 *
5879 * @param slotIndex - device slot.
5880 *
5881 * @return true on success; false on any failure
5882 */
5883 @Override
5884 public boolean rebootModem(int slotIndex) {
5885 Phone phone = PhoneFactory.getPhone(slotIndex);
5886 if (phone != null) {
5887 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5888 mApp, phone.getSubId(), "rebootModem");
5889
5890 final long identity = Binder.clearCallingIdentity();
5891 try {
5892 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5893 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5894 return success;
5895 } finally {
5896 Binder.restoreCallingIdentity(identity);
5897 }
5898 }
5899 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005900 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005901
Brad Ebinger51f743a2017-01-23 13:50:20 -08005902 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005903 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5904 * {@link #disableIms(int)}.
5905 * @param slotIndex device slot.
5906 */
5907 public void resetIms(int slotIndex) {
5908 enforceModifyPermission();
5909
5910 final long identity = Binder.clearCallingIdentity();
5911 try {
5912 if (mImsResolver == null) {
5913 // may happen if the does not support IMS.
5914 return;
5915 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005916 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005917 } finally {
5918 Binder.restoreCallingIdentity(identity);
5919 }
5920 }
5921
5922 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005923 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5924 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005925 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005926 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005927 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005928
5929 final long identity = Binder.clearCallingIdentity();
5930 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005931 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005932 // may happen if the device does not support IMS.
5933 return;
5934 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005935 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005936 } finally {
5937 Binder.restoreCallingIdentity(identity);
5938 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005939 }
5940
5941 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005942 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5943 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005944 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005945 public void disableIms(int slotId) {
5946 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005947
5948 final long identity = Binder.clearCallingIdentity();
5949 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005950 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005951 // may happen if the device does not support IMS.
5952 return;
5953 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005954 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005955 } finally {
5956 Binder.restoreCallingIdentity(identity);
5957 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005958 }
5959
5960 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005961 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5962 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005963 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005964 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005965 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005966 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005967
5968 final long identity = Binder.clearCallingIdentity();
5969 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005970 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005971 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5972 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005973 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005974 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005975 } finally {
5976 Binder.restoreCallingIdentity(identity);
5977 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005978 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005979 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005980 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5981 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005982 @Override
5983 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005984 enforceModifyPermission();
5985
5986 final long identity = Binder.clearCallingIdentity();
5987 try {
5988 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005989 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005990 } finally {
5991 Binder.restoreCallingIdentity(identity);
5992 }
5993 }
5994
5995 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005996 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005997 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005998 */
5999 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
6000 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006001
6002 final long identity = Binder.clearCallingIdentity();
6003 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006004 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006005 // may happen if the device does not support IMS.
6006 return null;
6007 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006008 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006009 } finally {
6010 Binder.restoreCallingIdentity(identity);
6011 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006012 }
6013
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006014 /**
6015 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006016 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006017 */
6018 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6019 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006020
6021 final long identity = Binder.clearCallingIdentity();
6022 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006023 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006024 // may happen if the device does not support IMS.
6025 return null;
6026 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006027 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006028 } finally {
6029 Binder.restoreCallingIdentity(identity);
6030 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006031 }
6032
Brad Ebinger884c07b2018-02-15 16:17:40 -08006033 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006034 * Sets the ImsService Package Name that Telephony will bind to.
6035 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006036 * @param slotIndex the slot ID that the ImsService should bind for.
6037 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006038 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006039 * @param featureTypes An integer array of feature types associated with a packageName.
6040 * @param packageName The name of the package that the current configuration will be replaced
6041 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006042 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006043 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006044 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6045 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006046 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006047 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006048 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006049
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006050 final long identity = Binder.clearCallingIdentity();
6051 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006052 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006053 // may happen if the device does not support IMS.
6054 return false;
6055 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006056 Map<Integer, String> featureConfig = new HashMap<>();
6057 for (int featureType : featureTypes) {
6058 featureConfig.put(featureType, packageName);
6059 }
6060 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6061 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006062 } finally {
6063 Binder.restoreCallingIdentity(identity);
6064 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006065 }
6066
6067 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006068 * Clears any carrier ImsService overrides for the slot index specified that were previously
6069 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6070 *
6071 * This should only be used for testing.
6072 *
6073 * @param slotIndex the slot ID that the ImsService should bind for.
6074 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6075 */
6076 @Override
6077 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006078 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6079 "clearCarrierImsServiceOverride");
6080 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006081 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006082
6083 final long identity = Binder.clearCallingIdentity();
6084 try {
6085 if (mImsResolver == null) {
6086 // may happen if the device does not support IMS.
6087 return false;
6088 }
6089 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6090 } finally {
6091 Binder.restoreCallingIdentity(identity);
6092 }
6093 }
6094
6095 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006096 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006097 *
6098 * @param slotId The slot that the ImsService is associated with.
6099 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6100 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006101 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006102 * @return the package name of the ImsService configuration.
6103 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006104 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6105 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006106 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006107 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6108 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006109
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006110 final long identity = Binder.clearCallingIdentity();
6111 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006112 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006113 // may happen if the device does not support IMS.
6114 return "";
6115 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006116 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006117 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6118 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006119 } finally {
6120 Binder.restoreCallingIdentity(identity);
6121 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006122 }
6123
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006124 /**
6125 * Get the MmTelFeature state associated with the requested subscription id.
6126 * @param subId The subscription that the MmTelFeature is associated with.
6127 * @param callback A callback with an integer containing the
6128 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6129 */
6130 @Override
6131 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6132 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006133 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6134 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6135 "IMS not available on device.");
6136 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006137 final long token = Binder.clearCallingIdentity();
6138 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006139 int slotId = getSlotIndex(subId);
6140 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6141 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6142 + subId + "'");
6143 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6144 }
6145 verifyImsMmTelConfiguredOrThrow(slotId);
6146 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6147 try {
6148 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6149 } catch (RemoteException e) {
6150 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6151 + "Ignore");
6152 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006153 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006154 } catch (ImsException e) {
6155 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006156 } finally {
6157 Binder.restoreCallingIdentity(token);
6158 }
6159 }
6160
Daniel Brightbb5840b2021-01-12 15:48:18 -08006161 /**
6162 * Sets the ims registration state on all valid {@link Phone}s.
6163 */
6164 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006165 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006166
6167 final long identity = Binder.clearCallingIdentity();
6168 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006169 // NOTE: Before S, this method only set the default phone.
6170 for (final Phone phone : PhoneFactory.getPhones()) {
6171 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6172 phone.setImsRegistrationState(registered);
6173 }
6174 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006175 } finally {
6176 Binder.restoreCallingIdentity(identity);
6177 }
Wink Saville36469e72014-06-11 15:17:00 -07006178 }
6179
6180 /**
Stuart Scott54788802015-03-30 13:18:01 -07006181 * Set the network selection mode to automatic.
6182 *
6183 */
6184 @Override
6185 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006186 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6187 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006188
6189 final long identity = Binder.clearCallingIdentity();
6190 try {
shilufc958392020-01-20 11:36:01 -08006191 if (!isActiveSubscription(subId)) {
6192 return;
6193 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006194 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006195 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6196 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006197 } finally {
6198 Binder.restoreCallingIdentity(identity);
6199 }
Stuart Scott54788802015-03-30 13:18:01 -07006200 }
6201
Jack Yud10cdd42020-09-28 20:28:01 -07006202 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006203 * Ask the radio to connect to the input network and change selection mode to manual.
6204 *
6205 * @param subId the id of the subscription.
6206 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6207 * the operator to attach to.
6208 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6209 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6210 * normal network selection next time.
6211 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006212 */
6213 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006214 public boolean setNetworkSelectionModeManual(
6215 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006216 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6217 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006218
Jack Yu285100e2022-12-02 22:48:35 -08006219 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006220 if (!isActiveSubscription(subId)) {
6221 return false;
6222 }
6223
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006224 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006225 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006226 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006227 if (DBG) {
6228 log("setNetworkSelectionModeManual: subId: " + subId
6229 + " operator: " + operatorInfo);
6230 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006231 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6232 } finally {
6233 Binder.restoreCallingIdentity(identity);
6234 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006235 }
shilu84f6e8b2019-12-19 13:58:01 -08006236 /**
6237 * Get the manual network selection
6238 *
6239 * @param subId the id of the subscription.
6240 *
6241 * @return the previously saved user selected PLMN
6242 */
6243 @Override
6244 public String getManualNetworkSelectionPlmn(int subId) {
6245 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006246 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006247 mApp, subId, "getManualNetworkSelectionPlmn");
6248
6249 final long identity = Binder.clearCallingIdentity();
6250 try {
6251 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006252 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006253 }
6254
6255 final Phone phone = getPhone(subId);
6256 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006257 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006258 }
6259 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6260 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6261 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6262 } finally {
6263 Binder.restoreCallingIdentity(identity);
6264 }
6265 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006266
6267 /**
6268 * Scans for available networks.
6269 */
6270 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006271 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6272 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006273 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6274 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006275 LocationAccessPolicy.LocationPermissionResult locationResult =
6276 LocationAccessPolicy.checkLocationPermission(mApp,
6277 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6278 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006279 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006280 .setCallingPid(Binder.getCallingPid())
6281 .setCallingUid(Binder.getCallingUid())
6282 .setMethod("getCellNetworkScanResults")
6283 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006284 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6285 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006286 .build());
6287 switch (locationResult) {
6288 case DENIED_HARD:
6289 throw new SecurityException("Not allowed to access scan results -- location");
6290 case DENIED_SOFT:
6291 return null;
6292 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006293
Pengquan Menga1bb6272018-09-06 09:59:22 -07006294 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006295 try {
6296 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006297 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006298 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006299 } finally {
6300 Binder.restoreCallingIdentity(identity);
6301 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006302 }
6303
6304 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006305 * Get the call forwarding info, given the call forwarding reason.
6306 */
6307 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006308 public void getCallForwarding(int subId, int callForwardingReason,
6309 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006310 enforceReadPrivilegedPermission("getCallForwarding");
6311 long identity = Binder.clearCallingIdentity();
6312 try {
6313 if (DBG) {
6314 log("getCallForwarding: subId " + subId
6315 + " callForwardingReason" + callForwardingReason);
6316 }
Hall Liu27d24262020-09-18 19:04:59 -07006317
6318 Phone phone = getPhone(subId);
6319 if (phone == null) {
6320 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006321 callback.onError(
6322 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006323 } catch (RemoteException e) {
6324 // ignore
6325 }
6326 return;
6327 }
6328
6329 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6330 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6331 @Override
6332 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6333 try {
6334 callback.onCallForwardingInfoAvailable(info);
6335 } catch (RemoteException e) {
6336 // ignore
6337 }
6338 }
6339
6340 @Override
6341 public void onError(int error) {
6342 try {
6343 callback.onError(error);
6344 } catch (RemoteException e) {
6345 // ignore
6346 }
6347 }
6348 });
6349 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006350 } finally {
6351 Binder.restoreCallingIdentity(identity);
6352 }
6353 }
6354
6355 /**
6356 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6357 * reason, the number to forward, and the timeout before the forwarding is attempted.
6358 */
6359 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006360 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6361 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006362 enforceModifyPermission();
6363 long identity = Binder.clearCallingIdentity();
6364 try {
6365 if (DBG) {
6366 log("setCallForwarding: subId " + subId
6367 + " callForwardingInfo" + callForwardingInfo);
6368 }
Hall Liu27d24262020-09-18 19:04:59 -07006369
6370 Phone phone = getPhone(subId);
6371 if (phone == null) {
6372 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006373 callback.accept(
6374 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006375 } catch (RemoteException e) {
6376 // ignore
6377 }
6378 return;
6379 }
6380
6381 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6382 FunctionalUtils.ignoreRemoteException(callback::accept));
6383
6384 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006385 } finally {
6386 Binder.restoreCallingIdentity(identity);
6387 }
6388 }
6389
6390 /**
Hall Liu27d24262020-09-18 19:04:59 -07006391 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006392 */
6393 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006394 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006395 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006396 long identity = Binder.clearCallingIdentity();
6397 try {
Hall Liu27d24262020-09-18 19:04:59 -07006398 Phone phone = getPhone(subId);
6399 if (phone == null) {
6400 try {
6401 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6402 } catch (RemoteException e) {
6403 // ignore
6404 }
6405 return;
6406 }
SongFerngWang0e767992021-03-31 22:08:45 +08006407 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6408 PersistableBundle c = configManager.getConfigForSubId(subId);
6409 boolean requireUssd = c.getBoolean(
6410 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006411
Shuo Qian4a594052020-01-23 11:59:30 -08006412 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006413 if (requireUssd) {
6414 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6415 getSubscriptionCarrierId(subId));
6416 String newUssdCommand = "";
6417 try {
6418 newUssdCommand = carrierXmlParser.getFeature(
6419 CarrierXmlParser.FEATURE_CALL_WAITING)
6420 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6421 } catch (NullPointerException e) {
6422 loge("Failed to generate USSD number" + e);
6423 }
6424 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6425 mMainThreadHandler, callback, carrierXmlParser,
6426 CarrierXmlParser.SsEntry.SSAction.QUERY);
6427 final String ussdCommand = newUssdCommand;
6428 Executors.newSingleThreadExecutor().execute(() -> {
6429 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6430 });
6431 } else {
6432 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6433 callback::accept);
6434 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6435 }
Shuo Qian4a594052020-01-23 11:59:30 -08006436 } finally {
6437 Binder.restoreCallingIdentity(identity);
6438 }
6439 }
6440
6441 /**
Hall Liu27d24262020-09-18 19:04:59 -07006442 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006443 */
6444 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006445 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006446 enforceModifyPermission();
6447 long identity = Binder.clearCallingIdentity();
6448 try {
Hall Liu27d24262020-09-18 19:04:59 -07006449 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6450
6451 Phone phone = getPhone(subId);
6452 if (phone == null) {
6453 try {
6454 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6455 } catch (RemoteException e) {
6456 // ignore
6457 }
6458 return;
6459 }
6460
SongFerngWang0e767992021-03-31 22:08:45 +08006461 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6462 PersistableBundle c = configManager.getConfigForSubId(subId);
6463 boolean requireUssd = c.getBoolean(
6464 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006465
SongFerngWang0e767992021-03-31 22:08:45 +08006466 if (DBG) log("getCallWaitingStatus: subId " + subId);
6467 if (requireUssd) {
6468 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6469 getSubscriptionCarrierId(subId));
6470 CarrierXmlParser.SsEntry.SSAction ssAction =
6471 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6472 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6473 String newUssdCommand = "";
6474 try {
6475 newUssdCommand = carrierXmlParser.getFeature(
6476 CarrierXmlParser.FEATURE_CALL_WAITING)
6477 .makeCommand(ssAction, null);
6478 } catch (NullPointerException e) {
6479 loge("Failed to generate USSD number" + e);
6480 }
6481 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6482 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6483 final String ussdCommand = newUssdCommand;
6484 Executors.newSingleThreadExecutor().execute(() -> {
6485 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6486 });
6487 } else {
6488 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6489 FunctionalUtils.ignoreRemoteException(callback::accept));
6490
6491 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6492 }
Shuo Qian4a594052020-01-23 11:59:30 -08006493 } finally {
6494 Binder.restoreCallingIdentity(identity);
6495 }
6496 }
6497
6498 /**
yinxub1bed742017-04-17 11:45:04 -07006499 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006500 *
yinxub1bed742017-04-17 11:45:04 -07006501 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006502 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6503 * location related information which will be sent if the caller already possess
6504 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006505 * @param request contains the radio access networks with bands/channels to scan
6506 * @param messenger callback messenger for scan results or errors
6507 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006508 * @return the id of the requested scan which can be used to stop the scan.
6509 */
6510 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006511 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6512 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006513 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006514 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6515 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006516 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006517 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6518 if (!renounceFineLocationAccess) {
6519 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6520 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6521 .setCallingPackage(callingPackage)
6522 .setCallingFeatureId(callingFeatureId)
6523 .setCallingPid(Binder.getCallingPid())
6524 .setCallingUid(Binder.getCallingUid())
6525 .setMethod("requestNetworkScan")
6526 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6527 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6528 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6529 .build());
6530 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006531 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006532 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6533 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006534 if (e != null) {
6535 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6536 throw e;
6537 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006538 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006539 return TelephonyScanManager.INVALID_SCAN_ID;
6540 }
6541 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006542 }
Hall Liu912dfd32019-04-25 14:02:26 -07006543 int callingUid = Binder.getCallingUid();
6544 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006545 final long identity = Binder.clearCallingIdentity();
6546 try {
6547 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006548 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006549 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006550 } finally {
6551 Binder.restoreCallingIdentity(identity);
6552 }
yinxu504e1392017-04-12 16:03:22 -07006553 }
6554
Hall Liub2ac8ef2019-02-28 15:56:23 -08006555 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006556 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006557 boolean hasCarrierPriv;
6558 final long identity = Binder.clearCallingIdentity();
6559 try {
6560 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6561 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6562 } finally {
6563 Binder.restoreCallingIdentity(identity);
6564 }
Hall Liu558027f2019-05-15 19:14:05 -07006565 boolean hasNetworkScanPermission =
6566 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6567 == PERMISSION_GRANTED;
6568
6569 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6570 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6571 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006572 }
6573
6574 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6575 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006576 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6577 return new SecurityException("Specific channels must not be"
6578 + " scanned without location access.");
6579 }
6580 }
6581 }
6582
Hall Liub2ac8ef2019-02-28 15:56:23 -08006583 return null;
6584 }
6585
yinxu504e1392017-04-12 16:03:22 -07006586 /**
6587 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006588 *
6589 * @param subId id of the subscription
6590 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006591 */
6592 @Override
6593 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006594 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6595 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006596
Hall Liu912dfd32019-04-25 14:02:26 -07006597 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006598 final long identity = Binder.clearCallingIdentity();
6599 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006600 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006601 } finally {
6602 Binder.restoreCallingIdentity(identity);
6603 }
yinxu504e1392017-04-12 16:03:22 -07006604 }
6605
6606 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006607 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006608 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006609 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006610 */
6611 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006612 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006613 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006614 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006615 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006616
6617 final long identity = Binder.clearCallingIdentity();
6618 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006619 if (DBG) log("getAllowedNetworkTypesBitmask");
6620 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6621 int networkTypesBitmask = (result != null ? result[0] : -1);
6622 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6623 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006624 } finally {
6625 Binder.restoreCallingIdentity(identity);
6626 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006627 }
6628
6629 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006630 * Get the allowed network types for certain reason.
6631 *
6632 * @param subId the id of the subscription.
6633 * @param reason the reason the allowed network type change is taking place
6634 * @return the allowed network types.
6635 */
6636 @Override
6637 public long getAllowedNetworkTypesForReason(int subId,
6638 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006639 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006640 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006641 final long identity = Binder.clearCallingIdentity();
6642 try {
6643 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6644 } finally {
6645 Binder.restoreCallingIdentity(identity);
6646 }
6647 }
6648
6649 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006650 * Enable/Disable E-UTRA-NR Dual Connectivity
6651 * @param subId subscription id of the sim card
6652 * @param nrDualConnectivityState expected NR dual connectivity state
6653 * This can be passed following states
6654 * <ol>
6655 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6656 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6657 * <li>Disable NR dual connectivity and force secondary cell to be released
6658 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6659 * </ol>
6660 * @return operation result.
6661 */
6662 @Override
6663 public int setNrDualConnectivityState(int subId,
6664 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6665 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6666 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006667 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006668 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6669 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6670 }
6671
Sooraj Sasindran37444802020-08-11 10:40:43 -07006672 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6673 final long identity = Binder.clearCallingIdentity();
6674 try {
6675 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6676 nrDualConnectivityState, subId,
6677 workSource);
6678 if (DBG) log("enableNRDualConnectivity result: " + result);
6679 return result;
6680 } finally {
6681 Binder.restoreCallingIdentity(identity);
6682 }
6683 }
6684
6685 /**
6686 * Is E-UTRA-NR Dual Connectivity enabled
6687 * @return true if dual connectivity is enabled else false
6688 */
6689 @Override
6690 public boolean isNrDualConnectivityEnabled(int subId) {
6691 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006692 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006693 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006694 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006695 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6696 return false;
6697 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006698 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6699 final long identity = Binder.clearCallingIdentity();
6700 try {
6701 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6702 null, subId, workSource);
6703 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6704 return isEnabled;
6705 } finally {
6706 Binder.restoreCallingIdentity(identity);
6707 }
6708 }
6709
6710 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006711 * Set the allowed network types of the device and
6712 * provide the reason triggering the allowed network change.
6713 *
6714 * @param subId the id of the subscription.
6715 * @param reason the reason the allowed network type change is taking place
6716 * @param allowedNetworkTypes the allowed network types.
6717 * @return true on success; false on any failure.
6718 */
6719 @Override
6720 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006721 @TelephonyManager.AllowedNetworkTypesReason int reason,
6722 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006723 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6724 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006725 // If the caller only has carrier privileges, then they should not be able to override
6726 // any network types which were set for security reasons.
6727 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6728 != PERMISSION_GRANTED
6729 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6730 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6731 throw new SecurityException(
6732 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6733 + " reason "
6734 + reason);
6735 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006736 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006737 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6738 return false;
6739 }
6740 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6741 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006742 return false;
6743 }
6744
Jack Yue37dd262022-12-16 11:53:37 -08006745 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason=" + reason + " value: "
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006746 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6747
Jack Yue37dd262022-12-16 11:53:37 -08006748 Phone phone = getPhone(subId);
6749 if (phone == null) {
6750 return false;
6751 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006752
Jack Yue37dd262022-12-16 11:53:37 -08006753 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006754 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6755 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006756 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006757
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006758 final long identity = Binder.clearCallingIdentity();
6759 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006760 Boolean success = (Boolean) sendRequest(
6761 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6762 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6763
6764 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6765 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006766 } finally {
6767 Binder.restoreCallingIdentity(identity);
6768 }
6769 }
6770
6771 /**
Miaoa84611c2019-03-15 09:21:10 +08006772 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006773 *
Miaoa84611c2019-03-15 09:21:10 +08006774 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006775 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006776 * @hide
6777 */
6778 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006779 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006780 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006781 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006782 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006783 try {
Miaoa84611c2019-03-15 09:21:10 +08006784 if (phone != null) {
6785 return phone.hasMatchedTetherApnSetting();
6786 } else {
6787 return false;
6788 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006789 } finally {
6790 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006791 }
Junda Liu475951f2014-11-07 16:45:03 -08006792 }
6793
6794 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006795 * Get the user enabled state of Mobile Data.
6796 *
6797 * TODO: remove and use isUserDataEnabled.
6798 * This can't be removed now because some vendor codes
6799 * calls through ITelephony directly while they should
6800 * use TelephonyManager.
6801 *
6802 * @return true on enabled
6803 */
6804 @Override
6805 public boolean getDataEnabled(int subId) {
6806 return isUserDataEnabled(subId);
6807 }
6808
6809 /**
6810 * Get whether mobile data is enabled per user setting.
6811 *
6812 * There are other factors deciding whether mobile data is actually enabled, but they are
6813 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006814 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006815 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6816 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006817 *
6818 * @return {@code true} if data is enabled else {@code false}
6819 */
6820 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006821 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006822 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006823 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006824 try {
6825 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6826 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006827 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006828 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6829 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006830 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006831 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006832 mApp, subId, functionName);
6833
Robert Greenwalt646120a2014-05-23 11:54:03 -07006834 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006835
6836 final long identity = Binder.clearCallingIdentity();
6837 try {
Jack Yu285100e2022-12-02 22:48:35 -08006838 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006839 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6840 Phone phone = PhoneFactory.getPhone(phoneId);
6841 if (phone != null) {
6842 boolean retVal = phone.isUserDataEnabled();
6843 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6844 return retVal;
6845 } else {
6846 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6847 return false;
6848 }
6849 } finally {
6850 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006851 }
6852 }
6853
6854 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006855 * Checks if the device is capable of mobile data by considering whether whether the
6856 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6857 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006858 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006859 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006860 */
6861 @Override
6862 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006863 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006864 try {
6865 try {
6866 mApp.enforceCallingOrSelfPermission(
6867 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006868 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006869 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006870 try {
6871 mApp.enforceCallingOrSelfPermission(
6872 android.Manifest.permission.READ_PHONE_STATE,
6873 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006874 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006875 mApp.enforceCallingOrSelfPermission(
6876 permission.READ_BASIC_PHONE_STATE, functionName);
6877 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006878 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006879 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006880 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006881 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006882
6883 final long identity = Binder.clearCallingIdentity();
6884 try {
Jack Yu285100e2022-12-02 22:48:35 -08006885 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006886 Phone phone = PhoneFactory.getPhone(phoneId);
6887 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006888 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006889 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006890 return retVal;
6891 } else {
6892 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6893 return false;
6894 }
6895 } finally {
6896 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006897 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006898 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006899
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006900 /**
6901 * Check if data is enabled for a specific reason
6902 * @param subId Subscription index
6903 * @param reason the reason the data enable change is taking place
6904 * @return {@code true} if the overall data is enabled; {@code false} if not.
6905 */
6906 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006907 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006908 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006909 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006910 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006911 try {
6912 mApp.enforceCallingOrSelfPermission(
6913 android.Manifest.permission.ACCESS_NETWORK_STATE,
6914 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006915 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006916 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6917 functionName);
6918 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006919 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006920 try {
6921 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006922 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006923 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006924 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006925 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006926 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006927 }
6928
6929
6930 final long identity = Binder.clearCallingIdentity();
6931 try {
Jack Yu285100e2022-12-02 22:48:35 -08006932 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006933 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006934 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006935 + " reason=" + reason);
6936 }
6937 Phone phone = PhoneFactory.getPhone(phoneId);
6938 if (phone != null) {
6939 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006940 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006941 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006942 return retVal;
6943 } else {
6944 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006945 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006946 + subId + " retVal=false");
6947 }
6948 return false;
6949 }
6950 } finally {
6951 Binder.restoreCallingIdentity(identity);
6952 }
6953 }
6954
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006955 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006956 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006957 // No permission needed; this only lets the caller inspect their own status.
6958 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006959 }
Junda Liu29340342014-07-10 15:23:27 -07006960
6961 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006962 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006963 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006964 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6965 }
6966
6967 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6968 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006969 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006970 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006971 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6972 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006973 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6974 if (cpt == null) {
6975 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006976 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6977 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006978 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006979 }
6980
6981 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006982 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006983 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006984 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006985 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006986 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006987 Phone phone = getPhone(subId);
6988 if (phone == null) {
6989 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6990 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6991 }
6992 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6993 if (cpt == null) {
6994 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006995 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6996 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006997 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006998 }
6999
7000 @Override
7001 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007002 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007003 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7004 }
7005
7006 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007007 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007008 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007009 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007010 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007011 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7012 Phone phone = PhoneFactory.getPhone(phoneId);
7013 if (phone == null) {
7014 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007015 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007016 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7017 if (cpt == null) {
7018 continue;
7019 }
7020 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007021 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7022 break;
7023 }
7024 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007025 return result;
Junda Liu29340342014-07-10 15:23:27 -07007026 }
Derek Tan89e89d42014-07-08 17:00:10 -07007027
7028 @Override
Junda Liue64de782015-04-16 17:19:16 -07007029 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007030 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007031 Phone phone = PhoneFactory.getPhone(phoneId);
7032 if (phone == null) {
7033 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007034 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007035 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7036 if (cpt == null) {
7037 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007038 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007039 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007040 }
7041
Amith Yamasani6e118872016-02-19 12:53:51 -08007042 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007043 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007044 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007045 Phone phone = PhoneFactory.getPhone(phoneId);
7046 if (phone == null) {
7047 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007048 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007049 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7050 if (cpt == null) {
7051 return Collections.emptyList();
7052 }
7053 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007054 }
7055
chen xuf7e9fe82019-05-09 19:31:02 -07007056 @Override
7057 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007058 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007059 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007060 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007061 try {
7062 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7063 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7064 }
7065 } finally {
7066 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007067 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007068 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007069 }
7070
Rambo Wang6812ffb2022-03-15 16:54:17 -07007071 @Override
7072 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7073 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7074
7075 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7076 if (phone == null) {
7077 return null;
7078 }
7079 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7080 if (cpt == null) {
7081 return null;
7082 }
7083 return cpt.getCarrierServicePackageName();
7084 }
7085
Wink Savilleb564aae2014-10-23 10:18:09 -07007086 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007087 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007088 UiccPort port = phone == null ? null : phone.getUiccPort();
7089 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007090 return null;
7091 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007092 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007093 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007094 return null;
7095 }
7096 return iccId;
7097 }
7098
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007099 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007100 public void setCallComposerStatus(int subId, int status) {
7101 enforceModifyPermission();
7102
7103 final long identity = Binder.clearCallingIdentity();
7104 try {
7105 Phone phone = getPhone(subId);
7106 if (phone != null) {
7107 Phone defaultPhone = phone.getImsPhone();
7108 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7109 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7110 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007111 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7112 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007113 }
7114 }
Shuo Qian284ae752020-12-22 19:10:14 -08007115 } catch (ImsException e) {
7116 throw new ServiceSpecificException(e.getCode());
7117 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007118 Binder.restoreCallingIdentity(identity);
7119 }
7120 }
7121
7122 @Override
7123 public int getCallComposerStatus(int subId) {
7124 enforceReadPrivilegedPermission("getCallComposerStatus");
7125
7126 final long identity = Binder.clearCallingIdentity();
7127 try {
7128 Phone phone = getPhone(subId);
7129 if (phone != null) {
7130 Phone defaultPhone = phone.getImsPhone();
7131 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7132 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7133 return imsPhone.getCallComposerStatus();
7134 }
7135 }
7136 } finally {
7137 Binder.restoreCallingIdentity(identity);
7138 }
7139 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7140 }
7141
7142 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007143 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7144 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007145 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007146 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007147
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007148 final long identity = Binder.clearCallingIdentity();
7149 try {
7150 final String iccId = getIccId(subId);
7151 final Phone phone = getPhone(subId);
7152 if (phone == null) {
7153 return false;
7154 }
7155 final String subscriberId = phone.getSubscriberId();
7156
7157 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007158 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007159 + subscriberId + " to " + number);
7160 }
7161
7162 if (TextUtils.isEmpty(iccId)) {
7163 return false;
7164 }
7165
7166 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7167
7168 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7169 if (alphaTag == null) {
7170 editor.remove(alphaTagPrefKey);
7171 } else {
7172 editor.putString(alphaTagPrefKey, alphaTag);
7173 }
7174
7175 // Record both the line number and IMSI for this ICCID, since we need to
7176 // track all merged IMSIs based on line number
7177 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7178 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7179 if (number == null) {
7180 editor.remove(numberPrefKey);
7181 editor.remove(subscriberPrefKey);
7182 } else {
7183 editor.putString(numberPrefKey, number);
7184 editor.putString(subscriberPrefKey, subscriberId);
7185 }
7186
7187 editor.commit();
7188 return true;
7189 } finally {
7190 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007191 }
Derek Tan7226c842014-07-02 17:42:23 -07007192 }
7193
7194 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007195 public String getLine1NumberForDisplay(int subId, String callingPackage,
7196 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007197 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007198 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007199 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007200 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007201 return null;
7202 }
Derek Tan97ebb422014-09-05 16:55:38 -07007203
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007204 final long identity = Binder.clearCallingIdentity();
7205 try {
7206 String iccId = getIccId(subId);
7207 if (iccId != null) {
7208 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7209 if (DBG_MERGE) {
7210 log("getLine1NumberForDisplay returning "
7211 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7212 }
7213 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007214 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007215 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7216 return null;
7217 } finally {
7218 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007219 }
Derek Tan7226c842014-07-02 17:42:23 -07007220 }
7221
7222 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007223 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7224 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007225 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007226 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007227 return null;
7228 }
Derek Tan97ebb422014-09-05 16:55:38 -07007229
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007230 final long identity = Binder.clearCallingIdentity();
7231 try {
7232 String iccId = getIccId(subId);
7233 if (iccId != null) {
7234 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7235 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7236 }
7237 return null;
7238 } finally {
7239 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007240 }
Derek Tan7226c842014-07-02 17:42:23 -07007241 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007242
7243 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007244 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7245 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007246 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7247 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007248 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007249 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007250 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007251 return null;
7252 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007253
Jordan Liub49b04b2019-05-06 14:45:15 -07007254 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7255 // the process, where TelephonyManager was instantiated.
7256 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007257 final long identity = Binder.clearCallingIdentity();
7258 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007259 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007260 final TelephonyManager tele = TelephonyManager.from(context);
7261 final SubscriptionManager sub = SubscriptionManager.from(context);
7262
7263 // Figure out what subscribers are currently active
7264 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007265
Jordan Liub49b04b2019-05-06 14:45:15 -07007266 // Only consider subs which match the current subId
7267 // This logic can be simplified. See b/131189269 for progress.
7268 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007269 activeSubscriberIds.add(tele.getSubscriberId(subId));
7270 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007271
7272 // First pass, find a number override for an active subscriber
7273 String mergeNumber = null;
7274 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7275 for (String key : prefs.keySet()) {
7276 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7277 final String subscriberId = (String) prefs.get(key);
7278 if (activeSubscriberIds.contains(subscriberId)) {
7279 final String iccId = key.substring(
7280 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7281 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7282 mergeNumber = (String) prefs.get(numberKey);
7283 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007284 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007285 + " for active subscriber " + subscriberId);
7286 }
7287 if (!TextUtils.isEmpty(mergeNumber)) {
7288 break;
7289 }
7290 }
7291 }
7292 }
7293
7294 // Shortcut when no active merged subscribers
7295 if (TextUtils.isEmpty(mergeNumber)) {
7296 return null;
7297 }
7298
7299 // Second pass, find all subscribers under that line override
7300 final ArraySet<String> result = new ArraySet<>();
7301 for (String key : prefs.keySet()) {
7302 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7303 final String number = (String) prefs.get(key);
7304 if (mergeNumber.equals(number)) {
7305 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7306 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7307 final String subscriberId = (String) prefs.get(subscriberKey);
7308 if (!TextUtils.isEmpty(subscriberId)) {
7309 result.add(subscriberId);
7310 }
7311 }
7312 }
7313 }
7314
7315 final String[] resultArray = result.toArray(new String[result.size()]);
7316 Arrays.sort(resultArray);
7317 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007318 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007319 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7320 }
7321 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007322 } finally {
7323 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007324 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007325 }
7326
7327 @Override
zoey chen38003472019-12-13 17:16:31 +08007328 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7329 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007330
7331 final long identity = Binder.clearCallingIdentity();
7332 try {
7333 final TelephonyManager telephonyManager = mApp.getSystemService(
7334 TelephonyManager.class);
7335 String subscriberId = telephonyManager.getSubscriberId(subId);
7336 if (subscriberId == null) {
7337 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007338 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007339 + subId);
7340 }
7341 return null;
7342 }
7343
Jack Yu285100e2022-12-02 22:48:35 -08007344 ParcelUuid groupUuid;
7345 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7346 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7347 .getSubscriptionInfo(subId);
7348 groupUuid = info.getGroupUuid();
7349 } else {
7350 final SubscriptionInfo info = mSubscriptionController
7351 .getSubscriptionInfo(subId);
7352 groupUuid = info.getGroupUuid();
7353 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007354 // If it doesn't belong to any group, return just subscriberId of itself.
7355 if (groupUuid == null) {
7356 return new String[]{subscriberId};
7357 }
7358
7359 // Get all subscriberIds from the group.
7360 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007361 List<SubscriptionInfo> groupInfos;
7362 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7363 groupInfos = SubscriptionManagerService.getInstance()
7364 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7365 mApp.getAttributionTag());
7366 } else {
7367 groupInfos = mSubscriptionController
7368 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7369 mApp.getAttributionTag());
7370 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007371 for (SubscriptionInfo subInfo : groupInfos) {
7372 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7373 if (subscriberId != null) {
7374 mergedSubscriberIds.add(subscriberId);
7375 }
7376 }
7377
7378 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7379 } finally {
7380 Binder.restoreCallingIdentity(identity);
7381
7382 }
7383 }
7384
7385 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007386 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007387 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007388 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007389
7390 final long identity = Binder.clearCallingIdentity();
7391 try {
7392 final Phone phone = getPhone(subId);
7393 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7394 } finally {
7395 Binder.restoreCallingIdentity(identity);
7396 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007397 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007398
7399 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007400 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007401 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7402 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007403 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7404 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007405
7406 final long identity = Binder.clearCallingIdentity();
7407 try {
7408 final Phone phone = getPhone(subId);
7409 if (phone == null) {
7410 return false;
7411 }
7412 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7413 cdmaNonRoamingList);
7414 } finally {
7415 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007416 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007417 }
7418
7419 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007420 @Deprecated
7421 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7422 enforceModifyPermission();
7423
7424 int returnValue = 0;
7425 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007426 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007427 if(result.exception == null) {
7428 if (result.result != null) {
7429 byte[] responseData = (byte[])(result.result);
7430 if(responseData.length > oemResp.length) {
7431 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7432 responseData.length + "bytes. Buffer Size is " +
7433 oemResp.length + "bytes.");
7434 }
7435 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7436 returnValue = responseData.length;
7437 }
7438 } else {
7439 CommandException ex = (CommandException) result.exception;
7440 returnValue = ex.getCommandError().ordinal();
7441 if(returnValue > 0) returnValue *= -1;
7442 }
7443 } catch (RuntimeException e) {
7444 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7445 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7446 if(returnValue > 0) returnValue *= -1;
7447 }
7448
7449 return returnValue;
7450 }
7451
7452 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007453 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007454 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007455 try {
7456 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007457 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007458 mApp, phone.getSubId(), "getRadioAccessFamily");
7459 } catch (SecurityException e) {
7460 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7461 throw e;
7462 }
chen xub97461a2018-10-26 14:17:57 -07007463 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007464 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007465 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007466 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007467 final long identity = Binder.clearCallingIdentity();
7468 try {
chen xub97461a2018-10-26 14:17:57 -07007469 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007470 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007471 mApp, phone.getSubId(), "getRadioAccessFamily");
7472 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007473 } finally {
7474 Binder.restoreCallingIdentity(identity);
7475 }
chen xub97461a2018-10-26 14:17:57 -07007476 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007477 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007478
7479 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007480 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007481 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007482 try {
7483 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7484 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007485 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007486 }
7487 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007488 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007489 }
7490 RoleManager rm = mApp.getSystemService(RoleManager.class);
7491 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7492 if (!dialerRoleHolders.contains(callingPackage)) {
7493 throw new SecurityException("App must be the dialer role holder to"
7494 + " upload a call composer pic");
7495 }
7496
7497 Executors.newSingleThreadExecutor().execute(() -> {
7498 ByteArrayOutputStream output = new ByteArrayOutputStream(
7499 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7500 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7501 boolean readUntilEnd = false;
7502 int totalBytesRead = 0;
7503 byte[] buffer = new byte[16 * 1024];
7504 while (true) {
7505 int numRead;
7506 try {
7507 numRead = input.read(buffer);
7508 } catch (IOException e) {
7509 try {
7510 fd.checkError();
7511 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7512 null);
7513 } catch (IOException e1) {
7514 // This means that the other side closed explicitly with an error. If this
7515 // happens, log and ignore.
7516 loge("Remote end of call composer picture pipe closed: " + e1);
7517 }
7518 break;
7519 }
7520 if (numRead == -1) {
7521 readUntilEnd = true;
7522 break;
7523 }
7524 totalBytesRead += numRead;
7525 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7526 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7527 try {
7528 input.close();
7529 } catch (IOException e) {
7530 // ignore
7531 }
7532 break;
7533 }
7534 output.write(buffer, 0, numRead);
7535 }
7536 // Generally, the remote end will close the file descriptors. The only case where we
7537 // close is above, where the picture size is too big.
7538
7539 try {
7540 fd.checkError();
7541 } catch (IOException e) {
7542 loge("Remote end for call composer closed with an error: " + e);
7543 return;
7544 }
7545
Hall Liuaa4211e2021-01-20 15:43:39 -08007546 if (!readUntilEnd) {
7547 loge("Did not finish reading entire image; aborting");
7548 return;
7549 }
Hall Liu82694d52020-12-11 18:22:04 -08007550
Hall Liuaa4211e2021-01-20 15:43:39 -08007551 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7552 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7553 new CallComposerPictureTransfer.Factory() {},
7554 imageData,
7555 (result) -> {
7556 if (result.first != null) {
7557 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7558 Bundle outputResult = new Bundle();
7559 outputResult.putParcelable(
7560 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7561 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7562 outputResult);
7563 } else {
7564 callback.send(result.second, null);
7565 }
7566 }
7567 );
Hall Liu82694d52020-12-11 18:22:04 -08007568 });
7569 }
7570
7571 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007572 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007573 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007574 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007575
7576 final long identity = Binder.clearCallingIdentity();
7577 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007578 ImsManager.getInstance(defaultPhone.getContext(),
7579 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007580 } finally {
7581 Binder.restoreCallingIdentity(identity);
7582 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007583 }
7584
7585 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007586 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007587 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007588 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7589 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007590 return false;
7591 }
Svet Ganovb320e182015-04-16 12:30:10 -07007592
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007593 final long identity = Binder.clearCallingIdentity();
7594 try {
7595 // Check the user preference and the system-level IMS setting. Even if the user has
7596 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7597 // In the long run, we may instead need to check if there exists a connection service
7598 // which can support video calling.
7599 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007600 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007601 return imsManager.isVtEnabledByPlatform()
7602 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7603 && imsManager.isVtEnabledByUser();
7604 } finally {
7605 Binder.restoreCallingIdentity(identity);
7606 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007607 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007608
Andrew Leea1239f22015-03-02 17:44:07 -08007609 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007610 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7611 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007612 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007613 mApp, subId, callingPackage, callingFeatureId,
7614 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007615 return false;
7616 }
7617
7618 final long identity = Binder.clearCallingIdentity();
7619 try {
7620 CarrierConfigManager configManager =
7621 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007622 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007623 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7624 } finally {
7625 Binder.restoreCallingIdentity(identity);
7626 }
Andrew Leea1239f22015-03-02 17:44:07 -08007627 }
7628
7629 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007630 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007631 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007632 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007633 return false;
7634 }
7635
7636 final long identity = Binder.clearCallingIdentity();
7637 try {
7638 CarrierConfigManager configManager =
7639 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007640 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007641 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7642 } finally {
7643 Binder.restoreCallingIdentity(identity);
7644 }
Andrew Leea1239f22015-03-02 17:44:07 -08007645 }
7646
Andrew Lee9431b832015-03-09 18:46:45 -07007647 @Override
7648 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007649 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007650 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007651 }
7652
7653 @Override
7654 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007655 final long identity = Binder.clearCallingIdentity();
7656 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007657 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007658 } finally {
7659 Binder.restoreCallingIdentity(identity);
7660 }
Andrew Lee9431b832015-03-09 18:46:45 -07007661 }
7662
Hall Liuf6668912018-10-31 17:05:23 -07007663 /**
7664 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7665 * support for the feature and device firmware support.
7666 *
7667 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7668 */
7669 @Override
7670 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007671 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007672 final Phone phone = getPhone(subscriptionId);
7673 if (phone == null) {
7674 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7675 return false;
7676 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007677 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007678 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007679 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7680 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007681 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007682 return isCarrierSupported && isDeviceSupported;
7683 } finally {
7684 Binder.restoreCallingIdentity(identity);
7685 }
Hall Liu98187582018-01-22 19:15:32 -08007686 }
7687
Hall Liuf6668912018-10-31 17:05:23 -07007688 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007689 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7690 * RTT setting, will return true if the device and carrier both support RTT.
7691 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007692 */
7693 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007694 final long identity = Binder.clearCallingIdentity();
7695 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007696 boolean isRttSupported = isRttSupported(subscriptionId);
7697 boolean isUserRttSettingOn = Settings.Secure.getInt(
7698 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7699 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7700 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7701 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007702 } finally {
7703 Binder.restoreCallingIdentity(identity);
7704 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007705 }
7706
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007707 @Deprecated
7708 @Override
7709 public String getDeviceId(String callingPackage) {
7710 return getDeviceIdWithFeature(callingPackage, null);
7711 }
7712
Sanket Padawe7310cc72015-01-14 09:53:20 -08007713 /**
7714 * Returns the unique device ID of phone, for example, the IMEI for
7715 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7716 *
7717 * <p>Requires Permission:
7718 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7719 */
7720 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007721 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007722 try {
7723 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7724 } catch (SecurityException se) {
7725 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7726 throw new SecurityException("Package " + callingPackage + " does not belong to "
7727 + Binder.getCallingUid());
7728 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007729 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007730 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007731 return null;
7732 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007733 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007734 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007735 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007736 return null;
7737 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007738
7739 final long identity = Binder.clearCallingIdentity();
7740 try {
7741 return phone.getDeviceId();
7742 } finally {
7743 Binder.restoreCallingIdentity(identity);
7744 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007745 }
7746
Ping Sunc67b7c22016-03-02 19:16:45 +08007747 /**
7748 * {@hide}
7749 * Returns the IMS Registration Status on a particular subid
7750 *
7751 * @param subId
7752 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007753 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007754 Phone phone = getPhone(subId);
7755 if (phone != null) {
7756 return phone.isImsRegistered();
7757 } else {
7758 return false;
7759 }
7760 }
7761
Santos Cordon7a1885b2015-02-03 11:15:19 -08007762 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007763 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007764 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007765 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007766 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007767 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7768 }
7769 final long identity = Binder.clearCallingIdentity();
7770 try {
7771 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7772 } finally {
7773 Binder.restoreCallingIdentity(identity);
7774 }
7775 }
7776
7777 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007778 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007779 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007780 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007781 mApp,
7782 subscriptionId,
7783 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007784 final long identity = Binder.clearCallingIdentity();
7785 try {
7786 Phone phone = getPhone(subscriptionId);
7787 if (phone == null) {
7788 return null;
7789 }
7790 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7791 } finally {
7792 Binder.restoreCallingIdentity(identity);
7793 }
7794 }
7795
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007796 /**
7797 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007798 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007799 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007800 final long identity = Binder.clearCallingIdentity();
7801 try {
7802 Phone phone = getPhone(subId);
7803 if (phone != null) {
7804 return phone.isWifiCallingEnabled();
7805 } else {
7806 return false;
7807 }
7808 } finally {
7809 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007810 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007811 }
7812
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007813 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007814 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007815 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007816 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007817 final long identity = Binder.clearCallingIdentity();
7818 try {
7819 Phone phone = getPhone(subId);
7820 if (phone != null) {
7821 return phone.isVideoEnabled();
7822 } else {
7823 return false;
7824 }
7825 } finally {
7826 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007827 }
7828 }
7829
7830 /**
7831 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7832 * defined in {@link ImsRegistrationImplBase}.
7833 */
7834 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007835 final long identity = Binder.clearCallingIdentity();
7836 try {
7837 Phone phone = getPhone(subId);
7838 if (phone != null) {
7839 return phone.getImsRegistrationTech();
7840 } else {
7841 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7842 }
7843 } finally {
7844 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007845 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007846 }
7847
Stuart Scott8eef64f2015-04-08 15:13:54 -07007848 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007849 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007850 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007851 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7852 return;
7853 }
Kai Shif70f46f2021-03-03 13:59:46 -08007854 Phone defaultPhone = getDefaultPhone();
7855 if (defaultPhone != null) {
7856 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7857 mApp, getDefaultPhone().getSubId(), "factoryReset");
7858 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007859 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007860
Svet Ganovcc087f82015-05-12 20:35:54 -07007861 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007862 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7863 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007864 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007865 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007866 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007867 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007868 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007869 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007870 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007871 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007872 // There has been issues when Sms raw table somehow stores orphan
7873 // fragments. They lead to garbled message when new fragments come
7874 // in and combined with those stale ones. In case this happens again,
7875 // user can reset all network settings which will clean up this table.
7876 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007877 // Clean up IMS settings as well here.
7878 int slotId = getSlotIndex(subId);
7879 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7880 ImsManager.getInstance(mApp, slotId).factoryReset();
7881 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007882
Kai Shif70f46f2021-03-03 13:59:46 -08007883 if (defaultPhone == null) {
7884 return;
7885 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007886 // Erase modem config if erase modem on network setting is enabled.
7887 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7888 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7889 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007890 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007891 }
Kai Shif70f46f2021-03-03 13:59:46 -08007892
7893 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007894 } finally {
7895 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007896 }
7897 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007898
SongFerngWangfd89b102021-05-27 22:44:54 +08007899 @VisibleForTesting
7900 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7901 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7902 return;
7903 }
7904 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7905 RILConstants.PREFERRED_NETWORK_MODE);
7906 SubscriptionManager.setSubscriptionProperty(subId,
7907 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7908 "user=" + defaultNetworkType);
7909 phone.loadAllowedNetworksFromSubscriptionDatabase();
7910 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7911 defaultNetworkType, null);
7912 }
7913
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007914 private void cleanUpSmsRawTable(Context context) {
7915 ContentResolver resolver = context.getContentResolver();
7916 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7917 resolver.delete(uri, null, null);
7918 }
7919
Narayan Kamath1c496c22015-04-16 14:40:19 +01007920 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007921 public String getSimLocaleForSubscriber(int subId) {
7922 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7923 final Phone phone = getPhone(subId);
7924 if (phone == null) {
7925 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007926 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007927 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007928 final long identity = Binder.clearCallingIdentity();
7929 try {
Jack Yu285100e2022-12-02 22:48:35 -08007930 SubscriptionInfo info;
7931 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7932 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7933 phone.getContext().getOpPackageName(),
7934 phone.getContext().getAttributionTag());
7935 if (info == null) {
7936 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7937 return null;
7938 }
7939 } else {
7940 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7941 phone.getContext().getOpPackageName(),
7942 phone.getContext().getAttributionTag());
7943 if (info == null) {
7944 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7945 return null;
7946 }
chen xu6291c472019-02-04 12:55:53 -08007947 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007948 // Try and fetch the locale from the carrier properties or from the SIM language
7949 // preferences (EF-PL and EF-LI)...
7950 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007951 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007952 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7953 if (localeFromDefaultSim != null) {
7954 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7955 if (DBG) log("Using locale from subId: " + subId + " locale: "
7956 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007957 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007958 } else {
7959 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007960 }
7961 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007962
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007963 // The SIM language preferences only store a language (e.g. fr = French), not an
7964 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7965 // the SIM and carrier preferences does not include a country we add the country
7966 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007967 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007968 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007969 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08007970 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007971 }
7972
7973 if (DBG) log("No locale found - returning null");
7974 return null;
7975 } finally {
7976 Binder.restoreCallingIdentity(identity);
7977 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007978 }
7979
tom hsu0b59d292022-09-29 23:49:21 +08007980 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08007981 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08007982 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08007983 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08007984 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08007985 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
7986 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08007987 Locale.forLanguageTag(localeTag).getCountry())) {
7988 return localeTag;
7989 }
7990 }
7991 return inputLocale.toLanguageTag();
7992 }
7993
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007994 /**
7995 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7996 */
7997 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08007998 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7999 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
8000 mApp.getOpPackageName(), mApp.getAttributionTag());
8001 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008002 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008003 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008004 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008005
Gary Jian3aa9a762022-01-24 16:41:19 +08008006 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8007 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008008
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008009 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008010 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8011 * representing the state of the modem.
8012 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008013 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8014 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008015 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008016 */
8017 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008018 public void requestModemActivityInfo(ResultReceiver result) {
8019 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008020 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008021
8022 final long identity = Binder.clearCallingIdentity();
8023 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008024 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008025 } finally {
8026 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008027 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008028 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008029
Gary Jian76280a42022-12-07 16:18:33 +08008030 // Checks that ModemActivityInfo is valid. Sleep time and Idle time should be
Siddharth Rayb8114062018-06-17 15:02:38 -07008031 // less than total activity duration.
8032 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8033 if (info == null) {
8034 return false;
8035 }
8036 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008037 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
Gary Jian76280a42022-12-07 16:18:33 +08008038 activityDurationMs += MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS;
8039
Hall Liu49656c02020-10-09 19:00:11 -07008040 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8041
Siddharth Rayb8114062018-06-17 15:02:38 -07008042 return (info.isValid()
8043 && (info.getSleepTimeMillis() <= activityDurationMs)
Gary Jian76280a42022-12-07 16:18:33 +08008044 && (info.getIdleTimeMillis() <= activityDurationMs));
Siddharth Rayb8114062018-06-17 15:02:38 -07008045 }
8046
Gary Jian3aa9a762022-01-24 16:41:19 +08008047 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8048 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8049 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8050 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8051
8052 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8053 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8054 }
8055
8056 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8057 mLastModemActivityInfo.setReceiveTimeMillis(
8058 rat,
8059 freq,
8060 info.getReceiveTimeMillis(rat, freq)
8061 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8062 }
8063
8064 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8065 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8066 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8067 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8068
8069 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8070 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8071 }
8072 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8073 mLastModemActivityInfo.setReceiveTimeMillis(
8074 rat,
8075 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8076 }
8077
8078 /**
8079 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8080 * @param info recent ModemActivityInfo
8081 */
8082 private void mergeModemActivityInfo(ModemActivityInfo info) {
8083 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008084 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008085 boolean matched;
8086 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8087 matched = false;
8088 int rat = info.getSpecificInfoRat(i);
8089 int freq = info.getSpecificInfoFrequencyRange(i);
8090 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8091 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8092 //if it already exists
8093 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8094 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8095 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8096 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8097 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8098 updateLastModemActivityInfo(info, rat, freq);
8099 matched = true;
8100 }
8101 } else {
8102 updateLastModemActivityInfo(info, rat);
8103 matched = true;
8104 }
8105 }
8106 }
8107
8108 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008109 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008110 new ActivityStatsTechSpecificInfo(
8111 rat,
8112 freq,
8113 info.getTransmitTimeMillis(rat, freq),
8114 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008115 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008116 }
8117 }
8118 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8119 mLastModemActivitySpecificInfo =
8120 new ActivityStatsTechSpecificInfo[merged.size()];
8121 merged.toArray(mLastModemActivitySpecificInfo);
8122
8123 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8124 mLastModemActivityInfo.setSleepTimeMillis(
8125 info.getSleepTimeMillis()
8126 + mLastModemActivityInfo.getSleepTimeMillis());
8127 mLastModemActivityInfo.setIdleTimeMillis(
8128 info.getIdleTimeMillis()
8129 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008130
8131 mLastModemActivityInfo =
8132 new ModemActivityInfo(
8133 mLastModemActivityInfo.getTimestampMillis(),
8134 mLastModemActivityInfo.getSleepTimeMillis(),
8135 mLastModemActivityInfo.getIdleTimeMillis(),
8136 mLastModemActivitySpecificInfo);
8137 }
8138
8139 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8140 ActivityStatsTechSpecificInfo[] info) {
8141 int infoSize = info.length;
8142 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8143 for (int i = 0; i < infoSize; i++) {
8144 ret[i] = new ActivityStatsTechSpecificInfo(
8145 info[i].getRat(), info[i].getFrequencyRange(),
8146 info[i].getTransmitTimeMillis(),
8147 (int) info[i].getReceiveTimeMillis());
8148 }
8149 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008150 }
8151
Jack Yu85bd38a2015-11-09 11:34:32 -08008152 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008153 * Returns the service state information on specified subscription.
8154 */
8155 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008156 public ServiceState getServiceStateForSubscriber(int subId,
8157 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8158 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008159 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008160 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008161 return null;
8162 }
8163
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008164 boolean hasFinePermission = false;
8165 boolean hasCoarsePermission = false;
8166 if (!renounceFineLocationAccess) {
8167 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8168 LocationAccessPolicy.checkLocationPermission(mApp,
8169 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8170 .setCallingPackage(callingPackage)
8171 .setCallingFeatureId(callingFeatureId)
8172 .setCallingPid(Binder.getCallingPid())
8173 .setCallingUid(Binder.getCallingUid())
8174 .setMethod("getServiceStateForSubscriber")
8175 .setLogAsInfo(true)
8176 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8177 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8178 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8179 .build());
8180 hasFinePermission =
8181 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8182 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008183
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008184 if (!renounceCoarseLocationAccess) {
8185 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8186 LocationAccessPolicy.checkLocationPermission(mApp,
8187 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8188 .setCallingPackage(callingPackage)
8189 .setCallingFeatureId(callingFeatureId)
8190 .setCallingPid(Binder.getCallingPid())
8191 .setCallingUid(Binder.getCallingUid())
8192 .setMethod("getServiceStateForSubscriber")
8193 .setLogAsInfo(true)
8194 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8195 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8196 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8197 .build());
8198 hasCoarsePermission =
8199 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8200 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008201
Jack Yu479f40e2020-10-27 21:29:25 -07008202 final Phone phone = getPhone(subId);
8203 if (phone == null) {
8204 return null;
8205 }
8206
Jordan Liu0f2bc442020-11-18 16:47:37 -08008207 final long identity = Binder.clearCallingIdentity();
8208
Jack Yu479f40e2020-10-27 21:29:25 -07008209 boolean isCallingPackageDataService = phone.getDataServicePackages()
8210 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008211 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008212 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008213 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8214 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8215 .getSubscriptionInfoInternal(subId);
8216 if (subInfo == null || !subInfo.isActive()) {
8217 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8218 + "subId=" + subId);
8219 return null;
8220 }
8221 } else {
8222 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8223 callingFeatureId)) {
8224 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8225 + "subId=" + subId);
8226 return null;
8227 }
Jordan Liuc437b192020-08-17 10:59:12 -07008228 }
8229
Hall Liuf19c44f2018-11-27 14:38:17 -08008230 ServiceState ss = phone.getServiceState();
8231
8232 // Scrub out the location info in ServiceState depending on what level of access
8233 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008234 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008235 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8236 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008237 } finally {
8238 Binder.restoreCallingIdentity(identity);
8239 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008240 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008241
8242 /**
8243 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8244 *
8245 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8246 * voicemail ringtone.
8247 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8248 * PhoneAccount.
8249 */
8250 @Override
8251 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008252 final long identity = Binder.clearCallingIdentity();
8253 try {
8254 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8255 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008256 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008257 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008258
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008259 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8260 } finally {
8261 Binder.restoreCallingIdentity(identity);
8262 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008263 }
8264
8265 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008266 * Sets the per-account voicemail ringtone.
8267 *
8268 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8269 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8270 *
8271 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8272 * voicemail ringtone.
8273 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8274 * PhoneAccount.
8275 */
8276 @Override
8277 public void setVoicemailRingtoneUri(String callingPackage,
8278 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008279 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008280 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008281 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8282 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008283 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8284 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8285 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008286 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008287
8288 final long identity = Binder.clearCallingIdentity();
8289 try {
8290 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8291 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008292 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008293 }
8294 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8295 } finally {
8296 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008297 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008298 }
8299
8300 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008301 * Returns whether vibration is set for voicemail notification in Phone settings.
8302 *
8303 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8304 * voicemail vibration setting.
8305 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8306 */
8307 @Override
8308 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008309 final long identity = Binder.clearCallingIdentity();
8310 try {
8311 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8312 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008313 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008314 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008315
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008316 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8317 } finally {
8318 Binder.restoreCallingIdentity(identity);
8319 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008320 }
8321
Youhan Wange64578a2016-05-02 15:32:42 -07008322 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008323 * Sets the per-account voicemail vibration.
8324 *
8325 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8326 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8327 *
8328 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8329 * voicemail vibration setting.
8330 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8331 * specific PhoneAccount.
8332 */
8333 @Override
8334 public void setVoicemailVibrationEnabled(String callingPackage,
8335 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008336 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008337 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008338 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8339 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008340 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8341 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8342 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008343 }
8344
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008345 final long identity = Binder.clearCallingIdentity();
8346 try {
8347 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8348 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008349 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008350 }
8351 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8352 } finally {
8353 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008354 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008355 }
8356
8357 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008358 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8359 *
8360 * @throws SecurityException if the caller does not have the required permission
8361 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008362 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008363 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008364 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008365 }
8366
8367 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008368 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8369 * permission.
8370 *
8371 * @throws SecurityException if the caller does not have the required permission
8372 */
8373 private void enforceSendSmsPermission() {
8374 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8375 }
8376
8377 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008378 * Make sure either called from same process as self (phone) or IPC caller has interact across
8379 * users permission.
8380 *
8381 * @throws SecurityException if the caller does not have the required permission
8382 */
8383 private void enforceInteractAcrossUsersPermission(String message) {
8384 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8385 }
8386
8387 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008388 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008389 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008390 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008391 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008392 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008393 final long identity = Binder.clearCallingIdentity();
8394 try {
8395 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008396 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008397 if (componentName == null) {
8398 throw new SecurityException(
8399 "Caller not current active visual voicemail package[null]");
8400 }
8401 String vvmPackage = componentName.getPackageName();
8402 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008403 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008404 }
8405 } finally {
8406 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008407 }
8408 }
8409
8410 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008411 * Return the application ID for the app type.
8412 *
8413 * @param subId the subscription ID that this request applies to.
8414 * @param appType the uicc app type.
8415 * @return Application ID for specificied app type, or null if no uicc.
8416 */
8417 @Override
8418 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008419 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008420 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008421
8422 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008423 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008424 if (phone == null) {
8425 return null;
8426 }
8427 String aid = null;
8428 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008429 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008430 .getApplicationByType(appType).getAid();
8431 } catch (Exception e) {
8432 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8433 }
8434 return aid;
8435 } finally {
8436 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008437 }
Youhan Wange64578a2016-05-02 15:32:42 -07008438 }
8439
Youhan Wang4001d252016-05-11 10:29:41 -07008440 /**
8441 * Return the Electronic Serial Number.
8442 *
8443 * @param subId the subscription ID that this request applies to.
8444 * @return ESN or null if error.
8445 */
8446 @Override
8447 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008448 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008449 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008450
8451 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008452 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008453 if (phone == null) {
8454 return null;
8455 }
8456 String esn = null;
8457 try {
8458 esn = phone.getEsn();
8459 } catch (Exception e) {
8460 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8461 }
8462 return esn;
8463 } finally {
8464 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008465 }
Youhan Wang4001d252016-05-11 10:29:41 -07008466 }
8467
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008468 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008469 * Return the Preferred Roaming List Version.
8470 *
8471 * @param subId the subscription ID that this request applies to.
8472 * @return PRLVersion or null if error.
8473 */
8474 @Override
8475 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008476 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008477 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008478
8479 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008480 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008481 if (phone == null) {
8482 return null;
8483 }
8484 String cdmaPrlVersion = null;
8485 try {
8486 cdmaPrlVersion = phone.getCdmaPrlVersion();
8487 } catch (Exception e) {
8488 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8489 }
8490 return cdmaPrlVersion;
8491 } finally {
8492 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008493 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008494 }
8495
8496 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008497 * Get snapshot of Telephony histograms
8498 * @return List of Telephony histograms
8499 * @hide
8500 */
8501 @Override
8502 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008503 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8504 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008505
8506 final long identity = Binder.clearCallingIdentity();
8507 try {
8508 return RIL.getTelephonyRILTimingHistograms();
8509 } finally {
8510 Binder.restoreCallingIdentity(identity);
8511 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008512 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008513
8514 /**
8515 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008516 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8517 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008518 * Require system privileges. In the future we may add this to carrier APIs.
8519 *
Michele Berionne482f8202018-11-27 18:57:59 -08008520 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008521 */
8522 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008523 @TelephonyManager.SetCarrierRestrictionResult
8524 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008525 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008526 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008527
Michele Berionne482f8202018-11-27 18:57:59 -08008528 if (carrierRestrictionRules == null) {
8529 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008530 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008531
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008532 final long identity = Binder.clearCallingIdentity();
8533 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008534 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008535 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008536 } finally {
8537 Binder.restoreCallingIdentity(identity);
8538 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008539 }
8540
8541 /**
8542 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008543 * Get the allowed carrier list and the excluded carrier list, including the priority between
8544 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008545 * Require system privileges. In the future we may add this to carrier APIs.
8546 *
Michele Berionne482f8202018-11-27 18:57:59 -08008547 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008548 */
8549 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008550 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008551 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008552 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008553
8554 final long identity = Binder.clearCallingIdentity();
8555 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008556 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8557 if (response instanceof CarrierRestrictionRules) {
8558 return (CarrierRestrictionRules) response;
8559 }
8560 // Response is an Exception of some kind,
8561 // which is signalled to the user as a NULL retval
8562 return null;
8563 } catch (Exception e) {
8564 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8565 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008566 } finally {
8567 Binder.restoreCallingIdentity(identity);
8568 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008569 }
8570
fionaxu59545b42016-05-25 15:53:37 -07008571 /**
fionaxu59545b42016-05-25 15:53:37 -07008572 * Action set from carrier signalling broadcast receivers to enable/disable radio
8573 * @param subId the subscription ID that this action applies to.
8574 * @param enabled control enable or disable radio.
8575 * {@hide}
8576 */
8577 @Override
8578 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8579 enforceModifyPermission();
8580 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008581
8582 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008583 if (phone == null) {
8584 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8585 return;
8586 }
8587 try {
8588 phone.carrierActionSetRadioEnabled(enabled);
8589 } catch (Exception e) {
8590 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008591 } finally {
8592 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008593 }
8594 }
8595
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008596 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008597 * Enable or disable Voice over NR (VoNR)
8598 * @param subId the subscription ID that this action applies to.
8599 * @param enabled enable or disable VoNR.
8600 * @return operation result.
8601 */
8602 @Override
8603 public int setVoNrEnabled(int subId, boolean enabled) {
8604 enforceModifyPermission();
8605 final Phone phone = getPhone(subId);
8606
8607 final long identity = Binder.clearCallingIdentity();
8608 if (phone == null) {
8609 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8610 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8611 }
8612
8613 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8614 try {
8615 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8616 workSource);
8617 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008618
8619 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8620 if (DBG) {
8621 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8622 }
8623 SubscriptionManager.setSubscriptionProperty(
8624 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8625 (enabled ? "1" : "0"));
8626 }
8627
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008628 return result;
8629 } finally {
8630 Binder.restoreCallingIdentity(identity);
8631 }
8632 }
8633
8634 /**
8635 * Is voice over NR enabled
8636 * @return true if VoNR is enabled else false
8637 */
8638 @Override
8639 public boolean isVoNrEnabled(int subId) {
8640 enforceReadPrivilegedPermission("isVoNrEnabled");
8641 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8642 final long identity = Binder.clearCallingIdentity();
8643 try {
8644 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8645 null, subId, workSource);
8646 if (DBG) log("isVoNrEnabled: " + isEnabled);
8647 return isEnabled;
8648 } finally {
8649 Binder.restoreCallingIdentity(identity);
8650 }
8651 }
8652
8653 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008654 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8655 * network status based on which carrier apps could apply actions accordingly,
8656 * enable/disable default url handler for example.
8657 *
8658 * @param subId the subscription ID that this action applies to.
8659 * @param report control start/stop reporting the default network status.
8660 * {@hide}
8661 */
8662 @Override
8663 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8664 enforceModifyPermission();
8665 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008666
8667 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008668 if (phone == null) {
8669 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8670 return;
8671 }
8672 try {
8673 phone.carrierActionReportDefaultNetworkStatus(report);
8674 } catch (Exception e) {
8675 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008676 } finally {
8677 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008678 }
8679 }
8680
8681 /**
fionaxud9622282017-07-17 17:51:30 -07008682 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8683 * @param subId the subscription ID that this action applies to.
8684 * {@hide}
8685 */
8686 @Override
8687 public void carrierActionResetAll(int subId) {
8688 enforceModifyPermission();
8689 final Phone phone = getPhone(subId);
8690 if (phone == null) {
8691 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8692 return;
8693 }
8694 try {
8695 phone.carrierActionResetAll();
8696 } catch (Exception e) {
8697 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8698 }
8699 }
8700
8701 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008702 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8703 * bug report is being generated.
8704 */
8705 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008706 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008707 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8708 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008709 writer.println("Permission Denial: can't dump Phone from pid="
8710 + Binder.getCallingPid()
8711 + ", uid=" + Binder.getCallingUid()
8712 + "without permission "
8713 + android.Manifest.permission.DUMP);
8714 return;
8715 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008716 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008717 }
Jack Yueb89b242016-06-22 13:27:47 -07008718
Brad Ebingerdac2f002018-04-03 15:17:52 -07008719 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008720 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8721 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8722 @NonNull String[] args) {
8723 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8724 this, in.getFileDescriptor(), out.getFileDescriptor(),
8725 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008726 }
8727
Jack Yueb89b242016-06-22 13:27:47 -07008728 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008729 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008730 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008731 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008732 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008733 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008734 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008735 */
8736 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008737 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008738 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008739 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8740 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8741 try {
8742 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008743 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008744 } catch (SecurityException se) {
8745 enforceModifyPermission();
8746 }
8747 } else {
8748 enforceModifyPermission();
8749 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008750
8751 final long identity = Binder.clearCallingIdentity();
8752 try {
8753 Phone phone = getPhone(subId);
8754 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008755 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8756 phone.carrierActionSetMeteredApnsEnabled(enabled);
8757 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008758 phone.getDataSettingsManager().setDataEnabled(
8759 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008760 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008761 }
8762 } finally {
8763 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008764 }
8765 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008766
8767 /**
8768 * Get Client request stats
8769 * @return List of Client Request Stats
8770 * @hide
8771 */
8772 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008773 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8774 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008775 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008776 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008777 return null;
8778 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008779 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008780
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008781 final long identity = Binder.clearCallingIdentity();
8782 try {
8783 if (phone != null) {
8784 return phone.getClientRequestStats();
8785 }
8786
8787 return null;
8788 } finally {
8789 Binder.restoreCallingIdentity(identity);
8790 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008791 }
8792
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008793 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008794 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008795 if (uid == Process.ROOT_UID && packageName == null) {
8796 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8797 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8798 // exception. ROOT_UID seems not to have a valid package name returned by
8799 // PackageManager, so just fake it here to avoid issues when running telephony shell
8800 // commands that plumb through the RIL as root, like so:
8801 // $ adb root
8802 // $ adb shell cmd phone ...
8803 packageName = "root";
8804 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008805 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008806 }
Jack Yueb4124c2017-02-16 15:32:43 -08008807
8808 /**
Grace Chen70990072017-03-24 17:21:30 -07008809 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008810 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008811 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008812 * @param state State of SIM (power down, power up, pass through)
8813 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8814 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8815 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008816 *
8817 **/
8818 @Override
Grace Chen70990072017-03-24 17:21:30 -07008819 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008820 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008821 Phone phone = PhoneFactory.getPhone(slotIndex);
8822
vagdeviaf9a5b92018-08-15 16:01:53 -07008823 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8824
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008825 final long identity = Binder.clearCallingIdentity();
8826 try {
8827 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008828 phone.setSimPowerState(state, null, workSource);
8829 }
8830 } finally {
8831 Binder.restoreCallingIdentity(identity);
8832 }
8833 }
8834
8835 /**
8836 * Set SIM card power state.
8837 *
8838 * @param slotIndex SIM slot id.
8839 * @param state State of SIM (power down, power up, pass through)
8840 * @param callback callback to trigger after success or failure
8841 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8842 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8843 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8844 *
8845 **/
8846 @Override
8847 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8848 IIntegerConsumer callback) {
8849 enforceModifyPermission();
8850 Phone phone = PhoneFactory.getPhone(slotIndex);
8851
8852 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8853
8854 final long identity = Binder.clearCallingIdentity();
8855 try {
8856 if (phone != null) {
8857 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8858 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008859 }
8860 } finally {
8861 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008862 }
8863 }
Shuo Qiandd210312017-04-12 22:11:33 +00008864
Tyler Gunn65d45c22017-06-05 11:22:26 -07008865 private boolean isUssdApiAllowed(int subId) {
8866 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008867 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008868 if (configManager == null) {
8869 return false;
8870 }
8871 PersistableBundle pb = configManager.getConfigForSubId(subId);
8872 if (pb == null) {
8873 return false;
8874 }
8875 return pb.getBoolean(
8876 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8877 }
8878
Shuo Qiandd210312017-04-12 22:11:33 +00008879 /**
8880 * Check if phone is in emergency callback mode
8881 * @return true if phone is in emergency callback mode
8882 * @param subId sub id
8883 */
goneil9c5f4872017-12-05 14:07:56 -08008884 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008885 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008886 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008887 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008888
8889 final long identity = Binder.clearCallingIdentity();
8890 try {
8891 if (phone != null) {
8892 return phone.isInEcm();
8893 } else {
8894 return false;
8895 }
8896 } finally {
8897 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008898 }
8899 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008900
8901 /**
8902 * Get the current signal strength information for the given subscription.
8903 * Because this information is not updated when the device is in a low power state
8904 * it should not be relied-upon to be current.
8905 * @param subId Subscription index
8906 * @return the most recent cached signal strength info from the modem
8907 */
8908 @Override
8909 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008910 final long identity = Binder.clearCallingIdentity();
8911 try {
8912 Phone p = getPhone(subId);
8913 if (p == null) {
8914 return null;
8915 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008916
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008917 return p.getSignalStrength();
8918 } finally {
8919 Binder.restoreCallingIdentity(identity);
8920 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008921 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008922
Pengquan Meng77b7f132018-08-22 14:49:57 -07008923 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008924 * Get the current modem radio state for the given slot.
8925 * @param slotIndex slot index.
8926 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008927 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008928 * @return the current radio power state from the modem
8929 */
8930 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008931 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008932 Phone phone = PhoneFactory.getPhone(slotIndex);
8933 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008934 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8935 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008936 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8937 }
8938
8939 final long identity = Binder.clearCallingIdentity();
8940 try {
8941 return phone.getRadioPowerState();
8942 } finally {
8943 Binder.restoreCallingIdentity(identity);
8944 }
8945 }
8946 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8947 }
8948
8949 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008950 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8951 *
8952 * <p>Requires one of the following permissions:
8953 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008954 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008955 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8956 * privileges.
8957 *
8958 * @param subId subscription id
8959 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8960 * {@code false}.
8961 */
8962 @Override
8963 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008964 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008965 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008966 try {
8967 mApp.enforceCallingOrSelfPermission(
8968 android.Manifest.permission.ACCESS_NETWORK_STATE,
8969 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008970 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008971 mApp.enforceCallingOrSelfPermission(
8972 permission.READ_BASIC_PHONE_STATE, functionName);
8973 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008974 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008975 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008976 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008977 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008978
Pengquan Menga1bb6272018-09-06 09:59:22 -07008979 boolean isEnabled = false;
8980 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008981 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008982 Phone phone = getPhone(subId);
8983 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008984 } finally {
8985 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008986 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008987 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008988 }
8989
8990
8991 /**
8992 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8993 *
8994 * <p> Requires permission:
8995 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8996 * privileges.
8997 *
8998 * @param subId subscription id
8999 * @param isEnabled {@code true} means enable, {@code false} means disable.
9000 */
9001 @Override
9002 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009003 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9004 mApp, subId, "setDataRoamingEnabled");
9005
Pengquan Menga1bb6272018-09-06 09:59:22 -07009006 final long identity = Binder.clearCallingIdentity();
9007 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009008 Phone phone = getPhone(subId);
9009 if (phone != null) {
9010 phone.setDataRoamingEnabled(isEnabled);
9011 }
9012 } finally {
9013 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009014 }
9015 }
9016
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009017 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009018 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009019 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009020 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009021 mApp, subId, "isManualNetworkSelectionAllowed");
9022
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009023 boolean isAllowed = true;
9024 final long identity = Binder.clearCallingIdentity();
9025 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009026 Phone phone = getPhone(subId);
9027 if (phone != null) {
9028 isAllowed = phone.isCspPlmnEnabled();
9029 }
9030 } finally {
9031 Binder.restoreCallingIdentity(identity);
9032 }
9033 return isAllowed;
9034 }
9035
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009036 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9037 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009038 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009039 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009040 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009041 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9042 if (phone == null) {
9043 return false;
9044 }
9045 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9046 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9047 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009048 }
9049
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009050 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009051 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009052 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009053 mApp.getSystemService(AppOpsManager.class)
9054 .checkPackage(Binder.getCallingUid(), callingPackage);
9055
Jordan Liu1e142fc2019-04-22 15:10:43 -07009056 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009057 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009058 try {
9059 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009060 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009061 } catch (SecurityException e) {
9062 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9063 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009064 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009065 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009066 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009067 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009068 }
sandeepjsb6c87872021-09-27 15:34:44 +00009069 // checking compatibility, if calling app's target SDK is T and beyond.
9070 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9071 Binder.getCallingUid())) {
9072 isIccIdAccessRestricted = true;
9073 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009074 final long identity = Binder.clearCallingIdentity();
9075 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009076 UiccController uiccController = UiccController.getInstance();
9077 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009078 if (hasReadPermission) {
9079 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009080 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009081
9082 // Remove private info if the caller doesn't have access
9083 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9084 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009085 //setting the value after compatibility check
9086 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009087 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9088 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009089 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009090 if (card == null) {
9091 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009092 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009093 continue;
9094 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009095 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9096 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009097 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009098 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009099 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009100 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9101 for (UiccPortInfo portInfo : portInfos) {
9102 UiccPort port = uiccController.getUiccPortForSlot(
9103 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9104 if (port == null) {
9105 // assume no access if port is null
9106 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9107 continue;
9108 }
9109 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9110 uiccPortInfos.add(portInfo);
9111 } else {
9112 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9113 }
9114 }
9115 filteredInfos.add(new UiccCardInfo(
9116 cardInfo.isEuicc(),
9117 cardInfo.getCardId(),
9118 null,
9119 cardInfo.getPhysicalSlotIndex(),
9120 cardInfo.isRemovable(),
9121 cardInfo.isMultipleEnabledProfilesSupported(),
9122 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009123 }
9124 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009125 } finally {
9126 Binder.restoreCallingIdentity(identity);
9127 }
9128 }
9129
sandeepjsb6c87872021-09-27 15:34:44 +00009130 /**
9131 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9132 * generally private and require carrier privileges to view.
9133 *
9134 * @hide
9135 */
9136 @NonNull
9137 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9138 List<UiccPortInfo> portinfo = new ArrayList<>();
9139 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9140 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9141 }
9142 return new UiccCardInfo(
9143 cardInfo.isEuicc(),
9144 cardInfo.getCardId(),
9145 null,
9146 cardInfo.getPhysicalSlotIndex(),
9147 cardInfo.isRemovable(),
9148 cardInfo.isMultipleEnabledProfilesSupported(),
9149 portinfo
9150 );
9151 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009152
sandeepjsb6c87872021-09-27 15:34:44 +00009153 /**
9154 * @hide
9155 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9156 * These values are generally private and require carrier privileges to view.
9157 */
9158 @NonNull
9159 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9160 return new UiccPortInfo(
9161 UiccPortInfo.ICCID_REDACTED,
9162 portInfo.getPortIndex(),
9163 portInfo.getLogicalSlotIndex(),
9164 portInfo.isActive()
9165 );
9166 }
9167 @Override
9168 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009169 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009170 mApp.getSystemService(AppOpsManager.class)
9171 .checkPackage(Binder.getCallingUid(), callingPackage);
9172
sandeepjsb6c87872021-09-27 15:34:44 +00009173 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009174
Aman Guptaf3c90b32022-03-17 04:54:16 +00009175 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9176 // we are reading iccId which is PII data.
9177 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009178
9179 // checking compatibility, if calling app's target SDK is T and beyond.
9180 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9181 Binder.getCallingUid())) {
9182 isLogicalSlotAccessRestricted = true;
9183 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009184 final long identity = Binder.clearCallingIdentity();
9185 try {
9186 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009187 if (slots == null || slots.length == 0) {
9188 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009189 return null;
9190 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009191 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9192 for (int i = 0; i < slots.length; i++) {
9193 UiccSlot slot = slots[i];
9194 if (slot == null) {
9195 continue;
9196 }
9197
Jordan Liu7be7e652019-05-06 18:55:02 +00009198 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009199 UiccCard card = slot.getUiccCard();
9200 if (card != null) {
9201 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009202 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009203 cardId = slot.getEid();
9204 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009205 // If cardId is null, use iccId of default port as cardId.
9206 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009207 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009208 }
9209
Jordan Liu857451f2019-05-09 16:35:35 -07009210 if (cardId != null) {
9211 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9212 // if cardId is an EID, it's all digits so this is fine
9213 cardId = IccUtils.stripTrailingFs(cardId);
9214 }
9215
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009216 int cardState = 0;
9217 switch (slot.getCardState()) {
9218 case CARDSTATE_ABSENT:
9219 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9220 break;
9221 case CARDSTATE_PRESENT:
9222 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9223 break;
9224 case CARDSTATE_ERROR:
9225 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9226 break;
9227 case CARDSTATE_RESTRICTED:
9228 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9229 break;
9230 default:
9231 break;
9232
9233 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009234 List<UiccPortInfo> portInfos = new ArrayList<>();
9235 int[] portIndexes = slot.getPortList();
9236 for (int portIdx : portIndexes) {
9237 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009238 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009239 portInfos.add(new UiccPortInfo(iccId, portIdx,
9240 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009241 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009242 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009243 slot.isEuicc(),
9244 cardId,
9245 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009246 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009247 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009248 //setting the value after compatibility check
9249 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009250 }
9251 return infos;
9252 } finally {
9253 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009254 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009255 }
9256
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009257 /* Returns null if doesn't have read permission or carrier privilege access. */
9258 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9259 boolean hasReadPermission) {
9260 String iccId = slot.getIccId(portIndex);
9261 if (hasReadPermission) { // if has read permission
9262 return iccId;
9263 } else {
9264 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9265 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9266 // if no read permission, checking carrier privilege access
9267 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9268 return iccId;
9269 }
9270 }
9271 }
9272 // No read permission or carrier privilege access.
9273 return UiccPortInfo.ICCID_REDACTED;
9274 }
9275
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009276 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009277 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009278 public boolean switchSlots(int[] physicalSlots) {
9279 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009280
9281 final long identity = Binder.clearCallingIdentity();
9282 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009283 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9284 for (int i = 0; i < physicalSlots.length; i++) {
9285 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9286 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9287 physicalSlots[i], i));
9288 }
9289 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009290 } finally {
9291 Binder.restoreCallingIdentity(identity);
9292 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009293 }
Jack Yu4c988042018-02-27 15:30:01 -08009294
9295 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009296 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9297 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9298 enforceModifyPermission();
9299
9300 final long identity = Binder.clearCallingIdentity();
9301 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009302 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009303 } finally {
9304 Binder.restoreCallingIdentity(identity);
9305 }
9306 }
9307
9308 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009309 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009310 final long identity = Binder.clearCallingIdentity();
9311 try {
9312 return UiccController.getInstance().getCardIdForDefaultEuicc();
9313 } finally {
9314 Binder.restoreCallingIdentity(identity);
9315 }
9316 }
9317
Pengquan Meng85728fb2018-03-12 16:31:21 -07009318 /**
goneil47ffb6e2018-04-06 15:40:58 -07009319 * A test API to reload the UICC profile.
9320 *
9321 * <p>Requires that the calling app has permission
9322 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9323 * @hide
9324 */
9325 @Override
9326 public void refreshUiccProfile(int subId) {
9327 enforceModifyPermission();
9328
9329 final long identity = Binder.clearCallingIdentity();
9330 try {
9331 Phone phone = getPhone(subId);
9332 if (phone == null) {
9333 return;
9334 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009335 UiccPort uiccPort = phone.getUiccPort();
9336 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009337 return;
9338 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009339 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009340 if (uiccProfile == null) {
9341 return;
9342 }
9343 uiccProfile.refresh();
9344 } finally {
9345 Binder.restoreCallingIdentity(identity);
9346 }
9347 }
9348
9349 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009350 * Returns false if the mobile data is disabled by default, otherwise return true.
9351 */
9352 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009353 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009354 }
9355
9356 /**
9357 * Returns true if the data roaming is enabled by default, i.e the system property
9358 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9359 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9360 */
9361 private boolean getDefaultDataRoamingEnabled(int subId) {
9362 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009363 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009364 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009365 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9366 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9367 return isDataRoamingEnabled;
9368 }
9369
9370 /**
9371 * Returns the default network type for the given {@code subId}, if the default network type is
9372 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9373 */
9374 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009375 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009376 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009377 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9378 return list.get(phoneId);
9379 }
9380 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009381 }
fionaxua13278b2018-03-21 00:08:13 -07009382
9383 @Override
9384 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009385 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009386 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009387
9388 final long identity = Binder.clearCallingIdentity();
9389 try {
9390 final Phone phone = getPhone(subId);
9391 if (phone == null) {
9392 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9393 return;
9394 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009395 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9396 if (cpt != null) {
9397 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9398 }
9399 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009400 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9401 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009402 if (carrierPrivilegeRules == null) {
9403 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9404 } else {
9405 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9406 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009407 } finally {
9408 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009409 }
fionaxua13278b2018-03-21 00:08:13 -07009410 }
9411
9412 @Override
9413 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009414 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009415
9416 final long identity = Binder.clearCallingIdentity();
9417 try {
9418 final Phone phone = getPhone(subId);
9419 if (phone == null) {
9420 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9421 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9422 }
9423 return phone.getCarrierIdListVersion();
9424 } finally {
9425 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009426 }
fionaxua13278b2018-03-21 00:08:13 -07009427 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009428
9429 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009430 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9431 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009432 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009433 mApp, subId, callingPackage, callingFeatureId,
9434 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009435 return -1;
9436 }
9437
9438 final long identity = Binder.clearCallingIdentity();
9439 try {
9440 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9441 } finally {
9442 Binder.restoreCallingIdentity(identity);
9443 }
9444 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009445
9446 @Override
9447 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009448 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009449 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009450 mApp, subId, "getCdmaRoamingMode");
9451
9452 final long identity = Binder.clearCallingIdentity();
9453 try {
9454 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9455 } finally {
9456 Binder.restoreCallingIdentity(identity);
9457 }
9458 }
9459
9460 @Override
9461 public boolean setCdmaRoamingMode(int subId, int mode) {
9462 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9463 mApp, subId, "setCdmaRoamingMode");
9464
9465 final long identity = Binder.clearCallingIdentity();
9466 try {
9467 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9468 } finally {
9469 Binder.restoreCallingIdentity(identity);
9470 }
9471 }
9472
9473 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009474 public int getCdmaSubscriptionMode(int subId) {
9475 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009476 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009477 mApp, subId, "getCdmaSubscriptionMode");
9478
9479 final long identity = Binder.clearCallingIdentity();
9480 try {
9481 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9482 } finally {
9483 Binder.restoreCallingIdentity(identity);
9484 }
9485 }
9486
9487 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009488 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9489 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9490 mApp, subId, "setCdmaSubscriptionMode");
9491
9492 final long identity = Binder.clearCallingIdentity();
9493 try {
9494 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9495 } finally {
9496 Binder.restoreCallingIdentity(identity);
9497 }
9498 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009499
sqianc5eccab2018-10-19 18:46:41 -07009500 @Override
sqian8c685422019-02-22 15:55:18 -08009501 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009502 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009503 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009504 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9505 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009506 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9507 }
9508 final long identity = Binder.clearCallingIdentity();
9509 try {
sqian854d44b2018-12-12 16:48:18 -08009510 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9511 for (Phone phone: PhoneFactory.getPhones()) {
9512 if (phone.getEmergencyNumberTracker() != null
9513 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9514 emergencyNumberListInternal.put(
9515 phone.getSubId(),
9516 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9517 }
sqian11b7a0e2018-12-05 18:48:28 -08009518 }
sqian854d44b2018-12-12 16:48:18 -08009519 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009520 } finally {
9521 Binder.restoreCallingIdentity(identity);
9522 }
sqianc5eccab2018-10-19 18:46:41 -07009523 }
9524
9525 @Override
sqian8c685422019-02-22 15:55:18 -08009526 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009527 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009528 if (!exactMatch) {
9529 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009530 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009531 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009532 }
9533 final long identity = Binder.clearCallingIdentity();
9534 try {
sqian854d44b2018-12-12 16:48:18 -08009535 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009536 //Note: we ignore passed in param exactMatch. We can remove it once
9537 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009538 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009539 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009540 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009541 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009542 }
sqian11b7a0e2018-12-05 18:48:28 -08009543 }
9544 return false;
9545 } finally {
9546 Binder.restoreCallingIdentity(identity);
9547 }
9548 }
9549
sqianf4ca7ed2019-01-15 18:32:07 -08009550 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009551 * Start emergency callback mode for GsmCdmaPhone for testing.
9552 */
9553 @Override
9554 public void startEmergencyCallbackMode() {
9555 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9556 "startEmergencyCallbackMode");
9557 enforceModifyPermission();
9558 final long identity = Binder.clearCallingIdentity();
9559 try {
9560 for (Phone phone : PhoneFactory.getPhones()) {
9561 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9562 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9563 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9564 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9565 gsmCdmaPhone.obtainMessage(
9566 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9567 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9568 }
9569 }
9570 } finally {
9571 Binder.restoreCallingIdentity(identity);
9572 }
9573 }
9574
9575 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009576 * Update emergency number list for test mode.
9577 */
9578 @Override
9579 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9580 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9581 "updateEmergencyNumberListTestMode");
9582
9583 final long identity = Binder.clearCallingIdentity();
9584 try {
9585 for (Phone phone: PhoneFactory.getPhones()) {
9586 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9587 if (tracker != null) {
9588 tracker.executeEmergencyNumberTestModeCommand(action, num);
9589 }
9590 }
9591 } finally {
9592 Binder.restoreCallingIdentity(identity);
9593 }
9594 }
9595
9596 /**
9597 * Get the full emergency number list for test mode.
9598 */
9599 @Override
9600 public List<String> getEmergencyNumberListTestMode() {
9601 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9602 "getEmergencyNumberListTestMode");
9603
9604 final long identity = Binder.clearCallingIdentity();
9605 try {
9606 Set<String> emergencyNumbers = new HashSet<>();
9607 for (Phone phone: PhoneFactory.getPhones()) {
9608 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9609 if (tracker != null) {
9610 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9611 emergencyNumbers.add(num.getNumber());
9612 }
9613 }
9614 }
9615 return new ArrayList<>(emergencyNumbers);
9616 } finally {
9617 Binder.restoreCallingIdentity(identity);
9618 }
9619 }
9620
chen xud6b45bd2018-10-30 22:27:10 -07009621 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009622 public int getEmergencyNumberDbVersion(int subId) {
9623 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9624
9625 final long identity = Binder.clearCallingIdentity();
9626 try {
9627 final Phone phone = getPhone(subId);
9628 if (phone == null) {
9629 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9630 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9631 }
9632 return phone.getEmergencyNumberDbVersion();
9633 } finally {
9634 Binder.restoreCallingIdentity(identity);
9635 }
9636 }
9637
9638 @Override
9639 public void notifyOtaEmergencyNumberDbInstalled() {
9640 enforceModifyPermission();
9641
9642 final long identity = Binder.clearCallingIdentity();
9643 try {
9644 for (Phone phone: PhoneFactory.getPhones()) {
9645 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9646 if (tracker != null) {
9647 tracker.updateOtaEmergencyNumberDatabase();
9648 }
9649 }
9650 } finally {
9651 Binder.restoreCallingIdentity(identity);
9652 }
9653 }
9654
9655 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009656 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009657 enforceActiveEmergencySessionPermission();
9658
9659 final long identity = Binder.clearCallingIdentity();
9660 try {
9661 for (Phone phone: PhoneFactory.getPhones()) {
9662 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9663 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009664 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9665 }
9666 }
9667 } finally {
9668 Binder.restoreCallingIdentity(identity);
9669 }
9670 }
9671
9672 @Override
9673 public void resetOtaEmergencyNumberDbFilePath() {
9674 enforceActiveEmergencySessionPermission();
9675
9676 final long identity = Binder.clearCallingIdentity();
9677 try {
9678 for (Phone phone: PhoneFactory.getPhones()) {
9679 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9680 if (tracker != null) {
9681 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009682 }
9683 }
9684 } finally {
9685 Binder.restoreCallingIdentity(identity);
9686 }
9687 }
9688
9689 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009690 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9691 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9692 Phone phone = getPhone(subId);
9693 if (phone == null) {
9694 return null;
9695 }
9696 final long identity = Binder.clearCallingIdentity();
9697 try {
9698 UiccProfile profile = UiccController.getInstance()
9699 .getUiccProfileForPhone(phone.getPhoneId());
9700 if (profile != null) {
9701 return profile.getCertsFromCarrierPrivilegeAccessRules();
9702 }
9703 } finally {
9704 Binder.restoreCallingIdentity(identity);
9705 }
9706 return null;
9707 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009708
9709 /**
9710 * Enable or disable a modem stack.
9711 */
9712 @Override
9713 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9714 enforceModifyPermission();
9715
9716 final long identity = Binder.clearCallingIdentity();
9717 try {
9718 Phone phone = PhoneFactory.getPhone(slotIndex);
9719 if (phone == null) {
9720 return false;
9721 } else {
9722 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9723 }
9724 } finally {
9725 Binder.restoreCallingIdentity(identity);
9726 }
9727 }
Michelecea4cf22018-12-21 15:00:11 -08009728
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009729 /**
9730 * Whether a modem stack is enabled or not.
9731 */
9732 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009733 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9734 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009735 Phone phone = PhoneFactory.getPhone(slotIndex);
9736 if (phone == null) return false;
9737
9738 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009739 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9740 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009741 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9742 }
9743
9744 final long identity = Binder.clearCallingIdentity();
9745 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009746 try {
9747 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9748 } catch (NoSuchElementException ex) {
9749 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9750 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009751 } finally {
9752 Binder.restoreCallingIdentity(identity);
9753 }
9754 }
9755
Michelecea4cf22018-12-21 15:00:11 -08009756 @Override
Michele0ea7d782019-03-19 14:58:42 -07009757 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009758 enforceModifyPermission();
9759
9760 final long identity = Binder.clearCallingIdentity();
9761 try {
9762 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009763 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009764 .commit();
9765 } finally {
9766 Binder.restoreCallingIdentity(identity);
9767 }
9768 }
9769
9770 @Override
Michele0ea7d782019-03-19 14:58:42 -07009771 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009772 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009773 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009774 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9775 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009776 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009777 }
Michelecea4cf22018-12-21 15:00:11 -08009778
9779 final long identity = Binder.clearCallingIdentity();
9780 try {
Michele0ea7d782019-03-19 14:58:42 -07009781 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009782 } finally {
9783 Binder.restoreCallingIdentity(identity);
9784 }
9785 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009786
Michele0ea7d782019-03-19 14:58:42 -07009787 @TelephonyManager.IsMultiSimSupportedResult
9788 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009789 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9790 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9791 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009792 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9793 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009794 }
9795 // Check if the hardware supports multisim functionality. If usage of multisim is not
9796 // supported by the modem, indicate that it is restricted.
9797 PhoneCapability staticCapability =
9798 mPhoneConfigurationManager.getStaticPhoneCapability();
9799 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009800 loge("isMultiSimSupportedInternal: no static configuration available");
9801 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009802 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009803 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009804 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9805 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009806 }
9807 // Check if support of multiple SIMs is restricted by carrier
9808 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009809 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009810 }
9811
Michele0ea7d782019-03-19 14:58:42 -07009812 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009813 }
9814
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009815 /**
9816 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009817 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9818 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9819 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009820 * @param numOfSims number of active sims we want to switch to
9821 */
9822 @Override
9823 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009824 if (numOfSims == 1) {
9825 enforceModifyPermission();
9826 } else {
9827 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9828 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9829 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009830 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009831
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009832 try {
Michele30b57b22019-03-01 12:01:14 -08009833 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009834 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009835 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9836 return;
9837 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009838 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9839 } finally {
9840 Binder.restoreCallingIdentity(identity);
9841 }
9842 }
9843
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009844 @Override
9845 public boolean isApplicationOnUicc(int subId, int appType) {
9846 enforceReadPrivilegedPermission("isApplicationOnUicc");
9847 Phone phone = getPhone(subId);
9848 if (phone == null) {
9849 return false;
9850 }
9851 final long identity = Binder.clearCallingIdentity();
9852 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009853 UiccPort uiccPort = phone.getUiccPort();
9854 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009855 return false;
9856 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009857 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009858 if (uiccProfile == null) {
9859 return false;
9860 }
9861 if (TelephonyManager.APPTYPE_SIM <= appType
9862 && appType <= TelephonyManager.APPTYPE_ISIM) {
9863 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9864 }
9865 return false;
9866 } finally {
9867 Binder.restoreCallingIdentity(identity);
9868 }
9869 }
9870
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009871 /**
chen xub4baa772019-04-03 10:23:41 -07009872 * Get whether making changes to modem configurations will trigger reboot.
9873 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009874 */
9875 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009876 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9877 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009878 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009879 mApp, subId, callingPackage, callingFeatureId,
9880 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009881 return false;
9882 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009883 final long identity = Binder.clearCallingIdentity();
9884 try {
9885 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9886 } finally {
9887 Binder.restoreCallingIdentity(identity);
9888 }
9889 }
9890
Nathan Harold29f5f052019-02-15 13:41:57 -08009891 private void updateModemStateMetrics() {
9892 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9893 // TODO: check the state for each modem if the api is ready.
9894 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9895 }
9896
Pengquan Meng3889a572019-01-23 11:16:29 -08009897 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009898 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009899 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009900 // Verify that the callingPackage belongs to the calling UID
9901 mApp.getSystemService(AppOpsManager.class)
9902 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009903 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009904 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009905 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009906 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9907 if (slotInfos != null) {
9908 for (int i = 0; i < slotInfos.length; i++) {
9909 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9910 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9911 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9912 portInfo.getLogicalSlotIndex()));
9913 }
9914 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009915 }
9916 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009917 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009918 } finally {
9919 Binder.restoreCallingIdentity(identity);
9920 }
9921 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009922
9923 /**
9924 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009925 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009926 */
jimsunf9ec1622022-09-13 21:18:43 +08009927 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009928 @Override
9929 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009930 return getHalVersion(HAL_SERVICE_RADIO);
9931 }
9932
9933 /**
9934 * Get the HAL Version of a specific service
9935 */
9936 @Override
9937 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009938 Phone phone = getDefaultPhone();
9939 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009940 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009941 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9942 return hv.major * 100 + hv.minor;
9943 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009944
9945 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009946 * Get the current calling package name.
9947 * @return the current calling package name
9948 */
9949 @Override
9950 public String getCurrentPackageName() {
9951 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9952 }
9953
9954 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009955 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9956 * corresponding network requests on a subId.
9957 *
9958 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009959 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009960 * 2) APN is un-metered for this subscription, or
9961 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009962 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009963 *
9964 * @return whether data is allowed for a apn type.
9965 *
9966 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009967 */
9968 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009969 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009970 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9971 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009972
9973 // Now that all security checks passes, perform the operation as ourselves.
9974 final long identity = Binder.clearCallingIdentity();
9975 try {
9976 Phone phone = getPhone(subId);
9977 if (phone == null) return false;
9978
Jack Yu27422a52022-03-21 10:38:05 -07009979 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009980 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009981 isMetered = phone.getDataNetworkController().getDataConfigManager()
9982 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9983 phone.getServiceState().getDataRoaming());
9984 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009985 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009986 } finally {
9987 Binder.restoreCallingIdentity(identity);
9988 }
9989 }
9990
9991 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009992 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009993 enforceReadPrivilegedPermission("isApnMetered");
9994
9995 // Now that all security checks passes, perform the operation as ourselves.
9996 final long identity = Binder.clearCallingIdentity();
9997 try {
9998 Phone phone = getPhone(subId);
9999 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -070010000 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10001 DataUtils.apnTypeToNetworkCapability(apnType),
10002 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010003 } finally {
10004 Binder.restoreCallingIdentity(identity);
10005 }
10006 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010007
10008 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010009 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10010 int subscriptionId, IBooleanConsumer resultCallback) {
10011 enforceModifyPermission();
10012 long token = Binder.clearCallingIdentity();
10013 try {
10014 Phone phone = getPhone(subscriptionId);
10015 if (phone == null) {
10016 try {
10017 if (resultCallback != null) {
10018 resultCallback.accept(false);
10019 }
10020 } catch (RemoteException e) {
10021 // ignore
10022 }
10023 return;
10024 }
10025 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10026 Pair.create(specifiers, (x) -> {
10027 try {
10028 if (resultCallback != null) {
10029 resultCallback.accept(x);
10030 }
10031 } catch (RemoteException e) {
10032 // ignore
10033 }
10034 });
10035 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10036 } finally {
10037 Binder.restoreCallingIdentity(token);
10038 }
10039 }
10040
10041 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010042 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10043 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010044 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010045 mApp, subId, "getSystemSelectionChannels");
10046 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10047 final long identity = Binder.clearCallingIdentity();
10048 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010049 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10050 if (result instanceof IllegalStateException) {
10051 throw (IllegalStateException) result;
10052 }
10053 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010054 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10055 return specifiers;
10056 } finally {
10057 Binder.restoreCallingIdentity(identity);
10058 }
10059 }
10060
10061 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010062 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010063 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010064 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010065 }
10066
10067 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010068 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10069 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010070 if (callingPackage == null) {
10071 callingPackage = getCurrentPackageName();
10072 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010073 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10074 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010075 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10076 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010077 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10078 }
10079 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10080 Intent intent = new Intent();
10081 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10082 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10083 // Bring up choose default SMS subscription dialog right now
10084 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10085 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10086 mApp.startActivity(intent);
10087 }
chen xud5ca2d52019-05-28 15:20:57 -070010088
10089 @Override
10090 public String getMmsUAProfUrl(int subId) {
10091 //TODO investigate if this API should require proper permission check in R b/133791609
10092 final long identity = Binder.clearCallingIdentity();
10093 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010094 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10095 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10096 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10097 return carrierUAProfUrl;
10098 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010099 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10100 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010101 } finally {
10102 Binder.restoreCallingIdentity(identity);
10103 }
10104 }
10105
10106 @Override
10107 public String getMmsUserAgent(int subId) {
10108 //TODO investigate if this API should require proper permission check in R b/133791609
10109 final long identity = Binder.clearCallingIdentity();
10110 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010111 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10112 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10113 if (!TextUtils.isEmpty(carrierUserAgent)) {
10114 return carrierUserAgent;
10115 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010116 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10117 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010118 } finally {
10119 Binder.restoreCallingIdentity(identity);
10120 }
10121 }
Jack Yub07d4972019-05-28 16:12:25 -070010122
10123 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010124 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10125 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010126
Jack Yub07d4972019-05-28 16:12:25 -070010127 final long identity = Binder.clearCallingIdentity();
10128 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010129 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010130 if (phone == null) return false;
10131
Ling Maf188d502022-09-16 15:22:36 -070010132 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010133 } finally {
10134 Binder.restoreCallingIdentity(identity);
10135 }
10136 }
10137
10138 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010139 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010140 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010141 enforceModifyPermission();
10142
changbettyd5c246e2019-12-24 15:40:37 +080010143 final long identity = Binder.clearCallingIdentity();
10144 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010145 Phone phone = getPhone(subscriptionId);
10146 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010147
Ling Maf188d502022-09-16 15:22:36 -070010148 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010149 } finally {
10150 Binder.restoreCallingIdentity(identity);
10151 }
10152 }
10153
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010154 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010155 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010156 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10157 * otherwise.
10158 */
10159 @Override
10160 public void setCepEnabled(boolean isCepEnabled) {
10161 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10162
10163 final long identity = Binder.clearCallingIdentity();
10164 try {
10165 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10166 for (Phone phone : PhoneFactory.getPhones()) {
10167 Phone defaultPhone = phone.getImsPhone();
10168 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10169 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10170 ImsPhoneCallTracker imsPhoneCallTracker =
10171 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10172 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10173 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10174 + imsPhone.getMsisdn());
10175 }
10176 }
10177 } finally {
10178 Binder.restoreCallingIdentity(identity);
10179 }
10180 }
allenwtsu46dcc572020-01-08 18:24:03 +080010181
10182 /**
10183 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10184 *
10185 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10186 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10187 * before being read.
10188 */
10189 @Override
10190 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10191 isCompressed) {
10192 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10193 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010194 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10195 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10196 }
10197 if (!isImsAvailableOnDevice()) {
10198 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10199 "IMS not available on device.");
10200 }
10201
10202 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010203 try {
Hui Wang761a6682020-10-31 05:12:53 +000010204 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10205 } finally {
10206 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010207 }
10208 }
zoey chene02881a2019-12-30 16:11:23 +080010209
10210 @Override
10211 public boolean isIccLockEnabled(int subId) {
10212 enforceReadPrivilegedPermission("isIccLockEnabled");
10213
10214 // Now that all security checks passes, perform the operation as ourselves.
10215 final long identity = Binder.clearCallingIdentity();
10216 try {
10217 Phone phone = getPhone(subId);
10218 if (phone != null && phone.getIccCard() != null) {
10219 return phone.getIccCard().getIccLockEnabled();
10220 } else {
10221 return false;
10222 }
10223 } finally {
10224 Binder.restoreCallingIdentity(identity);
10225 }
10226 }
10227
10228 /**
10229 * Set the ICC pin lock enabled or disabled.
10230 *
10231 * @return an integer representing the status of IccLock enabled or disabled in the following
10232 * three cases:
10233 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10234 * successfully.
10235 * - Positive number and zero for remaining password attempts.
10236 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10237 *
10238 */
10239 @Override
10240 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10241 enforceModifyPermission();
10242
10243 Phone phone = getPhone(subId);
10244 if (phone == null) {
10245 return 0;
10246 }
10247 // Now that all security checks passes, perform the operation as ourselves.
10248 final long identity = Binder.clearCallingIdentity();
10249 try {
10250 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10251 new Pair<Boolean, String>(enabled, password), phone, null);
10252 return attemptsRemaining;
10253
10254 } catch (Exception e) {
10255 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10256 } finally {
10257 Binder.restoreCallingIdentity(identity);
10258 }
10259 return 0;
10260 }
10261
10262 /**
10263 * Change the ICC password used in ICC pin lock.
10264 *
10265 * @return an integer representing the status of IccLock changed in the following three cases:
10266 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10267 * - Positive number and zero for remaining password attempts.
10268 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10269 *
10270 */
10271 @Override
10272 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10273 enforceModifyPermission();
10274
10275 Phone phone = getPhone(subId);
10276 if (phone == null) {
10277 return 0;
10278 }
10279 // Now that all security checks passes, perform the operation as ourselves.
10280 final long identity = Binder.clearCallingIdentity();
10281 try {
10282 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10283 new Pair<String, String>(oldPassword, newPassword), phone, null);
10284 return attemptsRemaining;
10285
10286 } catch (Exception e) {
10287 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10288 } finally {
10289 Binder.restoreCallingIdentity(identity);
10290 }
10291 return 0;
10292 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010293
10294 /**
10295 * Request for receiving user activity notification
10296 */
10297 @Override
10298 public void requestUserActivityNotification() {
10299 if (!mNotifyUserActivity.get()
10300 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10301 mNotifyUserActivity.set(true);
10302 }
10303 }
10304
10305 /**
10306 * Called when userActivity is signalled in the power manager.
10307 * This is safe to call from any thread, with any window manager locks held or not.
10308 */
10309 @Override
10310 public void userActivity() {
10311 // ***************************************
10312 // * Inherited from PhoneWindowManager *
10313 // ***************************************
10314 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10315 // WITH ITS LOCKS HELD.
10316 //
10317 // This code must be VERY careful about the locks
10318 // it acquires.
10319 // In fact, the current code acquires way too many,
10320 // and probably has lurking deadlocks.
10321
10322 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10323 throw new SecurityException("Only the OS may call notifyUserActivity()");
10324 }
10325
10326 if (mNotifyUserActivity.getAndSet(false)) {
10327 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10328 USER_ACTIVITY_NOTIFICATION_DELAY);
10329 }
10330 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010331
10332 @Override
10333 public boolean canConnectTo5GInDsdsMode() {
10334 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10335 }
Jack Yud10cdd42020-09-28 20:28:01 -070010336
10337 @Override
10338 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10339 String callingFeatureId) {
10340 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10341 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10342 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10343 }
10344
10345 Phone phone = getPhone(subId);
10346 if (phone == null) {
10347 throw new RuntimeException("phone is not available");
10348 }
10349 // Now that all security checks passes, perform the operation as ourselves.
10350 final long identity = Binder.clearCallingIdentity();
10351 try {
10352 return phone.getEquivalentHomePlmns();
10353 } finally {
10354 Binder.restoreCallingIdentity(identity);
10355 }
10356 }
Daniel Bright59e67312020-11-13 11:49:37 -080010357
10358 @Override
10359 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010360 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10361 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010362 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010363 if (radioInterfaceCapabilities == null) {
10364 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010365 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010366 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010367 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010368
Hui Wang641e81c2020-10-12 12:14:23 -070010369 @Override
10370 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10371 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010372 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10373 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10374 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10375 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10376 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010377 if (DBG) {
10378 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10379 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10380 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10381 }
10382
10383 if (!SubscriptionManager.isValidSubscriptionId(subId)
10384 || appType < TelephonyManager.APPTYPE_UNKNOWN
10385 || appType > TelephonyManager.APPTYPE_ISIM
10386 || nafUrl == null || securityProtocol == null || callback == null) {
10387 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10388 if (callback != null) {
10389 try {
10390 callback.onAuthenticationFailure(
10391 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10392 } catch (RemoteException exception) {
10393 log("Fail to notify onAuthenticationFailure due to " + exception);
10394 }
10395 return;
10396 }
10397 }
10398
10399 final long token = Binder.clearCallingIdentity();
10400 try {
10401 getGbaManager(subId).bootstrapAuthenticationRequest(
10402 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10403 forceBootStrapping, callback));
10404 } finally {
10405 Binder.restoreCallingIdentity(token);
10406 }
10407 }
10408
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010409 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010410 * Attempts to set the radio power state for all phones for thermal reason.
10411 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010412 * requested radio power state will actually be set. See {@link
10413 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10414 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010415 * @param enable {@code true} if trying to turn radio on.
10416 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10417 * false}.
10418 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010419 private boolean setRadioPowerForThermal(boolean enable) {
10420 boolean isPhoneAvailable = false;
10421 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10422 Phone phone = PhoneFactory.getPhone(i);
10423 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010424 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010425 isPhoneAvailable = true;
10426 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010427 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010428
10429 // return true if successfully informed the phone object about the thermal radio power
10430 // request.
10431 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010432 }
10433
10434 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010435 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010436 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10437 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10438 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10439 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10440 throw new IllegalArgumentException("modem does not support data throttling");
10441 }
10442
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010443 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10444 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010445 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010446 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10447 }
10448
Sarah Chinecc78c42022-03-31 21:16:48 -070010449 setDataEnabledForReason(
10450 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010451
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010452 if (isDataThrottlingSupported) {
10453 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010454 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010455 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10456 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10457 } else if (thermalMitigationResult
10458 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010459 log("Modem likely does not support data throttling on secondary carrier. Data " +
10460 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10461 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010462 }
10463 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010464 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010465
10466 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010467 }
10468
Jack Nudelman644b91a2021-03-12 14:09:48 -080010469 private static List<String> getThermalMitigationAllowlist(Context context) {
10470 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10471 for (String pckg : context.getResources()
10472 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10473 sThermalMitigationAllowlistedPackages.add(pckg);
10474 }
10475 }
10476
10477 return sThermalMitigationAllowlistedPackages;
10478 }
10479
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010480 private boolean isAnyPhoneInEmergencyState() {
10481 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10482 if (tm.isInEmergencyCall()) {
10483 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10484 return true;
10485 }
10486 for (Phone phone : PhoneFactory.getPhones()) {
10487 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10488 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10489 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10490 + phone.isInEcm());
10491 return true;
10492 }
10493 }
10494
10495 return false;
10496 }
10497
Jack Nudelman644b91a2021-03-12 14:09:48 -080010498 /**
10499 * Used by shell commands to add an authorized package name for thermal mitigation.
10500 * @param packageName name of package to be allowlisted
10501 * @param context
10502 */
10503 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10504 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10505 sThermalMitigationAllowlistedPackages.add(packageName);
10506 }
10507
10508 /**
10509 * Used by shell commands to remove an authorized package name for thermal mitigation.
10510 * @param packageName name of package to remove from allowlist
10511 * @param context
10512 */
10513 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10514 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10515 sThermalMitigationAllowlistedPackages.remove(packageName);
10516 }
10517
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010518 /**
10519 * Thermal mitigation request to control functionalities at modem.
10520 *
10521 * @param subId the id of the subscription.
10522 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010523 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010524 *
10525 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10526 */
10527 @Override
10528 @ThermalMitigationResult
10529 public int sendThermalMitigationRequest(
10530 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010531 ThermalMitigationRequest thermalMitigationRequest,
10532 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010533 enforceModifyPermission();
10534
Jack Nudelman644b91a2021-03-12 14:09:48 -080010535 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10536 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10537 .contains(callingPackage)) {
10538 throw new SecurityException("Calling package must be configured in the device config. "
10539 + "calling package: " + callingPackage);
10540 }
10541
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010542 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10543 final long identity = Binder.clearCallingIdentity();
10544
10545 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10546 try {
10547 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10548 switch (thermalMitigationAction) {
10549 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10550 thermalMitigationResult =
10551 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010552 thermalMitigationRequest.getDataThrottlingRequest(),
10553 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010554 break;
10555 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10556 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10557 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10558 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10559 }
10560
10561 // Ensure that radio is on. If not able to power on due to phone being
10562 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010563 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010564 thermalMitigationResult =
10565 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10566 break;
10567 }
10568
10569 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010570 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010571 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10572 break;
10573 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10574 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10575 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10576 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10577 }
10578
10579 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10580 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010581 Phone phone = getPhone(subId);
10582 if (phone == null) {
10583 thermalMitigationResult =
10584 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10585 break;
10586 }
10587
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010588 TelephonyConnectionService service =
10589 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010590 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010591 Log.e(LOG_TAG, "An emergency call is pending");
10592 thermalMitigationResult =
10593 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10594 break;
10595 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010596 thermalMitigationResult =
10597 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10598 break;
10599 }
10600 } else {
10601 thermalMitigationResult =
10602 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10603 break;
10604 }
10605
10606 // Turn radio off. If not able to power off due to phone being unavailable,
10607 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010608 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010609 thermalMitigationResult =
10610 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10611 break;
10612 }
10613 thermalMitigationResult =
10614 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10615 break;
10616 default:
10617 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10618 + "not exist. Requested action: " + thermalMitigationAction);
10619 }
10620 } catch (IllegalArgumentException e) {
10621 throw e;
10622 } catch (Exception e) {
10623 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10624 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10625 } finally {
10626 Binder.restoreCallingIdentity(identity);
10627 }
10628
10629 if (DBG) {
10630 log("thermalMitigationRequest returning with thermalMitigationResult: "
10631 + thermalMitigationResult);
10632 }
10633
10634 return thermalMitigationResult;
10635 }
Hui Wang641e81c2020-10-12 12:14:23 -070010636
10637 /**
10638 * Set the GbaService Package Name that Telephony will bind to.
10639 *
10640 * @param subId The sim that the GbaService is associated with.
10641 * @param packageName The name of the package to be replaced with.
10642 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10643 */
10644 @Override
10645 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10646 enforceModifyPermission();
10647
10648 final long identity = Binder.clearCallingIdentity();
10649 try {
10650 return getGbaManager(subId).overrideServicePackage(packageName);
10651 } finally {
10652 Binder.restoreCallingIdentity(identity);
10653 }
10654 }
10655
10656 /**
10657 * Return the package name of the currently bound GbaService.
10658 *
10659 * @param subId The sim that the GbaService is associated with.
10660 * @return the package name of the GbaService configuration, null if GBA is not supported.
10661 */
10662 @Override
10663 public String getBoundGbaService(int subId) {
10664 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10665
10666 final long identity = Binder.clearCallingIdentity();
10667 try {
10668 return getGbaManager(subId).getServicePackage();
10669 } finally {
10670 Binder.restoreCallingIdentity(identity);
10671 }
10672 }
10673
10674 /**
10675 * Set the release time for telephony to unbind GbaService.
10676 *
10677 * @param subId The sim that the GbaService is associated with.
10678 * @param interval The release time to unbind GbaService by millisecond.
10679 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10680 */
10681 @Override
10682 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10683 enforceModifyPermission();
10684
10685 final long identity = Binder.clearCallingIdentity();
10686 try {
10687 return getGbaManager(subId).overrideReleaseTime(interval);
10688 } finally {
10689 Binder.restoreCallingIdentity(identity);
10690 }
10691 }
10692
10693 /**
10694 * Return the release time for telephony to unbind GbaService.
10695 *
10696 * @param subId The sim that the GbaService is associated with.
10697 * @return The release time to unbind GbaService by millisecond.
10698 */
10699 @Override
10700 public int getGbaReleaseTime(int subId) {
10701 enforceReadPrivilegedPermission("getGbaReleaseTime");
10702
10703 final long identity = Binder.clearCallingIdentity();
10704 try {
10705 return getGbaManager(subId).getReleaseTime();
10706 } finally {
10707 Binder.restoreCallingIdentity(identity);
10708 }
10709 }
10710
10711 private GbaManager getGbaManager(int subId) {
10712 GbaManager instance = GbaManager.getInstance(subId);
10713 if (instance == null) {
10714 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10715 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10716 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10717 }
10718 return instance;
10719 }
Hui Wang761a6682020-10-31 05:12:53 +000010720
10721 /**
10722 * indicate whether the device and the carrier can support
10723 * RCS VoLTE single registration.
10724 */
10725 @Override
10726 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010727 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10728 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10729 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10730 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010731
10732 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10733 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10734 }
10735
10736 final long identity = Binder.clearCallingIdentity();
10737 try {
10738 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10739 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010740 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10741 if (isCapable != null) {
10742 return isCapable;
10743 }
Hui Wang761a6682020-10-31 05:12:53 +000010744 }
Hui Wang67af90e2021-06-04 16:57:15 -070010745 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10746 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010747 } finally {
10748 Binder.restoreCallingIdentity(identity);
10749 }
10750 }
10751
10752 /**
10753 * Register RCS provisioning callback.
10754 */
10755 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010756 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010757 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010758 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010759 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010760 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10761 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010762
10763 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10764 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10765 }
10766 if (!isImsAvailableOnDevice()) {
10767 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10768 "IMS not available on device.");
10769 }
10770
10771 final long identity = Binder.clearCallingIdentity();
10772 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010773 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010774 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010775 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10776 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010777 }
Hui Wang761a6682020-10-31 05:12:53 +000010778 } finally {
10779 Binder.restoreCallingIdentity(identity);
10780 }
10781 }
10782
10783 /**
10784 * Unregister RCS provisioning callback.
10785 */
10786 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010787 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010788 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010789 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010790 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010791 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10792 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010793
10794 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10795 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10796 }
10797 if (!isImsAvailableOnDevice()) {
10798 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10799 "IMS not available on device.");
10800 }
10801
10802 final long identity = Binder.clearCallingIdentity();
10803 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010804 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010805 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010806 } finally {
10807 Binder.restoreCallingIdentity(identity);
10808 }
10809 }
10810
10811 /**
10812 * trigger RCS reconfiguration.
10813 */
10814 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010815 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10816 "triggerRcsReconfiguration",
10817 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010818
10819 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10820 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10821 }
10822 if (!isImsAvailableOnDevice()) {
10823 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10824 "IMS not available on device.");
10825 }
10826
10827 final long identity = Binder.clearCallingIdentity();
10828 try {
10829 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10830 } finally {
10831 Binder.restoreCallingIdentity(identity);
10832 }
10833 }
10834
10835 /**
10836 * Provide the client configuration parameters of the RCS application.
10837 */
10838 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010839 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10840 "setRcsClientConfiguration",
10841 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010842
10843 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10844 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10845 }
10846 if (!isImsAvailableOnDevice()) {
10847 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10848 "IMS not available on device.");
10849 }
10850
10851 final long identity = Binder.clearCallingIdentity();
10852
10853 try {
10854 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10855 if (configBinder == null) {
10856 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010857 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10858 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010859 } else {
10860 configBinder.setRcsClientConfiguration(rcc);
10861 }
joonhunshin3e154242021-09-17 06:33:39 +000010862
10863 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10864 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010865 } catch (RemoteException e) {
10866 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010867 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10868 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010869 } finally {
10870 Binder.restoreCallingIdentity(identity);
10871 }
10872 }
10873
10874 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010875 * Enables or disables the test mode for RCS VoLTE single registration.
10876 */
10877 @Override
10878 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10879 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10880 "setRcsSingleRegistrationTestModeEnabled");
10881
10882 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10883 }
10884
10885 /**
10886 * Gets the test mode for RCS VoLTE single registration.
10887 */
10888 @Override
10889 public boolean getRcsSingleRegistrationTestModeEnabled() {
10890 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10891 "getRcsSingleRegistrationTestModeEnabled");
10892
10893 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10894 }
10895
10896 /**
Hui Wang761a6682020-10-31 05:12:53 +000010897 * Overrides the config of RCS VoLTE single registration enabled for the device.
10898 */
10899 @Override
10900 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10901 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10902 "setDeviceSingleRegistrationEnabledOverride");
10903 enforceModifyPermission();
10904
10905 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10906 : Boolean.parseBoolean(enabledStr);
10907 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010908 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010909 }
10910
10911 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010912 * Sends a device to device communication message. Only usable via shell.
10913 * @param message message to send.
10914 * @param value message value.
10915 */
10916 @Override
10917 public void sendDeviceToDeviceMessage(int message, int value) {
10918 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010919 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010920 enforceModifyPermission();
10921
10922 final long identity = Binder.clearCallingIdentity();
10923 try {
10924 TelephonyConnectionService service =
10925 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10926 if (service == null) {
10927 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10928 return;
10929 }
10930 service.sendTestDeviceToDeviceMessage(message, value);
10931 } finally {
10932 Binder.restoreCallingIdentity(identity);
10933 }
10934 }
10935
Tyler Gunnbabbda02021-02-10 11:05:02 -080010936 /**
10937 * Sets the specified device to device transport active.
10938 * @param transport The transport to set active.
10939 */
10940 @Override
10941 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10942 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10943 "setActiveDeviceToDeviceTransport");
10944 enforceModifyPermission();
10945
10946 final long identity = Binder.clearCallingIdentity();
10947 try {
10948 TelephonyConnectionService service =
10949 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10950 if (service == null) {
10951 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10952 return;
10953 }
10954 service.setActiveDeviceToDeviceTransport(transport);
10955 } finally {
10956 Binder.restoreCallingIdentity(identity);
10957 }
10958 }
Tyler Gunn92479152021-01-20 16:30:10 -080010959
Tyler Gunnd4339262021-05-03 14:46:49 -070010960 @Override
10961 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10962 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10963 "setDeviceToDeviceForceEnabled");
10964
10965 final long identity = Binder.clearCallingIdentity();
10966 try {
10967 Arrays.stream(PhoneFactory.getPhones()).forEach(
10968 p -> {
10969 Phone thePhone = p.getImsPhone();
10970 if (thePhone != null && thePhone instanceof ImsPhone) {
10971 ImsPhone imsPhone = (ImsPhone) thePhone;
10972 CallTracker tracker = imsPhone.getCallTracker();
10973 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10974 ImsPhoneCallTracker imsPhoneCallTracker =
10975 (ImsPhoneCallTracker) tracker;
10976 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10977 }
10978 }
10979 }
10980 );
10981 } finally {
10982 Binder.restoreCallingIdentity(identity);
10983 }
10984 }
10985
Tyler Gunn92479152021-01-20 16:30:10 -080010986 /**
Hui Wang761a6682020-10-31 05:12:53 +000010987 * Gets the config of RCS VoLTE single registration enabled for the device.
10988 */
10989 @Override
10990 public boolean getDeviceSingleRegistrationEnabled() {
10991 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10992 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10993 }
10994
10995 /**
10996 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10997 */
10998 @Override
10999 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11000 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11001 "setCarrierSingleRegistrationEnabledOverride");
11002 enforceModifyPermission();
11003
11004 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11005 : Boolean.parseBoolean(enabledStr);
11006 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11007 subId, enabled);
11008 }
11009
11010 /**
11011 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11012 */
11013 @Override
11014 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11015 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11016 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11017 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011018
11019 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011020 * Overrides the ims feature validation result
11021 */
11022 @Override
11023 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11024 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11025 "setImsFeatureValidationOverride");
11026
11027 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11028 : Boolean.parseBoolean(enabledStr);
11029 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11030 subId, enabled);
11031 }
11032
11033 /**
11034 * Gets the ims feature validation override value
11035 */
11036 @Override
11037 public boolean getImsFeatureValidationOverride(int subId) {
11038 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11039 "getImsFeatureValidationOverride");
11040 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11041 }
11042
11043 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011044 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11045 * their mobile plan.
11046 */
11047 @Override
11048 public String getMobileProvisioningUrl() {
11049 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11050 final long identity = Binder.clearCallingIdentity();
11051 try {
11052 return getDefaultPhone().getMobileProvisioningUrl();
11053 } finally {
11054 Binder.restoreCallingIdentity(identity);
11055 }
11056 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011057
James.cf Linbcdf8b32021-01-14 16:44:13 +080011058 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011059 * Get the EAB contact from the EAB database.
11060 */
11061 @Override
11062 public String getContactFromEab(String contact) {
11063 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11064 enforceModifyPermission();
11065 final long identity = Binder.clearCallingIdentity();
11066 try {
11067 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11068 } finally {
11069 Binder.restoreCallingIdentity(identity);
11070 }
11071 }
11072
11073 /**
Calvin Pana1434322021-07-01 19:27:01 +080011074 * Get the EAB capability from the EAB database.
11075 */
11076 @Override
11077 public String getCapabilityFromEab(String contact) {
11078 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11079 enforceModifyPermission();
11080 final long identity = Binder.clearCallingIdentity();
11081 try {
11082 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11083 } finally {
11084 Binder.restoreCallingIdentity(identity);
11085 }
11086 }
11087
11088 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011089 * Remove the EAB contacts from the EAB database.
11090 */
11091 @Override
11092 public int removeContactFromEab(int subId, String contacts) {
11093 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11094 enforceModifyPermission();
11095 final long identity = Binder.clearCallingIdentity();
11096 try {
11097 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11098 } finally {
11099 Binder.restoreCallingIdentity(identity);
11100 }
11101 }
11102
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011103 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011104 public boolean getDeviceUceEnabled() {
11105 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11106 final long identity = Binder.clearCallingIdentity();
11107 try {
11108 return mApp.getDeviceUceEnabled();
11109 } finally {
11110 Binder.restoreCallingIdentity(identity);
11111 }
11112 }
11113
11114 @Override
11115 public void setDeviceUceEnabled(boolean isEnabled) {
11116 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11117 final long identity = Binder.clearCallingIdentity();
11118 try {
11119 mApp.setDeviceUceEnabled(isEnabled);
11120 } finally {
11121 Binder.restoreCallingIdentity(identity);
11122 }
11123 }
11124
Brad Ebinger14d467f2021-02-12 06:18:28 +000011125 /**
11126 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11127 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11128 */
11129 // Used for SHELL command only right now.
11130 @Override
11131 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11132 List<String> featureTags) {
11133 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11134 "addUceRegistrationOverrideShell");
11135 final long identity = Binder.clearCallingIdentity();
11136 try {
11137 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11138 new ArraySet<>(featureTags));
11139 } catch (ImsException e) {
11140 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11141 } finally {
11142 Binder.restoreCallingIdentity(identity);
11143 }
11144 }
11145
11146 /**
11147 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11148 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11149 */
11150 // Used for SHELL command only right now.
11151 @Override
11152 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11153 List<String> featureTags) {
11154 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11155 "removeUceRegistrationOverrideShell");
11156 final long identity = Binder.clearCallingIdentity();
11157 try {
11158 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11159 new ArraySet<>(featureTags));
11160 } catch (ImsException e) {
11161 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11162 } finally {
11163 Binder.restoreCallingIdentity(identity);
11164 }
11165 }
11166
11167 /**
11168 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11169 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11170 */
11171 // Used for SHELL command only right now.
11172 @Override
11173 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11174 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11175 "clearUceRegistrationOverrideShell");
11176 final long identity = Binder.clearCallingIdentity();
11177 try {
11178 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11179 } catch (ImsException e) {
11180 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11181 } finally {
11182 Binder.restoreCallingIdentity(identity);
11183 }
11184 }
11185
11186 /**
11187 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11188 */
11189 // Used for SHELL command only right now.
11190 @Override
11191 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11192 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11193 "getLatestRcsContactUceCapabilityShell");
11194 final long identity = Binder.clearCallingIdentity();
11195 try {
11196 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11197 } catch (ImsException e) {
11198 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11199 } finally {
11200 Binder.restoreCallingIdentity(identity);
11201 }
11202 }
11203
11204 /**
11205 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11206 * device does not have an active PUBLISH.
11207 */
11208 // Used for SHELL command only right now.
11209 @Override
11210 public String getLastUcePidfXmlShell(int subId) {
11211 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11212 final long identity = Binder.clearCallingIdentity();
11213 try {
11214 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11215 } catch (ImsException e) {
11216 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11217 } finally {
11218 Binder.restoreCallingIdentity(identity);
11219 }
11220 }
11221
James.cf Line8713a42021-04-29 16:04:26 +080011222 /**
11223 * Remove UCE requests cannot be sent to the network status.
11224 */
11225 // Used for SHELL command only right now.
11226 @Override
11227 public boolean removeUceRequestDisallowedStatus(int subId) {
11228 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11229 final long identity = Binder.clearCallingIdentity();
11230 try {
11231 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11232 } catch (ImsException e) {
11233 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11234 } finally {
11235 Binder.restoreCallingIdentity(identity);
11236 }
11237 }
11238
James.cf Lin18bb9002021-05-25 01:37:38 +080011239 /**
11240 * Remove UCE requests cannot be sent to the network status.
11241 */
11242 // Used for SHELL command only.
11243 @Override
11244 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11245 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11246 final long identity = Binder.clearCallingIdentity();
11247 try {
11248 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11249 } catch (ImsException e) {
11250 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11251 } finally {
11252 Binder.restoreCallingIdentity(identity);
11253 }
11254 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011255
James.cf Lin4b784aa2021-01-31 03:25:15 +080011256 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011257 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11258 String callingPackage) {
11259 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11260 mApp, subId, "setSignalStrengthUpdateRequest");
11261
11262 final int callingUid = Binder.getCallingUid();
11263 // Verify that tha callingPackage belongs to the calling UID
11264 mApp.getSystemService(AppOpsManager.class)
11265 .checkPackage(callingUid, callingPackage);
11266
Rambo Wang3607f502021-02-01 21:51:40 -080011267 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011268
11269 final long identity = Binder.clearCallingIdentity();
11270 try {
11271 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11272 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11273
11274 if (result instanceof IllegalStateException) {
11275 throw (IllegalStateException) result;
11276 }
11277 } finally {
11278 Binder.restoreCallingIdentity(identity);
11279 }
11280 }
11281
11282 @Override
11283 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11284 String callingPackage) {
11285 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11286 mApp, subId, "clearSignalStrengthUpdateRequest");
11287
11288 final int callingUid = Binder.getCallingUid();
11289 // Verify that tha callingPackage belongs to the calling UID
11290 mApp.getSystemService(AppOpsManager.class)
11291 .checkPackage(callingUid, callingPackage);
11292
11293 final long identity = Binder.clearCallingIdentity();
11294 try {
11295 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11296 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11297
11298 if (result instanceof IllegalStateException) {
11299 throw (IllegalStateException) result;
11300 }
11301 } finally {
11302 Binder.restoreCallingIdentity(identity);
11303 }
11304 }
11305
Rambo Wang3607f502021-02-01 21:51:40 -080011306 private static void validateSignalStrengthUpdateRequest(Context context,
11307 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011308 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11309 // phone/system process do not have further restriction on request
11310 return;
11311 }
11312
11313 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011314 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011315 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011316 context.enforceCallingOrSelfPermission(
11317 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11318 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011319 }
11320
11321 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011322 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011323 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011324 || info.isEnabled()) {
11325 throw new IllegalArgumentException(
11326 "Only system can set hide fields in SignalThresholdInfo");
11327 }
11328
11329 // Thresholds length for each RAN need in range. This has been validated in
11330 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11331 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11332 final int[] thresholds = info.getThresholds();
11333 Objects.requireNonNull(thresholds);
11334 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11335 || thresholds.length
11336 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11337 throw new IllegalArgumentException(
11338 "thresholds length is out of range: " + thresholds.length);
11339 }
11340 }
11341 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011342
11343 /**
11344 * Gets the current phone capability.
11345 *
11346 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11347 * @return the PhoneCapability which describes the data connection capability of modem.
11348 * It's used to evaluate possible phone config change, for example from single
11349 * SIM device to multi-SIM device.
11350 */
11351 @Override
11352 public PhoneCapability getPhoneCapability() {
11353 enforceReadPrivilegedPermission("getPhoneCapability");
11354 final long identity = Binder.clearCallingIdentity();
11355 try {
11356 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11357 } finally {
11358 Binder.restoreCallingIdentity(identity);
11359 }
11360 }
Michele Berionne5e411512020-11-13 02:36:59 +000011361
11362 /**
11363 * Prepare TelephonyManager for an unattended reboot. The reboot is
11364 * required to be done shortly after the API is invoked.
11365 */
11366 @Override
11367 @TelephonyManager.PrepareUnattendedRebootResult
11368 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011369 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011370 enforceRebootPermission();
11371
11372 final long identity = Binder.clearCallingIdentity();
11373 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011374 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011375 } finally {
11376 Binder.restoreCallingIdentity(identity);
11377 }
11378 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011379
11380 /**
11381 * Request to get the current slicing configuration including URSP rules and
11382 * NSSAIs (configured, allowed and rejected).
11383 *
11384 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11385 */
11386 @Override
11387 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011388 TelephonyPermissions
11389 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11390 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011391
11392 final long identity = Binder.clearCallingIdentity();
11393 try {
11394 Phone phone = getDefaultPhone();
11395 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11396 } finally {
11397 Binder.restoreCallingIdentity(identity);
11398 }
11399 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011400
11401 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011402 * Check whether the given premium capability is available for purchase from the carrier.
11403 *
11404 * @param capability The premium capability to check.
11405 * @param subId The subId to check the premium capability for.
11406 *
11407 * @return Whether the given premium capability is available to purchase.
11408 */
11409 @Override
11410 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11411 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11412 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11413 log("Premium capability "
11414 + TelephonyManager.convertPremiumCapabilityToString(capability)
11415 + " is not available for purchase due to missing permissions.");
11416 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11417 + "permission READ_BASIC_PHONE_STATE.");
11418 }
11419
11420 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011421 if (phone == null) {
11422 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11423 return false;
11424 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011425 final long identity = Binder.clearCallingIdentity();
11426 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011427 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011428 .isPremiumCapabilityAvailableForPurchase(capability);
11429 } finally {
11430 Binder.restoreCallingIdentity(identity);
11431 }
11432 }
11433
11434 /**
11435 * Purchase the given premium capability from the carrier.
11436 *
11437 * @param capability The premium capability to purchase.
11438 * @param callback The result of the purchase request.
11439 * @param subId The subId to purchase the premium capability for.
11440 */
11441 @Override
11442 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11443 log("purchasePremiumCapability: capability="
11444 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11445 + getCurrentPackageName());
11446
11447 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11448 mApp, "purchasePremiumCapability")) {
11449 log("purchasePremiumCapability "
11450 + TelephonyManager.convertPremiumCapabilityToString(capability)
11451 + " failed due to missing permissions.");
11452 throw new SecurityException("purchasePremiumCapability requires permission "
11453 + "READ_BASIC_PHONE_STATE.");
11454 }
11455
11456 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011457 if (phone == null) {
11458 try {
11459 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11460 callback.accept(result);
11461 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11462 } catch (RemoteException e) {
11463 String logStr = "Purchase premium capability "
11464 + TelephonyManager.convertPremiumCapabilityToString(capability)
11465 + " failed due to RemoteException handling null phone: " + e;
11466 if (DBG) log(logStr);
11467 AnomalyReporter.reportAnomaly(
11468 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11469 }
11470 return;
11471 }
Sarah Chin71b3a852022-09-28 15:54:19 -070011472 String appName;
11473 try {
11474 appName = mApp.getPackageManager().getApplicationLabel(mApp.getPackageManager()
11475 .getApplicationInfo(getCurrentPackageName(), 0)).toString();
11476 } catch (PackageManager.NameNotFoundException e) {
11477 appName = "An application";
11478 }
11479 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
11480 new PurchasePremiumCapabilityArgument(capability, appName, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011481 }
11482
11483 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011484 * Register an IMS connection state callback
11485 */
11486 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011487 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11488 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011489 if (feature == ImsFeature.FEATURE_MMTEL) {
11490 // ImsMmTelManager
11491 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11492 TelephonyPermissions
11493 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11494 mApp, subId, "registerImsStateCallback");
11495 } else if (feature == ImsFeature.FEATURE_RCS) {
11496 // ImsRcsManager or SipDelegateManager
11497 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11498 Binder.getCallingUid(), "registerImsStateCallback",
11499 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11500 Manifest.permission.READ_PRECISE_PHONE_STATE,
11501 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11502 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11503 }
11504
11505 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11506 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11507 "IMS not available on device.");
11508 }
11509
11510 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11511 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11512 }
11513
11514 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11515 if (controller == null) {
11516 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11517 "IMS not available on device.");
11518 }
11519
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011520 if (callingPackage == null) {
11521 callingPackage = getCurrentPackageName();
11522 }
11523
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011524 final long token = Binder.clearCallingIdentity();
11525 try {
11526 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011527 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011528 } catch (ImsException e) {
11529 throw new ServiceSpecificException(e.getCode());
11530 } finally {
11531 Binder.restoreCallingIdentity(token);
11532 }
11533 }
11534
11535 /**
11536 * Unregister an IMS connection state callback
11537 */
11538 @Override
11539 public void unregisterImsStateCallback(IImsStateCallback cb) {
11540 final long token = Binder.clearCallingIdentity();
11541 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11542 if (controller == null) {
11543 return;
11544 }
11545 try {
11546 controller.unregisterImsStateCallback(cb);
11547 } finally {
11548 Binder.restoreCallingIdentity(token);
11549 }
11550 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011551
11552 /**
11553 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11554 *
11555 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11556 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11557 * SecurityException.
11558 * If there is current registered network this value will be same as the registered cell
11559 * identity. If the device goes out of service the previous cell identity is cached and
11560 * will be returned. If the cache age of the Cell identity is more than 24 hours
11561 * it will be cleared and null will be returned.
11562 *
11563 */
11564 @Override
11565 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11566 String callingFeatureId) {
11567 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11568 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11569 LocationAccessPolicy.checkLocationPermission(mApp,
11570 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11571 .setCallingPackage(callingPackage)
11572 .setCallingFeatureId(callingFeatureId)
11573 .setCallingPid(Binder.getCallingPid())
11574 .setCallingUid(Binder.getCallingUid())
11575 .setMethod("getLastKnownCellIdentity")
11576 .setLogAsInfo(true)
11577 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11578 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11579 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11580 .build());
11581
11582 boolean hasFinePermission =
11583 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11584 if (!hasFinePermission
11585 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11586 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011587 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011588 }
11589
11590 final long identity = Binder.clearCallingIdentity();
11591 try {
11592 Phone phone = getPhone(subId);
11593 if (phone == null) return null;
11594 ServiceStateTracker sst = phone.getServiceStateTracker();
11595 if (sst == null) return null;
11596 return sst.getLastKnownCellIdentity();
11597 } finally {
11598 Binder.restoreCallingIdentity(identity);
11599 }
11600 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011601
jimsun3b9ccac2021-10-26 15:01:23 +080011602 /**
11603 * Sets the modem service class Name that Telephony will bind to.
11604 *
11605 * @param serviceName The class name of the modem service.
11606 * @return true if the operation is succeed, otherwise false.
11607 */
11608 public boolean setModemService(String serviceName) {
11609 Log.d(LOG_TAG, "setModemService - " + serviceName);
11610 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11611 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11612 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11613 "setModemService");
11614 return mPhoneConfigurationManager.setModemService(serviceName);
11615 }
11616
11617 /**
11618 * Return the class name of the currently bounded modem service.
11619 *
11620 * @return the class name of the modem service.
11621 */
11622 public String getModemService() {
11623 String result;
11624 Log.d(LOG_TAG, "getModemService");
11625 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11626 TelephonyPermissions
11627 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11628 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11629 "getModemService");
11630 result = mPhoneConfigurationManager.getModemService();
11631 Log.d(LOG_TAG, "result = " + result);
11632 return result;
11633 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011634
11635 @Override
11636 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11637 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11638 mApp.enforceCallingOrSelfPermission(
11639 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11640 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11641
11642 final long identity = Binder.clearCallingIdentity();
11643 try {
11644 Phone phone = getPhone(subId);
11645 if (phone == null) return;
11646 phone.setVoiceServiceStateOverride(hasService);
11647 } finally {
11648 Binder.restoreCallingIdentity(identity);
11649 }
11650 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011651
11652 /**
11653 * set removable eSIM as default eUICC.
11654 *
11655 * @hide
11656 */
11657 @Override
11658 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11659 enforceModifyPermission();
11660 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11661
11662 final long identity = Binder.clearCallingIdentity();
11663 try {
11664 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11665 } finally {
11666 Binder.restoreCallingIdentity(identity);
11667 }
11668 }
11669
11670 /**
11671 * Returns whether the removable eSIM is default eUICC or not.
11672 *
11673 * @hide
11674 */
11675 @Override
11676 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11677 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11678 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11679
11680 final long identity = Binder.clearCallingIdentity();
11681 try {
11682 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11683 } finally {
11684 Binder.restoreCallingIdentity(identity);
11685 }
11686 }
11687
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011688 /**
11689 * Get the component name of the default app to direct respond-via-message intent for the
11690 * user associated with this subscription, update the cache if there is no respond-via-message
11691 * application currently configured for this user.
11692 * @return component name of the app and class to direct Respond Via Message intent to, or
11693 * {@code null} if the functionality is not supported.
11694 * @hide
11695 */
11696 @Override
11697 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11698 boolean updateIfNeeded) {
11699 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011700
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011701 Context context = getPhone(subId).getContext();
11702 UserHandle userHandle = null;
11703 final long identity = Binder.clearCallingIdentity();
11704 try {
11705 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11706 } finally {
11707 Binder.restoreCallingIdentity(identity);
11708 }
11709 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11710 updateIfNeeded, userHandle);
11711 }
Jack Yuf5badd92022-12-08 00:50:53 -080011712
11713 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011714 * Set whether the device is able to connect with null ciphering or integrity
11715 * algorithms. This is a global setting and will apply to all active subscriptions
11716 * and all new subscriptions after this.
11717 *
11718 * @param enabled when true, null cipher and integrity algorithms are allowed.
11719 * @hide
11720 */
11721 @Override
11722 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11723 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11724 enforceModifyPermission();
11725 checkForNullCipherAndIntegritySupport();
11726
11727 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11728 // for listening to these preference changes and applying them immediately.
11729 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11730 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11731 editor.apply();
11732
11733 for (Phone phone: PhoneFactory.getPhones()) {
11734 phone.handleNullCipherEnabledChange();
11735 }
11736 }
11737
11738
11739 /**
11740 * Get whether the device is able to connect with null ciphering or integrity
11741 * algorithms. Note that this retrieves the phone-global preference and not
11742 * the state of the radio.
11743 *
11744 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11745 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11746 * version for this feature.
11747 * @hide
11748 */
11749 @Override
11750 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11751 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11752 enforceReadPermission();
11753 checkForNullCipherAndIntegritySupport();
11754 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11755 }
11756
11757 private void checkForNullCipherAndIntegritySupport() {
11758 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11759 throw new UnsupportedOperationException(
11760 "Null cipher and integrity operations require HAL 2.1 or above");
11761 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011762 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11763 throw new UnsupportedOperationException(
11764 "Null cipher and integrity operations unsupported by modem");
11765 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011766 }
11767
11768 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011769 * Get the SIM state for the slot index.
11770 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11771 *
11772 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11773 */
11774 @Override
11775 @SimState
11776 public int getSimStateForSlotIndex(int slotIndex) {
11777 IccCardConstants.State simState;
11778 if (slotIndex < 0) {
11779 simState = IccCardConstants.State.UNKNOWN;
11780 } else {
11781 Phone phone = null;
11782 try {
11783 phone = PhoneFactory.getPhone(slotIndex);
11784 } catch (IllegalStateException e) {
11785 // ignore
11786 }
11787 if (phone == null) {
11788 simState = IccCardConstants.State.UNKNOWN;
11789 } else {
11790 IccCard icc = phone.getIccCard();
11791 if (icc == null) {
11792 simState = IccCardConstants.State.UNKNOWN;
11793 } else {
11794 simState = icc.getState();
11795 }
11796 }
11797 }
11798 return simState.ordinal();
11799 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011800
11801 /**
11802 * Get current cell broadcast ranges.
11803 */
11804 @Override
11805 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11806 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11807 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11808 "getCellBroadcastIdRanges");
11809 final long identity = Binder.clearCallingIdentity();
11810 try {
11811 return getPhone(subId).getCellBroadcastIdRanges();
11812 } finally {
11813 Binder.restoreCallingIdentity(identity);
11814 }
11815 }
11816
11817 /**
11818 * Set reception of cell broadcast messages with the list of the given ranges
11819 *
11820 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11821 */
11822 @Override
11823 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11824 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11825 @Nullable IIntegerConsumer callback) {
11826 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11827 "setCellBroadcastIdRanges");
11828 final long identity = Binder.clearCallingIdentity();
11829 try {
11830 Phone phone = getPhoneFromSubId(subId);
11831 if (DBG) {
11832 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11833 }
11834 phone.setCellBroadcastIdRanges(ranges, result -> {
11835 if (callback != null) {
11836 try {
11837 callback.accept(result);
11838 } catch (RemoteException e) {
11839 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11840 }
11841 }
11842 });
11843 } finally {
11844 Binder.restoreCallingIdentity(identity);
11845 }
11846 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011847
11848 /**
11849 * Returns whether the device supports the domain selection service.
11850 *
11851 * @return {@code true} if the device supports the domain selection service.
11852 */
11853 @Override
11854 public boolean isDomainSelectionSupported() {
11855 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11856 "isDomainSelectionSupported");
11857
11858 final long identity = Binder.clearCallingIdentity();
11859 try {
11860 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11861 } finally {
11862 Binder.restoreCallingIdentity(identity);
11863 }
11864 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011865
11866 /**
11867 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11868 *
11869 * <p>This method behaves in one of the following ways:
11870 * <ul>
11871 * <li>return true : if the calling package has the appop permission {@link
11872 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11873 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11874 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11875 * Owner device identifier access check, or the calling package has carrier privileges</>
11876 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11877 * </ul>
11878 */
11879 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11880 String callingPackage, @Nullable String callingFeatureId, String message) {
11881 for (Phone phone : PhoneFactory.getPhones()) {
11882 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11883 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11884 return true;
11885 }
11886 }
11887 return false;
11888 }
11889}