blob: 991ebdd66f5240d713a7b02f97435fb5227a5308 [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);
Hwangoo Park8646b0d2023-01-13 02:42:34 +00004390
4391 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4392 if (controller != null) {
4393 ImsManager imsManager = controller.getImsManager(subId);
4394 if (imsManager != null) {
4395 imsManager.addCapabilitiesCallbackForSubscription(c, subId);
4396 } else {
4397 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4398 }
4399 } else {
4400 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4401 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004402 } catch (ImsException e) {
4403 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004404 } finally {
4405 Binder.restoreCallingIdentity(token);
4406 }
4407 }
4408
shilu366312e2019-12-17 09:28:10 -08004409 /**
4410 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4411 * @param subId The subscription to use to check the configuration.
4412 * @param c The callback that will be used to send the result.
4413 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004414 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004415 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004416 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004417 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004418 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4419 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4420 }
Meng Wangafbc5852019-09-19 17:37:13 -07004421
4422 final long token = Binder.clearCallingIdentity();
4423 try {
Hwangoo Park8646b0d2023-01-13 02:42:34 +00004424 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4425 if (controller != null) {
4426 ImsManager imsManager = controller.getImsManager(subId);
4427 if (imsManager != null) {
4428 imsManager.removeCapabilitiesCallbackForSubscription(c, subId);
4429 } else {
4430 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4431 + " is inactive, ignoring unregister.");
4432 // If the ImsManager is not valid, just return, since the callback
4433 // will already have been removed internally.
4434 }
4435 }
Meng Wangafbc5852019-09-19 17:37:13 -07004436 } finally {
4437 Binder.restoreCallingIdentity(token);
4438 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004439 }
4440
4441 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004442 public boolean isCapable(int subId, int capability, int regTech) {
4443 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004444 final long token = Binder.clearCallingIdentity();
4445 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004446 int slotId = getSlotIndexOrException(subId);
4447 verifyImsMmTelConfiguredOrThrow(slotId);
4448 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4449 } catch (com.android.ims.ImsException e) {
4450 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4451 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004452 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004453 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4454 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004455 } finally {
4456 Binder.restoreCallingIdentity(token);
4457 }
4458 }
4459
4460 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004461 public boolean isAvailable(int subId, int capability, int regTech) {
4462 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004463 final long token = Binder.clearCallingIdentity();
4464 try {
4465 Phone phone = getPhone(subId);
4466 if (phone == null) return false;
4467 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004468 } catch (com.android.ims.ImsException e) {
4469 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4470 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004471 } finally {
4472 Binder.restoreCallingIdentity(token);
4473 }
4474 }
4475
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004476 /**
4477 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4478 * subscription.
4479 * @param subId The subscription to use to check the configuration.
4480 * @param callback The callback that will be used to send the result.
4481 * @param capability The MmTelFeature capability that will be used to send the result.
4482 * @param transportType The transport type of the MmTelFeature capability.
4483 */
4484 @Override
4485 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4486 int transportType) {
4487 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004488 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4489 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4490 "IMS not available on device.");
4491 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004492 final long token = Binder.clearCallingIdentity();
4493 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004494 int slotId = getSlotIndex(subId);
4495 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4496 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4497 + subId + "'");
4498 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4499 }
4500 verifyImsMmTelConfiguredOrThrow(slotId);
4501 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4502 transportType, aBoolean -> {
4503 try {
4504 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4505 } catch (RemoteException e) {
4506 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4507 + "running. Ignore");
4508 }
4509 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004510 } catch (ImsException e) {
4511 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004512 } finally {
4513 Binder.restoreCallingIdentity(token);
4514 }
4515 }
4516
shilu366312e2019-12-17 09:28:10 -08004517 /**
4518 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4519 * @param subId The subscription to use to check the configuration.
4520 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004521 @Override
4522 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004523 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004524 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004525
Brad Ebinger35c841c2018-10-01 10:40:55 -07004526 final long token = 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.
Brad Ebinger919631e2021-06-02 17:46:35 -07004530 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004531 } catch (ImsException e) {
4532 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004533 } finally {
4534 Binder.restoreCallingIdentity(token);
4535 }
4536 }
4537
4538 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004539 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004540 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004541 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004542 final long identity = Binder.clearCallingIdentity();
4543 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004544 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004545 // This setting doesn't require an active ImsService connection, so do not verify. The
4546 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004547 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004548 } catch (ImsException e) {
4549 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004550 } finally {
4551 Binder.restoreCallingIdentity(identity);
4552 }
4553 }
4554
shilu366312e2019-12-17 09:28:10 -08004555 /**
4556 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4557 * @param subId The subscription to use to check the configuration.
4558 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004559 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004560 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004561 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004562 mApp, subId, "isVtSettingEnabled");
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.
Brad Ebinger919631e2021-06-02 17:46:35 -07004567 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004568 } catch (ImsException e) {
4569 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004570 } finally {
4571 Binder.restoreCallingIdentity(identity);
4572 }
4573 }
4574
4575 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004576 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004577 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004578 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004579 final long identity = Binder.clearCallingIdentity();
4580 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004581 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004582 // This setting doesn't require an active ImsService connection, so do not verify. The
4583 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004584 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004585 } catch (ImsException e) {
4586 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004587 } finally {
4588 Binder.restoreCallingIdentity(identity);
4589 }
4590 }
4591
shilu366312e2019-12-17 09:28:10 -08004592 /**
4593 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4594 * @param subId The subscription to use to check the configuration.
4595 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004596 @Override
4597 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004598 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004599 mApp, subId, "isVoWiFiSettingEnabled");
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.
Brad Ebinger919631e2021-06-02 17:46:35 -07004604 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004605 } catch (ImsException e) {
4606 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004607 } finally {
4608 Binder.restoreCallingIdentity(identity);
4609 }
4610 }
4611
4612 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004613 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004614 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004615 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004616 final long identity = Binder.clearCallingIdentity();
4617 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004618 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004619 // This setting doesn't require an active ImsService connection, so do not verify. The
4620 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004621 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004622 } catch (ImsException e) {
4623 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004624 } finally {
4625 Binder.restoreCallingIdentity(identity);
4626 }
4627 }
4628
shilu366312e2019-12-17 09:28:10 -08004629 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004630 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4631 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4632 * @param subId The subscription to use to check the configuration.
4633 */
4634 @Override
4635 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004636 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004637 mApp, subId, "isCrossSimCallingEnabledByUser");
4638 final long identity = Binder.clearCallingIdentity();
4639 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004640 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004641 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004642 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004643 } catch (ImsException e) {
4644 throw new ServiceSpecificException(e.getCode());
4645 } finally {
4646 Binder.restoreCallingIdentity(identity);
4647 }
4648 }
4649
4650 /**
4651 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4652 * Requires MODIFY_PHONE_STATE permission.
4653 * @param subId The subscription to use to check the configuration.
4654 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4655 * false otherwise
4656 */
4657 @Override
4658 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4659 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4660 "setCrossSimCallingEnabled");
4661 final long identity = Binder.clearCallingIdentity();
4662 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004663 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004664 // This setting doesn't require an active ImsService connection, so do not verify. The
4665 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004666 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004667 } catch (ImsException e) {
4668 throw new ServiceSpecificException(e.getCode());
4669 } finally {
4670 Binder.restoreCallingIdentity(identity);
4671 }
4672 }
4673
4674 /**
shilu366312e2019-12-17 09:28:10 -08004675 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4676 * @param subId The subscription to use to check the configuration.
4677 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004678 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004679
Brad Ebinger35c841c2018-10-01 10:40:55 -07004680 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004681 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004682 mApp, subId, "isVoWiFiRoamingSettingEnabled");
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.
Brad Ebinger919631e2021-06-02 17:46:35 -07004687 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004688 } catch (ImsException e) {
4689 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004690 } finally {
4691 Binder.restoreCallingIdentity(identity);
4692 }
4693 }
4694
4695 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004696 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004697 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004698 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004699 final long identity = Binder.clearCallingIdentity();
4700 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004701 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004702 // This setting doesn't require an active ImsService connection, so do not verify. The
4703 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004704 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
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
4712 @Override
4713 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4714 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4715 "setVoWiFiNonPersistent");
4716 final long identity = Binder.clearCallingIdentity();
4717 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004718 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004719 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004720 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004721 } catch (ImsException e) {
4722 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004723 } finally {
4724 Binder.restoreCallingIdentity(identity);
4725 }
4726 }
4727
shilu366312e2019-12-17 09:28:10 -08004728 /**
4729 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4730 * @param subId The subscription to use to check the configuration.
4731 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004732 @Override
4733 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004734 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004735 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004736 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.
Brad Ebinger919631e2021-06-02 17:46:35 -07004740 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004741 } catch (ImsException e) {
4742 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004743 } finally {
4744 Binder.restoreCallingIdentity(identity);
4745 }
4746 }
4747
4748 @Override
4749 public void setVoWiFiModeSetting(int subId, int mode) {
4750 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4751 "setVoWiFiModeSetting");
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. The
4756 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004757 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004758 } catch (ImsException e) {
4759 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004760 } finally {
4761 Binder.restoreCallingIdentity(identity);
4762 }
4763 }
4764
4765 @Override
4766 public int getVoWiFiRoamingModeSetting(int subId) {
4767 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
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.
Brad Ebinger919631e2021-06-02 17:46:35 -07004772 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004773 } catch (ImsException e) {
4774 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004775 } finally {
4776 Binder.restoreCallingIdentity(identity);
4777 }
4778 }
4779
4780 @Override
4781 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4782 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4783 "setVoWiFiRoamingModeSetting");
4784 final long identity = Binder.clearCallingIdentity();
4785 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004786 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004787 // This setting doesn't require an active ImsService connection, so do not verify. The
4788 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004789 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004790 } catch (ImsException e) {
4791 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004792 } finally {
4793 Binder.restoreCallingIdentity(identity);
4794 }
4795 }
4796
4797 @Override
4798 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4799 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4800 "setRttCapabilityEnabled");
4801 final long identity = Binder.clearCallingIdentity();
4802 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004803 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004804 // This setting doesn't require an active ImsService connection, so do not verify. The
4805 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004806 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004807 } catch (ImsException e) {
4808 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004809 } finally {
4810 Binder.restoreCallingIdentity(identity);
4811 }
4812 }
4813
shilu366312e2019-12-17 09:28:10 -08004814 /**
4815 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4816 * @param subId The subscription to use to check the configuration.
4817 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004818 @Override
4819 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004820 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004821 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004822 final long identity = Binder.clearCallingIdentity();
4823 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004824 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004825 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004826 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004827 } catch (ImsException e) {
4828 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004829 } finally {
4830 Binder.restoreCallingIdentity(identity);
4831 }
4832 }
4833
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004834 @Override
4835 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4836 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004837
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004838 final long identity = Binder.clearCallingIdentity();
4839 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004840 if (!isImsAvailableOnDevice()) {
4841 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4842 "IMS not available on device.");
4843 }
4844 int slotId = getSlotIndexOrException(subId);
4845 verifyImsMmTelConfiguredOrThrow(slotId);
Hwangoo Park8646b0d2023-01-13 02:42:34 +00004846
4847 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4848 if (controller != null) {
4849 ImsManager imsManager = controller.getImsManager(subId);
4850 if (imsManager != null) {
4851 imsManager.addProvisioningCallbackForSubscription(callback, subId);
4852 } else {
4853 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4854 }
4855 } else {
4856 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4857 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004858 } catch (ImsException e) {
4859 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004860 } finally {
4861 Binder.restoreCallingIdentity(identity);
4862 }
4863 }
4864
4865 @Override
4866 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4867 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004868
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004869 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004870 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4871 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4872 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004873 try {
Hwangoo Park8646b0d2023-01-13 02:42:34 +00004874 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4875 if (controller != null) {
4876 ImsManager imsManager = controller.getImsManager(subId);
4877 if (imsManager != null) {
4878 imsManager.removeProvisioningCallbackForSubscription(callback, subId);
4879 } else {
4880 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4881 + " is inactive, ignoring unregister.");
4882 // If the ImsManager is not valid, just return, since the callback will already
4883 // have been removed internally.
4884 }
4885 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004886 } finally {
4887 Binder.restoreCallingIdentity(identity);
4888 }
4889 }
4890
joonhunshincffb7fc2021-11-28 07:32:01 +00004891 @Override
4892 public void registerFeatureProvisioningChangedCallback(int subId,
4893 IFeatureProvisioningCallback callback) {
4894 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4895 mApp, subId, "registerFeatureProvisioningChangedCallback");
4896
4897 final long identity = Binder.clearCallingIdentity();
4898 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4899 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4900 }
4901
joonhunshin91bc1952022-04-29 08:47:15 +00004902 try {
4903 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4904 if (controller == null) {
4905 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4906 "Device does not support IMS");
4907 }
4908 controller.addFeatureProvisioningChangedCallback(subId, callback);
4909 } finally {
4910 Binder.restoreCallingIdentity(identity);
4911 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004912 }
4913
4914 @Override
4915 public void unregisterFeatureProvisioningChangedCallback(int subId,
4916 IFeatureProvisioningCallback callback) {
4917 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4918 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4919
4920 final long identity = Binder.clearCallingIdentity();
4921 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4922 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4923 }
4924
joonhunshin91bc1952022-04-29 08:47:15 +00004925 try {
4926 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4927 if (controller == null) {
4928 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4929 return;
4930 }
4931 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4932 } finally {
4933 Binder.restoreCallingIdentity(identity);
4934 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004935 }
allenwtsu99c623b2020-01-03 18:24:23 +08004936
4937 private void checkModifyPhoneStatePermission(int subId, String message) {
4938 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4939 message);
4940 }
4941
allenwtsu99c623b2020-01-03 18:24:23 +08004942 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004943 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004944 boolean isProvisioned) {
4945 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4946
4947 final long identity = Binder.clearCallingIdentity();
4948 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004949 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4950 if (controller == null) {
4951 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4952 return;
4953 }
4954 controller.setRcsProvisioningStatusForCapability(
4955 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004956 } finally {
4957 Binder.restoreCallingIdentity(identity);
4958 }
allenwtsu99c623b2020-01-03 18:24:23 +08004959 }
4960
4961
4962 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004963 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4964 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4965 mApp, subId, "getRcsProvisioningStatusForCapability");
4966
allenwtsu99c623b2020-01-03 18:24:23 +08004967 final long identity = Binder.clearCallingIdentity();
4968 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004969 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4970 if (controller == null) {
4971 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4972
4973 // device does not support IMS, this method will return true always.
4974 return true;
4975 }
4976 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004977 } finally {
4978 Binder.restoreCallingIdentity(identity);
4979 }
4980 }
4981
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004982 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004983 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4984 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004985 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004986
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004987 final long identity = Binder.clearCallingIdentity();
4988 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004989 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4990 if (controller == null) {
4991 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4992 return;
4993 }
4994 controller.setImsProvisioningStatusForCapability(
4995 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004996 } finally {
4997 Binder.restoreCallingIdentity(identity);
4998 }
4999 }
5000
5001 @Override
5002 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00005003 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5004 mApp, subId, "getProvisioningStatusForCapability");
5005
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005006 final long identity = Binder.clearCallingIdentity();
5007 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005008 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5009 if (controller == null) {
5010 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005011
joonhunshin91bc1952022-04-29 08:47:15 +00005012 // device does not support IMS, this method will return true always.
5013 return true;
5014 }
5015 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005016 } finally {
5017 Binder.restoreCallingIdentity(identity);
5018 }
5019 }
5020
5021 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005022 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
5023 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5024 mApp, subId, "isProvisioningRequiredForCapability");
5025
5026 final long identity = Binder.clearCallingIdentity();
5027 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005028 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5029 if (controller == null) {
5030 loge("isProvisioningRequiredForCapability: Device does not support IMS");
5031
5032 // device does not support IMS, this method will return false
5033 return false;
5034 }
5035 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005036 } finally {
5037 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005038 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005039 }
5040
5041 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005042 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5043 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5044 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005045
joonhunshincffb7fc2021-11-28 07:32:01 +00005046 final long identity = Binder.clearCallingIdentity();
5047 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005048 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5049 if (controller == null) {
5050 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5051
5052 // device does not support IMS, this method will return false
5053 return false;
5054 }
5055 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005056 } finally {
5057 Binder.restoreCallingIdentity(identity);
5058 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005059 }
5060
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005061 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005062 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005063 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5064 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5065 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005066 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5067 mApp, subId, "getImsProvisioningInt");
5068
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005069 final long identity = Binder.clearCallingIdentity();
5070 try {
5071 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005072 int slotId = getSlotIndex(subId);
5073 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5074 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5075 + subId + "' for key:" + key);
5076 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5077 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005078
joonhunshin91bc1952022-04-29 08:47:15 +00005079 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5080 if (controller == null) {
5081 loge("getImsProvisioningInt: Device does not support IMS");
5082
5083 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5084 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5085 }
5086 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005087 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5088 return retVal;
5089 }
5090
calvinpanb5a34062021-02-08 19:59:36 +08005091 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005092 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005093 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5094 + subId + "' for key:" + key);
5095 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005096 } finally {
5097 Binder.restoreCallingIdentity(identity);
5098 }
5099 }
5100
5101 @Override
5102 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005103 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5104 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5105 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005106 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5107 mApp, subId, "getImsProvisioningString");
5108
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005109 final long identity = Binder.clearCallingIdentity();
5110 try {
5111 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005112 int slotId = getSlotIndex(subId);
5113 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5114 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5115 + subId + "' for key:" + key);
5116 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5117 }
calvinpanb5a34062021-02-08 19:59:36 +08005118 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005119 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005120 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5121 + subId + "' for key:" + key);
5122 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005123 } finally {
5124 Binder.restoreCallingIdentity(identity);
5125 }
5126 }
5127
5128 @Override
5129 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005130 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5131 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5132 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005133 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5134 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005135
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005136 final long identity = Binder.clearCallingIdentity();
5137 try {
5138 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005139 int slotId = getSlotIndex(subId);
5140 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5141 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5142 + subId + "' for key:" + key);
5143 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5144 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005145
joonhunshin91bc1952022-04-29 08:47:15 +00005146 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5147 if (controller == null) {
5148 loge("setImsProvisioningInt: Device does not support IMS");
5149
5150 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5151 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5152 }
5153 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005154 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5155 return retVal;
5156 }
5157
calvinpanb5a34062021-02-08 19:59:36 +08005158 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5159 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005160 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005161 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005162 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005163 } finally {
5164 Binder.restoreCallingIdentity(identity);
5165 }
5166 }
5167
5168 @Override
5169 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005170 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5171 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5172 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005173 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5174 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005175 final long identity = Binder.clearCallingIdentity();
5176 try {
5177 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005178 int slotId = getSlotIndex(subId);
5179 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5180 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5181 + subId + "' for key:" + key);
5182 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5183 }
calvinpanb5a34062021-02-08 19:59:36 +08005184 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5185 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005186 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005187 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005188 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005189 } finally {
5190 Binder.restoreCallingIdentity(identity);
5191 }
5192 }
5193
Brad Ebinger919631e2021-06-02 17:46:35 -07005194 /**
5195 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5196 * for the given slot ID or no ImsResolver instance has been created.
5197 * @param slotId The slot ID that the IMS service is created for.
5198 * @throws ImsException If there is no ImsService configured for this slot.
5199 */
5200 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5201 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5202 ImsFeature.FEATURE_MMTEL)) {
5203 throw new ImsException("This subscription does not support MMTEL over IMS",
5204 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5205 }
5206 }
5207
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005208 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005209 int slotId = SubscriptionManager.getSlotIndex(subId);
5210 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005211 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5212 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005213 }
5214 return slotId;
5215 }
5216
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005217 private int getSlotIndex(int subId) {
5218 int slotId = SubscriptionManager.getSlotIndex(subId);
5219 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5220 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5221 }
5222 return slotId;
5223 }
5224
Wink Saville36469e72014-06-11 15:17:00 -07005225 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005226 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005227 */
5228 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005229 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5230 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005231 try {
5232 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5233 } catch (SecurityException se) {
5234 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5235 throw new SecurityException("Package " + callingPackage + " does not belong to "
5236 + Binder.getCallingUid());
5237 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005238 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005239 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005240 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005241 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005242 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005243 mApp, subId, callingPackage, callingFeatureId,
5244 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005245 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5246 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005247
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005248 final long identity = Binder.clearCallingIdentity();
5249 try {
5250 final Phone phone = getPhone(subId);
5251 if (phone != null) {
5252 return phone.getServiceState().getDataNetworkType();
5253 } else {
5254 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5255 }
5256 } finally {
5257 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005258 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005259 }
5260
5261 /**
5262 * Returns the data network type
5263 */
5264 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005265 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005266 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005267 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005268 }
5269
5270 /**
5271 * Returns the data network type for a subId
5272 */
5273 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005274 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5275 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005276 String functionName = "getDataNetworkTypeForSubscriber";
5277 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5278 mApp, functionName)) {
5279 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5280 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5281 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5282 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005283 }
5284
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005285 final long identity = Binder.clearCallingIdentity();
5286 try {
5287 final Phone phone = getPhone(subId);
5288 if (phone != null) {
5289 return phone.getServiceState().getDataNetworkType();
5290 } else {
5291 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5292 }
5293 } finally {
5294 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005296 }
5297
5298 /**
Wink Saville36469e72014-06-11 15:17:00 -07005299 * Returns the Voice network type for a subId
5300 */
5301 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005302 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5303 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005304 String functionName = "getVoiceNetworkTypeForSubscriber";
5305 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5306 mApp, functionName)) {
5307 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5308 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5309 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5310 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005311 }
5312
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005313 final long identity = Binder.clearCallingIdentity();
5314 try {
5315 final Phone phone = getPhone(subId);
5316 if (phone != null) {
5317 return phone.getServiceState().getVoiceNetworkType();
5318 } else {
5319 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5320 }
5321 } finally {
5322 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005323 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005324 }
5325
5326 /**
5327 * @return true if a ICC card is present
5328 */
5329 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005330 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005331 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005332 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005333 }
5334
5335 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005336 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005337 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005338 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005339 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005340 final long identity = Binder.clearCallingIdentity();
5341 try {
5342 final Phone phone = PhoneFactory.getPhone(slotIndex);
5343 if (phone != null) {
5344 return phone.getIccCard().hasIccCard();
5345 } else {
5346 return false;
5347 }
5348 } finally {
5349 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005350 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005351 }
5352
5353 /**
5354 * Return if the current radio is LTE on CDMA. This
5355 * is a tri-state return value as for a period of time
5356 * the mode may be unknown.
5357 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005358 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005359 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005360 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005361 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005362 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005363 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5364 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5365 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005366 }
5367
Sanket Padawe356d7632015-06-22 14:03:32 -07005368 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005369 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5370 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005371 try {
5372 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5373 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005374 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5375 }
5376
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005377 final long identity = Binder.clearCallingIdentity();
5378 try {
5379 final Phone phone = getPhone(subId);
5380 if (phone == null) {
5381 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5382 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005383 return TelephonyProperties.lte_on_cdma_device()
5384 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005385 }
5386 } finally {
5387 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005388 }
Wink Saville36469e72014-06-11 15:17:00 -07005389 }
5390
Wink Saville36469e72014-06-11 15:17:00 -07005391 /**
5392 * {@hide}
5393 * Returns Default subId, 0 in the case of single standby.
5394 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005395 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005396 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005397 }
5398
Shishir Agrawala9f32182016-04-12 12:00:16 -07005399 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005400 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005401 }
5402
Wink Savilleb564aae2014-10-23 10:18:09 -07005403 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005404 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005405 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005406
Pengquan Menge92a50d2018-09-21 15:54:48 -07005407 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005408 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5409 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5410 mApp.getOpPackageName(), mApp.getFeatureId());
5411 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005412 return mSubscriptionController.isActiveSubId(subId);
5413 }
5414
Ihab Awadf2177b72013-11-25 13:33:23 -08005415 /**
5416 * @see android.telephony.TelephonyManager.WifiCallingChoices
5417 */
5418 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005419 final long identity = Binder.clearCallingIdentity();
5420 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005421 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005422 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5423 getWhenToMakeWifiCallsDefaultPreference());
5424 } finally {
5425 Binder.restoreCallingIdentity(identity);
5426 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005427 }
5428
5429 /**
5430 * @see android.telephony.TelephonyManager.WifiCallingChoices
5431 */
5432 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005433 final long identity = Binder.clearCallingIdentity();
5434 try {
5435 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005436 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005437 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5438 } finally {
5439 Binder.restoreCallingIdentity(identity);
5440 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005441 }
5442
Sailesh Nepald1e68152013-12-12 19:08:02 -08005443 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005444 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005445 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005446 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005447
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005448 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5449 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5450 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005451 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005452 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5453 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005454 }
5455 return PhoneFactory.getPhone(phoneId);
5456 }
5457
Shishir Agrawal566b7612013-10-28 14:41:00 -07005458 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005459 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005460 @NonNull IccLogicalChannelRequest request) {
5461 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5462 /*message=*/ "iccOpenLogicalChannel");
5463
5464 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5465 // Verify that the callingPackage in the request belongs to the calling UID
5466 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5467
5468 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005469 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005470
Rambo Wanga1782702021-11-10 20:15:19 -08005471 private Phone getPhoneFromValidIccLogicalChannelRequest(
5472 @NonNull IccLogicalChannelRequest request, String message) {
5473 Phone phone;
5474 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5475 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5476 mApp, request.subId, message);
5477 phone = getPhoneFromSubId(request.subId);
5478 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5479 enforceModifyPermission();
5480 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5481 } else {
5482 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005483 }
Rambo Wanga1782702021-11-10 20:15:19 -08005484 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005485 }
5486
5487 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005488 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005489 final long identity = Binder.clearCallingIdentity();
5490 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005491 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005492 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005493 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5494 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005495 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5496 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005497 loge("The calling package is not allowed to access ISD-R.");
5498 throw new SecurityException(
5499 "The calling package is not allowed to access ISD-R.");
5500 }
Derek Tan740e1672017-06-27 14:56:27 -07005501 }
Derek Tan740e1672017-06-27 14:56:27 -07005502
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005503 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005504 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5505 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005506 return response;
5507 } finally {
5508 Binder.restoreCallingIdentity(identity);
5509 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005510 }
5511
5512 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005513 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5514 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5515 /*message=*/"iccCloseLogicalChannel");
5516
5517 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5518
5519 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005520 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005521
Rambo Wanga1782702021-11-10 20:15:19 -08005522 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5523 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005524 // before this feature is enabled, this API should only return false if
5525 // the operation fails instead of throwing runtime exception for
5526 // backward-compatibility.
5527 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5528 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005529 final long identity = Binder.clearCallingIdentity();
5530 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005531 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005532 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005533 }
Chen Xue9d737e2022-01-01 23:41:31 -08005534 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005535 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005536 Boolean success = false;
5537 if (result instanceof RuntimeException) {
5538 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005539 if (shouldThrowExceptionOnFailure) {
5540 throw (RuntimeException) result;
5541 } else {
5542 return false;
5543 }
Chen Xue9d737e2022-01-01 23:41:31 -08005544 } else if (result instanceof Boolean) {
5545 success = (Boolean) result;
5546 } else {
5547 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5548 }
Rambo Wanga1782702021-11-10 20:15:19 -08005549 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005550 return success;
5551 } finally {
5552 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005553 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005554 }
5555
5556 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005557 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005558 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005559 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5560 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005561 if (DBG) {
5562 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5563 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5564 + p3 + " data=" + data);
5565 }
5566 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5567 command, p1, p2, p3, data);
5568 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005569
Jordan Liu4c733742019-02-28 12:03:40 -08005570 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005571 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5572 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005573 enforceModifyPermission();
5574 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005575 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5576 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5577 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005578 }
5579 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005580 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5581 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005582 }
5583
5584 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5585 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005586 final long identity = Binder.clearCallingIdentity();
5587 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005588 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005589 return "";
5590 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005591
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005592 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005593 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5594 null /* workSource */);
5595 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005596
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005597 // Append the returned status code to the end of the response payload.
5598 String s = Integer.toHexString(
5599 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5600 if (response.payload != null) {
5601 s = IccUtils.bytesToHexString(response.payload) + s;
5602 }
5603 return s;
5604 } finally {
5605 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005606 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005607 }
Jake Hambye994d462014-02-03 13:10:13 -08005608
Evan Charltonc66da362014-05-16 14:06:40 -07005609 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005610 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5611 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005612 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5613 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005614 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005615 if (DBG) {
5616 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5617 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5618 }
5619 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5620 cla, command, p1, p2, p3, data);
5621 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005622
Jordan Liu4c733742019-02-28 12:03:40 -08005623 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005624 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5625 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005626 enforceModifyPermission();
5627 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5628 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005629 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5630 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5631 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005632 }
5633
5634 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005635 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5636 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005637 }
5638
5639 // open APDU basic channel assuming the caller has sufficient permissions
5640 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5641 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005642 final long identity = Binder.clearCallingIdentity();
5643 try {
5644 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5645 && TextUtils.equals(ISDR_AID, data)) {
5646 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005647 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5648 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005649 if (bestComponent == null
5650 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5651 loge("The calling package is not allowed to select ISD-R.");
5652 throw new SecurityException(
5653 "The calling package is not allowed to select ISD-R.");
5654 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005655 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005656
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005657 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005658 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5659 null /* workSource */);
5660 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005661
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005662 // Append the returned status code to the end of the response payload.
5663 String s = Integer.toHexString(
5664 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5665 if (response.payload != null) {
5666 s = IccUtils.bytesToHexString(response.payload) + s;
5667 }
5668 return s;
5669 } finally {
5670 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005671 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005672 }
5673
5674 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005675 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005676 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005677 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5678 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005679
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005680 final long identity = Binder.clearCallingIdentity();
5681 try {
5682 if (DBG) {
5683 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5684 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5685 }
5686
5687 IccIoResult response =
5688 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5689 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5690 subId);
5691
5692 if (DBG) {
5693 log("Exchange SIM_IO [R]" + response);
5694 }
5695
5696 byte[] result = null;
5697 int length = 2;
5698 if (response.payload != null) {
5699 length = 2 + response.payload.length;
5700 result = new byte[length];
5701 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5702 } else {
5703 result = new byte[length];
5704 }
5705
5706 result[length - 1] = (byte) response.sw2;
5707 result[length - 2] = (byte) response.sw1;
5708 return result;
5709 } finally {
5710 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005711 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005712 }
5713
Nathan Haroldb3014052017-01-25 15:57:32 -08005714 /**
5715 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5716 * on a particular subscription
5717 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005718 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5719 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005720 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005721 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005722 return null;
5723 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005724
5725 final long identity = Binder.clearCallingIdentity();
5726 try {
5727 if (appType != TelephonyManager.APPTYPE_USIM
5728 && appType != TelephonyManager.APPTYPE_SIM) {
5729 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5730 return null;
5731 }
5732 Object response = sendRequest(
5733 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5734 if (response instanceof String[]) {
5735 return (String[]) response;
5736 }
yincheng zhao2737e882019-09-06 17:06:54 -07005737 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005738 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005739 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005740 } finally {
5741 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005742 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005743 }
5744
yincheng zhao2737e882019-09-06 17:06:54 -07005745 /**
5746 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5747 * subscription.
5748 *
5749 * @param subId the id of the subscription.
5750 * @param appType the uicc app type, must be USIM or SIM.
5751 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5752 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005753 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005754 * @return number of fplmns that is successfully written to the SIM.
5755 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005756 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5757 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005758 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5759 mApp, subId, "setForbiddenPlmns");
5760
yincheng zhao2737e882019-09-06 17:06:54 -07005761 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5762 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5763 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5764 }
5765 if (fplmns == null) {
5766 throw new IllegalArgumentException("Fplmn List provided is null");
5767 }
5768 for (String fplmn : fplmns) {
5769 if (!CellIdentity.isValidPlmn(fplmn)) {
5770 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5771 }
5772 }
5773 final long identity = Binder.clearCallingIdentity();
5774 try {
5775 Object response = sendRequest(
5776 CMD_SET_FORBIDDEN_PLMNS,
5777 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5778 subId);
5779 return (int) response;
5780 } finally {
5781 Binder.restoreCallingIdentity(identity);
5782 }
5783 }
5784
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005785 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005786 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005787 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5788 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005789
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005790 final long identity = Binder.clearCallingIdentity();
5791 try {
5792 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5793 if (response.payload == null) {
5794 return "";
5795 }
Evan Charltonc66da362014-05-16 14:06:40 -07005796
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005797 // Append the returned status code to the end of the response payload.
5798 String s = Integer.toHexString(
5799 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5800 s = IccUtils.bytesToHexString(response.payload) + s;
5801 return s;
5802 } finally {
5803 Binder.restoreCallingIdentity(identity);
5804 }
Evan Charltonc66da362014-05-16 14:06:40 -07005805 }
5806
Jake Hambye994d462014-02-03 13:10:13 -08005807 /**
5808 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5809 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5810 *
5811 * @param itemID the ID of the item to read
5812 * @return the NV item as a String, or null on error.
5813 */
5814 @Override
5815 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005816 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005817 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5818 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005819
5820 final long identity = Binder.clearCallingIdentity();
5821 try {
5822 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005823 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005824 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5825 return value;
5826 } finally {
5827 Binder.restoreCallingIdentity(identity);
5828 }
Jake Hambye994d462014-02-03 13:10:13 -08005829 }
5830
5831 /**
5832 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5833 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5834 *
5835 * @param itemID the ID of the item to read
5836 * @param itemValue the value to write, as a String
5837 * @return true on success; false on any failure
5838 */
5839 @Override
5840 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005841 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005842 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5843 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005844
5845 final long identity = Binder.clearCallingIdentity();
5846 try {
5847 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5848 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005849 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005850 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5851 return success;
5852 } finally {
5853 Binder.restoreCallingIdentity(identity);
5854 }
Jake Hambye994d462014-02-03 13:10:13 -08005855 }
5856
5857 /**
5858 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5859 * Used for device configuration by some CDMA operators.
5860 *
5861 * @param preferredRoamingList byte array containing the new PRL
5862 * @return true on success; false on any failure
5863 */
5864 @Override
5865 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005866 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5867 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005868
5869 final long identity = Binder.clearCallingIdentity();
5870 try {
5871 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5872 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5873 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5874 return success;
5875 } finally {
5876 Binder.restoreCallingIdentity(identity);
5877 }
Jake Hambye994d462014-02-03 13:10:13 -08005878 }
5879
5880 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005881 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005882 * Used for device configuration by some CDMA operators.
5883 *
chen xu6dac5ab2018-10-26 17:39:23 -07005884 * @param slotIndex - device slot.
5885 *
Jake Hambye994d462014-02-03 13:10:13 -08005886 * @return true on success; false on any failure
5887 */
5888 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005889 public boolean resetModemConfig(int slotIndex) {
5890 Phone phone = PhoneFactory.getPhone(slotIndex);
5891 if (phone != null) {
5892 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5893 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005894
chen xu6dac5ab2018-10-26 17:39:23 -07005895 final long identity = Binder.clearCallingIdentity();
5896 try {
5897 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5898 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5899 return success;
5900 } finally {
5901 Binder.restoreCallingIdentity(identity);
5902 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005903 }
chen xu6dac5ab2018-10-26 17:39:23 -07005904 return false;
5905 }
5906
5907 /**
5908 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5909 *
5910 * @param slotIndex - device slot.
5911 *
5912 * @return true on success; false on any failure
5913 */
5914 @Override
5915 public boolean rebootModem(int slotIndex) {
5916 Phone phone = PhoneFactory.getPhone(slotIndex);
5917 if (phone != null) {
5918 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5919 mApp, phone.getSubId(), "rebootModem");
5920
5921 final long identity = Binder.clearCallingIdentity();
5922 try {
5923 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5924 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5925 return success;
5926 } finally {
5927 Binder.restoreCallingIdentity(identity);
5928 }
5929 }
5930 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005931 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005932
Brad Ebinger51f743a2017-01-23 13:50:20 -08005933 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005934 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5935 * {@link #disableIms(int)}.
5936 * @param slotIndex device slot.
5937 */
5938 public void resetIms(int slotIndex) {
5939 enforceModifyPermission();
5940
5941 final long identity = Binder.clearCallingIdentity();
5942 try {
5943 if (mImsResolver == null) {
5944 // may happen if the does not support IMS.
5945 return;
5946 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005947 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005948 } finally {
5949 Binder.restoreCallingIdentity(identity);
5950 }
5951 }
5952
5953 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005954 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5955 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005956 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005957 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005958 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005959
5960 final long identity = Binder.clearCallingIdentity();
5961 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005962 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005963 // may happen if the device does not support IMS.
5964 return;
5965 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005966 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005967 } finally {
5968 Binder.restoreCallingIdentity(identity);
5969 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005970 }
5971
5972 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005973 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5974 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005975 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005976 public void disableIms(int slotId) {
5977 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005978
5979 final long identity = Binder.clearCallingIdentity();
5980 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005981 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005982 // may happen if the device does not support IMS.
5983 return;
5984 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005985 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005986 } finally {
5987 Binder.restoreCallingIdentity(identity);
5988 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005989 }
5990
5991 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005992 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5993 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005994 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005995 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005996 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005997 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005998
5999 final long identity = Binder.clearCallingIdentity();
6000 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006001 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07006002 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6003 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006004 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07006005 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006006 } finally {
6007 Binder.restoreCallingIdentity(identity);
6008 }
Brad Ebinger34bef922017-11-09 10:27:08 -08006009 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006010 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006011 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
6012 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07006013 @Override
6014 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006015 enforceModifyPermission();
6016
6017 final long identity = Binder.clearCallingIdentity();
6018 try {
6019 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07006020 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006021 } finally {
6022 Binder.restoreCallingIdentity(identity);
6023 }
6024 }
6025
6026 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08006027 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006028 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08006029 */
6030 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
6031 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006032
6033 final long identity = Binder.clearCallingIdentity();
6034 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006035 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006036 // may happen if the device does not support IMS.
6037 return null;
6038 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006039 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006040 } finally {
6041 Binder.restoreCallingIdentity(identity);
6042 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006043 }
6044
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006045 /**
6046 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006047 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006048 */
6049 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6050 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006051
6052 final long identity = Binder.clearCallingIdentity();
6053 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006054 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006055 // may happen if the device does not support IMS.
6056 return null;
6057 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006058 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006059 } finally {
6060 Binder.restoreCallingIdentity(identity);
6061 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006062 }
6063
Brad Ebinger884c07b2018-02-15 16:17:40 -08006064 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006065 * Sets the ImsService Package Name that Telephony will bind to.
6066 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006067 * @param slotIndex the slot ID that the ImsService should bind for.
6068 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006069 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006070 * @param featureTypes An integer array of feature types associated with a packageName.
6071 * @param packageName The name of the package that the current configuration will be replaced
6072 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006073 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006074 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006075 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6076 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006077 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006078 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006079 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006080
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006081 final long identity = Binder.clearCallingIdentity();
6082 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006083 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006084 // may happen if the device does not support IMS.
6085 return false;
6086 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006087 Map<Integer, String> featureConfig = new HashMap<>();
6088 for (int featureType : featureTypes) {
6089 featureConfig.put(featureType, packageName);
6090 }
6091 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6092 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006093 } finally {
6094 Binder.restoreCallingIdentity(identity);
6095 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006096 }
6097
6098 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006099 * Clears any carrier ImsService overrides for the slot index specified that were previously
6100 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6101 *
6102 * This should only be used for testing.
6103 *
6104 * @param slotIndex the slot ID that the ImsService should bind for.
6105 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6106 */
6107 @Override
6108 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006109 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6110 "clearCarrierImsServiceOverride");
6111 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006112 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006113
6114 final long identity = Binder.clearCallingIdentity();
6115 try {
6116 if (mImsResolver == null) {
6117 // may happen if the device does not support IMS.
6118 return false;
6119 }
6120 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6121 } finally {
6122 Binder.restoreCallingIdentity(identity);
6123 }
6124 }
6125
6126 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006127 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006128 *
6129 * @param slotId The slot that the ImsService is associated with.
6130 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6131 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006132 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006133 * @return the package name of the ImsService configuration.
6134 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006135 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6136 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006137 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006138 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6139 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006140
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006141 final long identity = Binder.clearCallingIdentity();
6142 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006143 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006144 // may happen if the device does not support IMS.
6145 return "";
6146 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006147 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006148 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6149 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006150 } finally {
6151 Binder.restoreCallingIdentity(identity);
6152 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006153 }
6154
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006155 /**
6156 * Get the MmTelFeature state associated with the requested subscription id.
6157 * @param subId The subscription that the MmTelFeature is associated with.
6158 * @param callback A callback with an integer containing the
6159 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6160 */
6161 @Override
6162 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6163 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006164 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6165 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6166 "IMS not available on device.");
6167 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006168 final long token = Binder.clearCallingIdentity();
6169 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006170 int slotId = getSlotIndex(subId);
6171 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6172 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6173 + subId + "'");
6174 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6175 }
6176 verifyImsMmTelConfiguredOrThrow(slotId);
6177 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6178 try {
6179 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6180 } catch (RemoteException e) {
6181 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6182 + "Ignore");
6183 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006184 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006185 } catch (ImsException e) {
6186 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006187 } finally {
6188 Binder.restoreCallingIdentity(token);
6189 }
6190 }
6191
Daniel Brightbb5840b2021-01-12 15:48:18 -08006192 /**
6193 * Sets the ims registration state on all valid {@link Phone}s.
6194 */
6195 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006196 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006197
6198 final long identity = Binder.clearCallingIdentity();
6199 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006200 // NOTE: Before S, this method only set the default phone.
6201 for (final Phone phone : PhoneFactory.getPhones()) {
6202 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6203 phone.setImsRegistrationState(registered);
6204 }
6205 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006206 } finally {
6207 Binder.restoreCallingIdentity(identity);
6208 }
Wink Saville36469e72014-06-11 15:17:00 -07006209 }
6210
6211 /**
Stuart Scott54788802015-03-30 13:18:01 -07006212 * Set the network selection mode to automatic.
6213 *
6214 */
6215 @Override
6216 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006217 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6218 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006219
6220 final long identity = Binder.clearCallingIdentity();
6221 try {
shilufc958392020-01-20 11:36:01 -08006222 if (!isActiveSubscription(subId)) {
6223 return;
6224 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006225 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006226 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6227 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006228 } finally {
6229 Binder.restoreCallingIdentity(identity);
6230 }
Stuart Scott54788802015-03-30 13:18:01 -07006231 }
6232
Jack Yud10cdd42020-09-28 20:28:01 -07006233 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006234 * Ask the radio to connect to the input network and change selection mode to manual.
6235 *
6236 * @param subId the id of the subscription.
6237 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6238 * the operator to attach to.
6239 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6240 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6241 * normal network selection next time.
6242 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006243 */
6244 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006245 public boolean setNetworkSelectionModeManual(
6246 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006247 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6248 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006249
Jack Yu285100e2022-12-02 22:48:35 -08006250 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006251 if (!isActiveSubscription(subId)) {
6252 return false;
6253 }
6254
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006255 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006256 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006257 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006258 if (DBG) {
6259 log("setNetworkSelectionModeManual: subId: " + subId
6260 + " operator: " + operatorInfo);
6261 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006262 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6263 } finally {
6264 Binder.restoreCallingIdentity(identity);
6265 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006266 }
shilu84f6e8b2019-12-19 13:58:01 -08006267 /**
6268 * Get the manual network selection
6269 *
6270 * @param subId the id of the subscription.
6271 *
6272 * @return the previously saved user selected PLMN
6273 */
6274 @Override
6275 public String getManualNetworkSelectionPlmn(int subId) {
6276 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006277 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006278 mApp, subId, "getManualNetworkSelectionPlmn");
6279
6280 final long identity = Binder.clearCallingIdentity();
6281 try {
6282 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006283 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006284 }
6285
6286 final Phone phone = getPhone(subId);
6287 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006288 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006289 }
6290 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6291 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6292 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6293 } finally {
6294 Binder.restoreCallingIdentity(identity);
6295 }
6296 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006297
6298 /**
6299 * Scans for available networks.
6300 */
6301 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006302 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6303 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006304 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6305 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006306 LocationAccessPolicy.LocationPermissionResult locationResult =
6307 LocationAccessPolicy.checkLocationPermission(mApp,
6308 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6309 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006310 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006311 .setCallingPid(Binder.getCallingPid())
6312 .setCallingUid(Binder.getCallingUid())
6313 .setMethod("getCellNetworkScanResults")
6314 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006315 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6316 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006317 .build());
6318 switch (locationResult) {
6319 case DENIED_HARD:
6320 throw new SecurityException("Not allowed to access scan results -- location");
6321 case DENIED_SOFT:
6322 return null;
6323 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006324
Pengquan Menga1bb6272018-09-06 09:59:22 -07006325 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006326 try {
6327 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006328 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006329 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006330 } finally {
6331 Binder.restoreCallingIdentity(identity);
6332 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006333 }
6334
6335 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006336 * Get the call forwarding info, given the call forwarding reason.
6337 */
6338 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006339 public void getCallForwarding(int subId, int callForwardingReason,
6340 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006341 enforceReadPrivilegedPermission("getCallForwarding");
6342 long identity = Binder.clearCallingIdentity();
6343 try {
6344 if (DBG) {
6345 log("getCallForwarding: subId " + subId
6346 + " callForwardingReason" + callForwardingReason);
6347 }
Hall Liu27d24262020-09-18 19:04:59 -07006348
6349 Phone phone = getPhone(subId);
6350 if (phone == null) {
6351 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006352 callback.onError(
6353 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006354 } catch (RemoteException e) {
6355 // ignore
6356 }
6357 return;
6358 }
6359
6360 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6361 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6362 @Override
6363 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6364 try {
6365 callback.onCallForwardingInfoAvailable(info);
6366 } catch (RemoteException e) {
6367 // ignore
6368 }
6369 }
6370
6371 @Override
6372 public void onError(int error) {
6373 try {
6374 callback.onError(error);
6375 } catch (RemoteException e) {
6376 // ignore
6377 }
6378 }
6379 });
6380 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006381 } finally {
6382 Binder.restoreCallingIdentity(identity);
6383 }
6384 }
6385
6386 /**
6387 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6388 * reason, the number to forward, and the timeout before the forwarding is attempted.
6389 */
6390 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006391 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6392 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006393 enforceModifyPermission();
6394 long identity = Binder.clearCallingIdentity();
6395 try {
6396 if (DBG) {
6397 log("setCallForwarding: subId " + subId
6398 + " callForwardingInfo" + callForwardingInfo);
6399 }
Hall Liu27d24262020-09-18 19:04:59 -07006400
6401 Phone phone = getPhone(subId);
6402 if (phone == null) {
6403 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006404 callback.accept(
6405 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006406 } catch (RemoteException e) {
6407 // ignore
6408 }
6409 return;
6410 }
6411
6412 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6413 FunctionalUtils.ignoreRemoteException(callback::accept));
6414
6415 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006416 } finally {
6417 Binder.restoreCallingIdentity(identity);
6418 }
6419 }
6420
6421 /**
Hall Liu27d24262020-09-18 19:04:59 -07006422 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006423 */
6424 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006425 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006426 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006427 long identity = Binder.clearCallingIdentity();
6428 try {
Hall Liu27d24262020-09-18 19:04:59 -07006429 Phone phone = getPhone(subId);
6430 if (phone == null) {
6431 try {
6432 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6433 } catch (RemoteException e) {
6434 // ignore
6435 }
6436 return;
6437 }
SongFerngWang0e767992021-03-31 22:08:45 +08006438 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6439 PersistableBundle c = configManager.getConfigForSubId(subId);
6440 boolean requireUssd = c.getBoolean(
6441 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006442
Shuo Qian4a594052020-01-23 11:59:30 -08006443 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006444 if (requireUssd) {
6445 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6446 getSubscriptionCarrierId(subId));
6447 String newUssdCommand = "";
6448 try {
6449 newUssdCommand = carrierXmlParser.getFeature(
6450 CarrierXmlParser.FEATURE_CALL_WAITING)
6451 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6452 } catch (NullPointerException e) {
6453 loge("Failed to generate USSD number" + e);
6454 }
6455 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6456 mMainThreadHandler, callback, carrierXmlParser,
6457 CarrierXmlParser.SsEntry.SSAction.QUERY);
6458 final String ussdCommand = newUssdCommand;
6459 Executors.newSingleThreadExecutor().execute(() -> {
6460 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6461 });
6462 } else {
6463 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6464 callback::accept);
6465 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6466 }
Shuo Qian4a594052020-01-23 11:59:30 -08006467 } finally {
6468 Binder.restoreCallingIdentity(identity);
6469 }
6470 }
6471
6472 /**
Hall Liu27d24262020-09-18 19:04:59 -07006473 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006474 */
6475 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006476 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006477 enforceModifyPermission();
6478 long identity = Binder.clearCallingIdentity();
6479 try {
Hall Liu27d24262020-09-18 19:04:59 -07006480 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6481
6482 Phone phone = getPhone(subId);
6483 if (phone == null) {
6484 try {
6485 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6486 } catch (RemoteException e) {
6487 // ignore
6488 }
6489 return;
6490 }
6491
SongFerngWang0e767992021-03-31 22:08:45 +08006492 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6493 PersistableBundle c = configManager.getConfigForSubId(subId);
6494 boolean requireUssd = c.getBoolean(
6495 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006496
SongFerngWang0e767992021-03-31 22:08:45 +08006497 if (DBG) log("getCallWaitingStatus: subId " + subId);
6498 if (requireUssd) {
6499 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6500 getSubscriptionCarrierId(subId));
6501 CarrierXmlParser.SsEntry.SSAction ssAction =
6502 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6503 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6504 String newUssdCommand = "";
6505 try {
6506 newUssdCommand = carrierXmlParser.getFeature(
6507 CarrierXmlParser.FEATURE_CALL_WAITING)
6508 .makeCommand(ssAction, null);
6509 } catch (NullPointerException e) {
6510 loge("Failed to generate USSD number" + e);
6511 }
6512 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6513 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6514 final String ussdCommand = newUssdCommand;
6515 Executors.newSingleThreadExecutor().execute(() -> {
6516 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6517 });
6518 } else {
6519 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6520 FunctionalUtils.ignoreRemoteException(callback::accept));
6521
6522 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6523 }
Shuo Qian4a594052020-01-23 11:59:30 -08006524 } finally {
6525 Binder.restoreCallingIdentity(identity);
6526 }
6527 }
6528
6529 /**
yinxub1bed742017-04-17 11:45:04 -07006530 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006531 *
yinxub1bed742017-04-17 11:45:04 -07006532 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006533 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6534 * location related information which will be sent if the caller already possess
6535 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006536 * @param request contains the radio access networks with bands/channels to scan
6537 * @param messenger callback messenger for scan results or errors
6538 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006539 * @return the id of the requested scan which can be used to stop the scan.
6540 */
6541 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006542 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6543 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006544 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006545 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6546 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006547 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006548 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6549 if (!renounceFineLocationAccess) {
6550 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6551 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6552 .setCallingPackage(callingPackage)
6553 .setCallingFeatureId(callingFeatureId)
6554 .setCallingPid(Binder.getCallingPid())
6555 .setCallingUid(Binder.getCallingUid())
6556 .setMethod("requestNetworkScan")
6557 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6558 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6559 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6560 .build());
6561 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006562 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006563 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6564 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006565 if (e != null) {
6566 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6567 throw e;
6568 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006569 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006570 return TelephonyScanManager.INVALID_SCAN_ID;
6571 }
6572 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006573 }
Hall Liu912dfd32019-04-25 14:02:26 -07006574 int callingUid = Binder.getCallingUid();
6575 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006576 final long identity = Binder.clearCallingIdentity();
6577 try {
6578 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006579 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006580 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006581 } finally {
6582 Binder.restoreCallingIdentity(identity);
6583 }
yinxu504e1392017-04-12 16:03:22 -07006584 }
6585
Hall Liub2ac8ef2019-02-28 15:56:23 -08006586 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006587 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006588 boolean hasCarrierPriv;
6589 final long identity = Binder.clearCallingIdentity();
6590 try {
6591 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6592 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6593 } finally {
6594 Binder.restoreCallingIdentity(identity);
6595 }
Hall Liu558027f2019-05-15 19:14:05 -07006596 boolean hasNetworkScanPermission =
6597 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6598 == PERMISSION_GRANTED;
6599
6600 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6601 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6602 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006603 }
6604
6605 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6606 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006607 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6608 return new SecurityException("Specific channels must not be"
6609 + " scanned without location access.");
6610 }
6611 }
6612 }
6613
Hall Liub2ac8ef2019-02-28 15:56:23 -08006614 return null;
6615 }
6616
yinxu504e1392017-04-12 16:03:22 -07006617 /**
6618 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006619 *
6620 * @param subId id of the subscription
6621 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006622 */
6623 @Override
6624 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006625 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6626 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006627
Hall Liu912dfd32019-04-25 14:02:26 -07006628 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006629 final long identity = Binder.clearCallingIdentity();
6630 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006631 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006632 } finally {
6633 Binder.restoreCallingIdentity(identity);
6634 }
yinxu504e1392017-04-12 16:03:22 -07006635 }
6636
6637 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006638 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006639 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006640 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006641 */
6642 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006643 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006644 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006645 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006646 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006647
6648 final long identity = Binder.clearCallingIdentity();
6649 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006650 if (DBG) log("getAllowedNetworkTypesBitmask");
6651 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6652 int networkTypesBitmask = (result != null ? result[0] : -1);
6653 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6654 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006655 } finally {
6656 Binder.restoreCallingIdentity(identity);
6657 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006658 }
6659
6660 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006661 * Get the allowed network types for certain reason.
6662 *
6663 * @param subId the id of the subscription.
6664 * @param reason the reason the allowed network type change is taking place
6665 * @return the allowed network types.
6666 */
6667 @Override
6668 public long getAllowedNetworkTypesForReason(int subId,
6669 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006670 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006671 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006672 final long identity = Binder.clearCallingIdentity();
6673 try {
6674 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6675 } finally {
6676 Binder.restoreCallingIdentity(identity);
6677 }
6678 }
6679
6680 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006681 * Enable/Disable E-UTRA-NR Dual Connectivity
6682 * @param subId subscription id of the sim card
6683 * @param nrDualConnectivityState expected NR dual connectivity state
6684 * This can be passed following states
6685 * <ol>
6686 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6687 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6688 * <li>Disable NR dual connectivity and force secondary cell to be released
6689 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6690 * </ol>
6691 * @return operation result.
6692 */
6693 @Override
6694 public int setNrDualConnectivityState(int subId,
6695 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6696 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6697 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006698 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006699 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6700 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6701 }
6702
Sooraj Sasindran37444802020-08-11 10:40:43 -07006703 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6704 final long identity = Binder.clearCallingIdentity();
6705 try {
6706 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6707 nrDualConnectivityState, subId,
6708 workSource);
6709 if (DBG) log("enableNRDualConnectivity result: " + result);
6710 return result;
6711 } finally {
6712 Binder.restoreCallingIdentity(identity);
6713 }
6714 }
6715
6716 /**
6717 * Is E-UTRA-NR Dual Connectivity enabled
6718 * @return true if dual connectivity is enabled else false
6719 */
6720 @Override
6721 public boolean isNrDualConnectivityEnabled(int subId) {
6722 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006723 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006724 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006725 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006726 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6727 return false;
6728 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006729 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6730 final long identity = Binder.clearCallingIdentity();
6731 try {
6732 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6733 null, subId, workSource);
6734 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6735 return isEnabled;
6736 } finally {
6737 Binder.restoreCallingIdentity(identity);
6738 }
6739 }
6740
6741 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006742 * Set the allowed network types of the device and
6743 * provide the reason triggering the allowed network change.
6744 *
6745 * @param subId the id of the subscription.
6746 * @param reason the reason the allowed network type change is taking place
6747 * @param allowedNetworkTypes the allowed network types.
6748 * @return true on success; false on any failure.
6749 */
6750 @Override
6751 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006752 @TelephonyManager.AllowedNetworkTypesReason int reason,
6753 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006754 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6755 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006756 // If the caller only has carrier privileges, then they should not be able to override
6757 // any network types which were set for security reasons.
6758 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6759 != PERMISSION_GRANTED
6760 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6761 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6762 throw new SecurityException(
6763 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6764 + " reason "
6765 + reason);
6766 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006767 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006768 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6769 return false;
6770 }
6771 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6772 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006773 return false;
6774 }
6775
Jack Yue37dd262022-12-16 11:53:37 -08006776 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason=" + reason + " value: "
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006777 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6778
Jack Yue37dd262022-12-16 11:53:37 -08006779 Phone phone = getPhone(subId);
6780 if (phone == null) {
6781 return false;
6782 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006783
Jack Yue37dd262022-12-16 11:53:37 -08006784 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006785 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6786 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006787 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006788
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006789 final long identity = Binder.clearCallingIdentity();
6790 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006791 Boolean success = (Boolean) sendRequest(
6792 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6793 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6794
6795 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6796 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006797 } finally {
6798 Binder.restoreCallingIdentity(identity);
6799 }
6800 }
6801
6802 /**
Miaoa84611c2019-03-15 09:21:10 +08006803 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006804 *
Miaoa84611c2019-03-15 09:21:10 +08006805 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006806 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006807 * @hide
6808 */
6809 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006810 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006811 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006812 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006813 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006814 try {
Miaoa84611c2019-03-15 09:21:10 +08006815 if (phone != null) {
6816 return phone.hasMatchedTetherApnSetting();
6817 } else {
6818 return false;
6819 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006820 } finally {
6821 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006822 }
Junda Liu475951f2014-11-07 16:45:03 -08006823 }
6824
6825 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006826 * Get the user enabled state of Mobile Data.
6827 *
6828 * TODO: remove and use isUserDataEnabled.
6829 * This can't be removed now because some vendor codes
6830 * calls through ITelephony directly while they should
6831 * use TelephonyManager.
6832 *
6833 * @return true on enabled
6834 */
6835 @Override
6836 public boolean getDataEnabled(int subId) {
6837 return isUserDataEnabled(subId);
6838 }
6839
6840 /**
6841 * Get whether mobile data is enabled per user setting.
6842 *
6843 * There are other factors deciding whether mobile data is actually enabled, but they are
6844 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006845 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006846 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6847 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006848 *
6849 * @return {@code true} if data is enabled else {@code false}
6850 */
6851 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006852 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006853 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006854 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006855 try {
6856 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6857 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006858 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006859 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6860 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006861 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006862 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006863 mApp, subId, functionName);
6864
Robert Greenwalt646120a2014-05-23 11:54:03 -07006865 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006866
6867 final long identity = Binder.clearCallingIdentity();
6868 try {
Jack Yu285100e2022-12-02 22:48:35 -08006869 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006870 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6871 Phone phone = PhoneFactory.getPhone(phoneId);
6872 if (phone != null) {
6873 boolean retVal = phone.isUserDataEnabled();
6874 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6875 return retVal;
6876 } else {
6877 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6878 return false;
6879 }
6880 } finally {
6881 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006882 }
6883 }
6884
6885 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006886 * Checks if the device is capable of mobile data by considering whether whether the
6887 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6888 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006889 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006890 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006891 */
6892 @Override
6893 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006894 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006895 try {
6896 try {
6897 mApp.enforceCallingOrSelfPermission(
6898 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006899 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006900 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006901 try {
6902 mApp.enforceCallingOrSelfPermission(
6903 android.Manifest.permission.READ_PHONE_STATE,
6904 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006905 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006906 mApp.enforceCallingOrSelfPermission(
6907 permission.READ_BASIC_PHONE_STATE, functionName);
6908 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006909 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006910 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006911 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006912 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006913
6914 final long identity = Binder.clearCallingIdentity();
6915 try {
Jack Yu285100e2022-12-02 22:48:35 -08006916 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006917 Phone phone = PhoneFactory.getPhone(phoneId);
6918 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006919 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006920 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006921 return retVal;
6922 } else {
6923 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6924 return false;
6925 }
6926 } finally {
6927 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006928 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006929 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006930
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006931 /**
6932 * Check if data is enabled for a specific reason
6933 * @param subId Subscription index
6934 * @param reason the reason the data enable change is taking place
6935 * @return {@code true} if the overall data is enabled; {@code false} if not.
6936 */
6937 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006938 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006939 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006940 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006941 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006942 try {
6943 mApp.enforceCallingOrSelfPermission(
6944 android.Manifest.permission.ACCESS_NETWORK_STATE,
6945 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006946 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006947 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6948 functionName);
6949 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006950 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006951 try {
6952 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006953 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006954 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006955 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006956 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006957 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006958 }
6959
6960
6961 final long identity = Binder.clearCallingIdentity();
6962 try {
Jack Yu285100e2022-12-02 22:48:35 -08006963 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006964 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006965 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006966 + " reason=" + reason);
6967 }
6968 Phone phone = PhoneFactory.getPhone(phoneId);
6969 if (phone != null) {
6970 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006971 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006972 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006973 return retVal;
6974 } else {
6975 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006976 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006977 + subId + " retVal=false");
6978 }
6979 return false;
6980 }
6981 } finally {
6982 Binder.restoreCallingIdentity(identity);
6983 }
6984 }
6985
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006986 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006987 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006988 // No permission needed; this only lets the caller inspect their own status.
6989 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006990 }
Junda Liu29340342014-07-10 15:23:27 -07006991
6992 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006993 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006994 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006995 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6996 }
6997
6998 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6999 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08007000 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09007001 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08007002 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7003 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007004 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7005 if (cpt == null) {
7006 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08007007 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7008 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007009 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08007010 }
7011
7012 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007013 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07007014 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07007015 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007016 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07007017 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007018 Phone phone = getPhone(subId);
7019 if (phone == null) {
7020 loge("checkCarrierPrivilegesForPackage: Invalid subId");
7021 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7022 }
7023 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7024 if (cpt == null) {
7025 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07007026 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7027 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007028 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007029 }
7030
7031 @Override
7032 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007033 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007034 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7035 }
7036
7037 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007038 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007039 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007040 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007041 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007042 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7043 Phone phone = PhoneFactory.getPhone(phoneId);
7044 if (phone == null) {
7045 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007046 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007047 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7048 if (cpt == null) {
7049 continue;
7050 }
7051 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007052 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7053 break;
7054 }
7055 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007056 return result;
Junda Liu29340342014-07-10 15:23:27 -07007057 }
Derek Tan89e89d42014-07-08 17:00:10 -07007058
7059 @Override
Junda Liue64de782015-04-16 17:19:16 -07007060 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007061 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007062 Phone phone = PhoneFactory.getPhone(phoneId);
7063 if (phone == null) {
7064 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007065 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007066 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7067 if (cpt == null) {
7068 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007069 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007070 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007071 }
7072
Amith Yamasani6e118872016-02-19 12:53:51 -08007073 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007074 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007075 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007076 Phone phone = PhoneFactory.getPhone(phoneId);
7077 if (phone == null) {
7078 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007079 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007080 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7081 if (cpt == null) {
7082 return Collections.emptyList();
7083 }
7084 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007085 }
7086
chen xuf7e9fe82019-05-09 19:31:02 -07007087 @Override
7088 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007089 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007090 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007091 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007092 try {
7093 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7094 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7095 }
7096 } finally {
7097 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007098 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007099 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007100 }
7101
Rambo Wang6812ffb2022-03-15 16:54:17 -07007102 @Override
7103 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7104 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7105
7106 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7107 if (phone == null) {
7108 return null;
7109 }
7110 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7111 if (cpt == null) {
7112 return null;
7113 }
7114 return cpt.getCarrierServicePackageName();
7115 }
7116
Wink Savilleb564aae2014-10-23 10:18:09 -07007117 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007118 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007119 UiccPort port = phone == null ? null : phone.getUiccPort();
7120 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007121 return null;
7122 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007123 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007124 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007125 return null;
7126 }
7127 return iccId;
7128 }
7129
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007130 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007131 public void setCallComposerStatus(int subId, int status) {
7132 enforceModifyPermission();
7133
7134 final long identity = Binder.clearCallingIdentity();
7135 try {
7136 Phone phone = getPhone(subId);
7137 if (phone != null) {
7138 Phone defaultPhone = phone.getImsPhone();
7139 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7140 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7141 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007142 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7143 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007144 }
7145 }
Shuo Qian284ae752020-12-22 19:10:14 -08007146 } catch (ImsException e) {
7147 throw new ServiceSpecificException(e.getCode());
7148 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007149 Binder.restoreCallingIdentity(identity);
7150 }
7151 }
7152
7153 @Override
7154 public int getCallComposerStatus(int subId) {
7155 enforceReadPrivilegedPermission("getCallComposerStatus");
7156
7157 final long identity = Binder.clearCallingIdentity();
7158 try {
7159 Phone phone = getPhone(subId);
7160 if (phone != null) {
7161 Phone defaultPhone = phone.getImsPhone();
7162 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7163 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7164 return imsPhone.getCallComposerStatus();
7165 }
7166 }
7167 } finally {
7168 Binder.restoreCallingIdentity(identity);
7169 }
7170 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7171 }
7172
7173 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007174 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7175 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007176 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007177 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007178
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007179 final long identity = Binder.clearCallingIdentity();
7180 try {
7181 final String iccId = getIccId(subId);
7182 final Phone phone = getPhone(subId);
7183 if (phone == null) {
7184 return false;
7185 }
7186 final String subscriberId = phone.getSubscriberId();
7187
7188 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007189 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007190 + subscriberId + " to " + number);
7191 }
7192
7193 if (TextUtils.isEmpty(iccId)) {
7194 return false;
7195 }
7196
7197 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7198
7199 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7200 if (alphaTag == null) {
7201 editor.remove(alphaTagPrefKey);
7202 } else {
7203 editor.putString(alphaTagPrefKey, alphaTag);
7204 }
7205
7206 // Record both the line number and IMSI for this ICCID, since we need to
7207 // track all merged IMSIs based on line number
7208 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7209 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7210 if (number == null) {
7211 editor.remove(numberPrefKey);
7212 editor.remove(subscriberPrefKey);
7213 } else {
7214 editor.putString(numberPrefKey, number);
7215 editor.putString(subscriberPrefKey, subscriberId);
7216 }
7217
7218 editor.commit();
7219 return true;
7220 } finally {
7221 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007222 }
Derek Tan7226c842014-07-02 17:42:23 -07007223 }
7224
7225 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007226 public String getLine1NumberForDisplay(int subId, String callingPackage,
7227 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007228 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007229 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007230 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007231 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007232 return null;
7233 }
Derek Tan97ebb422014-09-05 16:55:38 -07007234
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007235 final long identity = Binder.clearCallingIdentity();
7236 try {
7237 String iccId = getIccId(subId);
7238 if (iccId != null) {
7239 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7240 if (DBG_MERGE) {
7241 log("getLine1NumberForDisplay returning "
7242 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7243 }
7244 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007245 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007246 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7247 return null;
7248 } finally {
7249 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007250 }
Derek Tan7226c842014-07-02 17:42:23 -07007251 }
7252
7253 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007254 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7255 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007256 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007257 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007258 return null;
7259 }
Derek Tan97ebb422014-09-05 16:55:38 -07007260
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007261 final long identity = Binder.clearCallingIdentity();
7262 try {
7263 String iccId = getIccId(subId);
7264 if (iccId != null) {
7265 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7266 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7267 }
7268 return null;
7269 } finally {
7270 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007271 }
Derek Tan7226c842014-07-02 17:42:23 -07007272 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007273
7274 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007275 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7276 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007277 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7278 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007279 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007280 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007281 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007282 return null;
7283 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007284
Jordan Liub49b04b2019-05-06 14:45:15 -07007285 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7286 // the process, where TelephonyManager was instantiated.
7287 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007288 final long identity = Binder.clearCallingIdentity();
7289 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007290 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007291 final TelephonyManager tele = TelephonyManager.from(context);
7292 final SubscriptionManager sub = SubscriptionManager.from(context);
7293
7294 // Figure out what subscribers are currently active
7295 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007296
Jordan Liub49b04b2019-05-06 14:45:15 -07007297 // Only consider subs which match the current subId
7298 // This logic can be simplified. See b/131189269 for progress.
7299 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007300 activeSubscriberIds.add(tele.getSubscriberId(subId));
7301 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007302
7303 // First pass, find a number override for an active subscriber
7304 String mergeNumber = null;
7305 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7306 for (String key : prefs.keySet()) {
7307 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7308 final String subscriberId = (String) prefs.get(key);
7309 if (activeSubscriberIds.contains(subscriberId)) {
7310 final String iccId = key.substring(
7311 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7312 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7313 mergeNumber = (String) prefs.get(numberKey);
7314 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007315 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007316 + " for active subscriber " + subscriberId);
7317 }
7318 if (!TextUtils.isEmpty(mergeNumber)) {
7319 break;
7320 }
7321 }
7322 }
7323 }
7324
7325 // Shortcut when no active merged subscribers
7326 if (TextUtils.isEmpty(mergeNumber)) {
7327 return null;
7328 }
7329
7330 // Second pass, find all subscribers under that line override
7331 final ArraySet<String> result = new ArraySet<>();
7332 for (String key : prefs.keySet()) {
7333 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7334 final String number = (String) prefs.get(key);
7335 if (mergeNumber.equals(number)) {
7336 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7337 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7338 final String subscriberId = (String) prefs.get(subscriberKey);
7339 if (!TextUtils.isEmpty(subscriberId)) {
7340 result.add(subscriberId);
7341 }
7342 }
7343 }
7344 }
7345
7346 final String[] resultArray = result.toArray(new String[result.size()]);
7347 Arrays.sort(resultArray);
7348 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007349 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007350 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7351 }
7352 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007353 } finally {
7354 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007355 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007356 }
7357
7358 @Override
zoey chen38003472019-12-13 17:16:31 +08007359 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7360 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007361
7362 final long identity = Binder.clearCallingIdentity();
7363 try {
7364 final TelephonyManager telephonyManager = mApp.getSystemService(
7365 TelephonyManager.class);
7366 String subscriberId = telephonyManager.getSubscriberId(subId);
7367 if (subscriberId == null) {
7368 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007369 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007370 + subId);
7371 }
7372 return null;
7373 }
7374
Jack Yu285100e2022-12-02 22:48:35 -08007375 ParcelUuid groupUuid;
7376 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7377 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7378 .getSubscriptionInfo(subId);
7379 groupUuid = info.getGroupUuid();
7380 } else {
7381 final SubscriptionInfo info = mSubscriptionController
7382 .getSubscriptionInfo(subId);
7383 groupUuid = info.getGroupUuid();
7384 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007385 // If it doesn't belong to any group, return just subscriberId of itself.
7386 if (groupUuid == null) {
7387 return new String[]{subscriberId};
7388 }
7389
7390 // Get all subscriberIds from the group.
7391 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007392 List<SubscriptionInfo> groupInfos;
7393 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7394 groupInfos = SubscriptionManagerService.getInstance()
7395 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7396 mApp.getAttributionTag());
7397 } else {
7398 groupInfos = mSubscriptionController
7399 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7400 mApp.getAttributionTag());
7401 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007402 for (SubscriptionInfo subInfo : groupInfos) {
7403 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7404 if (subscriberId != null) {
7405 mergedSubscriberIds.add(subscriberId);
7406 }
7407 }
7408
7409 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7410 } finally {
7411 Binder.restoreCallingIdentity(identity);
7412
7413 }
7414 }
7415
7416 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007417 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007418 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007419 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007420
7421 final long identity = Binder.clearCallingIdentity();
7422 try {
7423 final Phone phone = getPhone(subId);
7424 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7425 } finally {
7426 Binder.restoreCallingIdentity(identity);
7427 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007428 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007429
7430 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007431 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007432 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7433 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007434 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7435 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007436
7437 final long identity = Binder.clearCallingIdentity();
7438 try {
7439 final Phone phone = getPhone(subId);
7440 if (phone == null) {
7441 return false;
7442 }
7443 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7444 cdmaNonRoamingList);
7445 } finally {
7446 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007447 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007448 }
7449
7450 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007451 @Deprecated
7452 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7453 enforceModifyPermission();
7454
7455 int returnValue = 0;
7456 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007457 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007458 if(result.exception == null) {
7459 if (result.result != null) {
7460 byte[] responseData = (byte[])(result.result);
7461 if(responseData.length > oemResp.length) {
7462 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7463 responseData.length + "bytes. Buffer Size is " +
7464 oemResp.length + "bytes.");
7465 }
7466 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7467 returnValue = responseData.length;
7468 }
7469 } else {
7470 CommandException ex = (CommandException) result.exception;
7471 returnValue = ex.getCommandError().ordinal();
7472 if(returnValue > 0) returnValue *= -1;
7473 }
7474 } catch (RuntimeException e) {
7475 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7476 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7477 if(returnValue > 0) returnValue *= -1;
7478 }
7479
7480 return returnValue;
7481 }
7482
7483 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007484 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007485 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007486 try {
7487 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007488 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007489 mApp, phone.getSubId(), "getRadioAccessFamily");
7490 } catch (SecurityException e) {
7491 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7492 throw e;
7493 }
chen xub97461a2018-10-26 14:17:57 -07007494 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007495 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007496 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007497 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007498 final long identity = Binder.clearCallingIdentity();
7499 try {
chen xub97461a2018-10-26 14:17:57 -07007500 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007501 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007502 mApp, phone.getSubId(), "getRadioAccessFamily");
7503 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007504 } finally {
7505 Binder.restoreCallingIdentity(identity);
7506 }
chen xub97461a2018-10-26 14:17:57 -07007507 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007508 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007509
7510 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007511 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007512 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007513 try {
7514 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7515 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007516 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007517 }
7518 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007519 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007520 }
7521 RoleManager rm = mApp.getSystemService(RoleManager.class);
7522 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7523 if (!dialerRoleHolders.contains(callingPackage)) {
7524 throw new SecurityException("App must be the dialer role holder to"
7525 + " upload a call composer pic");
7526 }
7527
7528 Executors.newSingleThreadExecutor().execute(() -> {
7529 ByteArrayOutputStream output = new ByteArrayOutputStream(
7530 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7531 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7532 boolean readUntilEnd = false;
7533 int totalBytesRead = 0;
7534 byte[] buffer = new byte[16 * 1024];
7535 while (true) {
7536 int numRead;
7537 try {
7538 numRead = input.read(buffer);
7539 } catch (IOException e) {
7540 try {
7541 fd.checkError();
7542 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7543 null);
7544 } catch (IOException e1) {
7545 // This means that the other side closed explicitly with an error. If this
7546 // happens, log and ignore.
7547 loge("Remote end of call composer picture pipe closed: " + e1);
7548 }
7549 break;
7550 }
7551 if (numRead == -1) {
7552 readUntilEnd = true;
7553 break;
7554 }
7555 totalBytesRead += numRead;
7556 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7557 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7558 try {
7559 input.close();
7560 } catch (IOException e) {
7561 // ignore
7562 }
7563 break;
7564 }
7565 output.write(buffer, 0, numRead);
7566 }
7567 // Generally, the remote end will close the file descriptors. The only case where we
7568 // close is above, where the picture size is too big.
7569
7570 try {
7571 fd.checkError();
7572 } catch (IOException e) {
7573 loge("Remote end for call composer closed with an error: " + e);
7574 return;
7575 }
7576
Hall Liuaa4211e2021-01-20 15:43:39 -08007577 if (!readUntilEnd) {
7578 loge("Did not finish reading entire image; aborting");
7579 return;
7580 }
Hall Liu82694d52020-12-11 18:22:04 -08007581
Hall Liuaa4211e2021-01-20 15:43:39 -08007582 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7583 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7584 new CallComposerPictureTransfer.Factory() {},
7585 imageData,
7586 (result) -> {
7587 if (result.first != null) {
7588 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7589 Bundle outputResult = new Bundle();
7590 outputResult.putParcelable(
7591 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7592 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7593 outputResult);
7594 } else {
7595 callback.send(result.second, null);
7596 }
7597 }
7598 );
Hall Liu82694d52020-12-11 18:22:04 -08007599 });
7600 }
7601
7602 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007603 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007604 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007605 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007606
7607 final long identity = Binder.clearCallingIdentity();
7608 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007609 ImsManager.getInstance(defaultPhone.getContext(),
7610 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007611 } finally {
7612 Binder.restoreCallingIdentity(identity);
7613 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007614 }
7615
7616 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007617 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007618 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007619 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7620 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007621 return false;
7622 }
Svet Ganovb320e182015-04-16 12:30:10 -07007623
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007624 final long identity = Binder.clearCallingIdentity();
7625 try {
7626 // Check the user preference and the system-level IMS setting. Even if the user has
7627 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7628 // In the long run, we may instead need to check if there exists a connection service
7629 // which can support video calling.
7630 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007631 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007632 return imsManager.isVtEnabledByPlatform()
7633 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7634 && imsManager.isVtEnabledByUser();
7635 } finally {
7636 Binder.restoreCallingIdentity(identity);
7637 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007638 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007639
Andrew Leea1239f22015-03-02 17:44:07 -08007640 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007641 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7642 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007643 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007644 mApp, subId, callingPackage, callingFeatureId,
7645 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007646 return false;
7647 }
7648
7649 final long identity = Binder.clearCallingIdentity();
7650 try {
7651 CarrierConfigManager configManager =
7652 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007653 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007654 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7655 } finally {
7656 Binder.restoreCallingIdentity(identity);
7657 }
Andrew Leea1239f22015-03-02 17:44:07 -08007658 }
7659
7660 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007661 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007662 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007663 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007664 return false;
7665 }
7666
7667 final long identity = Binder.clearCallingIdentity();
7668 try {
7669 CarrierConfigManager configManager =
7670 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007671 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007672 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7673 } finally {
7674 Binder.restoreCallingIdentity(identity);
7675 }
Andrew Leea1239f22015-03-02 17:44:07 -08007676 }
7677
Andrew Lee9431b832015-03-09 18:46:45 -07007678 @Override
7679 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007680 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007681 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007682 }
7683
7684 @Override
7685 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007686 final long identity = Binder.clearCallingIdentity();
7687 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007688 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007689 } finally {
7690 Binder.restoreCallingIdentity(identity);
7691 }
Andrew Lee9431b832015-03-09 18:46:45 -07007692 }
7693
Hall Liuf6668912018-10-31 17:05:23 -07007694 /**
7695 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7696 * support for the feature and device firmware support.
7697 *
7698 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7699 */
7700 @Override
7701 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007702 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007703 final Phone phone = getPhone(subscriptionId);
7704 if (phone == null) {
7705 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7706 return false;
7707 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007708 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007709 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007710 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7711 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007712 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007713 return isCarrierSupported && isDeviceSupported;
7714 } finally {
7715 Binder.restoreCallingIdentity(identity);
7716 }
Hall Liu98187582018-01-22 19:15:32 -08007717 }
7718
Hall Liuf6668912018-10-31 17:05:23 -07007719 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007720 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7721 * RTT setting, will return true if the device and carrier both support RTT.
7722 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007723 */
7724 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007725 final long identity = Binder.clearCallingIdentity();
7726 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007727 boolean isRttSupported = isRttSupported(subscriptionId);
7728 boolean isUserRttSettingOn = Settings.Secure.getInt(
7729 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7730 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7731 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7732 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007733 } finally {
7734 Binder.restoreCallingIdentity(identity);
7735 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007736 }
7737
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007738 @Deprecated
7739 @Override
7740 public String getDeviceId(String callingPackage) {
7741 return getDeviceIdWithFeature(callingPackage, null);
7742 }
7743
Sanket Padawe7310cc72015-01-14 09:53:20 -08007744 /**
7745 * Returns the unique device ID of phone, for example, the IMEI for
7746 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7747 *
7748 * <p>Requires Permission:
7749 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7750 */
7751 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007752 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007753 try {
7754 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7755 } catch (SecurityException se) {
7756 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7757 throw new SecurityException("Package " + callingPackage + " does not belong to "
7758 + Binder.getCallingUid());
7759 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007760 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007761 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007762 return null;
7763 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007764 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007765 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007766 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007767 return null;
7768 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007769
7770 final long identity = Binder.clearCallingIdentity();
7771 try {
7772 return phone.getDeviceId();
7773 } finally {
7774 Binder.restoreCallingIdentity(identity);
7775 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007776 }
7777
Ping Sunc67b7c22016-03-02 19:16:45 +08007778 /**
7779 * {@hide}
7780 * Returns the IMS Registration Status on a particular subid
7781 *
7782 * @param subId
7783 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007784 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007785 Phone phone = getPhone(subId);
7786 if (phone != null) {
7787 return phone.isImsRegistered();
7788 } else {
7789 return false;
7790 }
7791 }
7792
Santos Cordon7a1885b2015-02-03 11:15:19 -08007793 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007794 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007795 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007796 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007797 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007798 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7799 }
7800 final long identity = Binder.clearCallingIdentity();
7801 try {
7802 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7803 } finally {
7804 Binder.restoreCallingIdentity(identity);
7805 }
7806 }
7807
7808 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007809 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007810 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007811 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007812 mApp,
7813 subscriptionId,
7814 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007815 final long identity = Binder.clearCallingIdentity();
7816 try {
7817 Phone phone = getPhone(subscriptionId);
7818 if (phone == null) {
7819 return null;
7820 }
7821 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7822 } finally {
7823 Binder.restoreCallingIdentity(identity);
7824 }
7825 }
7826
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007827 /**
7828 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007829 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007830 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007831 final long identity = Binder.clearCallingIdentity();
7832 try {
7833 Phone phone = getPhone(subId);
7834 if (phone != null) {
7835 return phone.isWifiCallingEnabled();
7836 } else {
7837 return false;
7838 }
7839 } finally {
7840 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007841 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007842 }
7843
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007844 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007845 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007846 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007847 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007848 final long identity = Binder.clearCallingIdentity();
7849 try {
7850 Phone phone = getPhone(subId);
7851 if (phone != null) {
7852 return phone.isVideoEnabled();
7853 } else {
7854 return false;
7855 }
7856 } finally {
7857 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007858 }
7859 }
7860
7861 /**
7862 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7863 * defined in {@link ImsRegistrationImplBase}.
7864 */
7865 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007866 final long identity = Binder.clearCallingIdentity();
7867 try {
7868 Phone phone = getPhone(subId);
7869 if (phone != null) {
7870 return phone.getImsRegistrationTech();
7871 } else {
7872 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7873 }
7874 } finally {
7875 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007876 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007877 }
7878
Stuart Scott8eef64f2015-04-08 15:13:54 -07007879 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007880 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007881 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007882 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7883 return;
7884 }
Kai Shif70f46f2021-03-03 13:59:46 -08007885 Phone defaultPhone = getDefaultPhone();
7886 if (defaultPhone != null) {
7887 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7888 mApp, getDefaultPhone().getSubId(), "factoryReset");
7889 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007890 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007891
Svet Ganovcc087f82015-05-12 20:35:54 -07007892 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007893 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7894 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007895 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007896 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007897 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007898 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007899 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007900 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007901 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007902 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007903 // There has been issues when Sms raw table somehow stores orphan
7904 // fragments. They lead to garbled message when new fragments come
7905 // in and combined with those stale ones. In case this happens again,
7906 // user can reset all network settings which will clean up this table.
7907 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007908 // Clean up IMS settings as well here.
7909 int slotId = getSlotIndex(subId);
7910 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7911 ImsManager.getInstance(mApp, slotId).factoryReset();
7912 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007913
Kai Shif70f46f2021-03-03 13:59:46 -08007914 if (defaultPhone == null) {
7915 return;
7916 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007917 // Erase modem config if erase modem on network setting is enabled.
7918 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7919 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7920 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007921 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007922 }
Kai Shif70f46f2021-03-03 13:59:46 -08007923
7924 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007925 } finally {
7926 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007927 }
7928 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007929
SongFerngWangfd89b102021-05-27 22:44:54 +08007930 @VisibleForTesting
7931 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7932 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7933 return;
7934 }
7935 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7936 RILConstants.PREFERRED_NETWORK_MODE);
7937 SubscriptionManager.setSubscriptionProperty(subId,
7938 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7939 "user=" + defaultNetworkType);
7940 phone.loadAllowedNetworksFromSubscriptionDatabase();
7941 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7942 defaultNetworkType, null);
7943 }
7944
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007945 private void cleanUpSmsRawTable(Context context) {
7946 ContentResolver resolver = context.getContentResolver();
7947 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7948 resolver.delete(uri, null, null);
7949 }
7950
Narayan Kamath1c496c22015-04-16 14:40:19 +01007951 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007952 public String getSimLocaleForSubscriber(int subId) {
7953 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7954 final Phone phone = getPhone(subId);
7955 if (phone == null) {
7956 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007957 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007958 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007959 final long identity = Binder.clearCallingIdentity();
7960 try {
Jack Yu285100e2022-12-02 22:48:35 -08007961 SubscriptionInfo info;
7962 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7963 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7964 phone.getContext().getOpPackageName(),
7965 phone.getContext().getAttributionTag());
7966 if (info == null) {
7967 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7968 return null;
7969 }
7970 } else {
7971 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7972 phone.getContext().getOpPackageName(),
7973 phone.getContext().getAttributionTag());
7974 if (info == null) {
7975 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7976 return null;
7977 }
chen xu6291c472019-02-04 12:55:53 -08007978 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007979 // Try and fetch the locale from the carrier properties or from the SIM language
7980 // preferences (EF-PL and EF-LI)...
7981 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007982 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007983 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7984 if (localeFromDefaultSim != null) {
7985 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7986 if (DBG) log("Using locale from subId: " + subId + " locale: "
7987 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007988 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007989 } else {
7990 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007991 }
7992 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007993
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007994 // The SIM language preferences only store a language (e.g. fr = French), not an
7995 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7996 // the SIM and carrier preferences does not include a country we add the country
7997 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007998 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007999 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08008000 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08008001 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008002 }
8003
8004 if (DBG) log("No locale found - returning null");
8005 return null;
8006 } finally {
8007 Binder.restoreCallingIdentity(identity);
8008 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008009 }
8010
tom hsu0b59d292022-09-29 23:49:21 +08008011 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08008012 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08008013 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08008014 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08008015 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08008016 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
8017 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08008018 Locale.forLanguageTag(localeTag).getCountry())) {
8019 return localeTag;
8020 }
8021 }
8022 return inputLocale.toLanguageTag();
8023 }
8024
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008025 /**
8026 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
8027 */
8028 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08008029 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8030 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
8031 mApp.getOpPackageName(), mApp.getAttributionTag());
8032 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008033 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008034 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008035 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008036
Gary Jian3aa9a762022-01-24 16:41:19 +08008037 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8038 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008039
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008040 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008041 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8042 * representing the state of the modem.
8043 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008044 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8045 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008046 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008047 */
8048 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008049 public void requestModemActivityInfo(ResultReceiver result) {
8050 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008051 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008052
8053 final long identity = Binder.clearCallingIdentity();
8054 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008055 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008056 } finally {
8057 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008058 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008059 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008060
Gary Jian76280a42022-12-07 16:18:33 +08008061 // Checks that ModemActivityInfo is valid. Sleep time and Idle time should be
Siddharth Rayb8114062018-06-17 15:02:38 -07008062 // less than total activity duration.
8063 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8064 if (info == null) {
8065 return false;
8066 }
8067 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008068 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
Gary Jian76280a42022-12-07 16:18:33 +08008069 activityDurationMs += MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS;
8070
Hall Liu49656c02020-10-09 19:00:11 -07008071 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8072
Siddharth Rayb8114062018-06-17 15:02:38 -07008073 return (info.isValid()
8074 && (info.getSleepTimeMillis() <= activityDurationMs)
Gary Jian76280a42022-12-07 16:18:33 +08008075 && (info.getIdleTimeMillis() <= activityDurationMs));
Siddharth Rayb8114062018-06-17 15:02:38 -07008076 }
8077
Gary Jian3aa9a762022-01-24 16:41:19 +08008078 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8079 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8080 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8081 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8082
8083 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8084 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8085 }
8086
8087 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8088 mLastModemActivityInfo.setReceiveTimeMillis(
8089 rat,
8090 freq,
8091 info.getReceiveTimeMillis(rat, freq)
8092 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8093 }
8094
8095 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8096 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8097 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8098 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8099
8100 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8101 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8102 }
8103 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8104 mLastModemActivityInfo.setReceiveTimeMillis(
8105 rat,
8106 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8107 }
8108
8109 /**
8110 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8111 * @param info recent ModemActivityInfo
8112 */
8113 private void mergeModemActivityInfo(ModemActivityInfo info) {
8114 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008115 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008116 boolean matched;
8117 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8118 matched = false;
8119 int rat = info.getSpecificInfoRat(i);
8120 int freq = info.getSpecificInfoFrequencyRange(i);
8121 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8122 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8123 //if it already exists
8124 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8125 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8126 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8127 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8128 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8129 updateLastModemActivityInfo(info, rat, freq);
8130 matched = true;
8131 }
8132 } else {
8133 updateLastModemActivityInfo(info, rat);
8134 matched = true;
8135 }
8136 }
8137 }
8138
8139 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008140 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008141 new ActivityStatsTechSpecificInfo(
8142 rat,
8143 freq,
8144 info.getTransmitTimeMillis(rat, freq),
8145 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008146 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008147 }
8148 }
8149 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8150 mLastModemActivitySpecificInfo =
8151 new ActivityStatsTechSpecificInfo[merged.size()];
8152 merged.toArray(mLastModemActivitySpecificInfo);
8153
8154 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8155 mLastModemActivityInfo.setSleepTimeMillis(
8156 info.getSleepTimeMillis()
8157 + mLastModemActivityInfo.getSleepTimeMillis());
8158 mLastModemActivityInfo.setIdleTimeMillis(
8159 info.getIdleTimeMillis()
8160 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008161
8162 mLastModemActivityInfo =
8163 new ModemActivityInfo(
8164 mLastModemActivityInfo.getTimestampMillis(),
8165 mLastModemActivityInfo.getSleepTimeMillis(),
8166 mLastModemActivityInfo.getIdleTimeMillis(),
8167 mLastModemActivitySpecificInfo);
8168 }
8169
8170 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8171 ActivityStatsTechSpecificInfo[] info) {
8172 int infoSize = info.length;
8173 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8174 for (int i = 0; i < infoSize; i++) {
8175 ret[i] = new ActivityStatsTechSpecificInfo(
8176 info[i].getRat(), info[i].getFrequencyRange(),
8177 info[i].getTransmitTimeMillis(),
8178 (int) info[i].getReceiveTimeMillis());
8179 }
8180 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008181 }
8182
Jack Yu85bd38a2015-11-09 11:34:32 -08008183 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008184 * Returns the service state information on specified subscription.
8185 */
8186 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008187 public ServiceState getServiceStateForSubscriber(int subId,
8188 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8189 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008190 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008191 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008192 return null;
8193 }
8194
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008195 boolean hasFinePermission = false;
8196 boolean hasCoarsePermission = false;
8197 if (!renounceFineLocationAccess) {
8198 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8199 LocationAccessPolicy.checkLocationPermission(mApp,
8200 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8201 .setCallingPackage(callingPackage)
8202 .setCallingFeatureId(callingFeatureId)
8203 .setCallingPid(Binder.getCallingPid())
8204 .setCallingUid(Binder.getCallingUid())
8205 .setMethod("getServiceStateForSubscriber")
8206 .setLogAsInfo(true)
8207 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8208 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8209 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8210 .build());
8211 hasFinePermission =
8212 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8213 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008214
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008215 if (!renounceCoarseLocationAccess) {
8216 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8217 LocationAccessPolicy.checkLocationPermission(mApp,
8218 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8219 .setCallingPackage(callingPackage)
8220 .setCallingFeatureId(callingFeatureId)
8221 .setCallingPid(Binder.getCallingPid())
8222 .setCallingUid(Binder.getCallingUid())
8223 .setMethod("getServiceStateForSubscriber")
8224 .setLogAsInfo(true)
8225 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8226 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8227 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8228 .build());
8229 hasCoarsePermission =
8230 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8231 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008232
Jack Yu479f40e2020-10-27 21:29:25 -07008233 final Phone phone = getPhone(subId);
8234 if (phone == null) {
8235 return null;
8236 }
8237
Jordan Liu0f2bc442020-11-18 16:47:37 -08008238 final long identity = Binder.clearCallingIdentity();
8239
Jack Yu479f40e2020-10-27 21:29:25 -07008240 boolean isCallingPackageDataService = phone.getDataServicePackages()
8241 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008242 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008243 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008244 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8245 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8246 .getSubscriptionInfoInternal(subId);
8247 if (subInfo == null || !subInfo.isActive()) {
8248 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8249 + "subId=" + subId);
8250 return null;
8251 }
8252 } else {
8253 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8254 callingFeatureId)) {
8255 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8256 + "subId=" + subId);
8257 return null;
8258 }
Jordan Liuc437b192020-08-17 10:59:12 -07008259 }
8260
Hall Liuf19c44f2018-11-27 14:38:17 -08008261 ServiceState ss = phone.getServiceState();
8262
8263 // Scrub out the location info in ServiceState depending on what level of access
8264 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008265 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008266 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8267 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008268 } finally {
8269 Binder.restoreCallingIdentity(identity);
8270 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008271 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008272
8273 /**
8274 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8275 *
8276 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8277 * voicemail ringtone.
8278 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8279 * PhoneAccount.
8280 */
8281 @Override
8282 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008283 final long identity = Binder.clearCallingIdentity();
8284 try {
8285 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8286 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008287 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008288 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008289
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008290 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8291 } finally {
8292 Binder.restoreCallingIdentity(identity);
8293 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008294 }
8295
8296 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008297 * Sets the per-account voicemail ringtone.
8298 *
8299 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8300 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8301 *
8302 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8303 * voicemail ringtone.
8304 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8305 * PhoneAccount.
8306 */
8307 @Override
8308 public void setVoicemailRingtoneUri(String callingPackage,
8309 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008310 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008311 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008312 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8313 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008314 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8315 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8316 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008317 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008318
8319 final long identity = Binder.clearCallingIdentity();
8320 try {
8321 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8322 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008323 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008324 }
8325 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8326 } finally {
8327 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008328 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008329 }
8330
8331 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008332 * Returns whether vibration is set for voicemail notification in Phone settings.
8333 *
8334 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8335 * voicemail vibration setting.
8336 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8337 */
8338 @Override
8339 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008340 final long identity = Binder.clearCallingIdentity();
8341 try {
8342 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8343 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008344 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008345 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008346
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008347 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8348 } finally {
8349 Binder.restoreCallingIdentity(identity);
8350 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008351 }
8352
Youhan Wange64578a2016-05-02 15:32:42 -07008353 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008354 * Sets the per-account voicemail vibration.
8355 *
8356 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8357 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8358 *
8359 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8360 * voicemail vibration setting.
8361 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8362 * specific PhoneAccount.
8363 */
8364 @Override
8365 public void setVoicemailVibrationEnabled(String callingPackage,
8366 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008367 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008368 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008369 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8370 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008371 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8372 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8373 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008374 }
8375
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008376 final long identity = Binder.clearCallingIdentity();
8377 try {
8378 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8379 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008380 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008381 }
8382 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8383 } finally {
8384 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008385 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008386 }
8387
8388 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008389 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8390 *
8391 * @throws SecurityException if the caller does not have the required permission
8392 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008393 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008394 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008395 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008396 }
8397
8398 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008399 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8400 * permission.
8401 *
8402 * @throws SecurityException if the caller does not have the required permission
8403 */
8404 private void enforceSendSmsPermission() {
8405 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8406 }
8407
8408 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008409 * Make sure either called from same process as self (phone) or IPC caller has interact across
8410 * users permission.
8411 *
8412 * @throws SecurityException if the caller does not have the required permission
8413 */
8414 private void enforceInteractAcrossUsersPermission(String message) {
8415 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8416 }
8417
8418 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008419 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008420 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008421 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008422 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008423 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008424 final long identity = Binder.clearCallingIdentity();
8425 try {
8426 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008427 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008428 if (componentName == null) {
8429 throw new SecurityException(
8430 "Caller not current active visual voicemail package[null]");
8431 }
8432 String vvmPackage = componentName.getPackageName();
8433 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008434 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008435 }
8436 } finally {
8437 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008438 }
8439 }
8440
8441 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008442 * Return the application ID for the app type.
8443 *
8444 * @param subId the subscription ID that this request applies to.
8445 * @param appType the uicc app type.
8446 * @return Application ID for specificied app type, or null if no uicc.
8447 */
8448 @Override
8449 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008450 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008451 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008452
8453 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008454 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008455 if (phone == null) {
8456 return null;
8457 }
8458 String aid = null;
8459 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008460 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008461 .getApplicationByType(appType).getAid();
8462 } catch (Exception e) {
8463 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8464 }
8465 return aid;
8466 } finally {
8467 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008468 }
Youhan Wange64578a2016-05-02 15:32:42 -07008469 }
8470
Youhan Wang4001d252016-05-11 10:29:41 -07008471 /**
8472 * Return the Electronic Serial Number.
8473 *
8474 * @param subId the subscription ID that this request applies to.
8475 * @return ESN or null if error.
8476 */
8477 @Override
8478 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008479 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008480 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008481
8482 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008483 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008484 if (phone == null) {
8485 return null;
8486 }
8487 String esn = null;
8488 try {
8489 esn = phone.getEsn();
8490 } catch (Exception e) {
8491 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8492 }
8493 return esn;
8494 } finally {
8495 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008496 }
Youhan Wang4001d252016-05-11 10:29:41 -07008497 }
8498
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008499 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008500 * Return the Preferred Roaming List Version.
8501 *
8502 * @param subId the subscription ID that this request applies to.
8503 * @return PRLVersion or null if error.
8504 */
8505 @Override
8506 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008507 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008508 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008509
8510 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008511 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008512 if (phone == null) {
8513 return null;
8514 }
8515 String cdmaPrlVersion = null;
8516 try {
8517 cdmaPrlVersion = phone.getCdmaPrlVersion();
8518 } catch (Exception e) {
8519 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8520 }
8521 return cdmaPrlVersion;
8522 } finally {
8523 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008524 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008525 }
8526
8527 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008528 * Get snapshot of Telephony histograms
8529 * @return List of Telephony histograms
8530 * @hide
8531 */
8532 @Override
8533 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008534 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8535 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008536
8537 final long identity = Binder.clearCallingIdentity();
8538 try {
8539 return RIL.getTelephonyRILTimingHistograms();
8540 } finally {
8541 Binder.restoreCallingIdentity(identity);
8542 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008543 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008544
8545 /**
8546 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008547 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8548 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008549 * Require system privileges. In the future we may add this to carrier APIs.
8550 *
Michele Berionne482f8202018-11-27 18:57:59 -08008551 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008552 */
8553 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008554 @TelephonyManager.SetCarrierRestrictionResult
8555 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008556 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008557 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008558
Michele Berionne482f8202018-11-27 18:57:59 -08008559 if (carrierRestrictionRules == null) {
8560 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008561 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008562
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008563 final long identity = Binder.clearCallingIdentity();
8564 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008565 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008566 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008567 } finally {
8568 Binder.restoreCallingIdentity(identity);
8569 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008570 }
8571
8572 /**
8573 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008574 * Get the allowed carrier list and the excluded carrier list, including the priority between
8575 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008576 * Require system privileges. In the future we may add this to carrier APIs.
8577 *
Michele Berionne482f8202018-11-27 18:57:59 -08008578 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008579 */
8580 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008581 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008582 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008583 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008584
8585 final long identity = Binder.clearCallingIdentity();
8586 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008587 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8588 if (response instanceof CarrierRestrictionRules) {
8589 return (CarrierRestrictionRules) response;
8590 }
8591 // Response is an Exception of some kind,
8592 // which is signalled to the user as a NULL retval
8593 return null;
8594 } catch (Exception e) {
8595 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8596 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008597 } finally {
8598 Binder.restoreCallingIdentity(identity);
8599 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008600 }
8601
fionaxu59545b42016-05-25 15:53:37 -07008602 /**
fionaxu59545b42016-05-25 15:53:37 -07008603 * Action set from carrier signalling broadcast receivers to enable/disable radio
8604 * @param subId the subscription ID that this action applies to.
8605 * @param enabled control enable or disable radio.
8606 * {@hide}
8607 */
8608 @Override
8609 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8610 enforceModifyPermission();
8611 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008612
8613 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008614 if (phone == null) {
8615 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8616 return;
8617 }
8618 try {
8619 phone.carrierActionSetRadioEnabled(enabled);
8620 } catch (Exception e) {
8621 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008622 } finally {
8623 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008624 }
8625 }
8626
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008627 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008628 * Enable or disable Voice over NR (VoNR)
8629 * @param subId the subscription ID that this action applies to.
8630 * @param enabled enable or disable VoNR.
8631 * @return operation result.
8632 */
8633 @Override
8634 public int setVoNrEnabled(int subId, boolean enabled) {
8635 enforceModifyPermission();
8636 final Phone phone = getPhone(subId);
8637
8638 final long identity = Binder.clearCallingIdentity();
8639 if (phone == null) {
8640 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8641 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8642 }
8643
8644 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8645 try {
8646 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8647 workSource);
8648 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008649
8650 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8651 if (DBG) {
8652 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8653 }
8654 SubscriptionManager.setSubscriptionProperty(
8655 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8656 (enabled ? "1" : "0"));
8657 }
8658
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008659 return result;
8660 } finally {
8661 Binder.restoreCallingIdentity(identity);
8662 }
8663 }
8664
8665 /**
8666 * Is voice over NR enabled
8667 * @return true if VoNR is enabled else false
8668 */
8669 @Override
8670 public boolean isVoNrEnabled(int subId) {
8671 enforceReadPrivilegedPermission("isVoNrEnabled");
8672 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8673 final long identity = Binder.clearCallingIdentity();
8674 try {
8675 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8676 null, subId, workSource);
8677 if (DBG) log("isVoNrEnabled: " + isEnabled);
8678 return isEnabled;
8679 } finally {
8680 Binder.restoreCallingIdentity(identity);
8681 }
8682 }
8683
8684 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008685 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8686 * network status based on which carrier apps could apply actions accordingly,
8687 * enable/disable default url handler for example.
8688 *
8689 * @param subId the subscription ID that this action applies to.
8690 * @param report control start/stop reporting the default network status.
8691 * {@hide}
8692 */
8693 @Override
8694 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8695 enforceModifyPermission();
8696 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008697
8698 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008699 if (phone == null) {
8700 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8701 return;
8702 }
8703 try {
8704 phone.carrierActionReportDefaultNetworkStatus(report);
8705 } catch (Exception e) {
8706 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008707 } finally {
8708 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008709 }
8710 }
8711
8712 /**
fionaxud9622282017-07-17 17:51:30 -07008713 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8714 * @param subId the subscription ID that this action applies to.
8715 * {@hide}
8716 */
8717 @Override
8718 public void carrierActionResetAll(int subId) {
8719 enforceModifyPermission();
8720 final Phone phone = getPhone(subId);
8721 if (phone == null) {
8722 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8723 return;
8724 }
8725 try {
8726 phone.carrierActionResetAll();
8727 } catch (Exception e) {
8728 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8729 }
8730 }
8731
8732 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008733 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8734 * bug report is being generated.
8735 */
8736 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008737 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008738 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8739 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008740 writer.println("Permission Denial: can't dump Phone from pid="
8741 + Binder.getCallingPid()
8742 + ", uid=" + Binder.getCallingUid()
8743 + "without permission "
8744 + android.Manifest.permission.DUMP);
8745 return;
8746 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008747 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008748 }
Jack Yueb89b242016-06-22 13:27:47 -07008749
Brad Ebingerdac2f002018-04-03 15:17:52 -07008750 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008751 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8752 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8753 @NonNull String[] args) {
8754 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8755 this, in.getFileDescriptor(), out.getFileDescriptor(),
8756 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008757 }
8758
Jack Yueb89b242016-06-22 13:27:47 -07008759 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008760 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008761 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008762 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008763 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008764 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008765 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008766 */
8767 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008768 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008769 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008770 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8771 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8772 try {
8773 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008774 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008775 } catch (SecurityException se) {
8776 enforceModifyPermission();
8777 }
8778 } else {
8779 enforceModifyPermission();
8780 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008781
8782 final long identity = Binder.clearCallingIdentity();
8783 try {
8784 Phone phone = getPhone(subId);
8785 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008786 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8787 phone.carrierActionSetMeteredApnsEnabled(enabled);
8788 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008789 phone.getDataSettingsManager().setDataEnabled(
8790 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008791 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008792 }
8793 } finally {
8794 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008795 }
8796 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008797
8798 /**
8799 * Get Client request stats
8800 * @return List of Client Request Stats
8801 * @hide
8802 */
8803 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008804 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8805 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008806 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008807 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008808 return null;
8809 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008810 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008811
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008812 final long identity = Binder.clearCallingIdentity();
8813 try {
8814 if (phone != null) {
8815 return phone.getClientRequestStats();
8816 }
8817
8818 return null;
8819 } finally {
8820 Binder.restoreCallingIdentity(identity);
8821 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008822 }
8823
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008824 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008825 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008826 if (uid == Process.ROOT_UID && packageName == null) {
8827 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8828 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8829 // exception. ROOT_UID seems not to have a valid package name returned by
8830 // PackageManager, so just fake it here to avoid issues when running telephony shell
8831 // commands that plumb through the RIL as root, like so:
8832 // $ adb root
8833 // $ adb shell cmd phone ...
8834 packageName = "root";
8835 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008836 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008837 }
Jack Yueb4124c2017-02-16 15:32:43 -08008838
8839 /**
Grace Chen70990072017-03-24 17:21:30 -07008840 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008841 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008842 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008843 * @param state State of SIM (power down, power up, pass through)
8844 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8845 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8846 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008847 *
8848 **/
8849 @Override
Grace Chen70990072017-03-24 17:21:30 -07008850 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008851 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008852 Phone phone = PhoneFactory.getPhone(slotIndex);
8853
vagdeviaf9a5b92018-08-15 16:01:53 -07008854 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8855
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008856 final long identity = Binder.clearCallingIdentity();
8857 try {
8858 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008859 phone.setSimPowerState(state, null, workSource);
8860 }
8861 } finally {
8862 Binder.restoreCallingIdentity(identity);
8863 }
8864 }
8865
8866 /**
8867 * Set SIM card power state.
8868 *
8869 * @param slotIndex SIM slot id.
8870 * @param state State of SIM (power down, power up, pass through)
8871 * @param callback callback to trigger after success or failure
8872 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8873 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8874 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8875 *
8876 **/
8877 @Override
8878 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8879 IIntegerConsumer callback) {
8880 enforceModifyPermission();
8881 Phone phone = PhoneFactory.getPhone(slotIndex);
8882
8883 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8884
8885 final long identity = Binder.clearCallingIdentity();
8886 try {
8887 if (phone != null) {
8888 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8889 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008890 }
8891 } finally {
8892 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008893 }
8894 }
Shuo Qiandd210312017-04-12 22:11:33 +00008895
Tyler Gunn65d45c22017-06-05 11:22:26 -07008896 private boolean isUssdApiAllowed(int subId) {
8897 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008898 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008899 if (configManager == null) {
8900 return false;
8901 }
8902 PersistableBundle pb = configManager.getConfigForSubId(subId);
8903 if (pb == null) {
8904 return false;
8905 }
8906 return pb.getBoolean(
8907 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8908 }
8909
Shuo Qiandd210312017-04-12 22:11:33 +00008910 /**
8911 * Check if phone is in emergency callback mode
8912 * @return true if phone is in emergency callback mode
8913 * @param subId sub id
8914 */
goneil9c5f4872017-12-05 14:07:56 -08008915 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008916 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008917 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008918 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008919
8920 final long identity = Binder.clearCallingIdentity();
8921 try {
8922 if (phone != null) {
8923 return phone.isInEcm();
8924 } else {
8925 return false;
8926 }
8927 } finally {
8928 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008929 }
8930 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008931
8932 /**
8933 * Get the current signal strength information for the given subscription.
8934 * Because this information is not updated when the device is in a low power state
8935 * it should not be relied-upon to be current.
8936 * @param subId Subscription index
8937 * @return the most recent cached signal strength info from the modem
8938 */
8939 @Override
8940 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008941 final long identity = Binder.clearCallingIdentity();
8942 try {
8943 Phone p = getPhone(subId);
8944 if (p == null) {
8945 return null;
8946 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008947
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008948 return p.getSignalStrength();
8949 } finally {
8950 Binder.restoreCallingIdentity(identity);
8951 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008952 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008953
Pengquan Meng77b7f132018-08-22 14:49:57 -07008954 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008955 * Get the current modem radio state for the given slot.
8956 * @param slotIndex slot index.
8957 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008958 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008959 * @return the current radio power state from the modem
8960 */
8961 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008962 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008963 Phone phone = PhoneFactory.getPhone(slotIndex);
8964 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008965 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8966 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008967 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8968 }
8969
8970 final long identity = Binder.clearCallingIdentity();
8971 try {
8972 return phone.getRadioPowerState();
8973 } finally {
8974 Binder.restoreCallingIdentity(identity);
8975 }
8976 }
8977 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8978 }
8979
8980 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008981 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8982 *
8983 * <p>Requires one of the following permissions:
8984 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008985 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008986 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8987 * privileges.
8988 *
8989 * @param subId subscription id
8990 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8991 * {@code false}.
8992 */
8993 @Override
8994 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008995 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008996 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008997 try {
8998 mApp.enforceCallingOrSelfPermission(
8999 android.Manifest.permission.ACCESS_NETWORK_STATE,
9000 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07009001 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009002 mApp.enforceCallingOrSelfPermission(
9003 permission.READ_BASIC_PHONE_STATE, functionName);
9004 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07009005 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07009006 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009007 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07009008 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07009009
Pengquan Menga1bb6272018-09-06 09:59:22 -07009010 boolean isEnabled = false;
9011 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07009012 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009013 Phone phone = getPhone(subId);
9014 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07009015 } finally {
9016 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009017 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009018 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07009019 }
9020
9021
9022 /**
9023 * Enables/Disables the data roaming on the subscription with id {@code subId}.
9024 *
9025 * <p> Requires permission:
9026 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
9027 * privileges.
9028 *
9029 * @param subId subscription id
9030 * @param isEnabled {@code true} means enable, {@code false} means disable.
9031 */
9032 @Override
9033 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009034 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9035 mApp, subId, "setDataRoamingEnabled");
9036
Pengquan Menga1bb6272018-09-06 09:59:22 -07009037 final long identity = Binder.clearCallingIdentity();
9038 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009039 Phone phone = getPhone(subId);
9040 if (phone != null) {
9041 phone.setDataRoamingEnabled(isEnabled);
9042 }
9043 } finally {
9044 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009045 }
9046 }
9047
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009048 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009049 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009050 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009051 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009052 mApp, subId, "isManualNetworkSelectionAllowed");
9053
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009054 boolean isAllowed = true;
9055 final long identity = Binder.clearCallingIdentity();
9056 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009057 Phone phone = getPhone(subId);
9058 if (phone != null) {
9059 isAllowed = phone.isCspPlmnEnabled();
9060 }
9061 } finally {
9062 Binder.restoreCallingIdentity(identity);
9063 }
9064 return isAllowed;
9065 }
9066
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009067 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9068 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009069 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009070 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009071 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009072 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9073 if (phone == null) {
9074 return false;
9075 }
9076 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9077 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9078 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009079 }
9080
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009081 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009082 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009083 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009084 mApp.getSystemService(AppOpsManager.class)
9085 .checkPackage(Binder.getCallingUid(), callingPackage);
9086
Jordan Liu1e142fc2019-04-22 15:10:43 -07009087 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009088 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009089 try {
9090 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009091 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009092 } catch (SecurityException e) {
9093 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9094 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009095 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009096 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009097 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009098 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009099 }
sandeepjsb6c87872021-09-27 15:34:44 +00009100 // checking compatibility, if calling app's target SDK is T and beyond.
9101 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9102 Binder.getCallingUid())) {
9103 isIccIdAccessRestricted = true;
9104 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009105 final long identity = Binder.clearCallingIdentity();
9106 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009107 UiccController uiccController = UiccController.getInstance();
9108 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009109 if (hasReadPermission) {
9110 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009111 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009112
9113 // Remove private info if the caller doesn't have access
9114 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9115 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009116 //setting the value after compatibility check
9117 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009118 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9119 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009120 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009121 if (card == null) {
9122 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009123 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009124 continue;
9125 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009126 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9127 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009128 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009129 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009130 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009131 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9132 for (UiccPortInfo portInfo : portInfos) {
9133 UiccPort port = uiccController.getUiccPortForSlot(
9134 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9135 if (port == null) {
9136 // assume no access if port is null
9137 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9138 continue;
9139 }
9140 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9141 uiccPortInfos.add(portInfo);
9142 } else {
9143 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9144 }
9145 }
9146 filteredInfos.add(new UiccCardInfo(
9147 cardInfo.isEuicc(),
9148 cardInfo.getCardId(),
9149 null,
9150 cardInfo.getPhysicalSlotIndex(),
9151 cardInfo.isRemovable(),
9152 cardInfo.isMultipleEnabledProfilesSupported(),
9153 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009154 }
9155 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009156 } finally {
9157 Binder.restoreCallingIdentity(identity);
9158 }
9159 }
9160
sandeepjsb6c87872021-09-27 15:34:44 +00009161 /**
9162 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9163 * generally private and require carrier privileges to view.
9164 *
9165 * @hide
9166 */
9167 @NonNull
9168 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9169 List<UiccPortInfo> portinfo = new ArrayList<>();
9170 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9171 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9172 }
9173 return new UiccCardInfo(
9174 cardInfo.isEuicc(),
9175 cardInfo.getCardId(),
9176 null,
9177 cardInfo.getPhysicalSlotIndex(),
9178 cardInfo.isRemovable(),
9179 cardInfo.isMultipleEnabledProfilesSupported(),
9180 portinfo
9181 );
9182 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009183
sandeepjsb6c87872021-09-27 15:34:44 +00009184 /**
9185 * @hide
9186 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9187 * These values are generally private and require carrier privileges to view.
9188 */
9189 @NonNull
9190 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9191 return new UiccPortInfo(
9192 UiccPortInfo.ICCID_REDACTED,
9193 portInfo.getPortIndex(),
9194 portInfo.getLogicalSlotIndex(),
9195 portInfo.isActive()
9196 );
9197 }
9198 @Override
9199 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009200 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009201 mApp.getSystemService(AppOpsManager.class)
9202 .checkPackage(Binder.getCallingUid(), callingPackage);
9203
sandeepjsb6c87872021-09-27 15:34:44 +00009204 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009205
Aman Guptaf3c90b32022-03-17 04:54:16 +00009206 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9207 // we are reading iccId which is PII data.
9208 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009209
9210 // checking compatibility, if calling app's target SDK is T and beyond.
9211 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9212 Binder.getCallingUid())) {
9213 isLogicalSlotAccessRestricted = true;
9214 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009215 final long identity = Binder.clearCallingIdentity();
9216 try {
9217 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009218 if (slots == null || slots.length == 0) {
9219 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009220 return null;
9221 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009222 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9223 for (int i = 0; i < slots.length; i++) {
9224 UiccSlot slot = slots[i];
9225 if (slot == null) {
9226 continue;
9227 }
9228
Jordan Liu7be7e652019-05-06 18:55:02 +00009229 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009230 UiccCard card = slot.getUiccCard();
9231 if (card != null) {
9232 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009233 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009234 cardId = slot.getEid();
9235 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009236 // If cardId is null, use iccId of default port as cardId.
9237 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009238 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009239 }
9240
Jordan Liu857451f2019-05-09 16:35:35 -07009241 if (cardId != null) {
9242 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9243 // if cardId is an EID, it's all digits so this is fine
9244 cardId = IccUtils.stripTrailingFs(cardId);
9245 }
9246
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009247 int cardState = 0;
9248 switch (slot.getCardState()) {
9249 case CARDSTATE_ABSENT:
9250 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9251 break;
9252 case CARDSTATE_PRESENT:
9253 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9254 break;
9255 case CARDSTATE_ERROR:
9256 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9257 break;
9258 case CARDSTATE_RESTRICTED:
9259 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9260 break;
9261 default:
9262 break;
9263
9264 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009265 List<UiccPortInfo> portInfos = new ArrayList<>();
9266 int[] portIndexes = slot.getPortList();
9267 for (int portIdx : portIndexes) {
9268 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009269 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009270 portInfos.add(new UiccPortInfo(iccId, portIdx,
9271 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009272 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009273 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009274 slot.isEuicc(),
9275 cardId,
9276 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009277 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009278 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009279 //setting the value after compatibility check
9280 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009281 }
9282 return infos;
9283 } finally {
9284 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009285 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009286 }
9287
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009288 /* Returns null if doesn't have read permission or carrier privilege access. */
9289 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9290 boolean hasReadPermission) {
9291 String iccId = slot.getIccId(portIndex);
9292 if (hasReadPermission) { // if has read permission
9293 return iccId;
9294 } else {
9295 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9296 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9297 // if no read permission, checking carrier privilege access
9298 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9299 return iccId;
9300 }
9301 }
9302 }
9303 // No read permission or carrier privilege access.
9304 return UiccPortInfo.ICCID_REDACTED;
9305 }
9306
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009307 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009308 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009309 public boolean switchSlots(int[] physicalSlots) {
9310 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009311
9312 final long identity = Binder.clearCallingIdentity();
9313 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009314 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9315 for (int i = 0; i < physicalSlots.length; i++) {
9316 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9317 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9318 physicalSlots[i], i));
9319 }
9320 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009321 } finally {
9322 Binder.restoreCallingIdentity(identity);
9323 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009324 }
Jack Yu4c988042018-02-27 15:30:01 -08009325
9326 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009327 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9328 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9329 enforceModifyPermission();
9330
9331 final long identity = Binder.clearCallingIdentity();
9332 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009333 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009334 } finally {
9335 Binder.restoreCallingIdentity(identity);
9336 }
9337 }
9338
9339 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009340 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009341 final long identity = Binder.clearCallingIdentity();
9342 try {
9343 return UiccController.getInstance().getCardIdForDefaultEuicc();
9344 } finally {
9345 Binder.restoreCallingIdentity(identity);
9346 }
9347 }
9348
Pengquan Meng85728fb2018-03-12 16:31:21 -07009349 /**
goneil47ffb6e2018-04-06 15:40:58 -07009350 * A test API to reload the UICC profile.
9351 *
9352 * <p>Requires that the calling app has permission
9353 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9354 * @hide
9355 */
9356 @Override
9357 public void refreshUiccProfile(int subId) {
9358 enforceModifyPermission();
9359
9360 final long identity = Binder.clearCallingIdentity();
9361 try {
9362 Phone phone = getPhone(subId);
9363 if (phone == null) {
9364 return;
9365 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009366 UiccPort uiccPort = phone.getUiccPort();
9367 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009368 return;
9369 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009370 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009371 if (uiccProfile == null) {
9372 return;
9373 }
9374 uiccProfile.refresh();
9375 } finally {
9376 Binder.restoreCallingIdentity(identity);
9377 }
9378 }
9379
9380 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009381 * Returns false if the mobile data is disabled by default, otherwise return true.
9382 */
9383 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009384 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009385 }
9386
9387 /**
9388 * Returns true if the data roaming is enabled by default, i.e the system property
9389 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9390 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9391 */
9392 private boolean getDefaultDataRoamingEnabled(int subId) {
9393 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009394 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009395 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009396 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9397 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9398 return isDataRoamingEnabled;
9399 }
9400
9401 /**
9402 * Returns the default network type for the given {@code subId}, if the default network type is
9403 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9404 */
9405 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009406 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009407 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009408 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9409 return list.get(phoneId);
9410 }
9411 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009412 }
fionaxua13278b2018-03-21 00:08:13 -07009413
9414 @Override
9415 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009416 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009417 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009418
9419 final long identity = Binder.clearCallingIdentity();
9420 try {
9421 final Phone phone = getPhone(subId);
9422 if (phone == null) {
9423 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9424 return;
9425 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009426 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9427 if (cpt != null) {
9428 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9429 }
9430 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009431 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9432 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009433 if (carrierPrivilegeRules == null) {
9434 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9435 } else {
9436 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9437 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009438 } finally {
9439 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009440 }
fionaxua13278b2018-03-21 00:08:13 -07009441 }
9442
9443 @Override
9444 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009445 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009446
9447 final long identity = Binder.clearCallingIdentity();
9448 try {
9449 final Phone phone = getPhone(subId);
9450 if (phone == null) {
9451 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9452 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9453 }
9454 return phone.getCarrierIdListVersion();
9455 } finally {
9456 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009457 }
fionaxua13278b2018-03-21 00:08:13 -07009458 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009459
9460 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009461 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9462 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009463 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009464 mApp, subId, callingPackage, callingFeatureId,
9465 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009466 return -1;
9467 }
9468
9469 final long identity = Binder.clearCallingIdentity();
9470 try {
9471 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9472 } finally {
9473 Binder.restoreCallingIdentity(identity);
9474 }
9475 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009476
9477 @Override
9478 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009479 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009480 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009481 mApp, subId, "getCdmaRoamingMode");
9482
9483 final long identity = Binder.clearCallingIdentity();
9484 try {
9485 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9486 } finally {
9487 Binder.restoreCallingIdentity(identity);
9488 }
9489 }
9490
9491 @Override
9492 public boolean setCdmaRoamingMode(int subId, int mode) {
9493 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9494 mApp, subId, "setCdmaRoamingMode");
9495
9496 final long identity = Binder.clearCallingIdentity();
9497 try {
9498 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9499 } finally {
9500 Binder.restoreCallingIdentity(identity);
9501 }
9502 }
9503
9504 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009505 public int getCdmaSubscriptionMode(int subId) {
9506 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009507 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009508 mApp, subId, "getCdmaSubscriptionMode");
9509
9510 final long identity = Binder.clearCallingIdentity();
9511 try {
9512 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9513 } finally {
9514 Binder.restoreCallingIdentity(identity);
9515 }
9516 }
9517
9518 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009519 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9520 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9521 mApp, subId, "setCdmaSubscriptionMode");
9522
9523 final long identity = Binder.clearCallingIdentity();
9524 try {
9525 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9526 } finally {
9527 Binder.restoreCallingIdentity(identity);
9528 }
9529 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009530
sqianc5eccab2018-10-19 18:46:41 -07009531 @Override
sqian8c685422019-02-22 15:55:18 -08009532 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009533 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009534 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009535 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9536 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009537 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9538 }
9539 final long identity = Binder.clearCallingIdentity();
9540 try {
sqian854d44b2018-12-12 16:48:18 -08009541 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9542 for (Phone phone: PhoneFactory.getPhones()) {
9543 if (phone.getEmergencyNumberTracker() != null
9544 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9545 emergencyNumberListInternal.put(
9546 phone.getSubId(),
9547 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9548 }
sqian11b7a0e2018-12-05 18:48:28 -08009549 }
sqian854d44b2018-12-12 16:48:18 -08009550 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009551 } finally {
9552 Binder.restoreCallingIdentity(identity);
9553 }
sqianc5eccab2018-10-19 18:46:41 -07009554 }
9555
9556 @Override
sqian8c685422019-02-22 15:55:18 -08009557 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009558 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009559 if (!exactMatch) {
9560 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009561 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009562 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009563 }
9564 final long identity = Binder.clearCallingIdentity();
9565 try {
sqian854d44b2018-12-12 16:48:18 -08009566 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009567 //Note: we ignore passed in param exactMatch. We can remove it once
9568 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009569 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009570 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009571 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009572 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009573 }
sqian11b7a0e2018-12-05 18:48:28 -08009574 }
9575 return false;
9576 } finally {
9577 Binder.restoreCallingIdentity(identity);
9578 }
9579 }
9580
sqianf4ca7ed2019-01-15 18:32:07 -08009581 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009582 * Start emergency callback mode for GsmCdmaPhone for testing.
9583 */
9584 @Override
9585 public void startEmergencyCallbackMode() {
9586 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9587 "startEmergencyCallbackMode");
9588 enforceModifyPermission();
9589 final long identity = Binder.clearCallingIdentity();
9590 try {
9591 for (Phone phone : PhoneFactory.getPhones()) {
9592 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9593 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9594 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9595 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9596 gsmCdmaPhone.obtainMessage(
9597 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9598 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9599 }
9600 }
9601 } finally {
9602 Binder.restoreCallingIdentity(identity);
9603 }
9604 }
9605
9606 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009607 * Update emergency number list for test mode.
9608 */
9609 @Override
9610 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9611 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9612 "updateEmergencyNumberListTestMode");
9613
9614 final long identity = Binder.clearCallingIdentity();
9615 try {
9616 for (Phone phone: PhoneFactory.getPhones()) {
9617 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9618 if (tracker != null) {
9619 tracker.executeEmergencyNumberTestModeCommand(action, num);
9620 }
9621 }
9622 } finally {
9623 Binder.restoreCallingIdentity(identity);
9624 }
9625 }
9626
9627 /**
9628 * Get the full emergency number list for test mode.
9629 */
9630 @Override
9631 public List<String> getEmergencyNumberListTestMode() {
9632 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9633 "getEmergencyNumberListTestMode");
9634
9635 final long identity = Binder.clearCallingIdentity();
9636 try {
9637 Set<String> emergencyNumbers = new HashSet<>();
9638 for (Phone phone: PhoneFactory.getPhones()) {
9639 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9640 if (tracker != null) {
9641 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9642 emergencyNumbers.add(num.getNumber());
9643 }
9644 }
9645 }
9646 return new ArrayList<>(emergencyNumbers);
9647 } finally {
9648 Binder.restoreCallingIdentity(identity);
9649 }
9650 }
9651
chen xud6b45bd2018-10-30 22:27:10 -07009652 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009653 public int getEmergencyNumberDbVersion(int subId) {
9654 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9655
9656 final long identity = Binder.clearCallingIdentity();
9657 try {
9658 final Phone phone = getPhone(subId);
9659 if (phone == null) {
9660 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9661 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9662 }
9663 return phone.getEmergencyNumberDbVersion();
9664 } finally {
9665 Binder.restoreCallingIdentity(identity);
9666 }
9667 }
9668
9669 @Override
9670 public void notifyOtaEmergencyNumberDbInstalled() {
9671 enforceModifyPermission();
9672
9673 final long identity = Binder.clearCallingIdentity();
9674 try {
9675 for (Phone phone: PhoneFactory.getPhones()) {
9676 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9677 if (tracker != null) {
9678 tracker.updateOtaEmergencyNumberDatabase();
9679 }
9680 }
9681 } finally {
9682 Binder.restoreCallingIdentity(identity);
9683 }
9684 }
9685
9686 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009687 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009688 enforceActiveEmergencySessionPermission();
9689
9690 final long identity = Binder.clearCallingIdentity();
9691 try {
9692 for (Phone phone: PhoneFactory.getPhones()) {
9693 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9694 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009695 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9696 }
9697 }
9698 } finally {
9699 Binder.restoreCallingIdentity(identity);
9700 }
9701 }
9702
9703 @Override
9704 public void resetOtaEmergencyNumberDbFilePath() {
9705 enforceActiveEmergencySessionPermission();
9706
9707 final long identity = Binder.clearCallingIdentity();
9708 try {
9709 for (Phone phone: PhoneFactory.getPhones()) {
9710 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9711 if (tracker != null) {
9712 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009713 }
9714 }
9715 } finally {
9716 Binder.restoreCallingIdentity(identity);
9717 }
9718 }
9719
9720 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009721 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9722 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9723 Phone phone = getPhone(subId);
9724 if (phone == null) {
9725 return null;
9726 }
9727 final long identity = Binder.clearCallingIdentity();
9728 try {
9729 UiccProfile profile = UiccController.getInstance()
9730 .getUiccProfileForPhone(phone.getPhoneId());
9731 if (profile != null) {
9732 return profile.getCertsFromCarrierPrivilegeAccessRules();
9733 }
9734 } finally {
9735 Binder.restoreCallingIdentity(identity);
9736 }
9737 return null;
9738 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009739
9740 /**
9741 * Enable or disable a modem stack.
9742 */
9743 @Override
9744 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9745 enforceModifyPermission();
9746
9747 final long identity = Binder.clearCallingIdentity();
9748 try {
9749 Phone phone = PhoneFactory.getPhone(slotIndex);
9750 if (phone == null) {
9751 return false;
9752 } else {
9753 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9754 }
9755 } finally {
9756 Binder.restoreCallingIdentity(identity);
9757 }
9758 }
Michelecea4cf22018-12-21 15:00:11 -08009759
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009760 /**
9761 * Whether a modem stack is enabled or not.
9762 */
9763 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009764 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9765 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009766 Phone phone = PhoneFactory.getPhone(slotIndex);
9767 if (phone == null) return false;
9768
9769 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009770 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9771 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009772 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9773 }
9774
9775 final long identity = Binder.clearCallingIdentity();
9776 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009777 try {
9778 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9779 } catch (NoSuchElementException ex) {
9780 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9781 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009782 } finally {
9783 Binder.restoreCallingIdentity(identity);
9784 }
9785 }
9786
Michelecea4cf22018-12-21 15:00:11 -08009787 @Override
Michele0ea7d782019-03-19 14:58:42 -07009788 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009789 enforceModifyPermission();
9790
9791 final long identity = Binder.clearCallingIdentity();
9792 try {
9793 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009794 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009795 .commit();
9796 } finally {
9797 Binder.restoreCallingIdentity(identity);
9798 }
9799 }
9800
9801 @Override
Michele0ea7d782019-03-19 14:58:42 -07009802 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009803 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009804 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009805 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9806 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009807 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009808 }
Michelecea4cf22018-12-21 15:00:11 -08009809
9810 final long identity = Binder.clearCallingIdentity();
9811 try {
Michele0ea7d782019-03-19 14:58:42 -07009812 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009813 } finally {
9814 Binder.restoreCallingIdentity(identity);
9815 }
9816 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009817
Michele0ea7d782019-03-19 14:58:42 -07009818 @TelephonyManager.IsMultiSimSupportedResult
9819 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009820 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9821 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9822 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009823 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9824 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009825 }
9826 // Check if the hardware supports multisim functionality. If usage of multisim is not
9827 // supported by the modem, indicate that it is restricted.
9828 PhoneCapability staticCapability =
9829 mPhoneConfigurationManager.getStaticPhoneCapability();
9830 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009831 loge("isMultiSimSupportedInternal: no static configuration available");
9832 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009833 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009834 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009835 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9836 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009837 }
9838 // Check if support of multiple SIMs is restricted by carrier
9839 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009840 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009841 }
9842
Michele0ea7d782019-03-19 14:58:42 -07009843 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009844 }
9845
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009846 /**
9847 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009848 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9849 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9850 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009851 * @param numOfSims number of active sims we want to switch to
9852 */
9853 @Override
9854 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009855 if (numOfSims == 1) {
9856 enforceModifyPermission();
9857 } else {
9858 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9859 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9860 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009861 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009862
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009863 try {
Michele30b57b22019-03-01 12:01:14 -08009864 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009865 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009866 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9867 return;
9868 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009869 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9870 } finally {
9871 Binder.restoreCallingIdentity(identity);
9872 }
9873 }
9874
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009875 @Override
9876 public boolean isApplicationOnUicc(int subId, int appType) {
9877 enforceReadPrivilegedPermission("isApplicationOnUicc");
9878 Phone phone = getPhone(subId);
9879 if (phone == null) {
9880 return false;
9881 }
9882 final long identity = Binder.clearCallingIdentity();
9883 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009884 UiccPort uiccPort = phone.getUiccPort();
9885 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009886 return false;
9887 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009888 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009889 if (uiccProfile == null) {
9890 return false;
9891 }
9892 if (TelephonyManager.APPTYPE_SIM <= appType
9893 && appType <= TelephonyManager.APPTYPE_ISIM) {
9894 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9895 }
9896 return false;
9897 } finally {
9898 Binder.restoreCallingIdentity(identity);
9899 }
9900 }
9901
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009902 /**
chen xub4baa772019-04-03 10:23:41 -07009903 * Get whether making changes to modem configurations will trigger reboot.
9904 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009905 */
9906 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009907 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9908 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009909 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009910 mApp, subId, callingPackage, callingFeatureId,
9911 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009912 return false;
9913 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009914 final long identity = Binder.clearCallingIdentity();
9915 try {
9916 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9917 } finally {
9918 Binder.restoreCallingIdentity(identity);
9919 }
9920 }
9921
Nathan Harold29f5f052019-02-15 13:41:57 -08009922 private void updateModemStateMetrics() {
9923 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9924 // TODO: check the state for each modem if the api is ready.
9925 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9926 }
9927
Pengquan Meng3889a572019-01-23 11:16:29 -08009928 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009929 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009930 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009931 // Verify that the callingPackage belongs to the calling UID
9932 mApp.getSystemService(AppOpsManager.class)
9933 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009934 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009935 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009936 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009937 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9938 if (slotInfos != null) {
9939 for (int i = 0; i < slotInfos.length; i++) {
9940 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9941 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9942 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9943 portInfo.getLogicalSlotIndex()));
9944 }
9945 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009946 }
9947 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009948 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009949 } finally {
9950 Binder.restoreCallingIdentity(identity);
9951 }
9952 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009953
9954 /**
9955 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009956 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009957 */
jimsunf9ec1622022-09-13 21:18:43 +08009958 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009959 @Override
9960 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009961 return getHalVersion(HAL_SERVICE_RADIO);
9962 }
9963
9964 /**
9965 * Get the HAL Version of a specific service
9966 */
9967 @Override
9968 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009969 Phone phone = getDefaultPhone();
9970 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009971 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009972 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9973 return hv.major * 100 + hv.minor;
9974 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009975
9976 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009977 * Get the current calling package name.
9978 * @return the current calling package name
9979 */
9980 @Override
9981 public String getCurrentPackageName() {
9982 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9983 }
9984
9985 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009986 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9987 * corresponding network requests on a subId.
9988 *
9989 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009990 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009991 * 2) APN is un-metered for this subscription, or
9992 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009993 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009994 *
9995 * @return whether data is allowed for a apn type.
9996 *
9997 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009998 */
9999 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -070010000 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -070010001 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
10002 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010003
10004 // Now that all security checks passes, perform the operation as ourselves.
10005 final long identity = Binder.clearCallingIdentity();
10006 try {
10007 Phone phone = getPhone(subId);
10008 if (phone == null) return false;
10009
Jack Yu27422a52022-03-21 10:38:05 -070010010 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -080010011 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -070010012 isMetered = phone.getDataNetworkController().getDataConfigManager()
10013 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
10014 phone.getServiceState().getDataRoaming());
10015 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -080010016 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -070010017 } finally {
10018 Binder.restoreCallingIdentity(identity);
10019 }
10020 }
10021
10022 @Override
Jack Yu41407ee2019-05-13 16:54:09 -070010023 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -070010024 enforceReadPrivilegedPermission("isApnMetered");
10025
10026 // Now that all security checks passes, perform the operation as ourselves.
10027 final long identity = Binder.clearCallingIdentity();
10028 try {
10029 Phone phone = getPhone(subId);
10030 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -070010031 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10032 DataUtils.apnTypeToNetworkCapability(apnType),
10033 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010034 } finally {
10035 Binder.restoreCallingIdentity(identity);
10036 }
10037 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010038
10039 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010040 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10041 int subscriptionId, IBooleanConsumer resultCallback) {
10042 enforceModifyPermission();
10043 long token = Binder.clearCallingIdentity();
10044 try {
10045 Phone phone = getPhone(subscriptionId);
10046 if (phone == null) {
10047 try {
10048 if (resultCallback != null) {
10049 resultCallback.accept(false);
10050 }
10051 } catch (RemoteException e) {
10052 // ignore
10053 }
10054 return;
10055 }
10056 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10057 Pair.create(specifiers, (x) -> {
10058 try {
10059 if (resultCallback != null) {
10060 resultCallback.accept(x);
10061 }
10062 } catch (RemoteException e) {
10063 // ignore
10064 }
10065 });
10066 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10067 } finally {
10068 Binder.restoreCallingIdentity(token);
10069 }
10070 }
10071
10072 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010073 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10074 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010075 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010076 mApp, subId, "getSystemSelectionChannels");
10077 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10078 final long identity = Binder.clearCallingIdentity();
10079 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010080 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10081 if (result instanceof IllegalStateException) {
10082 throw (IllegalStateException) result;
10083 }
10084 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010085 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10086 return specifiers;
10087 } finally {
10088 Binder.restoreCallingIdentity(identity);
10089 }
10090 }
10091
10092 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010093 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010094 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010095 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010096 }
10097
10098 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010099 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10100 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010101 if (callingPackage == null) {
10102 callingPackage = getCurrentPackageName();
10103 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010104 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10105 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010106 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10107 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010108 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10109 }
10110 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10111 Intent intent = new Intent();
10112 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10113 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10114 // Bring up choose default SMS subscription dialog right now
10115 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10116 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10117 mApp.startActivity(intent);
10118 }
chen xud5ca2d52019-05-28 15:20:57 -070010119
10120 @Override
10121 public String getMmsUAProfUrl(int subId) {
10122 //TODO investigate if this API should require proper permission check in R b/133791609
10123 final long identity = Binder.clearCallingIdentity();
10124 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010125 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10126 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10127 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10128 return carrierUAProfUrl;
10129 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010130 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10131 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010132 } finally {
10133 Binder.restoreCallingIdentity(identity);
10134 }
10135 }
10136
10137 @Override
10138 public String getMmsUserAgent(int subId) {
10139 //TODO investigate if this API should require proper permission check in R b/133791609
10140 final long identity = Binder.clearCallingIdentity();
10141 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010142 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10143 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10144 if (!TextUtils.isEmpty(carrierUserAgent)) {
10145 return carrierUserAgent;
10146 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010147 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10148 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010149 } finally {
10150 Binder.restoreCallingIdentity(identity);
10151 }
10152 }
Jack Yub07d4972019-05-28 16:12:25 -070010153
10154 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010155 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10156 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010157
Jack Yub07d4972019-05-28 16:12:25 -070010158 final long identity = Binder.clearCallingIdentity();
10159 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010160 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010161 if (phone == null) return false;
10162
Ling Maf188d502022-09-16 15:22:36 -070010163 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010164 } finally {
10165 Binder.restoreCallingIdentity(identity);
10166 }
10167 }
10168
10169 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010170 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010171 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010172 enforceModifyPermission();
10173
changbettyd5c246e2019-12-24 15:40:37 +080010174 final long identity = Binder.clearCallingIdentity();
10175 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010176 Phone phone = getPhone(subscriptionId);
10177 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010178
Ling Maf188d502022-09-16 15:22:36 -070010179 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010180 } finally {
10181 Binder.restoreCallingIdentity(identity);
10182 }
10183 }
10184
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010185 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010186 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010187 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10188 * otherwise.
10189 */
10190 @Override
10191 public void setCepEnabled(boolean isCepEnabled) {
10192 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10193
10194 final long identity = Binder.clearCallingIdentity();
10195 try {
10196 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10197 for (Phone phone : PhoneFactory.getPhones()) {
10198 Phone defaultPhone = phone.getImsPhone();
10199 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10200 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10201 ImsPhoneCallTracker imsPhoneCallTracker =
10202 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10203 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10204 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10205 + imsPhone.getMsisdn());
10206 }
10207 }
10208 } finally {
10209 Binder.restoreCallingIdentity(identity);
10210 }
10211 }
allenwtsu46dcc572020-01-08 18:24:03 +080010212
10213 /**
10214 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10215 *
10216 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10217 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10218 * before being read.
10219 */
10220 @Override
10221 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10222 isCompressed) {
10223 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10224 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010225 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10226 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10227 }
10228 if (!isImsAvailableOnDevice()) {
10229 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10230 "IMS not available on device.");
10231 }
10232
10233 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010234 try {
Hui Wang761a6682020-10-31 05:12:53 +000010235 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10236 } finally {
10237 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010238 }
10239 }
zoey chene02881a2019-12-30 16:11:23 +080010240
10241 @Override
10242 public boolean isIccLockEnabled(int subId) {
10243 enforceReadPrivilegedPermission("isIccLockEnabled");
10244
10245 // Now that all security checks passes, perform the operation as ourselves.
10246 final long identity = Binder.clearCallingIdentity();
10247 try {
10248 Phone phone = getPhone(subId);
10249 if (phone != null && phone.getIccCard() != null) {
10250 return phone.getIccCard().getIccLockEnabled();
10251 } else {
10252 return false;
10253 }
10254 } finally {
10255 Binder.restoreCallingIdentity(identity);
10256 }
10257 }
10258
10259 /**
10260 * Set the ICC pin lock enabled or disabled.
10261 *
10262 * @return an integer representing the status of IccLock enabled or disabled in the following
10263 * three cases:
10264 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10265 * successfully.
10266 * - Positive number and zero for remaining password attempts.
10267 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10268 *
10269 */
10270 @Override
10271 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10272 enforceModifyPermission();
10273
10274 Phone phone = getPhone(subId);
10275 if (phone == null) {
10276 return 0;
10277 }
10278 // Now that all security checks passes, perform the operation as ourselves.
10279 final long identity = Binder.clearCallingIdentity();
10280 try {
10281 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10282 new Pair<Boolean, String>(enabled, password), phone, null);
10283 return attemptsRemaining;
10284
10285 } catch (Exception e) {
10286 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10287 } finally {
10288 Binder.restoreCallingIdentity(identity);
10289 }
10290 return 0;
10291 }
10292
10293 /**
10294 * Change the ICC password used in ICC pin lock.
10295 *
10296 * @return an integer representing the status of IccLock changed in the following three cases:
10297 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10298 * - Positive number and zero for remaining password attempts.
10299 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10300 *
10301 */
10302 @Override
10303 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10304 enforceModifyPermission();
10305
10306 Phone phone = getPhone(subId);
10307 if (phone == null) {
10308 return 0;
10309 }
10310 // Now that all security checks passes, perform the operation as ourselves.
10311 final long identity = Binder.clearCallingIdentity();
10312 try {
10313 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10314 new Pair<String, String>(oldPassword, newPassword), phone, null);
10315 return attemptsRemaining;
10316
10317 } catch (Exception e) {
10318 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10319 } finally {
10320 Binder.restoreCallingIdentity(identity);
10321 }
10322 return 0;
10323 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010324
10325 /**
10326 * Request for receiving user activity notification
10327 */
10328 @Override
10329 public void requestUserActivityNotification() {
10330 if (!mNotifyUserActivity.get()
10331 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10332 mNotifyUserActivity.set(true);
10333 }
10334 }
10335
10336 /**
10337 * Called when userActivity is signalled in the power manager.
10338 * This is safe to call from any thread, with any window manager locks held or not.
10339 */
10340 @Override
10341 public void userActivity() {
10342 // ***************************************
10343 // * Inherited from PhoneWindowManager *
10344 // ***************************************
10345 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10346 // WITH ITS LOCKS HELD.
10347 //
10348 // This code must be VERY careful about the locks
10349 // it acquires.
10350 // In fact, the current code acquires way too many,
10351 // and probably has lurking deadlocks.
10352
10353 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10354 throw new SecurityException("Only the OS may call notifyUserActivity()");
10355 }
10356
10357 if (mNotifyUserActivity.getAndSet(false)) {
10358 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10359 USER_ACTIVITY_NOTIFICATION_DELAY);
10360 }
10361 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010362
10363 @Override
10364 public boolean canConnectTo5GInDsdsMode() {
10365 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10366 }
Jack Yud10cdd42020-09-28 20:28:01 -070010367
10368 @Override
10369 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10370 String callingFeatureId) {
10371 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10372 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10373 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10374 }
10375
10376 Phone phone = getPhone(subId);
10377 if (phone == null) {
10378 throw new RuntimeException("phone is not available");
10379 }
10380 // Now that all security checks passes, perform the operation as ourselves.
10381 final long identity = Binder.clearCallingIdentity();
10382 try {
10383 return phone.getEquivalentHomePlmns();
10384 } finally {
10385 Binder.restoreCallingIdentity(identity);
10386 }
10387 }
Daniel Bright59e67312020-11-13 11:49:37 -080010388
10389 @Override
10390 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010391 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10392 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010393 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010394 if (radioInterfaceCapabilities == null) {
10395 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010396 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010397 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010398 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010399
Hui Wang641e81c2020-10-12 12:14:23 -070010400 @Override
10401 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10402 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010403 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10404 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10405 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10406 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10407 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010408 if (DBG) {
10409 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10410 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10411 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10412 }
10413
10414 if (!SubscriptionManager.isValidSubscriptionId(subId)
10415 || appType < TelephonyManager.APPTYPE_UNKNOWN
10416 || appType > TelephonyManager.APPTYPE_ISIM
10417 || nafUrl == null || securityProtocol == null || callback == null) {
10418 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10419 if (callback != null) {
10420 try {
10421 callback.onAuthenticationFailure(
10422 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10423 } catch (RemoteException exception) {
10424 log("Fail to notify onAuthenticationFailure due to " + exception);
10425 }
10426 return;
10427 }
10428 }
10429
10430 final long token = Binder.clearCallingIdentity();
10431 try {
10432 getGbaManager(subId).bootstrapAuthenticationRequest(
10433 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10434 forceBootStrapping, callback));
10435 } finally {
10436 Binder.restoreCallingIdentity(token);
10437 }
10438 }
10439
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010440 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010441 * Attempts to set the radio power state for all phones for thermal reason.
10442 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010443 * requested radio power state will actually be set. See {@link
10444 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10445 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010446 * @param enable {@code true} if trying to turn radio on.
10447 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10448 * false}.
10449 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010450 private boolean setRadioPowerForThermal(boolean enable) {
10451 boolean isPhoneAvailable = false;
10452 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10453 Phone phone = PhoneFactory.getPhone(i);
10454 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010455 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010456 isPhoneAvailable = true;
10457 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010458 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010459
10460 // return true if successfully informed the phone object about the thermal radio power
10461 // request.
10462 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010463 }
10464
10465 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010466 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010467 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10468 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10469 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10470 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10471 throw new IllegalArgumentException("modem does not support data throttling");
10472 }
10473
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010474 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10475 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010476 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010477 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10478 }
10479
Sarah Chinecc78c42022-03-31 21:16:48 -070010480 setDataEnabledForReason(
10481 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010482
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010483 if (isDataThrottlingSupported) {
10484 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010485 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010486 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10487 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10488 } else if (thermalMitigationResult
10489 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010490 log("Modem likely does not support data throttling on secondary carrier. Data " +
10491 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10492 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010493 }
10494 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010495 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010496
10497 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010498 }
10499
Jack Nudelman644b91a2021-03-12 14:09:48 -080010500 private static List<String> getThermalMitigationAllowlist(Context context) {
10501 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10502 for (String pckg : context.getResources()
10503 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10504 sThermalMitigationAllowlistedPackages.add(pckg);
10505 }
10506 }
10507
10508 return sThermalMitigationAllowlistedPackages;
10509 }
10510
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010511 private boolean isAnyPhoneInEmergencyState() {
10512 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10513 if (tm.isInEmergencyCall()) {
10514 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10515 return true;
10516 }
10517 for (Phone phone : PhoneFactory.getPhones()) {
10518 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10519 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10520 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10521 + phone.isInEcm());
10522 return true;
10523 }
10524 }
10525
10526 return false;
10527 }
10528
Jack Nudelman644b91a2021-03-12 14:09:48 -080010529 /**
10530 * Used by shell commands to add an authorized package name for thermal mitigation.
10531 * @param packageName name of package to be allowlisted
10532 * @param context
10533 */
10534 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10535 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10536 sThermalMitigationAllowlistedPackages.add(packageName);
10537 }
10538
10539 /**
10540 * Used by shell commands to remove an authorized package name for thermal mitigation.
10541 * @param packageName name of package to remove from allowlist
10542 * @param context
10543 */
10544 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10545 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10546 sThermalMitigationAllowlistedPackages.remove(packageName);
10547 }
10548
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010549 /**
10550 * Thermal mitigation request to control functionalities at modem.
10551 *
10552 * @param subId the id of the subscription.
10553 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010554 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010555 *
10556 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10557 */
10558 @Override
10559 @ThermalMitigationResult
10560 public int sendThermalMitigationRequest(
10561 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010562 ThermalMitigationRequest thermalMitigationRequest,
10563 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010564 enforceModifyPermission();
10565
Jack Nudelman644b91a2021-03-12 14:09:48 -080010566 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10567 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10568 .contains(callingPackage)) {
10569 throw new SecurityException("Calling package must be configured in the device config. "
10570 + "calling package: " + callingPackage);
10571 }
10572
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010573 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10574 final long identity = Binder.clearCallingIdentity();
10575
10576 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10577 try {
10578 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10579 switch (thermalMitigationAction) {
10580 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10581 thermalMitigationResult =
10582 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010583 thermalMitigationRequest.getDataThrottlingRequest(),
10584 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010585 break;
10586 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10587 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10588 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10589 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10590 }
10591
10592 // Ensure that radio is on. If not able to power on due to phone being
10593 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010594 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010595 thermalMitigationResult =
10596 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10597 break;
10598 }
10599
10600 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010601 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010602 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10603 break;
10604 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10605 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10606 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10607 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10608 }
10609
10610 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10611 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010612 Phone phone = getPhone(subId);
10613 if (phone == null) {
10614 thermalMitigationResult =
10615 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10616 break;
10617 }
10618
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010619 TelephonyConnectionService service =
10620 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010621 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010622 Log.e(LOG_TAG, "An emergency call is pending");
10623 thermalMitigationResult =
10624 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10625 break;
10626 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010627 thermalMitigationResult =
10628 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10629 break;
10630 }
10631 } else {
10632 thermalMitigationResult =
10633 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10634 break;
10635 }
10636
10637 // Turn radio off. If not able to power off due to phone being unavailable,
10638 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010639 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010640 thermalMitigationResult =
10641 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10642 break;
10643 }
10644 thermalMitigationResult =
10645 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10646 break;
10647 default:
10648 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10649 + "not exist. Requested action: " + thermalMitigationAction);
10650 }
10651 } catch (IllegalArgumentException e) {
10652 throw e;
10653 } catch (Exception e) {
10654 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10655 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10656 } finally {
10657 Binder.restoreCallingIdentity(identity);
10658 }
10659
10660 if (DBG) {
10661 log("thermalMitigationRequest returning with thermalMitigationResult: "
10662 + thermalMitigationResult);
10663 }
10664
10665 return thermalMitigationResult;
10666 }
Hui Wang641e81c2020-10-12 12:14:23 -070010667
10668 /**
10669 * Set the GbaService Package Name that Telephony will bind to.
10670 *
10671 * @param subId The sim that the GbaService is associated with.
10672 * @param packageName The name of the package to be replaced with.
10673 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10674 */
10675 @Override
10676 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10677 enforceModifyPermission();
10678
10679 final long identity = Binder.clearCallingIdentity();
10680 try {
10681 return getGbaManager(subId).overrideServicePackage(packageName);
10682 } finally {
10683 Binder.restoreCallingIdentity(identity);
10684 }
10685 }
10686
10687 /**
10688 * Return the package name of the currently bound GbaService.
10689 *
10690 * @param subId The sim that the GbaService is associated with.
10691 * @return the package name of the GbaService configuration, null if GBA is not supported.
10692 */
10693 @Override
10694 public String getBoundGbaService(int subId) {
10695 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10696
10697 final long identity = Binder.clearCallingIdentity();
10698 try {
10699 return getGbaManager(subId).getServicePackage();
10700 } finally {
10701 Binder.restoreCallingIdentity(identity);
10702 }
10703 }
10704
10705 /**
10706 * Set the release time for telephony to unbind GbaService.
10707 *
10708 * @param subId The sim that the GbaService is associated with.
10709 * @param interval The release time to unbind GbaService by millisecond.
10710 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10711 */
10712 @Override
10713 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10714 enforceModifyPermission();
10715
10716 final long identity = Binder.clearCallingIdentity();
10717 try {
10718 return getGbaManager(subId).overrideReleaseTime(interval);
10719 } finally {
10720 Binder.restoreCallingIdentity(identity);
10721 }
10722 }
10723
10724 /**
10725 * Return the release time for telephony to unbind GbaService.
10726 *
10727 * @param subId The sim that the GbaService is associated with.
10728 * @return The release time to unbind GbaService by millisecond.
10729 */
10730 @Override
10731 public int getGbaReleaseTime(int subId) {
10732 enforceReadPrivilegedPermission("getGbaReleaseTime");
10733
10734 final long identity = Binder.clearCallingIdentity();
10735 try {
10736 return getGbaManager(subId).getReleaseTime();
10737 } finally {
10738 Binder.restoreCallingIdentity(identity);
10739 }
10740 }
10741
10742 private GbaManager getGbaManager(int subId) {
10743 GbaManager instance = GbaManager.getInstance(subId);
10744 if (instance == null) {
10745 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10746 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10747 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10748 }
10749 return instance;
10750 }
Hui Wang761a6682020-10-31 05:12:53 +000010751
10752 /**
10753 * indicate whether the device and the carrier can support
10754 * RCS VoLTE single registration.
10755 */
10756 @Override
10757 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010758 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10759 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10760 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
10767 final long identity = Binder.clearCallingIdentity();
10768 try {
10769 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10770 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010771 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10772 if (isCapable != null) {
10773 return isCapable;
10774 }
Hui Wang761a6682020-10-31 05:12:53 +000010775 }
Hui Wang67af90e2021-06-04 16:57:15 -070010776 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10777 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010778 } finally {
10779 Binder.restoreCallingIdentity(identity);
10780 }
10781 }
10782
10783 /**
10784 * Register RCS provisioning callback.
10785 */
10786 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010787 public void registerRcsProvisioningCallback(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(), "registerRcsProvisioningCallback",
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 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010805 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010806 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10807 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010808 }
Hui Wang761a6682020-10-31 05:12:53 +000010809 } finally {
10810 Binder.restoreCallingIdentity(identity);
10811 }
10812 }
10813
10814 /**
10815 * Unregister RCS provisioning callback.
10816 */
10817 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010818 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010819 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010820 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010821 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010822 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10823 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010824
10825 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10826 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10827 }
10828 if (!isImsAvailableOnDevice()) {
10829 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10830 "IMS not available on device.");
10831 }
10832
10833 final long identity = Binder.clearCallingIdentity();
10834 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010835 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010836 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010837 } finally {
10838 Binder.restoreCallingIdentity(identity);
10839 }
10840 }
10841
10842 /**
10843 * trigger RCS reconfiguration.
10844 */
10845 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010846 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10847 "triggerRcsReconfiguration",
10848 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010849
10850 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10851 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10852 }
10853 if (!isImsAvailableOnDevice()) {
10854 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10855 "IMS not available on device.");
10856 }
10857
10858 final long identity = Binder.clearCallingIdentity();
10859 try {
10860 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10861 } finally {
10862 Binder.restoreCallingIdentity(identity);
10863 }
10864 }
10865
10866 /**
10867 * Provide the client configuration parameters of the RCS application.
10868 */
10869 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010870 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10871 "setRcsClientConfiguration",
10872 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010873
10874 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10875 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10876 }
10877 if (!isImsAvailableOnDevice()) {
10878 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10879 "IMS not available on device.");
10880 }
10881
10882 final long identity = Binder.clearCallingIdentity();
10883
10884 try {
10885 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10886 if (configBinder == null) {
10887 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010888 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10889 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010890 } else {
10891 configBinder.setRcsClientConfiguration(rcc);
10892 }
joonhunshin3e154242021-09-17 06:33:39 +000010893
10894 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10895 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010896 } catch (RemoteException e) {
10897 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010898 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10899 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010900 } finally {
10901 Binder.restoreCallingIdentity(identity);
10902 }
10903 }
10904
10905 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010906 * Enables or disables the test mode for RCS VoLTE single registration.
10907 */
10908 @Override
10909 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10910 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10911 "setRcsSingleRegistrationTestModeEnabled");
10912
10913 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10914 }
10915
10916 /**
10917 * Gets the test mode for RCS VoLTE single registration.
10918 */
10919 @Override
10920 public boolean getRcsSingleRegistrationTestModeEnabled() {
10921 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10922 "getRcsSingleRegistrationTestModeEnabled");
10923
10924 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10925 }
10926
10927 /**
Hui Wang761a6682020-10-31 05:12:53 +000010928 * Overrides the config of RCS VoLTE single registration enabled for the device.
10929 */
10930 @Override
10931 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10932 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10933 "setDeviceSingleRegistrationEnabledOverride");
10934 enforceModifyPermission();
10935
10936 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10937 : Boolean.parseBoolean(enabledStr);
10938 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010939 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010940 }
10941
10942 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010943 * Sends a device to device communication message. Only usable via shell.
10944 * @param message message to send.
10945 * @param value message value.
10946 */
10947 @Override
10948 public void sendDeviceToDeviceMessage(int message, int value) {
10949 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010950 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010951 enforceModifyPermission();
10952
10953 final long identity = Binder.clearCallingIdentity();
10954 try {
10955 TelephonyConnectionService service =
10956 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10957 if (service == null) {
10958 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10959 return;
10960 }
10961 service.sendTestDeviceToDeviceMessage(message, value);
10962 } finally {
10963 Binder.restoreCallingIdentity(identity);
10964 }
10965 }
10966
Tyler Gunnbabbda02021-02-10 11:05:02 -080010967 /**
10968 * Sets the specified device to device transport active.
10969 * @param transport The transport to set active.
10970 */
10971 @Override
10972 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10973 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10974 "setActiveDeviceToDeviceTransport");
10975 enforceModifyPermission();
10976
10977 final long identity = Binder.clearCallingIdentity();
10978 try {
10979 TelephonyConnectionService service =
10980 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10981 if (service == null) {
10982 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10983 return;
10984 }
10985 service.setActiveDeviceToDeviceTransport(transport);
10986 } finally {
10987 Binder.restoreCallingIdentity(identity);
10988 }
10989 }
Tyler Gunn92479152021-01-20 16:30:10 -080010990
Tyler Gunnd4339262021-05-03 14:46:49 -070010991 @Override
10992 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10993 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10994 "setDeviceToDeviceForceEnabled");
10995
10996 final long identity = Binder.clearCallingIdentity();
10997 try {
10998 Arrays.stream(PhoneFactory.getPhones()).forEach(
10999 p -> {
11000 Phone thePhone = p.getImsPhone();
11001 if (thePhone != null && thePhone instanceof ImsPhone) {
11002 ImsPhone imsPhone = (ImsPhone) thePhone;
11003 CallTracker tracker = imsPhone.getCallTracker();
11004 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
11005 ImsPhoneCallTracker imsPhoneCallTracker =
11006 (ImsPhoneCallTracker) tracker;
11007 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
11008 }
11009 }
11010 }
11011 );
11012 } finally {
11013 Binder.restoreCallingIdentity(identity);
11014 }
11015 }
11016
Tyler Gunn92479152021-01-20 16:30:10 -080011017 /**
Hui Wang761a6682020-10-31 05:12:53 +000011018 * Gets the config of RCS VoLTE single registration enabled for the device.
11019 */
11020 @Override
11021 public boolean getDeviceSingleRegistrationEnabled() {
11022 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
11023 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
11024 }
11025
11026 /**
11027 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
11028 */
11029 @Override
11030 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11031 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11032 "setCarrierSingleRegistrationEnabledOverride");
11033 enforceModifyPermission();
11034
11035 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11036 : Boolean.parseBoolean(enabledStr);
11037 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11038 subId, enabled);
11039 }
11040
11041 /**
11042 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11043 */
11044 @Override
11045 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11046 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11047 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11048 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011049
11050 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011051 * Overrides the ims feature validation result
11052 */
11053 @Override
11054 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11055 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11056 "setImsFeatureValidationOverride");
11057
11058 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11059 : Boolean.parseBoolean(enabledStr);
11060 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11061 subId, enabled);
11062 }
11063
11064 /**
11065 * Gets the ims feature validation override value
11066 */
11067 @Override
11068 public boolean getImsFeatureValidationOverride(int subId) {
11069 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11070 "getImsFeatureValidationOverride");
11071 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11072 }
11073
11074 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011075 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11076 * their mobile plan.
11077 */
11078 @Override
11079 public String getMobileProvisioningUrl() {
11080 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11081 final long identity = Binder.clearCallingIdentity();
11082 try {
11083 return getDefaultPhone().getMobileProvisioningUrl();
11084 } finally {
11085 Binder.restoreCallingIdentity(identity);
11086 }
11087 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011088
James.cf Linbcdf8b32021-01-14 16:44:13 +080011089 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011090 * Get the EAB contact from the EAB database.
11091 */
11092 @Override
11093 public String getContactFromEab(String contact) {
11094 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11095 enforceModifyPermission();
11096 final long identity = Binder.clearCallingIdentity();
11097 try {
11098 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11099 } finally {
11100 Binder.restoreCallingIdentity(identity);
11101 }
11102 }
11103
11104 /**
Calvin Pana1434322021-07-01 19:27:01 +080011105 * Get the EAB capability from the EAB database.
11106 */
11107 @Override
11108 public String getCapabilityFromEab(String contact) {
11109 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11110 enforceModifyPermission();
11111 final long identity = Binder.clearCallingIdentity();
11112 try {
11113 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11114 } finally {
11115 Binder.restoreCallingIdentity(identity);
11116 }
11117 }
11118
11119 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011120 * Remove the EAB contacts from the EAB database.
11121 */
11122 @Override
11123 public int removeContactFromEab(int subId, String contacts) {
11124 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11125 enforceModifyPermission();
11126 final long identity = Binder.clearCallingIdentity();
11127 try {
11128 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11129 } finally {
11130 Binder.restoreCallingIdentity(identity);
11131 }
11132 }
11133
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011134 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011135 public boolean getDeviceUceEnabled() {
11136 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11137 final long identity = Binder.clearCallingIdentity();
11138 try {
11139 return mApp.getDeviceUceEnabled();
11140 } finally {
11141 Binder.restoreCallingIdentity(identity);
11142 }
11143 }
11144
11145 @Override
11146 public void setDeviceUceEnabled(boolean isEnabled) {
11147 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11148 final long identity = Binder.clearCallingIdentity();
11149 try {
11150 mApp.setDeviceUceEnabled(isEnabled);
11151 } finally {
11152 Binder.restoreCallingIdentity(identity);
11153 }
11154 }
11155
Brad Ebinger14d467f2021-02-12 06:18:28 +000011156 /**
11157 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11158 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11159 */
11160 // Used for SHELL command only right now.
11161 @Override
11162 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11163 List<String> featureTags) {
11164 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11165 "addUceRegistrationOverrideShell");
11166 final long identity = Binder.clearCallingIdentity();
11167 try {
11168 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11169 new ArraySet<>(featureTags));
11170 } catch (ImsException e) {
11171 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11172 } finally {
11173 Binder.restoreCallingIdentity(identity);
11174 }
11175 }
11176
11177 /**
11178 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11179 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11180 */
11181 // Used for SHELL command only right now.
11182 @Override
11183 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11184 List<String> featureTags) {
11185 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11186 "removeUceRegistrationOverrideShell");
11187 final long identity = Binder.clearCallingIdentity();
11188 try {
11189 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11190 new ArraySet<>(featureTags));
11191 } catch (ImsException e) {
11192 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11193 } finally {
11194 Binder.restoreCallingIdentity(identity);
11195 }
11196 }
11197
11198 /**
11199 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11200 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11201 */
11202 // Used for SHELL command only right now.
11203 @Override
11204 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11205 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11206 "clearUceRegistrationOverrideShell");
11207 final long identity = Binder.clearCallingIdentity();
11208 try {
11209 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11210 } catch (ImsException e) {
11211 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11212 } finally {
11213 Binder.restoreCallingIdentity(identity);
11214 }
11215 }
11216
11217 /**
11218 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11219 */
11220 // Used for SHELL command only right now.
11221 @Override
11222 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11223 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11224 "getLatestRcsContactUceCapabilityShell");
11225 final long identity = Binder.clearCallingIdentity();
11226 try {
11227 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11228 } catch (ImsException e) {
11229 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11230 } finally {
11231 Binder.restoreCallingIdentity(identity);
11232 }
11233 }
11234
11235 /**
11236 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11237 * device does not have an active PUBLISH.
11238 */
11239 // Used for SHELL command only right now.
11240 @Override
11241 public String getLastUcePidfXmlShell(int subId) {
11242 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11243 final long identity = Binder.clearCallingIdentity();
11244 try {
11245 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11246 } catch (ImsException e) {
11247 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11248 } finally {
11249 Binder.restoreCallingIdentity(identity);
11250 }
11251 }
11252
James.cf Line8713a42021-04-29 16:04:26 +080011253 /**
11254 * Remove UCE requests cannot be sent to the network status.
11255 */
11256 // Used for SHELL command only right now.
11257 @Override
11258 public boolean removeUceRequestDisallowedStatus(int subId) {
11259 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11260 final long identity = Binder.clearCallingIdentity();
11261 try {
11262 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11263 } catch (ImsException e) {
11264 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11265 } finally {
11266 Binder.restoreCallingIdentity(identity);
11267 }
11268 }
11269
James.cf Lin18bb9002021-05-25 01:37:38 +080011270 /**
11271 * Remove UCE requests cannot be sent to the network status.
11272 */
11273 // Used for SHELL command only.
11274 @Override
11275 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11276 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11277 final long identity = Binder.clearCallingIdentity();
11278 try {
11279 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11280 } catch (ImsException e) {
11281 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11282 } finally {
11283 Binder.restoreCallingIdentity(identity);
11284 }
11285 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011286
James.cf Lin4b784aa2021-01-31 03:25:15 +080011287 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011288 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11289 String callingPackage) {
11290 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11291 mApp, subId, "setSignalStrengthUpdateRequest");
11292
11293 final int callingUid = Binder.getCallingUid();
11294 // Verify that tha callingPackage belongs to the calling UID
11295 mApp.getSystemService(AppOpsManager.class)
11296 .checkPackage(callingUid, callingPackage);
11297
Rambo Wang3607f502021-02-01 21:51:40 -080011298 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011299
11300 final long identity = Binder.clearCallingIdentity();
11301 try {
11302 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11303 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11304
11305 if (result instanceof IllegalStateException) {
11306 throw (IllegalStateException) result;
11307 }
11308 } finally {
11309 Binder.restoreCallingIdentity(identity);
11310 }
11311 }
11312
11313 @Override
11314 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11315 String callingPackage) {
11316 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11317 mApp, subId, "clearSignalStrengthUpdateRequest");
11318
11319 final int callingUid = Binder.getCallingUid();
11320 // Verify that tha callingPackage belongs to the calling UID
11321 mApp.getSystemService(AppOpsManager.class)
11322 .checkPackage(callingUid, callingPackage);
11323
11324 final long identity = Binder.clearCallingIdentity();
11325 try {
11326 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11327 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11328
11329 if (result instanceof IllegalStateException) {
11330 throw (IllegalStateException) result;
11331 }
11332 } finally {
11333 Binder.restoreCallingIdentity(identity);
11334 }
11335 }
11336
Rambo Wang3607f502021-02-01 21:51:40 -080011337 private static void validateSignalStrengthUpdateRequest(Context context,
11338 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011339 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11340 // phone/system process do not have further restriction on request
11341 return;
11342 }
11343
11344 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011345 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011346 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011347 context.enforceCallingOrSelfPermission(
11348 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11349 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011350 }
11351
11352 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011353 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011354 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011355 || info.isEnabled()) {
11356 throw new IllegalArgumentException(
11357 "Only system can set hide fields in SignalThresholdInfo");
11358 }
11359
11360 // Thresholds length for each RAN need in range. This has been validated in
11361 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11362 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11363 final int[] thresholds = info.getThresholds();
11364 Objects.requireNonNull(thresholds);
11365 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11366 || thresholds.length
11367 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11368 throw new IllegalArgumentException(
11369 "thresholds length is out of range: " + thresholds.length);
11370 }
11371 }
11372 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011373
11374 /**
11375 * Gets the current phone capability.
11376 *
11377 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11378 * @return the PhoneCapability which describes the data connection capability of modem.
11379 * It's used to evaluate possible phone config change, for example from single
11380 * SIM device to multi-SIM device.
11381 */
11382 @Override
11383 public PhoneCapability getPhoneCapability() {
11384 enforceReadPrivilegedPermission("getPhoneCapability");
11385 final long identity = Binder.clearCallingIdentity();
11386 try {
11387 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11388 } finally {
11389 Binder.restoreCallingIdentity(identity);
11390 }
11391 }
Michele Berionne5e411512020-11-13 02:36:59 +000011392
11393 /**
11394 * Prepare TelephonyManager for an unattended reboot. The reboot is
11395 * required to be done shortly after the API is invoked.
11396 */
11397 @Override
11398 @TelephonyManager.PrepareUnattendedRebootResult
11399 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011400 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011401 enforceRebootPermission();
11402
11403 final long identity = Binder.clearCallingIdentity();
11404 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011405 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011406 } finally {
11407 Binder.restoreCallingIdentity(identity);
11408 }
11409 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011410
11411 /**
11412 * Request to get the current slicing configuration including URSP rules and
11413 * NSSAIs (configured, allowed and rejected).
11414 *
11415 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11416 */
11417 @Override
11418 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011419 TelephonyPermissions
11420 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11421 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011422
11423 final long identity = Binder.clearCallingIdentity();
11424 try {
11425 Phone phone = getDefaultPhone();
11426 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11427 } finally {
11428 Binder.restoreCallingIdentity(identity);
11429 }
11430 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011431
11432 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011433 * Check whether the given premium capability is available for purchase from the carrier.
11434 *
11435 * @param capability The premium capability to check.
11436 * @param subId The subId to check the premium capability for.
11437 *
11438 * @return Whether the given premium capability is available to purchase.
11439 */
11440 @Override
11441 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11442 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11443 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11444 log("Premium capability "
11445 + TelephonyManager.convertPremiumCapabilityToString(capability)
11446 + " is not available for purchase due to missing permissions.");
11447 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11448 + "permission READ_BASIC_PHONE_STATE.");
11449 }
11450
11451 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011452 if (phone == null) {
11453 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11454 return false;
11455 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011456 final long identity = Binder.clearCallingIdentity();
11457 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011458 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011459 .isPremiumCapabilityAvailableForPurchase(capability);
11460 } finally {
11461 Binder.restoreCallingIdentity(identity);
11462 }
11463 }
11464
11465 /**
11466 * Purchase the given premium capability from the carrier.
11467 *
11468 * @param capability The premium capability to purchase.
11469 * @param callback The result of the purchase request.
11470 * @param subId The subId to purchase the premium capability for.
11471 */
11472 @Override
11473 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11474 log("purchasePremiumCapability: capability="
11475 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11476 + getCurrentPackageName());
11477
11478 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11479 mApp, "purchasePremiumCapability")) {
11480 log("purchasePremiumCapability "
11481 + TelephonyManager.convertPremiumCapabilityToString(capability)
11482 + " failed due to missing permissions.");
11483 throw new SecurityException("purchasePremiumCapability requires permission "
11484 + "READ_BASIC_PHONE_STATE.");
11485 }
11486
11487 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011488 if (phone == null) {
11489 try {
11490 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11491 callback.accept(result);
11492 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11493 } catch (RemoteException e) {
11494 String logStr = "Purchase premium capability "
11495 + TelephonyManager.convertPremiumCapabilityToString(capability)
11496 + " failed due to RemoteException handling null phone: " + e;
11497 if (DBG) log(logStr);
11498 AnomalyReporter.reportAnomaly(
11499 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11500 }
11501 return;
11502 }
Sarah Chin71b3a852022-09-28 15:54:19 -070011503 String appName;
11504 try {
11505 appName = mApp.getPackageManager().getApplicationLabel(mApp.getPackageManager()
11506 .getApplicationInfo(getCurrentPackageName(), 0)).toString();
11507 } catch (PackageManager.NameNotFoundException e) {
11508 appName = "An application";
11509 }
11510 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
11511 new PurchasePremiumCapabilityArgument(capability, appName, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011512 }
11513
11514 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011515 * Register an IMS connection state callback
11516 */
11517 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011518 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11519 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011520 if (feature == ImsFeature.FEATURE_MMTEL) {
11521 // ImsMmTelManager
11522 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11523 TelephonyPermissions
11524 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11525 mApp, subId, "registerImsStateCallback");
11526 } else if (feature == ImsFeature.FEATURE_RCS) {
11527 // ImsRcsManager or SipDelegateManager
11528 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11529 Binder.getCallingUid(), "registerImsStateCallback",
11530 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11531 Manifest.permission.READ_PRECISE_PHONE_STATE,
11532 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11533 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11534 }
11535
11536 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11537 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11538 "IMS not available on device.");
11539 }
11540
11541 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11542 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11543 }
11544
11545 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11546 if (controller == null) {
11547 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11548 "IMS not available on device.");
11549 }
11550
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011551 if (callingPackage == null) {
11552 callingPackage = getCurrentPackageName();
11553 }
11554
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011555 final long token = Binder.clearCallingIdentity();
11556 try {
11557 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011558 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011559 } catch (ImsException e) {
11560 throw new ServiceSpecificException(e.getCode());
11561 } finally {
11562 Binder.restoreCallingIdentity(token);
11563 }
11564 }
11565
11566 /**
11567 * Unregister an IMS connection state callback
11568 */
11569 @Override
11570 public void unregisterImsStateCallback(IImsStateCallback cb) {
11571 final long token = Binder.clearCallingIdentity();
11572 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11573 if (controller == null) {
11574 return;
11575 }
11576 try {
11577 controller.unregisterImsStateCallback(cb);
11578 } finally {
11579 Binder.restoreCallingIdentity(token);
11580 }
11581 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011582
11583 /**
11584 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11585 *
11586 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11587 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11588 * SecurityException.
11589 * If there is current registered network this value will be same as the registered cell
11590 * identity. If the device goes out of service the previous cell identity is cached and
11591 * will be returned. If the cache age of the Cell identity is more than 24 hours
11592 * it will be cleared and null will be returned.
11593 *
11594 */
11595 @Override
11596 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11597 String callingFeatureId) {
11598 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11599 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11600 LocationAccessPolicy.checkLocationPermission(mApp,
11601 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11602 .setCallingPackage(callingPackage)
11603 .setCallingFeatureId(callingFeatureId)
11604 .setCallingPid(Binder.getCallingPid())
11605 .setCallingUid(Binder.getCallingUid())
11606 .setMethod("getLastKnownCellIdentity")
11607 .setLogAsInfo(true)
11608 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11609 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11610 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11611 .build());
11612
11613 boolean hasFinePermission =
11614 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11615 if (!hasFinePermission
11616 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11617 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011618 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011619 }
11620
11621 final long identity = Binder.clearCallingIdentity();
11622 try {
11623 Phone phone = getPhone(subId);
11624 if (phone == null) return null;
11625 ServiceStateTracker sst = phone.getServiceStateTracker();
11626 if (sst == null) return null;
11627 return sst.getLastKnownCellIdentity();
11628 } finally {
11629 Binder.restoreCallingIdentity(identity);
11630 }
11631 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011632
jimsun3b9ccac2021-10-26 15:01:23 +080011633 /**
11634 * Sets the modem service class Name that Telephony will bind to.
11635 *
11636 * @param serviceName The class name of the modem service.
11637 * @return true if the operation is succeed, otherwise false.
11638 */
11639 public boolean setModemService(String serviceName) {
11640 Log.d(LOG_TAG, "setModemService - " + serviceName);
11641 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11642 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11643 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11644 "setModemService");
11645 return mPhoneConfigurationManager.setModemService(serviceName);
11646 }
11647
11648 /**
11649 * Return the class name of the currently bounded modem service.
11650 *
11651 * @return the class name of the modem service.
11652 */
11653 public String getModemService() {
11654 String result;
11655 Log.d(LOG_TAG, "getModemService");
11656 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11657 TelephonyPermissions
11658 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11659 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11660 "getModemService");
11661 result = mPhoneConfigurationManager.getModemService();
11662 Log.d(LOG_TAG, "result = " + result);
11663 return result;
11664 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011665
11666 @Override
11667 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11668 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11669 mApp.enforceCallingOrSelfPermission(
11670 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11671 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11672
11673 final long identity = Binder.clearCallingIdentity();
11674 try {
11675 Phone phone = getPhone(subId);
11676 if (phone == null) return;
11677 phone.setVoiceServiceStateOverride(hasService);
11678 } finally {
11679 Binder.restoreCallingIdentity(identity);
11680 }
11681 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011682
11683 /**
11684 * set removable eSIM as default eUICC.
11685 *
11686 * @hide
11687 */
11688 @Override
11689 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11690 enforceModifyPermission();
11691 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11692
11693 final long identity = Binder.clearCallingIdentity();
11694 try {
11695 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11696 } finally {
11697 Binder.restoreCallingIdentity(identity);
11698 }
11699 }
11700
11701 /**
11702 * Returns whether the removable eSIM is default eUICC or not.
11703 *
11704 * @hide
11705 */
11706 @Override
11707 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11708 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11709 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11710
11711 final long identity = Binder.clearCallingIdentity();
11712 try {
11713 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11714 } finally {
11715 Binder.restoreCallingIdentity(identity);
11716 }
11717 }
11718
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011719 /**
11720 * Get the component name of the default app to direct respond-via-message intent for the
11721 * user associated with this subscription, update the cache if there is no respond-via-message
11722 * application currently configured for this user.
11723 * @return component name of the app and class to direct Respond Via Message intent to, or
11724 * {@code null} if the functionality is not supported.
11725 * @hide
11726 */
11727 @Override
11728 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11729 boolean updateIfNeeded) {
11730 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011731
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011732 Context context = getPhone(subId).getContext();
11733 UserHandle userHandle = null;
11734 final long identity = Binder.clearCallingIdentity();
11735 try {
11736 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11737 } finally {
11738 Binder.restoreCallingIdentity(identity);
11739 }
11740 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11741 updateIfNeeded, userHandle);
11742 }
Jack Yuf5badd92022-12-08 00:50:53 -080011743
11744 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011745 * Set whether the device is able to connect with null ciphering or integrity
11746 * algorithms. This is a global setting and will apply to all active subscriptions
11747 * and all new subscriptions after this.
11748 *
11749 * @param enabled when true, null cipher and integrity algorithms are allowed.
11750 * @hide
11751 */
11752 @Override
11753 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11754 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11755 enforceModifyPermission();
11756 checkForNullCipherAndIntegritySupport();
11757
11758 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11759 // for listening to these preference changes and applying them immediately.
11760 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11761 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11762 editor.apply();
11763
11764 for (Phone phone: PhoneFactory.getPhones()) {
11765 phone.handleNullCipherEnabledChange();
11766 }
11767 }
11768
11769
11770 /**
11771 * Get whether the device is able to connect with null ciphering or integrity
11772 * algorithms. Note that this retrieves the phone-global preference and not
11773 * the state of the radio.
11774 *
11775 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11776 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11777 * version for this feature.
11778 * @hide
11779 */
11780 @Override
11781 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11782 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11783 enforceReadPermission();
11784 checkForNullCipherAndIntegritySupport();
11785 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11786 }
11787
11788 private void checkForNullCipherAndIntegritySupport() {
11789 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11790 throw new UnsupportedOperationException(
11791 "Null cipher and integrity operations require HAL 2.1 or above");
11792 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011793 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11794 throw new UnsupportedOperationException(
11795 "Null cipher and integrity operations unsupported by modem");
11796 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011797 }
11798
11799 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011800 * Get the SIM state for the slot index.
11801 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11802 *
11803 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11804 */
11805 @Override
11806 @SimState
11807 public int getSimStateForSlotIndex(int slotIndex) {
11808 IccCardConstants.State simState;
11809 if (slotIndex < 0) {
11810 simState = IccCardConstants.State.UNKNOWN;
11811 } else {
11812 Phone phone = null;
11813 try {
11814 phone = PhoneFactory.getPhone(slotIndex);
11815 } catch (IllegalStateException e) {
11816 // ignore
11817 }
11818 if (phone == null) {
11819 simState = IccCardConstants.State.UNKNOWN;
11820 } else {
11821 IccCard icc = phone.getIccCard();
11822 if (icc == null) {
11823 simState = IccCardConstants.State.UNKNOWN;
11824 } else {
11825 simState = icc.getState();
11826 }
11827 }
11828 }
11829 return simState.ordinal();
11830 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011831
11832 /**
11833 * Get current cell broadcast ranges.
11834 */
11835 @Override
11836 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11837 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11838 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11839 "getCellBroadcastIdRanges");
11840 final long identity = Binder.clearCallingIdentity();
11841 try {
11842 return getPhone(subId).getCellBroadcastIdRanges();
11843 } finally {
11844 Binder.restoreCallingIdentity(identity);
11845 }
11846 }
11847
11848 /**
11849 * Set reception of cell broadcast messages with the list of the given ranges
11850 *
11851 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11852 */
11853 @Override
11854 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11855 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11856 @Nullable IIntegerConsumer callback) {
11857 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11858 "setCellBroadcastIdRanges");
11859 final long identity = Binder.clearCallingIdentity();
11860 try {
11861 Phone phone = getPhoneFromSubId(subId);
11862 if (DBG) {
11863 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11864 }
11865 phone.setCellBroadcastIdRanges(ranges, result -> {
11866 if (callback != null) {
11867 try {
11868 callback.accept(result);
11869 } catch (RemoteException e) {
11870 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11871 }
11872 }
11873 });
11874 } finally {
11875 Binder.restoreCallingIdentity(identity);
11876 }
11877 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011878
11879 /**
11880 * Returns whether the device supports the domain selection service.
11881 *
11882 * @return {@code true} if the device supports the domain selection service.
11883 */
11884 @Override
11885 public boolean isDomainSelectionSupported() {
11886 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11887 "isDomainSelectionSupported");
11888
11889 final long identity = Binder.clearCallingIdentity();
11890 try {
11891 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11892 } finally {
11893 Binder.restoreCallingIdentity(identity);
11894 }
11895 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011896
11897 /**
11898 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11899 *
11900 * <p>This method behaves in one of the following ways:
11901 * <ul>
11902 * <li>return true : if the calling package has the appop permission {@link
11903 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11904 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11905 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11906 * Owner device identifier access check, or the calling package has carrier privileges</>
11907 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11908 * </ul>
11909 */
11910 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11911 String callingPackage, @Nullable String callingFeatureId, String message) {
11912 for (Phone phone : PhoneFactory.getPhones()) {
11913 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11914 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11915 return true;
11916 }
11917 }
11918 return false;
11919 }
11920}