blob: 14962afbcc697fc756ee06afa27750570f49e062 [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;
Aishwarya Mallampati32336e82023-01-04 23:02:56 +0000397 private 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
sandeepjsb6c87872021-09-27 15:34:44 +0000439 /**
440 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
441 * one ICCID active at the same time.
442 * Apps should use below API signatures if targeting SDK is T and beyond.
443 *
444 * @hide
445 */
446 @ChangeId
447 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
448 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800449
Naina Nallurid63128d2019-09-17 14:10:30 -0700450 /**
Chen Xu540470b2021-12-14 17:15:47 -0800451 * Apps targeting on Android T and beyond will get exception whenever icc close channel
452 * operation fails.
453 */
454 @ChangeId
455 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
456 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
457
458 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700459 * A request object to use for transmitting data to an ICC.
460 */
461 private static final class IccAPDUArgument {
462 public int channel, cla, command, p1, p2, p3;
463 public String data;
464
465 public IccAPDUArgument(int channel, int cla, int command,
466 int p1, int p2, int p3, String data) {
467 this.channel = channel;
468 this.cla = cla;
469 this.command = command;
470 this.p1 = p1;
471 this.p2 = p2;
472 this.p3 = p3;
473 this.data = data;
474 }
475 }
476
477 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700478 * A request object to use for transmitting data to an ICC.
479 */
480 private static final class ManualNetworkSelectionArgument {
481 public OperatorInfo operatorInfo;
482 public boolean persistSelection;
483
484 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
485 this.operatorInfo = operatorInfo;
486 this.persistSelection = persistSelection;
487 }
488 }
489
Sarah Chin71b3a852022-09-28 15:54:19 -0700490 private static final class PurchasePremiumCapabilityArgument {
491 public @TelephonyManager.PremiumCapability int capability;
492 public @NonNull String appName;
493 public @NonNull IIntegerConsumer callback;
494
495 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
496 @NonNull String appName, @NonNull IIntegerConsumer callback) {
497 this.capability = capability;
498 this.appName = appName;
499 this.callback = callback;
500 }
501 }
502
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700503 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700504 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
505 * request after sending. The main thread will notify the request when it is complete.
506 */
507 private static final class MainThreadRequest {
508 /** The argument to use for the request */
509 public Object argument;
510 /** The result of the request that is run on the main thread */
511 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800512 // The subscriber id that this request applies to. Defaults to
513 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
514 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515
Nathan Harold92bed182018-10-12 18:16:49 -0700516 // In cases where subId is unavailable, the caller needs to specify the phone.
517 public Phone phone;
518
vagdeviaf9a5b92018-08-15 16:01:53 -0700519 public WorkSource workSource;
520
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700521 public MainThreadRequest(Object argument) {
522 this.argument = argument;
523 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800524
Nathan Harold92bed182018-10-12 18:16:49 -0700525 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
526 this.argument = argument;
527 if (phone != null) {
528 this.phone = phone;
529 }
530 this.workSource = workSource;
531 }
532
vagdeviaf9a5b92018-08-15 16:01:53 -0700533 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800534 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800535 if (subId != null) {
536 this.subId = subId;
537 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700538 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800539 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700540 }
541
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800542 private static final class IncomingThirdPartyCallArgs {
543 public final ComponentName component;
544 public final String callId;
545 public final String callerDisplayName;
546
547 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
548 String callerDisplayName) {
549 this.component = component;
550 this.callId = callId;
551 this.callerDisplayName = callerDisplayName;
552 }
553 }
554
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 /**
556 * A handler that processes messages on the main thread in the phone process. Since many
557 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
558 * inbound binder threads to the main thread in the phone process. The Binder thread
559 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
560 * on, which will be notified when the operation completes and will contain the result of the
561 * request.
562 *
563 * <p>If a MainThreadRequest object is provided in the msg.obj field,
564 * note that request.result must be set to something non-null for the calling thread to
565 * unblock.
566 */
567 private final class MainThreadHandler extends Handler {
568 @Override
569 public void handleMessage(Message msg) {
570 MainThreadRequest request;
571 Message onCompleted;
572 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000573 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700574 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800575 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700576
577 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700578 case CMD_HANDLE_USSD_REQUEST: {
579 request = (MainThreadRequest) msg.obj;
580 final Phone phone = getPhoneFromRequest(request);
581 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800582 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700583 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700584
Pengquan Menga1bb6272018-09-06 09:59:22 -0700585 if (!isUssdApiAllowed(request.subId)) {
586 // Carrier does not support use of this API, return failure.
587 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
588 UssdResponse response = new UssdResponse(ussdRequest, null);
589 Bundle returnData = new Bundle();
590 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
591 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700592
Pengquan Menga1bb6272018-09-06 09:59:22 -0700593 request.result = true;
594 notifyRequester(request);
595 return;
596 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700597
Pengquan Menga1bb6272018-09-06 09:59:22 -0700598 try {
599 request.result = phone != null
600 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
601 } catch (CallStateException cse) {
602 request.result = false;
603 }
604 // Wake up the requesting thread
605 notifyRequester(request);
606 break;
pkanwar32d516d2016-10-14 19:37:38 -0700607 }
608
Yorke Lee716f67e2015-06-17 15:39:16 -0700609 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700610 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700611 final Phone phone = getPhoneFromRequest(request);
612 request.result = phone != null ?
613 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
614 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700615 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700616 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700618 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700620 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700621 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700622 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000623 uiccPort = getUiccPortFromRequest(request);
624 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700625 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800626 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700627 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700628 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700629 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800630 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000631 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800632 iccArgument.channel, iccArgument.cla, iccArgument.command,
633 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
634 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700635 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700636 break;
637
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700638 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700639 ar = (AsyncResult) msg.obj;
640 request = (MainThreadRequest) ar.userObj;
641 if (ar.exception == null && ar.result != null) {
642 request.result = ar.result;
643 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800644 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700645 if (ar.result == null) {
646 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800647 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700648 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800649 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700650 } else {
651 loge("iccTransmitApduLogicalChannel: Unknown exception");
652 }
653 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700654 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700655 break;
656
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700657 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
658 request = (MainThreadRequest) msg.obj;
659 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000660 uiccPort = getUiccPortFromRequest(request);
661 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700662 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800663 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700664 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700665 } else {
666 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800667 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000668 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800669 iccArgument.cla, iccArgument.command, iccArgument.p1,
670 iccArgument.p2,
671 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700672 }
673 break;
674
675 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
676 ar = (AsyncResult) msg.obj;
677 request = (MainThreadRequest) ar.userObj;
678 if (ar.exception == null && ar.result != null) {
679 request.result = ar.result;
680 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800681 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700682 if (ar.result == null) {
683 loge("iccTransmitApduBasicChannel: Empty response");
684 } else if (ar.exception instanceof CommandException) {
685 loge("iccTransmitApduBasicChannel: CommandException: " +
686 ar.exception);
687 } else {
688 loge("iccTransmitApduBasicChannel: Unknown exception");
689 }
690 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700691 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700692 break;
693
694 case CMD_EXCHANGE_SIM_IO:
695 request = (MainThreadRequest) msg.obj;
696 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000697 uiccPort = getUiccPortFromRequest(request);
698 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700699 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800700 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700701 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700702 } else {
703 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
704 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000705 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700706 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
707 iccArgument.data, onCompleted);
708 }
709 break;
710
711 case EVENT_EXCHANGE_SIM_IO_DONE:
712 ar = (AsyncResult) msg.obj;
713 request = (MainThreadRequest) ar.userObj;
714 if (ar.exception == null && ar.result != null) {
715 request.result = ar.result;
716 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800717 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700718 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700719 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700720 break;
721
Derek Tan4d5e5c12014-02-04 11:54:58 -0800722 case CMD_SEND_ENVELOPE:
723 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000724 uiccPort = getUiccPortFromRequest(request);
725 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700726 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800727 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700728 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700729 } else {
730 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800731 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700732 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800733 break;
734
735 case EVENT_SEND_ENVELOPE_DONE:
736 ar = (AsyncResult) msg.obj;
737 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700738 if (ar.exception == null && ar.result != null) {
739 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800740 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800741 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700742 if (ar.result == null) {
743 loge("sendEnvelopeWithStatus: Empty response");
744 } else if (ar.exception instanceof CommandException) {
745 loge("sendEnvelopeWithStatus: CommandException: " +
746 ar.exception);
747 } else {
748 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
749 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800750 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700751 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800752 break;
753
Shishir Agrawal566b7612013-10-28 14:41:00 -0700754 case CMD_OPEN_CHANNEL:
755 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000756 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800757 IccLogicalChannelRequest openChannelRequest =
758 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000759 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700760 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800761 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800762 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700763 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700764 } else {
765 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800766 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
767 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700768 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700769 break;
770
771 case EVENT_OPEN_CHANNEL_DONE:
772 ar = (AsyncResult) msg.obj;
773 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700774 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700775 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700776 int[] result = (int[]) ar.result;
777 int channelId = result[0];
778 byte[] selectResponse = null;
779 if (result.length > 1) {
780 selectResponse = new byte[result.length - 1];
781 for (int i = 1; i < result.length; ++i) {
782 selectResponse[i - 1] = (byte) result[i];
783 }
784 }
785 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800786 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800787
788 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700789 if (uiccPort == null) {
790 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
791 } else {
792 IccLogicalChannelRequest channelRequest =
793 (IccLogicalChannelRequest) request.argument;
794 channelRequest.channel = channelId;
795 uiccPort.onLogicalChannelOpened(channelRequest);
796 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700797 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700798 if (ar.result == null) {
799 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700800 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700801 if (ar.exception != null) {
802 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
803 }
804
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700805 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700806 if (ar.exception instanceof CommandException) {
807 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800808 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700809 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700810 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700811 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700812 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700813 }
814 }
815 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800816 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700817 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700818 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700819 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700820 break;
821
822 case CMD_CLOSE_CHANNEL:
823 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000824 uiccPort = getUiccPortFromRequest(request);
825 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700826 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800827 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800828 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800829 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700830 } else {
831 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000832 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700833 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700834 break;
835
836 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800837 ar = (AsyncResult) msg.obj;
838 request = (MainThreadRequest) ar.userObj;
839 if (ar.exception == null) {
840 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800841 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700842 if (uiccPort == null) {
843 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
844 } else {
845 final int channelId = (Integer) request.argument;
846 uiccPort.onLogicalChannelClosed(channelId);
847 }
Chen Xu540470b2021-12-14 17:15:47 -0800848 } else {
849 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800850 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800851 if (ar.exception instanceof CommandException) {
852 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
853 CommandException.Error error =
854 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800855 if (error == CommandException.Error.INVALID_ARGUMENTS) {
856 // should only throw exceptions from the binder threads.
857 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800858 "iccCloseLogicalChannel: invalid argument ");
859 }
860 } else {
861 loge("iccCloseLogicalChannel: Unknown exception");
862 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800863 request.result = (exception != null) ? exception :
864 new IllegalStateException(
865 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800866 }
867 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800868 break;
869
870 case CMD_NV_READ_ITEM:
871 request = (MainThreadRequest) msg.obj;
872 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800873 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
874 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800875 break;
876
877 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700878 ar = (AsyncResult) msg.obj;
879 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800880 if (ar.exception == null && ar.result != null) {
881 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700882 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800883 request.result = "";
884 if (ar.result == null) {
885 loge("nvReadItem: Empty response");
886 } else if (ar.exception instanceof CommandException) {
887 loge("nvReadItem: CommandException: " +
888 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700889 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800890 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700891 }
892 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700893 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700894 break;
895
Jake Hambye994d462014-02-03 13:10:13 -0800896 case CMD_NV_WRITE_ITEM:
897 request = (MainThreadRequest) msg.obj;
898 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
899 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800900 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700901 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800902 break;
903
904 case EVENT_NV_WRITE_ITEM_DONE:
905 handleNullReturnEvent(msg, "nvWriteItem");
906 break;
907
908 case CMD_NV_WRITE_CDMA_PRL:
909 request = (MainThreadRequest) msg.obj;
910 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800911 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800912 break;
913
914 case EVENT_NV_WRITE_CDMA_PRL_DONE:
915 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
916 break;
917
chen xu6dac5ab2018-10-26 17:39:23 -0700918 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800919 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700920 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800921 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800922 break;
923
chen xu6dac5ab2018-10-26 17:39:23 -0700924 case EVENT_RESET_MODEM_CONFIG_DONE:
925 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800926 break;
927
Sooraj Sasindran37444802020-08-11 10:40:43 -0700928 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
929 request = (MainThreadRequest) msg.obj;
930 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
931 request);
932 Phone phone = getPhoneFromRequest(request);
933 if (phone != null) {
934 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
935 } else {
936 loge("isNRDualConnectivityEnabled: No phone object");
937 request.result = false;
938 notifyRequester(request);
939 }
940 break;
941 }
942
943 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
944 ar = (AsyncResult) msg.obj;
945 request = (MainThreadRequest) ar.userObj;
946 if (ar.exception == null && ar.result != null) {
947 request.result = ar.result;
948 } else {
949 // request.result must be set to something non-null
950 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700951 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700952 request.result = ar.result;
953 } else {
954 request.result = false;
955 }
956 if (ar.result == null) {
957 loge("isNRDualConnectivityEnabled: Empty response");
958 } else if (ar.exception instanceof CommandException) {
959 loge("isNRDualConnectivityEnabled: CommandException: "
960 + ar.exception);
961 } else {
962 loge("isNRDualConnectivityEnabled: Unknown exception");
963 }
964 }
965 notifyRequester(request);
966 break;
967
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700968 case CMD_IS_VONR_ENABLED: {
969 request = (MainThreadRequest) msg.obj;
970 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
971 request);
972 Phone phone = getPhoneFromRequest(request);
973 if (phone != null) {
974 phone.isVoNrEnabled(onCompleted, request.workSource);
975 } else {
976 loge("isVoNrEnabled: No phone object");
977 request.result = false;
978 notifyRequester(request);
979 }
980 break;
981 }
982
983 case EVENT_IS_VONR_ENABLED_DONE:
984 ar = (AsyncResult) msg.obj;
985 request = (MainThreadRequest) ar.userObj;
986 if (ar.exception == null && ar.result != null) {
987 request.result = ar.result;
988 } else {
989 // request.result must be set to something non-null
990 // for the calling thread to unblock
991 if (ar.result != null) {
992 request.result = ar.result;
993 } else {
994 request.result = false;
995 }
996 if (ar.result == null) {
997 loge("isVoNrEnabled: Empty response");
998 } else if (ar.exception instanceof CommandException) {
999 loge("isVoNrEnabled: CommandException: "
1000 + ar.exception);
1001 } else {
1002 loge("isVoNrEnabled: Unknown exception");
1003 }
1004 }
1005 notifyRequester(request);
1006 break;
1007
Sooraj Sasindran37444802020-08-11 10:40:43 -07001008 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1009 request = (MainThreadRequest) msg.obj;
1010 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1011 Phone phone = getPhoneFromRequest(request);
1012 if (phone != null) {
1013 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1014 request.workSource);
1015 } else {
1016 loge("enableNrDualConnectivity: No phone object");
1017 request.result =
1018 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1019 notifyRequester(request);
1020 }
1021 break;
1022 }
1023
1024 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1025 ar = (AsyncResult) msg.obj;
1026 request = (MainThreadRequest) ar.userObj;
1027 if (ar.exception == null) {
1028 request.result =
1029 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1030 } else {
1031 request.result =
1032 TelephonyManager
1033 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1034 if (ar.exception instanceof CommandException) {
1035 CommandException.Error error =
1036 ((CommandException) (ar.exception)).getCommandError();
1037 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1038 request.result =
1039 TelephonyManager
1040 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001041 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1042 request.result =
1043 TelephonyManager
1044 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001045 }
1046 loge("enableNrDualConnectivity" + ": CommandException: "
1047 + ar.exception);
1048 } else {
1049 loge("enableNrDualConnectivity" + ": Unknown exception");
1050 }
1051 }
1052 notifyRequester(request);
1053 break;
1054 }
1055
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001056 case CMD_ENABLE_VONR: {
1057 request = (MainThreadRequest) msg.obj;
1058 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1059 Phone phone = getPhoneFromRequest(request);
1060 if (phone != null) {
1061 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1062 request.workSource);
1063 } else {
1064 loge("setVoNrEnabled: No phone object");
1065 request.result =
1066 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1067 notifyRequester(request);
1068 }
1069 break;
1070 }
1071
1072 case EVENT_ENABLE_VONR_DONE: {
1073 ar = (AsyncResult) msg.obj;
1074 request = (MainThreadRequest) ar.userObj;
1075 if (ar.exception == null) {
1076 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1077 } else {
1078 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1079 if (ar.exception instanceof CommandException) {
1080 CommandException.Error error =
1081 ((CommandException) (ar.exception)).getCommandError();
1082 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1083 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1084 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1085 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1086 } else {
1087 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1088 }
1089 loge("setVoNrEnabled" + ": CommandException: "
1090 + ar.exception);
1091 } else {
1092 loge("setVoNrEnabled" + ": Unknown exception");
1093 }
1094 }
1095 notifyRequester(request);
1096 break;
1097 }
1098
SongFerngWang3ef3e072020-12-21 16:41:52 +08001099 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001100 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001101 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1102 request);
1103 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001104 break;
1105
SongFerngWang3ef3e072020-12-21 16:41:52 +08001106 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001107 ar = (AsyncResult) msg.obj;
1108 request = (MainThreadRequest) ar.userObj;
1109 if (ar.exception == null && ar.result != null) {
1110 request.result = ar.result; // Integer
1111 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301112 // request.result must be set to something non-null
1113 // for the calling thread to unblock
1114 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001115 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001116 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001117 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001118 loge("getAllowedNetworkTypesBitmask: CommandException: "
1119 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001120 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001121 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001122 }
1123 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001124 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001125 break;
1126
SongFerngWang3ef3e072020-12-21 16:41:52 +08001127 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001128 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001129 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1130 request);
1131 Pair<Integer, Long> reasonWithNetworkTypes =
1132 (Pair<Integer, Long>) request.argument;
1133 getPhoneFromRequest(request).setAllowedNetworkTypes(
1134 reasonWithNetworkTypes.first,
1135 reasonWithNetworkTypes.second,
1136 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001137 break;
1138
SongFerngWang3ef3e072020-12-21 16:41:52 +08001139 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1140 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001141 break;
1142
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001143 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1144 request = (MainThreadRequest)msg.obj;
1145 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001146 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001147 break;
1148
1149 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1150 ar = (AsyncResult)msg.obj;
1151 request = (MainThreadRequest)ar.userObj;
1152 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001153 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001154 break;
1155
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001156 case CMD_SET_VOICEMAIL_NUMBER:
1157 request = (MainThreadRequest) msg.obj;
1158 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1159 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001160 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1161 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001162 break;
1163
1164 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1165 handleNullReturnEvent(msg, "setVoicemailNumber");
1166 break;
1167
Stuart Scott54788802015-03-30 13:18:01 -07001168 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1169 request = (MainThreadRequest) msg.obj;
1170 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1171 request);
1172 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1173 break;
1174
1175 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1176 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1177 break;
1178
Shishir Agrawal302c8692015-06-19 13:49:39 -07001179 case CMD_PERFORM_NETWORK_SCAN:
1180 request = (MainThreadRequest) msg.obj;
1181 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1182 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1183 break;
1184
Hall Liu27d24262020-09-18 19:04:59 -07001185 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001186 request = (MainThreadRequest) msg.obj;
1187 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001188 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1189 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1190 request.argument;
1191 int callForwardingReason = args.first;
1192 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001193 break;
Hall Liu27d24262020-09-18 19:04:59 -07001194 }
1195 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001196 ar = (AsyncResult) msg.obj;
1197 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001198 TelephonyManager.CallForwardingInfoCallback callback =
1199 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1200 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001201 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001202 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001203 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1204 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1205 // Service Class is a bit mask per 3gpp 27.007. Search for
1206 // any service for voice call.
1207 if ((callForwardInfo.serviceClass
1208 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001209 callForwardingInfo = new CallForwardingInfo(
1210 callForwardInfo.status
1211 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001212 callForwardInfo.reason,
1213 callForwardInfo.number,
1214 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001215 break;
1216 }
1217 }
1218 // Didn't find a call forward info for voice call.
1219 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001220 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1221 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001222 }
Hall Liu27d24262020-09-18 19:04:59 -07001223 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001224 } else {
1225 if (ar.result == null) {
1226 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1227 }
1228 if (ar.exception != null) {
1229 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1230 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001231 int errorCode = TelephonyManager
1232 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001233 if (ar.exception instanceof CommandException) {
1234 CommandException.Error error =
1235 ((CommandException) (ar.exception)).getCommandError();
1236 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001237 errorCode = TelephonyManager
1238 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001239 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001240 errorCode = TelephonyManager
1241 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001242 }
1243 }
Hall Liu27d24262020-09-18 19:04:59 -07001244 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001245 }
Shuo Qian4a594052020-01-23 11:59:30 -08001246 break;
Hall Liu27d24262020-09-18 19:04:59 -07001247 }
Shuo Qian4a594052020-01-23 11:59:30 -08001248
Hall Liu27d24262020-09-18 19:04:59 -07001249 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001250 request = (MainThreadRequest) msg.obj;
1251 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001252 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001253 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001254 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1255 request.argument).first;
1256 request.phone.setCallForwardingOption(
1257 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001258 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001259 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001260 callForwardingInfoToSet.getReason(),
1261 callForwardingInfoToSet.getNumber(),
1262 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1263 break;
Hall Liu27d24262020-09-18 19:04:59 -07001264 }
Shuo Qian4a594052020-01-23 11:59:30 -08001265
Hall Liu27d24262020-09-18 19:04:59 -07001266 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001267 ar = (AsyncResult) msg.obj;
1268 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001269 Consumer<Integer> callback =
1270 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1271 request.argument).second;
1272 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001273 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001274 int errorCode = TelephonyManager.CallForwardingInfoCallback
1275 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001276 if (ar.exception instanceof CommandException) {
1277 CommandException.Error error =
1278 ((CommandException) (ar.exception)).getCommandError();
1279 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001280 errorCode = TelephonyManager.CallForwardingInfoCallback
1281 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001282 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001283 errorCode = TelephonyManager.CallForwardingInfoCallback
1284 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001285 }
1286 }
1287 callback.accept(errorCode);
1288 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001289 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001290 }
Shuo Qian4a594052020-01-23 11:59:30 -08001291 break;
Hall Liu27d24262020-09-18 19:04:59 -07001292 }
Shuo Qian4a594052020-01-23 11:59:30 -08001293
Hall Liu27d24262020-09-18 19:04:59 -07001294 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001295 request = (MainThreadRequest) msg.obj;
1296 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1297 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1298 break;
Hall Liu27d24262020-09-18 19:04:59 -07001299 }
Shuo Qian4a594052020-01-23 11:59:30 -08001300
Hall Liu27d24262020-09-18 19:04:59 -07001301 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001302 ar = (AsyncResult) msg.obj;
1303 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001304 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001305 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001306 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001307 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001308 // Service Class is a bit mask per 3gpp 27.007.
1309 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001310 if (callForwardResults.length > 1
1311 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001312 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001313 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001314 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1315 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001316 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001317 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001318 }
1319 } else {
1320 if (ar.result == null) {
1321 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1322 }
1323 if (ar.exception != null) {
1324 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1325 }
1326 if (ar.exception instanceof CommandException) {
1327 CommandException.Error error =
1328 ((CommandException) (ar.exception)).getCommandError();
1329 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001330 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001331 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001332 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1333 callWaitingStatus =
1334 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001335 }
1336 }
1337 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001338 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001339 break;
Hall Liu27d24262020-09-18 19:04:59 -07001340 }
Shuo Qian4a594052020-01-23 11:59:30 -08001341
Hall Liu27d24262020-09-18 19:04:59 -07001342 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001343 request = (MainThreadRequest) msg.obj;
1344 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001345 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1346 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001347 break;
Hall Liu27d24262020-09-18 19:04:59 -07001348 }
Shuo Qian4a594052020-01-23 11:59:30 -08001349
Hall Liu27d24262020-09-18 19:04:59 -07001350 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001351 ar = (AsyncResult) msg.obj;
1352 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001353 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1354 Consumer<Integer> callback =
1355 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1356 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001357 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001358 if (ar.exception instanceof CommandException) {
1359 CommandException.Error error =
1360 ((CommandException) (ar.exception)).getCommandError();
1361 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1362 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001363 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1364 callback.accept(
1365 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001366 } else {
1367 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1368 }
1369 } else {
1370 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1371 }
1372 } else {
1373 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1374 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001375 }
Shuo Qian4a594052020-01-23 11:59:30 -08001376 break;
Hall Liu27d24262020-09-18 19:04:59 -07001377 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001378 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1379 ar = (AsyncResult) msg.obj;
1380 request = (MainThreadRequest) ar.userObj;
1381 CellNetworkScanResult cellScanResult;
1382 if (ar.exception == null && ar.result != null) {
1383 cellScanResult = new CellNetworkScanResult(
1384 CellNetworkScanResult.STATUS_SUCCESS,
1385 (List<OperatorInfo>) ar.result);
1386 } else {
1387 if (ar.result == null) {
1388 loge("getCellNetworkScanResults: Empty response");
1389 }
1390 if (ar.exception != null) {
1391 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1392 }
1393 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1394 if (ar.exception instanceof CommandException) {
1395 CommandException.Error error =
1396 ((CommandException) (ar.exception)).getCommandError();
1397 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1398 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1399 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1400 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1401 }
1402 }
1403 cellScanResult = new CellNetworkScanResult(errorCode, null);
1404 }
1405 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001406 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001407 break;
1408
1409 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1410 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001411 ManualNetworkSelectionArgument selArg =
1412 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001413 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1414 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001415 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1416 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001417 break;
1418
1419 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001420 ar = (AsyncResult) msg.obj;
1421 request = (MainThreadRequest) ar.userObj;
1422 if (ar.exception == null) {
1423 request.result = true;
1424 } else {
1425 request.result = false;
1426 loge("setNetworkSelectionModeManual " + ar.exception);
1427 }
1428 notifyRequester(request);
1429 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001430 break;
1431
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001432 case CMD_GET_MODEM_ACTIVITY_INFO:
1433 request = (MainThreadRequest) msg.obj;
1434 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001435 if (defaultPhone != null) {
1436 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001437 } else {
1438 ResultReceiver result = (ResultReceiver) request.argument;
1439 Bundle bundle = new Bundle();
1440 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001441 new ModemActivityInfo(0, 0, 0,
1442 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001443 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001444 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001445 break;
1446
Hall Liud0f208c2020-10-14 16:54:44 -07001447 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001448 ar = (AsyncResult) msg.obj;
1449 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001450 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001451 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001452 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001453 if (mLastModemActivityInfo == null) {
1454 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1455 mLastModemActivitySpecificInfo[0] =
1456 new ActivityStatsTechSpecificInfo(
1457 0,
1458 0,
1459 new int[ModemActivityInfo.getNumTxPowerLevels()],
1460 0);
1461 mLastModemActivityInfo =
1462 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1463 }
1464
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001465 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001466 // Update the last modem activity info and the result of the request.
1467 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1468 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001469 mergeModemActivityInfo(info);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001470 }
Kai Shi917fdc62022-11-28 14:01:02 -08001471 // This is needed to decouple ret from mLastModemActivityInfo
1472 // We don't want to return mLastModemActivityInfo which is updated
1473 // inside mergeModemActivityInfo()
1474 ret = new ModemActivityInfo(
1475 mLastModemActivityInfo.getTimestampMillis(),
1476 mLastModemActivityInfo.getSleepTimeMillis(),
1477 mLastModemActivityInfo.getIdleTimeMillis(),
1478 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001479
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001480 } else {
1481 if (ar.result == null) {
1482 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001483 error = TelephonyManager.ModemActivityInfoException
1484 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001485 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001486 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001487 error = TelephonyManager.ModemActivityInfoException
1488 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001489 } else {
1490 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001491 error = TelephonyManager.ModemActivityInfoException
1492 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001493 }
1494 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001495 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001496 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001497 bundle.putParcelable(
1498 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001499 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001500 } else {
1501 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1502 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001503 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001504 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001505 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001506 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001507
Meng Wang1a7c35a2016-05-05 20:56:15 -07001508 case CMD_SET_ALLOWED_CARRIERS:
1509 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001510 CarrierRestrictionRules argument =
1511 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001512 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001513 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001514 break;
1515
1516 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1517 ar = (AsyncResult) msg.obj;
1518 request = (MainThreadRequest) ar.userObj;
1519 if (ar.exception == null && ar.result != null) {
1520 request.result = ar.result;
1521 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001522 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1523 if (ar.exception instanceof CommandException) {
1524 loge("setAllowedCarriers: CommandException: " + ar.exception);
1525 CommandException.Error error =
1526 ((CommandException) (ar.exception)).getCommandError();
1527 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1528 request.result =
1529 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1530 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001531 } else {
1532 loge("setAllowedCarriers: Unknown exception");
1533 }
1534 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001535 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001536 break;
1537
1538 case CMD_GET_ALLOWED_CARRIERS:
1539 request = (MainThreadRequest) msg.obj;
1540 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001541 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001542 break;
1543
1544 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1545 ar = (AsyncResult) msg.obj;
1546 request = (MainThreadRequest) ar.userObj;
1547 if (ar.exception == null && ar.result != null) {
1548 request.result = ar.result;
1549 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001550 request.result = new IllegalStateException(
1551 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001552 if (ar.result == null) {
1553 loge("getAllowedCarriers: Empty response");
1554 } else if (ar.exception instanceof CommandException) {
1555 loge("getAllowedCarriers: CommandException: " +
1556 ar.exception);
1557 } else {
1558 loge("getAllowedCarriers: Unknown exception");
1559 }
1560 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001561 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001562 break;
1563
Nathan Haroldb3014052017-01-25 15:57:32 -08001564 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1565 ar = (AsyncResult) msg.obj;
1566 request = (MainThreadRequest) ar.userObj;
1567 if (ar.exception == null && ar.result != null) {
1568 request.result = ar.result;
1569 } else {
1570 request.result = new IllegalArgumentException(
1571 "Failed to retrieve Forbidden Plmns");
1572 if (ar.result == null) {
1573 loge("getForbiddenPlmns: Empty response");
1574 } else {
1575 loge("getForbiddenPlmns: Unknown exception");
1576 }
1577 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001578 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001579 break;
1580
1581 case CMD_GET_FORBIDDEN_PLMNS:
1582 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001583 uiccPort = getUiccPortFromRequest(request);
1584 if (uiccPort == null) {
1585 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001586 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001587 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001588 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001589 break;
1590 }
1591 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001592 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001593 if (uiccApp == null) {
1594 loge("getForbiddenPlmns() no app with specified type -- "
1595 + appType);
1596 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001597 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001598 break;
1599 } else {
1600 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1601 + " specified type -- " + appType);
1602 }
1603 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1604 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1605 onCompleted);
1606 break;
1607
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001608 case CMD_SWITCH_SLOTS:
1609 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001610 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001611 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001612 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001613 break;
1614
1615 case EVENT_SWITCH_SLOTS_DONE:
1616 ar = (AsyncResult) msg.obj;
1617 request = (MainThreadRequest) ar.userObj;
1618 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001619 notifyRequester(request);
1620 break;
1621 case CMD_GET_NETWORK_SELECTION_MODE:
1622 request = (MainThreadRequest) msg.obj;
1623 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1624 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1625 break;
1626
1627 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1628 ar = (AsyncResult) msg.obj;
1629 request = (MainThreadRequest) ar.userObj;
1630 if (ar.exception != null) {
1631 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1632 } else {
1633 int mode = ((int[]) ar.result)[0];
1634 if (mode == 0) {
1635 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1636 } else {
1637 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1638 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001639 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001640 notifyRequester(request);
1641 break;
1642 case CMD_GET_CDMA_ROAMING_MODE:
1643 request = (MainThreadRequest) msg.obj;
1644 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1645 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1646 break;
1647 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1648 ar = (AsyncResult) msg.obj;
1649 request = (MainThreadRequest) ar.userObj;
1650 if (ar.exception != null) {
1651 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1652 } else {
1653 request.result = ((int[]) ar.result)[0];
1654 }
1655 notifyRequester(request);
1656 break;
1657 case CMD_SET_CDMA_ROAMING_MODE:
1658 request = (MainThreadRequest) msg.obj;
1659 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1660 int mode = (int) request.argument;
1661 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1662 break;
1663 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1664 ar = (AsyncResult) msg.obj;
1665 request = (MainThreadRequest) ar.userObj;
1666 request.result = ar.exception == null;
1667 notifyRequester(request);
1668 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001669 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1670 request = (MainThreadRequest) msg.obj;
1671 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1672 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1673 break;
1674 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1675 ar = (AsyncResult) msg.obj;
1676 request = (MainThreadRequest) ar.userObj;
1677 if (ar.exception != null) {
1678 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1679 } else {
1680 request.result = ((int[]) ar.result)[0];
1681 }
1682 notifyRequester(request);
1683 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001684 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1685 request = (MainThreadRequest) msg.obj;
1686 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1687 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001688 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1689 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001690 break;
1691 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1692 ar = (AsyncResult) msg.obj;
1693 request = (MainThreadRequest) ar.userObj;
1694 request.result = ar.exception == null;
1695 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001696 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001697 case CMD_GET_ALL_CELL_INFO:
1698 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001699 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001700 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001701 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001702 case EVENT_GET_ALL_CELL_INFO_DONE:
1703 ar = (AsyncResult) msg.obj;
1704 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001705 // If a timeout occurs, the response will be null
1706 request.result = (ar.exception == null && ar.result != null)
1707 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001708 synchronized (request) {
1709 request.notifyAll();
1710 }
1711 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001712 case CMD_REQUEST_CELL_INFO_UPDATE:
1713 request = (MainThreadRequest) msg.obj;
1714 request.phone.requestCellInfoUpdate(request.workSource,
1715 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1716 break;
1717 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1718 ar = (AsyncResult) msg.obj;
1719 request = (MainThreadRequest) ar.userObj;
1720 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1721 try {
1722 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001723 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001724 cb.onError(
1725 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1726 ar.exception.getClass().getName(),
1727 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001728 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001729 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001730 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001731 } else {
1732 // use the result as returned
1733 cb.onCellInfo((List<CellInfo>) ar.result);
1734 }
1735 } catch (RemoteException re) {
1736 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1737 }
1738 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001739 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001740 request = (MainThreadRequest) msg.obj;
1741 WorkSource ws = (WorkSource) request.argument;
1742 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001743 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001744 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001745 }
1746 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001747 ar = (AsyncResult) msg.obj;
1748 request = (MainThreadRequest) ar.userObj;
1749 if (ar.exception == null) {
1750 request.result = ar.result;
1751 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001752 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001753 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001754 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001755 }
1756
1757 synchronized (request) {
1758 request.notifyAll();
1759 }
1760 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001761 }
chen xu6dac5ab2018-10-26 17:39:23 -07001762 case CMD_MODEM_REBOOT:
1763 request = (MainThreadRequest) msg.obj;
1764 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001765 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001766 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001767 case EVENT_CMD_MODEM_REBOOT_DONE:
1768 handleNullReturnEvent(msg, "rebootModem");
1769 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001770 case CMD_REQUEST_ENABLE_MODEM:
1771 request = (MainThreadRequest) msg.obj;
1772 boolean enable = (boolean) request.argument;
1773 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001774 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001775 PhoneConfigurationManager.getInstance()
1776 .enablePhone(request.phone, enable, onCompleted);
1777 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001778 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001779 ar = (AsyncResult) msg.obj;
1780 request = (MainThreadRequest) ar.userObj;
1781 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001782 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001783 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001784 if ((boolean) request.result) {
1785 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1786 updateModemStateMetrics();
1787 } else {
1788 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1789 + ar.exception);
1790 }
1791 notifyRequester(request);
1792 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001793 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001794 case CMD_GET_MODEM_STATUS:
1795 request = (MainThreadRequest) msg.obj;
1796 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1797 PhoneConfigurationManager.getInstance()
1798 .getPhoneStatusFromModem(request.phone, onCompleted);
1799 break;
1800 case EVENT_GET_MODEM_STATUS_DONE:
1801 ar = (AsyncResult) msg.obj;
1802 request = (MainThreadRequest) ar.userObj;
1803 int id = request.phone.getPhoneId();
1804 if (ar.exception == null && ar.result != null) {
1805 request.result = ar.result;
1806 //update the cache as modem status has changed
1807 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1808 (boolean) request.result);
1809 } else {
1810 // Return true if modem status cannot be retrieved. For most cases,
1811 // modem status is on. And for older version modems, GET_MODEM_STATUS
1812 // and disable modem are not supported. Modem is always on.
1813 // TODO: this should be fixed in R to support a third
1814 // status UNKNOWN b/131631629
1815 request.result = true;
1816 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1817 + ar.exception);
1818 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001819 notifyRequester(request);
1820 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001821 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1822 request = (MainThreadRequest) msg.obj;
1823 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1824 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1825 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1826 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1827 break;
1828 }
1829 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1830 ar = (AsyncResult) msg.obj;
1831 request = (MainThreadRequest) ar.userObj;
1832 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1833 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1834 args.second.accept(ar.exception == null);
1835 notifyRequester(request);
1836 break;
1837 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001838 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1839 request = (MainThreadRequest) msg.obj;
1840 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1841 Phone phone = getPhoneFromRequest(request);
1842 if (phone != null) {
1843 phone.getSystemSelectionChannels(onCompleted);
1844 } else {
1845 loge("getSystemSelectionChannels: No phone object");
1846 request.result = new ArrayList<RadioAccessSpecifier>();
1847 notifyRequester(request);
1848 }
1849 break;
1850 }
1851 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1852 ar = (AsyncResult) msg.obj;
1853 request = (MainThreadRequest) ar.userObj;
1854 if (ar.exception == null && ar.result != null) {
1855 request.result = ar.result;
1856 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001857 request.result = new IllegalStateException(
1858 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001859 if (ar.result == null) {
1860 loge("getSystemSelectionChannels: Empty response");
1861 } else {
1862 loge("getSystemSelectionChannels: Unknown exception");
1863 }
1864 }
1865 notifyRequester(request);
1866 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001867 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1868 ar = (AsyncResult) msg.obj;
1869 request = (MainThreadRequest) ar.userObj;
1870 if (ar.exception == null && ar.result != null) {
1871 request.result = ar.result;
1872 } else {
1873 request.result = -1;
1874 loge("Failed to set Forbidden Plmns");
1875 if (ar.result == null) {
1876 loge("setForbidenPlmns: Empty response");
1877 } else if (ar.exception != null) {
1878 loge("setForbiddenPlmns: Exception: " + ar.exception);
1879 request.result = -1;
1880 } else {
1881 loge("setForbiddenPlmns: Unknown exception");
1882 }
1883 }
1884 notifyRequester(request);
1885 break;
1886 case CMD_SET_FORBIDDEN_PLMNS:
1887 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001888 uiccPort = getUiccPortFromRequest(request);
1889 if (uiccPort == null) {
1890 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001891 request.result = -1;
1892 notifyRequester(request);
1893 break;
1894 }
1895 Pair<Integer, List<String>> setFplmnsArgs =
1896 (Pair<Integer, List<String>>) request.argument;
1897 appType = setFplmnsArgs.first;
1898 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001899 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001900 if (uiccApp == null) {
1901 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1902 request.result = -1;
1903 loge("Failed to get UICC App");
1904 notifyRequester(request);
1905 } else {
1906 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1907 ((SIMRecords) uiccApp.getIccRecords())
1908 .setForbiddenPlmns(onCompleted, fplmns);
1909 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001910 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001911 case CMD_ERASE_MODEM_CONFIG:
1912 request = (MainThreadRequest) msg.obj;
1913 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1914 defaultPhone.eraseModemConfig(onCompleted);
1915 break;
1916 case EVENT_ERASE_MODEM_CONFIG_DONE:
1917 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001918 break;
zoey chene02881a2019-12-30 16:11:23 +08001919
Kai Shif70f46f2021-03-03 13:59:46 -08001920 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1921 request = (MainThreadRequest) msg.obj;
1922 request.result = defaultPhone.eraseDataInSharedPreferences();
1923 notifyRequester(request);
1924 break;
1925
zoey chene02881a2019-12-30 16:11:23 +08001926 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1927 request = (MainThreadRequest) msg.obj;
1928 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1929 Pair<String, String> changed = (Pair<String, String>) request.argument;
1930 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1931 changed.first, changed.second, onCompleted);
1932 break;
1933 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1934 ar = (AsyncResult) msg.obj;
1935 request = (MainThreadRequest) ar.userObj;
1936 if (ar.exception == null) {
1937 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001938 // If the operation is successful, update the PIN storage
1939 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1940 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001941 UiccController.getInstance().getPinStorage()
1942 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001943 } else {
1944 request.result = msg.arg1;
1945 }
1946 notifyRequester(request);
1947 break;
1948
Michele Berionne5e411512020-11-13 02:36:59 +00001949 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001950 request = (MainThreadRequest) msg.obj;
1951 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1952 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1953 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1954 enabled.first, enabled.second, onCompleted);
1955 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001956 }
zoey chene02881a2019-12-30 16:11:23 +08001957 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1958 ar = (AsyncResult) msg.obj;
1959 request = (MainThreadRequest) ar.userObj;
1960 if (ar.exception == null) {
1961 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001962 // If the operation is successful, update the PIN storage
1963 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1964 int phoneId = getPhoneFromRequest(request).getPhoneId();
1965 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001966 UiccController.getInstance().getPinStorage()
1967 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001968 } else {
1969 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1970 }
zoey chene02881a2019-12-30 16:11:23 +08001971 } else {
1972 request.result = msg.arg1;
1973 }
Michele Berionne5e411512020-11-13 02:36:59 +00001974
1975
zoey chene02881a2019-12-30 16:11:23 +08001976 notifyRequester(request);
1977 break;
1978
Peter Wangdafb9ac2020-01-15 14:13:38 -08001979 case MSG_NOTIFY_USER_ACTIVITY:
1980 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001981 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001982 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1983 getDefaultPhone().getContext().sendBroadcastAsUser(
1984 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1985 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001986
1987 case CMD_SET_DATA_THROTTLING: {
1988 request = (MainThreadRequest) msg.obj;
1989 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1990 DataThrottlingRequest dataThrottlingRequest =
1991 (DataThrottlingRequest) request.argument;
1992 Phone phone = getPhoneFromRequest(request);
1993 if (phone != null) {
1994 phone.setDataThrottling(onCompleted,
1995 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
1996 dataThrottlingRequest.getCompletionDurationMillis());
1997 } else {
1998 loge("setDataThrottling: No phone object");
1999 request.result =
2000 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2001 notifyRequester(request);
2002 }
2003
2004 break;
2005 }
2006 case EVENT_SET_DATA_THROTTLING_DONE:
2007 ar = (AsyncResult) msg.obj;
2008 request = (MainThreadRequest) ar.userObj;
2009
2010 if (ar.exception == null) {
2011 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2012 } else if (ar.exception instanceof CommandException) {
2013 loge("setDataThrottling: CommandException: " + ar.exception);
2014 CommandException.Error error =
2015 ((CommandException) (ar.exception)).getCommandError();
2016
2017 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2018 request.result = TelephonyManager
2019 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2020 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2021 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002022 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2023 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002024 } else {
2025 request.result =
2026 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2027 }
2028 } else {
2029 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2030 }
2031 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2032 notifyRequester(request);
2033 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002034
2035 case CMD_SET_SIM_POWER: {
2036 request = (MainThreadRequest) msg.obj;
2037 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2038 request = (MainThreadRequest) msg.obj;
2039 int stateToSet =
2040 ((Pair<Integer, IIntegerConsumer>)
2041 request.argument).first;
2042 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2043 break;
2044 }
2045 case EVENT_SET_SIM_POWER_DONE: {
2046 ar = (AsyncResult) msg.obj;
2047 request = (MainThreadRequest) ar.userObj;
2048 IIntegerConsumer callback =
2049 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2050 if (ar.exception != null) {
2051 loge("setSimPower exception: " + ar.exception);
2052 int errorCode = TelephonyManager.CallForwardingInfoCallback
2053 .RESULT_ERROR_UNKNOWN;
2054 if (ar.exception instanceof CommandException) {
2055 CommandException.Error error =
2056 ((CommandException) (ar.exception)).getCommandError();
2057 if (error == CommandException.Error.SIM_ERR) {
2058 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2059 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2060 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2061 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2062 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2063 } else {
2064 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2065 }
2066 }
2067 try {
2068 callback.accept(errorCode);
2069 } catch (RemoteException e) {
2070 // Ignore if the remote process is no longer available to call back.
2071 Log.w(LOG_TAG, "setSimPower: callback not available.");
2072 }
2073 } else {
2074 try {
2075 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2076 } catch (RemoteException e) {
2077 // Ignore if the remote process is no longer available to call back.
2078 Log.w(LOG_TAG, "setSimPower: callback not available.");
2079 }
2080 }
2081 break;
2082 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002083 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2084 request = (MainThreadRequest) msg.obj;
2085
2086 final Phone phone = getPhoneFromRequest(request);
2087 if (phone == null || phone.getServiceStateTracker() == null) {
2088 request.result = new IllegalStateException("Phone or SST is null");
2089 notifyRequester(request);
2090 break;
2091 }
2092
2093 Pair<Integer, SignalStrengthUpdateRequest> pair =
2094 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2095 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2096 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002097 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002098 request.subId, pair.first /*callingUid*/,
2099 pair.second /*request*/, onCompleted);
2100 break;
2101 }
2102 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2103 ar = (AsyncResult) msg.obj;
2104 request = (MainThreadRequest) ar.userObj;
2105 // request.result will be the exception of ar if present, true otherwise.
2106 // Be cautious not to leave result null which will wait() forever
2107 request.result = ar.exception != null ? ar.exception : true;
2108 notifyRequester(request);
2109 break;
2110 }
2111 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2112 request = (MainThreadRequest) msg.obj;
2113
2114 Phone phone = getPhoneFromRequest(request);
2115 if (phone == null || phone.getServiceStateTracker() == null) {
2116 request.result = new IllegalStateException("Phone or SST is null");
2117 notifyRequester(request);
2118 break;
2119 }
2120
2121 Pair<Integer, SignalStrengthUpdateRequest> pair =
2122 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2123 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2124 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002125 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002126 request.subId, pair.first /*callingUid*/,
2127 pair.second /*request*/, onCompleted);
2128 break;
2129 }
2130 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2131 ar = (AsyncResult) msg.obj;
2132 request = (MainThreadRequest) ar.userObj;
2133 request.result = ar.exception != null ? ar.exception : true;
2134 notifyRequester(request);
2135 break;
2136 }
Jordan Liu109698e2020-11-24 14:50:34 -08002137
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002138 case CMD_GET_SLICING_CONFIG: {
2139 request = (MainThreadRequest) msg.obj;
2140 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2141 request.phone.getSlicingConfig(onCompleted);
2142 break;
2143 }
2144 case EVENT_GET_SLICING_CONFIG_DONE: {
2145 ar = (AsyncResult) msg.obj;
2146 request = (MainThreadRequest) ar.userObj;
2147 ResultReceiver result = (ResultReceiver) request.argument;
2148
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002149 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002150 Bundle bundle = new Bundle();
2151 int resultCode = 0;
2152 if (ar.exception != null) {
2153 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2154 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002155 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002156 } else if (ar.result == null) {
2157 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002158 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002159 } else {
2160 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002161 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2162 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002163 }
2164
2165 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002166 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002167 }
2168 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2169 result.send(resultCode, bundle);
2170 notifyRequester(request);
2171 break;
2172 }
2173
Sarah Chin71b3a852022-09-28 15:54:19 -07002174 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002175 request = (MainThreadRequest) msg.obj;
2176 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002177 PurchasePremiumCapabilityArgument arg =
2178 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002179 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chin71b3a852022-09-28 15:54:19 -07002180 arg.capability, arg.appName, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002181 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002182 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002183
Sarah Chin71b3a852022-09-28 15:54:19 -07002184 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002185 ar = (AsyncResult) msg.obj;
2186 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002187 PurchasePremiumCapabilityArgument arg =
2188 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002189 try {
2190 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002191 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002192 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002193 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002194 + ", result= "
2195 + TelephonyManager.convertPurchaseResultToString(result));
2196 } catch (RemoteException e) {
2197 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002198 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002199 + " failed: " + e;
2200 if (DBG) log(logStr);
2201 AnomalyReporter.reportAnomaly(
2202 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2203 }
2204 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002205 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002206
Michele Berionne5e411512020-11-13 02:36:59 +00002207 case CMD_PREPARE_UNATTENDED_REBOOT:
2208 request = (MainThreadRequest) msg.obj;
2209 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002210 UiccController.getInstance().getPinStorage()
2211 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002212 notifyRequester(request);
2213 break;
2214
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002215 default:
2216 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2217 break;
2218 }
2219 }
Jake Hambye994d462014-02-03 13:10:13 -08002220
Pengquan Menga1bb6272018-09-06 09:59:22 -07002221 private void notifyRequester(MainThreadRequest request) {
2222 synchronized (request) {
2223 request.notifyAll();
2224 }
2225 }
2226
Jake Hambye994d462014-02-03 13:10:13 -08002227 private void handleNullReturnEvent(Message msg, String command) {
2228 AsyncResult ar = (AsyncResult) msg.obj;
2229 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2230 if (ar.exception == null) {
2231 request.result = true;
2232 } else {
2233 request.result = false;
2234 if (ar.exception instanceof CommandException) {
2235 loge(command + ": CommandException: " + ar.exception);
2236 } else {
2237 loge(command + ": Unknown exception");
2238 }
2239 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002240 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002241 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002242 }
2243
2244 /**
2245 * Posts the specified command to be executed on the main thread,
2246 * waits for the request to complete, and returns the result.
2247 * @see #sendRequestAsync
2248 */
2249 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002250 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2251 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002252 }
2253
2254 /**
2255 * Posts the specified command to be executed on the main thread,
2256 * waits for the request to complete, and returns the result.
2257 * @see #sendRequestAsync
2258 */
2259 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2260 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002261 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002262 }
2263
2264 /**
2265 * Posts the specified command to be executed on the main thread,
2266 * waits for the request to complete, and returns the result.
2267 * @see #sendRequestAsync
2268 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002269 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002270 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2271 }
2272
2273 /**
2274 * Posts the specified command to be executed on the main thread,
2275 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2276 * if not timeout or null otherwise.
2277 * @see #sendRequestAsync
2278 */
2279 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2280 long timeoutInMs) {
2281 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002282 }
2283
2284 /**
2285 * Posts the specified command to be executed on the main thread,
2286 * waits for the request to complete, and returns the result.
2287 * @see #sendRequestAsync
2288 */
Nathan Harold92bed182018-10-12 18:16:49 -07002289 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002290 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002291 }
2292
2293 /**
2294 * Posts the specified command to be executed on the main thread,
2295 * waits for the request to complete, and returns the result.
2296 * @see #sendRequestAsync
2297 */
2298 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002299 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2300 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002301 }
2302
2303 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002304 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2305 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2306 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002307 * @see #sendRequestAsync
2308 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002309 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2310 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002311 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2312 throw new RuntimeException("This method will deadlock if called from the main thread.");
2313 }
2314
Nathan Harold92bed182018-10-12 18:16:49 -07002315 MainThreadRequest request = null;
2316 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2317 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2318 } else if (phone != null) {
2319 request = new MainThreadRequest(argument, phone, workSource);
2320 } else {
2321 request = new MainThreadRequest(argument, subId, workSource);
2322 }
2323
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002324 Message msg = mMainThreadHandler.obtainMessage(command, request);
2325 msg.sendToTarget();
2326
Rambo Wang0f050d82021-02-12 11:43:36 -08002327
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002328 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002329 if (timeoutInMs >= 0) {
2330 // Wait for at least timeoutInMs before returning null request result
2331 long now = SystemClock.elapsedRealtime();
2332 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002333 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002334 try {
2335 request.wait(deadline - now);
2336 } catch (InterruptedException e) {
2337 // Do nothing, go back and check if request is completed or timeout
2338 } finally {
2339 now = SystemClock.elapsedRealtime();
2340 }
2341 }
2342 } else {
2343 // Wait for the request to complete
2344 while (request.result == null) {
2345 try {
2346 request.wait();
2347 } catch (InterruptedException e) {
2348 // Do nothing, go back and wait until the request is complete
2349 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002350 }
2351 }
2352 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002353 if (request.result == null) {
2354 Log.wtf(LOG_TAG,
2355 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2356 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002357 return request.result;
2358 }
2359
2360 /**
2361 * Asynchronous ("fire and forget") version of sendRequest():
2362 * Posts the specified command to be executed on the main thread, and
2363 * returns immediately.
2364 * @see #sendRequest
2365 */
2366 private void sendRequestAsync(int command) {
2367 mMainThreadHandler.sendEmptyMessage(command);
2368 }
2369
2370 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002371 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002372 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002373 */
2374 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002375 sendRequestAsync(command, argument, null, null);
2376 }
2377
2378 /**
2379 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2380 * @see {@link #sendRequest(int,Object)}
2381 */
2382 private void sendRequestAsync(
2383 int command, Object argument, Phone phone, WorkSource workSource) {
2384 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002385 Message msg = mMainThreadHandler.obtainMessage(command, request);
2386 msg.sendToTarget();
2387 }
2388
2389 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002390 * Initialize the singleton PhoneInterfaceManager instance.
2391 * This is only done once, at startup, from PhoneApp.onCreate().
2392 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002393 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002394 synchronized (PhoneInterfaceManager.class) {
2395 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002396 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002397 } else {
2398 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2399 }
2400 return sInstance;
2401 }
2402 }
2403
2404 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002405 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002406 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002407 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002408 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002409 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002410 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002411 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002412 mMainThreadHandler = new MainThreadHandler();
Aishwarya Mallampati32336e82023-01-04 23:02:56 +00002413 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002414 mTelephonySharedPreferences =
2415 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002416 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002417 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002418 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002419 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002420 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002421 mTelephony2gUpdater = new Telephony2gUpdater(
2422 Executors.newSingleThreadExecutor(), mApp);
2423 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002424 publish();
2425 }
2426
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002427 @VisibleForTesting
2428 public SharedPreferences getSharedPreferences() {
2429 return mTelephonySharedPreferences;
2430 }
2431
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002432 /**
2433 * Get the default phone for this device.
2434 */
2435 @VisibleForTesting
2436 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002437 Phone thePhone = getPhone(getDefaultSubscription());
2438 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2439 }
2440
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002441 private void publish() {
2442 if (DBG) log("publish: " + this);
2443
Peter Wangc035ce42020-01-08 21:00:22 -08002444 TelephonyFrameworkInitializer
2445 .getTelephonyServiceManager()
2446 .getTelephonyServiceRegisterer()
2447 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002448 }
2449
Stuart Scott584921c2015-01-15 17:10:34 -08002450 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002451 if (request.phone != null) {
2452 return request.phone;
2453 } else {
2454 return getPhoneFromSubId(request.subId);
2455 }
2456 }
2457
2458 private Phone getPhoneFromSubId(int subId) {
2459 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2460 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002461 }
2462
Rambo Wange53e07d2022-05-10 13:01:13 -07002463 @Nullable
2464 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002465 Phone phone = getPhoneFromRequest(request);
2466 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002467 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002468 }
2469
Wink Saville36469e72014-06-11 15:17:00 -07002470 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002471 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002472 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002473 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002474
Kai Shif70f46f2021-03-03 13:59:46 -08002475 private void sendEraseModemConfig(@NonNull Phone phone) {
2476 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2477 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2478 }
2479
2480 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2481 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2482 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002483 }
2484
Peter Wang44b186e2020-01-13 23:33:09 -08002485 private boolean isImsAvailableOnDevice() {
2486 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2487 if (pm == null) {
2488 // For some reason package manger is not available.. This will fail internally anyway,
2489 // so do not throw error and allow.
2490 return true;
2491 }
2492 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2493 }
2494
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002495 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002496 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002497 }
2498
Wink Savilleb564aae2014-10-23 10:18:09 -07002499 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002500 if (DBG) log("dial: " + number);
2501 // No permission check needed here: This is just a wrapper around the
2502 // ACTION_DIAL intent, which is available to any app since it puts up
2503 // the UI before it does anything.
2504
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002505 final long identity = Binder.clearCallingIdentity();
2506 try {
2507 String url = createTelUrl(number);
2508 if (url == null) {
2509 return;
2510 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002511
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002512 // PENDING: should we just silently fail if phone is offhook or ringing?
2513 PhoneConstants.State state = mCM.getState(subId);
2514 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2515 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2516 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2517 mApp.startActivity(intent);
2518 }
2519 } finally {
2520 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002521 }
2522 }
2523
2524 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002525 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002526 }
2527
Wink Savilleb564aae2014-10-23 10:18:09 -07002528 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002529 if (DBG) log("call: " + number);
2530
2531 // This is just a wrapper around the ACTION_CALL intent, but we still
2532 // need to do a permission check since we're calling startActivity()
2533 // from the context of the phone app.
2534 enforceCallPermission();
2535
Jordan Liu1617b712019-07-10 15:06:26 -07002536 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002537 != AppOpsManager.MODE_ALLOWED) {
2538 return;
2539 }
2540
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002541 final long identity = Binder.clearCallingIdentity();
2542 try {
2543 String url = createTelUrl(number);
2544 if (url == null) {
2545 return;
2546 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002547
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002548 boolean isValid = false;
2549 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2550 if (slist != null) {
2551 for (SubscriptionInfo subInfoRecord : slist) {
2552 if (subInfoRecord.getSubscriptionId() == subId) {
2553 isValid = true;
2554 break;
2555 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002556 }
Wink Saville08874612014-08-31 19:19:58 -07002557 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002558 if (!isValid) {
2559 return;
2560 }
Wink Saville08874612014-08-31 19:19:58 -07002561
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002562 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2563 intent.putExtra(SUBSCRIPTION_KEY, subId);
2564 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2565 mApp.startActivity(intent);
2566 } finally {
2567 Binder.restoreCallingIdentity(identity);
2568 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002569 }
2570
Wink Savilleb564aae2014-10-23 10:18:09 -07002571 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002572 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002573 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2574 }
2575
Wink Savilleb564aae2014-10-23 10:18:09 -07002576 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002577 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002578 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2579 }
2580
Wink Savilleb564aae2014-10-23 10:18:09 -07002581 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002582 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002583
2584 final long identity = Binder.clearCallingIdentity();
2585 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002586 Phone phone = getPhone(subId);
2587 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002588 checkSimPin.start();
2589 return checkSimPin.unlockSim(null, pin);
2590 } finally {
2591 Binder.restoreCallingIdentity(identity);
2592 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002593 }
2594
Wink Savilleb564aae2014-10-23 10:18:09 -07002595 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002596 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002597
2598 final long identity = Binder.clearCallingIdentity();
2599 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002600 Phone phone = getPhone(subId);
2601 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002602 checkSimPuk.start();
2603 return checkSimPuk.unlockSim(puk, pin);
2604 } finally {
2605 Binder.restoreCallingIdentity(identity);
2606 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002607 }
2608
2609 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002610 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002611 * a synchronous one.
2612 */
2613 private static class UnlockSim extends Thread {
2614
2615 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002616 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002617
2618 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002619 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2620 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002621
2622 // For replies from SimCard interface
2623 private Handler mHandler;
2624
2625 // For async handler to identify request type
2626 private static final int SUPPLY_PIN_COMPLETE = 100;
2627
Michele Berionne5e411512020-11-13 02:36:59 +00002628 UnlockSim(int phoneId, IccCard simCard) {
2629 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002630 mSimCard = simCard;
2631 }
2632
2633 @Override
2634 public void run() {
2635 Looper.prepare();
2636 synchronized (UnlockSim.this) {
2637 mHandler = new Handler() {
2638 @Override
2639 public void handleMessage(Message msg) {
2640 AsyncResult ar = (AsyncResult) msg.obj;
2641 switch (msg.what) {
2642 case SUPPLY_PIN_COMPLETE:
2643 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2644 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002645 mRetryCount = msg.arg1;
2646 if (ar.exception != null) {
2647 if (ar.exception instanceof CommandException &&
2648 ((CommandException)(ar.exception)).getCommandError()
2649 == CommandException.Error.PASSWORD_INCORRECT) {
2650 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002651 } //When UiccCardApp dispose,handle message and return exception
2652 else if (ar.exception instanceof CommandException &&
2653 ((CommandException) (ar.exception)).getCommandError()
2654 == CommandException.Error.ABORTED) {
2655 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002656 } else {
2657 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2658 }
2659 } else {
2660 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2661 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002662 mDone = true;
2663 UnlockSim.this.notifyAll();
2664 }
2665 break;
2666 }
2667 }
2668 };
2669 UnlockSim.this.notifyAll();
2670 }
2671 Looper.loop();
2672 }
2673
2674 /*
2675 * Use PIN or PUK to unlock SIM card
2676 *
2677 * If PUK is null, unlock SIM card with PIN
2678 *
2679 * If PUK is not null, unlock SIM card with PUK and set PIN code
2680 */
Wink Saville9de0f752013-10-22 19:04:03 -07002681 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002682
2683 while (mHandler == null) {
2684 try {
2685 wait();
2686 } catch (InterruptedException e) {
2687 Thread.currentThread().interrupt();
2688 }
2689 }
2690 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2691
2692 if (puk == null) {
2693 mSimCard.supplyPin(pin, callback);
2694 } else {
2695 mSimCard.supplyPuk(puk, pin, callback);
2696 }
2697
2698 while (!mDone) {
2699 try {
2700 Log.d(LOG_TAG, "wait for done");
2701 wait();
2702 } catch (InterruptedException e) {
2703 // Restore the interrupted status
2704 Thread.currentThread().interrupt();
2705 }
2706 }
2707 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002708 int[] resultArray = new int[2];
2709 resultArray[0] = mResult;
2710 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002711
2712 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002713 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002714 }
2715
Wink Saville9de0f752013-10-22 19:04:03 -07002716 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002717 }
2718 }
2719
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002720 /**
2721 * This method has been removed due to privacy and stability concerns.
2722 */
2723 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002724 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002725 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2726 return;
Wink Saville36469e72014-06-11 15:17:00 -07002727 }
2728
Nathan Harold1f889d82020-06-04 17:05:26 -07002729 @Override
2730 public void updateServiceLocationWithPackageName(String callingPackage) {
2731 mApp.getSystemService(AppOpsManager.class)
2732 .checkPackage(Binder.getCallingUid(), callingPackage);
2733
Nathan Haroldf096d982020-11-18 17:18:06 -08002734 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002735 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2736 // Callers targeting S have no business invoking this method.
2737 return;
2738 }
2739
2740 LocationAccessPolicy.LocationPermissionResult locationResult =
2741 LocationAccessPolicy.checkLocationPermission(mApp,
2742 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2743 .setCallingPackage(callingPackage)
2744 .setCallingFeatureId(null)
2745 .setCallingPid(Binder.getCallingPid())
2746 .setCallingUid(Binder.getCallingUid())
2747 .setMethod("updateServiceLocation")
2748 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2749 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2750 .build());
2751 // Apps that lack location permission have no business calling this method;
2752 // however, because no permission was declared in the public API, denials must
2753 // all be "soft".
2754 switch (locationResult) {
2755 case DENIED_HARD: /* fall through */
2756 case DENIED_SOFT:
2757 return;
2758 }
2759
2760 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002761 final long identity = Binder.clearCallingIdentity();
2762 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002763 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002764 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002765 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002766 }
2767 } finally {
2768 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002769 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002770 }
2771
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002772 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002773 @Override
2774 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002775 return isRadioOnWithFeature(callingPackage, null);
2776 }
2777
2778
2779 @Override
2780 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2781 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2782 callingFeatureId);
2783 }
2784
2785 @Deprecated
2786 @Override
2787 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2788 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002789 }
2790
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002791 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002792 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2793 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002794 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002795 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002796 return false;
2797 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002798
2799 final long identity = Binder.clearCallingIdentity();
2800 try {
2801 return isRadioOnForSubscriber(subId);
2802 } finally {
2803 Binder.restoreCallingIdentity(identity);
2804 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002805 }
2806
2807 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002808 final long identity = Binder.clearCallingIdentity();
2809 try {
2810 final Phone phone = getPhone(subId);
2811 if (phone != null) {
2812 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2813 } else {
2814 return false;
2815 }
2816 } finally {
2817 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002818 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002819 }
2820
2821 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002822 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002823 }
Wink Saville36469e72014-06-11 15:17:00 -07002824
Wink Savilleb564aae2014-10-23 10:18:09 -07002825 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002826 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002827
2828 final long identity = Binder.clearCallingIdentity();
2829 try {
2830 final Phone phone = getPhone(subId);
2831 if (phone != null) {
2832 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2833 }
2834 } finally {
2835 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002836 }
Wink Saville36469e72014-06-11 15:17:00 -07002837 }
2838
2839 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002840 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002841 }
2842
Wink Savilleb564aae2014-10-23 10:18:09 -07002843 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002844 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002845
2846 final long identity = Binder.clearCallingIdentity();
2847 try {
2848 final Phone phone = getPhone(subId);
2849 if (phone == null) {
2850 return false;
2851 }
2852 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2853 toggleRadioOnOffForSubscriber(subId);
2854 }
2855 return true;
2856 } finally {
2857 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002858 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002859 }
Wink Saville36469e72014-06-11 15:17:00 -07002860
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002861 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002862 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002863 /*
2864 * If any of the Radios are available, it will need to be
2865 * shutdown. So return true if any Radio is available.
2866 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002867 final long identity = Binder.clearCallingIdentity();
2868 try {
2869 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2870 Phone phone = PhoneFactory.getPhone(i);
2871 if (phone != null && phone.isRadioAvailable()) return true;
2872 }
2873 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2874 return false;
2875 } finally {
2876 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002877 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002878 }
2879
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002880 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002881 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002882 enforceModifyPermission();
2883
2884 final long identity = Binder.clearCallingIdentity();
2885 try {
2886 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2887 logv("Shutting down Phone " + i);
2888 shutdownRadioUsingPhoneId(i);
2889 }
2890 } finally {
2891 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002892 }
2893 }
2894
2895 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002896 Phone phone = PhoneFactory.getPhone(phoneId);
2897 if (phone != null && phone.isRadioAvailable()) {
2898 phone.shutdownRadio();
2899 }
2900 }
2901
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002902 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002903 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002904
Ling Ma83dc5ea2023-01-12 15:06:04 -08002905 if (!turnOn) {
2906 log("setRadioPower off: callingPackage=" + getCurrentPackageName());
2907 }
2908
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002909 final long identity = Binder.clearCallingIdentity();
2910 try {
2911 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2912 if (defaultPhone != null) {
2913 defaultPhone.setRadioPower(turnOn);
2914 return true;
2915 } else {
2916 loge("There's no default phone.");
2917 return false;
2918 }
2919 } finally {
2920 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002921 }
Wink Saville36469e72014-06-11 15:17:00 -07002922 }
2923
Wink Savilleb564aae2014-10-23 10:18:09 -07002924 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002925 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002926
Ling Ma83dc5ea2023-01-12 15:06:04 -08002927 if (!turnOn) {
2928 log("setRadioPowerForSubscriber off: subId=" + subId
2929 + ",callingPackage=" + getCurrentPackageName());
2930 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002931 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
Ling Ma83dc5ea2023-01-12 15:06:04 -08002965 log("requestRadioPowerOffForReason: subId=" + subId
2966 + ",reason=" + reason + ",callingPackage=" + getCurrentPackageName());
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002967 final long identity = Binder.clearCallingIdentity();
2968 try {
2969 final Phone phone = getPhone(subId);
2970 if (phone != null) {
2971 phone.setRadioPowerForReason(false, reason);
2972 return true;
2973 } else {
2974 return false;
2975 }
2976 } finally {
2977 Binder.restoreCallingIdentity(identity);
2978 }
2979 }
2980
2981 /**
2982 * Remove the vote on powering off the radio for a reason, as requested by
2983 * {@link requestRadioPowerOffForReason}.
2984 *
2985 * @param subId The subscription ID.
2986 * @param reason The reason for powering off radio.
2987 * @return true on success and false on failure.
2988 */
2989 public boolean clearRadioPowerOffForReason(int subId,
2990 @TelephonyManager.RadioPowerReason int reason) {
2991 enforceModifyPermission();
2992
2993 final long identity = Binder.clearCallingIdentity();
2994 try {
2995 final Phone phone = getPhone(subId);
2996 if (phone != null) {
2997 phone.setRadioPowerForReason(true, reason);
2998 return true;
2999 } else {
3000 return false;
3001 }
3002 } finally {
3003 Binder.restoreCallingIdentity(identity);
3004 }
3005 }
3006
3007 /**
3008 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3009 *
3010 * @param subId The subscription ID.
3011 * @param callingPackage The package making the call.
3012 * @param callingFeatureId The feature in the package.
3013 * @return List of reasons for powering off radio.
3014 */
3015 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3016 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3017
3018 final long identity = Binder.clearCallingIdentity();
3019 List result = new ArrayList();
3020 try {
3021 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3022 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3023 return result;
3024 }
3025
3026 final Phone phone = getPhone(subId);
3027 if (phone != null) {
3028 result.addAll(phone.getRadioPowerOffReasons());
3029 }
3030 } finally {
3031 Binder.restoreCallingIdentity(identity);
3032 }
3033 return result;
3034 }
3035
Wink Saville36469e72014-06-11 15:17:00 -07003036 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003037 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003038 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003039 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003040
3041 final long identity = Binder.clearCallingIdentity();
3042 try {
Jack Yu285100e2022-12-02 22:48:35 -08003043 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003044 final Phone phone = getPhone(subId);
3045 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003046 phone.getDataSettingsManager().setDataEnabled(
3047 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003048 return true;
3049 } else {
3050 return false;
3051 }
3052 } finally {
3053 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003054 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003055 }
3056
Wink Saville36469e72014-06-11 15:17:00 -07003057 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003058 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003059 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003060 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003061
3062 final long identity = Binder.clearCallingIdentity();
3063 try {
Jack Yu285100e2022-12-02 22:48:35 -08003064 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003065 final Phone phone = getPhone(subId);
3066 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003067 phone.getDataSettingsManager().setDataEnabled(
3068 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003069 return true;
3070 } else {
3071 return false;
3072 }
3073 } finally {
3074 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003075 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003076 }
3077
Sanket Padawe356d7632015-06-22 14:03:32 -07003078 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003079 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003080 final long identity = Binder.clearCallingIdentity();
3081 try {
3082 final Phone phone = getPhone(subId);
3083 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003084 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003085 } else {
3086 return false;
3087 }
3088 } finally {
3089 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003090 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003091 }
3092
3093 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003094 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003095 }
3096
pkanwarae03a6b2016-11-06 20:37:09 -08003097 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003098 enforceCallPermission();
3099
3100 final long identity = Binder.clearCallingIdentity();
3101 try {
3102 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3103 return;
3104 }
3105 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3106 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3107 } finally {
3108 Binder.restoreCallingIdentity(identity);
3109 }
pkanwar32d516d2016-10-14 19:37:38 -07003110 };
3111
Wink Savilleb564aae2014-10-23 10:18:09 -07003112 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003113 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003114
3115 final long identity = Binder.clearCallingIdentity();
3116 try {
3117 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3118 return false;
3119 }
3120 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3121 } finally {
3122 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003123 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003124 }
3125
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003126 /**
3127 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3128 * tag on getCallState Binder call.
3129 */
3130 @Deprecated
3131 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003132 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003133 if (CompatChanges.isChangeEnabled(
3134 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3135 Binder.getCallingUid())) {
3136 // Do not allow this API to be called on API version 31+, it should only be
3137 // called on old apps using this Binder call directly.
3138 throw new SecurityException("This method can only be used for applications "
3139 + "targeting API version 30 or less.");
3140 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003141 final long identity = Binder.clearCallingIdentity();
3142 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003143 Phone phone = getPhone(getDefaultSubscription());
3144 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3145 PhoneConstantConversions.convertCallState(phone.getState());
3146 } finally {
3147 Binder.restoreCallingIdentity(identity);
3148 }
3149 }
3150
3151 @Override
3152 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3153 if (CompatChanges.isChangeEnabled(
3154 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3155 Binder.getCallingUid())) {
3156 // Check READ_PHONE_STATE for API version 31+
3157 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3158 featureId, "getCallStateForSubscription")) {
3159 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3160 + "targeting API level 31+.");
3161 }
3162 }
3163 final long identity = Binder.clearCallingIdentity();
3164 try {
3165 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003166 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3167 PhoneConstantConversions.convertCallState(phone.getState());
3168 } finally {
3169 Binder.restoreCallingIdentity(identity);
3170 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003171 }
3172
Sanket Padawe356d7632015-06-22 14:03:32 -07003173 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003174 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003175 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003176 }
3177
3178 @Override
3179 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003180 final long identity = Binder.clearCallingIdentity();
3181 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003182 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003183 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003184 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003185 } else {
3186 return PhoneConstantConversions.convertDataState(
3187 PhoneConstants.DataState.DISCONNECTED);
3188 }
3189 } finally {
3190 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003191 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003192 }
3193
Sanket Padawe356d7632015-06-22 14:03:32 -07003194 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003195 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003196 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003197 }
3198
3199 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003200 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003201 final long identity = Binder.clearCallingIdentity();
3202 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003203 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003204 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003205 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003206 } else {
3207 return TelephonyManager.DATA_ACTIVITY_NONE;
3208 }
3209 } finally {
3210 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003211 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003212 }
3213
3214 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003215 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003216 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003217 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003218
3219 LocationAccessPolicy.LocationPermissionResult locationResult =
3220 LocationAccessPolicy.checkLocationPermission(mApp,
3221 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3222 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003223 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003224 .setCallingPid(Binder.getCallingPid())
3225 .setCallingUid(Binder.getCallingUid())
3226 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003227 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003228 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3229 .build());
3230 switch (locationResult) {
3231 case DENIED_HARD:
3232 throw new SecurityException("Not allowed to access cell location");
3233 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003234 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3235 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003236 }
3237
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003238 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003239 final long identity = Binder.clearCallingIdentity();
3240 try {
3241 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003242 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003243 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003244 } finally {
3245 Binder.restoreCallingIdentity(identity);
3246 }
Svetoslav64fad262015-04-14 14:35:21 -07003247 }
3248
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003249 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003250 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003251 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3252 // registered cell info, so return a NULL country instead.
3253 final long identity = Binder.clearCallingIdentity();
3254 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003255 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3256 // Get default phone in this case.
3257 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3258 }
Jack Yu285100e2022-12-02 22:48:35 -08003259 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003260 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003261 if (phone == null) return "";
3262 ServiceStateTracker sst = phone.getServiceStateTracker();
3263 if (sst == null) return "";
3264 LocaleTracker lt = sst.getLocaleTracker();
3265 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003266 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003267 } finally {
3268 Binder.restoreCallingIdentity(identity);
3269 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003270 }
3271
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003272 /**
3273 * This method was removed due to potential issues caused by performing partial
3274 * updates of service state, and lack of a credible use case.
3275 *
3276 * This has the ability to break the telephony implementation by disabling notification of
3277 * changes in device connectivity. DO NOT USE THIS!
3278 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003279 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003280 public void enableLocationUpdates() {
3281 mApp.enforceCallingOrSelfPermission(
3282 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003283 }
3284
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003285 /**
3286 * This method was removed due to potential issues caused by performing partial
3287 * updates of service state, and lack of a credible use case.
3288 *
3289 * This has the ability to break the telephony implementation by disabling notification of
3290 * changes in device connectivity. DO NOT USE THIS!
3291 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003292 @Override
3293 public void disableLocationUpdates() {
3294 mApp.enforceCallingOrSelfPermission(
3295 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003296 }
3297
3298 @Override
3299 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003300 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3301 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003302 try {
3303 mApp.getSystemService(AppOpsManager.class)
3304 .checkPackage(Binder.getCallingUid(), callingPackage);
3305 } catch (SecurityException e) {
3306 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3307 throw e;
3308 }
3309
Nathan Haroldf096d982020-11-18 17:18:06 -08003310 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003311 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3312 throw new SecurityException(
3313 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3314 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003315
Jordan Liu1617b712019-07-10 15:06:26 -07003316 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003317 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3318 return null;
3319 }
Svetoslav64fad262015-04-14 14:35:21 -07003320
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003321 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003322
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003323 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003324 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003325
Nathan Haroldf180aac2018-06-01 18:43:55 -07003326 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3327 for (CellInfo ci : info) {
3328 if (ci instanceof CellInfoGsm) {
3329 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3330 } else if (ci instanceof CellInfoWcdma) {
3331 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3332 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003333 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003334 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003335 }
3336
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003337 private List<CellInfo> getCachedCellInfo() {
3338 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3339 for (Phone phone : PhoneFactory.getPhones()) {
3340 List<CellInfo> info = phone.getAllCellInfo();
3341 if (info != null) cellInfos.addAll(info);
3342 }
3343 return cellInfos;
3344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003345
3346 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003347 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003348 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003349 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003350
3351 LocationAccessPolicy.LocationPermissionResult locationResult =
3352 LocationAccessPolicy.checkLocationPermission(mApp,
3353 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3354 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003355 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003356 .setCallingPid(Binder.getCallingPid())
3357 .setCallingUid(Binder.getCallingUid())
3358 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003359 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003360 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3361 .build());
3362 switch (locationResult) {
3363 case DENIED_HARD:
3364 throw new SecurityException("Not allowed to access cell info");
3365 case DENIED_SOFT:
3366 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003367 }
3368
Nathan Haroldf096d982020-11-18 17:18:06 -08003369 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003370 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3371 return getCachedCellInfo();
3372 }
3373
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003374 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003375 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003376 final long identity = Binder.clearCallingIdentity();
3377 try {
3378 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3379 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003380 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003381 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003382 if (info != null) cellInfos.addAll(info);
3383 }
3384 return cellInfos;
3385 } finally {
3386 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003387 }
3388 }
3389
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003390 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003391 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3392 String callingFeatureId) {
3393 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3394 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003395 }
3396
3397 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003398 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3399 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003400 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003401 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003402 }
3403
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003404 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3405 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003406 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003407 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003408
3409 LocationAccessPolicy.LocationPermissionResult locationResult =
3410 LocationAccessPolicy.checkLocationPermission(mApp,
3411 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3412 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003413 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003414 .setCallingPid(Binder.getCallingPid())
3415 .setCallingUid(Binder.getCallingUid())
3416 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003417 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3418 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003419 .build());
3420 switch (locationResult) {
3421 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003422 if (TelephonyPermissions
3423 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003424 // Safetynet logging for b/154934934
3425 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3426 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003427 throw new SecurityException("Not allowed to access cell info");
3428 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003429 if (TelephonyPermissions
3430 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003431 // Safetynet logging for b/154934934
3432 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3433 }
Nathan Harold5320c422019-05-09 10:26:08 -07003434 try {
3435 cb.onCellInfo(new ArrayList<CellInfo>());
3436 } catch (RemoteException re) {
3437 // Drop without consequences
3438 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003439 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003440 }
3441
Nathan Harolda939a962019-05-09 10:13:47 -07003442
3443 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003444 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3445
3446 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3447 }
3448
3449 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003450 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003451 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003452 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003453
3454 final long identity = Binder.clearCallingIdentity();
3455 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003456 Phone phone = getPhone(subId);
3457 if (phone == null) {
3458 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3459 } else {
3460 phone.setCellInfoListRate(rateInMillis, workSource);
3461 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003462 } finally {
3463 Binder.restoreCallingIdentity(identity);
3464 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003465 }
3466
Shishir Agrawala9f32182016-04-12 12:00:16 -07003467 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003468 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003469 Phone phone = PhoneFactory.getPhone(slotIndex);
3470 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003471 return null;
3472 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003473 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003474 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003475 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003476 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003477 return null;
3478 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003479
3480 final long identity = Binder.clearCallingIdentity();
3481 try {
3482 return phone.getImei();
3483 } finally {
3484 Binder.restoreCallingIdentity(identity);
3485 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003486 }
3487
3488 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003489 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3490 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3491 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3492 callingFeatureId, "getPrimaryImei")) {
3493 throw new SecurityException("Caller does not have permission");
3494 }
3495 final long identity = Binder.clearCallingIdentity();
3496 try {
3497 for (Phone phone : PhoneFactory.getPhones()) {
3498 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3499 return phone.getImei();
3500 }
3501 }
3502 throw new UnsupportedOperationException("Operation not supported");
3503 } finally {
3504 Binder.restoreCallingIdentity(identity);
3505 }
3506 }
3507
3508 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003509 public String getTypeAllocationCodeForSlot(int slotIndex) {
3510 Phone phone = PhoneFactory.getPhone(slotIndex);
3511 String tac = null;
3512 if (phone != null) {
3513 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003514 try {
3515 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3516 } catch (IndexOutOfBoundsException e) {
3517 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3518 return null;
3519 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003520 }
3521 return tac;
3522 }
3523
3524 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003525 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003526 try {
3527 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3528 } catch (SecurityException se) {
3529 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3530 throw new SecurityException("Package " + callingPackage + " does not belong to "
3531 + Binder.getCallingUid());
3532 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003533 Phone phone = PhoneFactory.getPhone(slotIndex);
3534 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003535 return null;
3536 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003537
Jeff Davidson913390f2018-02-23 17:11:49 -08003538 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003539 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003540 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003541 return null;
3542 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003543
3544 final long identity = Binder.clearCallingIdentity();
3545 try {
3546 return phone.getMeid();
3547 } finally {
3548 Binder.restoreCallingIdentity(identity);
3549 }
Jack Yu2af8d712017-03-15 17:14:14 -07003550 }
3551
3552 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003553 public String getManufacturerCodeForSlot(int slotIndex) {
3554 Phone phone = PhoneFactory.getPhone(slotIndex);
3555 String manufacturerCode = null;
3556 if (phone != null) {
3557 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003558 try {
3559 manufacturerCode =
3560 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3561 } catch (IndexOutOfBoundsException e) {
3562 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3563 return null;
3564 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003565 }
3566 return manufacturerCode;
3567 }
3568
3569 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003570 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3571 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003572 Phone phone = PhoneFactory.getPhone(slotIndex);
3573 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003574 return null;
3575 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003576 int subId = phone.getSubId();
3577 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003578 mApp, subId, callingPackage, callingFeatureId,
3579 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003580 return null;
3581 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003582
3583 final long identity = Binder.clearCallingIdentity();
3584 try {
3585 return phone.getDeviceSvn();
3586 } finally {
3587 Binder.restoreCallingIdentity(identity);
3588 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003589 }
3590
fionaxu43304da2017-11-27 22:51:16 -08003591 @Override
3592 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003593 final long identity = Binder.clearCallingIdentity();
3594 try {
3595 final Phone phone = getPhone(subId);
3596 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3597 } finally {
3598 Binder.restoreCallingIdentity(identity);
3599 }
fionaxu43304da2017-11-27 22:51:16 -08003600 }
3601
3602 @Override
3603 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003604 final long identity = Binder.clearCallingIdentity();
3605 try {
3606 final Phone phone = getPhone(subId);
3607 return phone == null ? null : phone.getCarrierName();
3608 } finally {
3609 Binder.restoreCallingIdentity(identity);
3610 }
fionaxu43304da2017-11-27 22:51:16 -08003611 }
3612
calvinpanffe225e2018-11-01 19:43:06 +08003613 @Override
chen xu0026ca62019-03-06 15:28:50 -08003614 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003615 final long identity = Binder.clearCallingIdentity();
3616 try {
3617 final Phone phone = getPhone(subId);
3618 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003619 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003620 } finally {
3621 Binder.restoreCallingIdentity(identity);
3622 }
3623 }
3624
3625 @Override
chen xu0026ca62019-03-06 15:28:50 -08003626 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003627 final long identity = Binder.clearCallingIdentity();
3628 try {
3629 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003630 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003631 } finally {
3632 Binder.restoreCallingIdentity(identity);
3633 }
3634 }
3635
chen xu651eec72018-11-11 19:03:44 -08003636 @Override
chen xu864e11c2018-12-06 22:10:03 -08003637 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3638 if (!isSubscriptionMccMnc) {
3639 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3640 }
chen xu651eec72018-11-11 19:03:44 -08003641 final Phone phone = PhoneFactory.getPhone(slotIndex);
3642 if (phone == null) {
3643 return TelephonyManager.UNKNOWN_CARRIER_ID;
3644 }
3645 final long identity = Binder.clearCallingIdentity();
3646 try {
3647 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3648 } finally {
3649 Binder.restoreCallingIdentity(identity);
3650 }
3651 }
3652
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003653 //
3654 // Internal helper methods.
3655 //
3656
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003657 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003658 * Make sure the caller is the calling package itself
3659 *
3660 * @throws SecurityException if the caller is not the calling package
3661 */
3662 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3663 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003664 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3665 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003666 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003667 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003668 } catch (PackageManager.NameNotFoundException e) {
3669 // packageUid is -1
3670 }
3671 if (packageUid != callingUid) {
3672 throw new SecurityException(message + ": Package " + callingPackage
3673 + " does not belong to " + callingUid);
3674 }
3675 }
3676
3677 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003678 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3679 *
3680 * @throws SecurityException if the caller does not have the required permission
3681 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003682 @VisibleForTesting
3683 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003684 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3685 }
3686
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003687 /**
3688 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3689 *
3690 * @throws SecurityException if the caller does not have the required permission
3691 */
3692 @VisibleForTesting
3693 public void enforceReadPermission() {
3694 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
3695 }
3696
Shuo Qian3b6ee772019-11-13 17:43:31 -08003697 private void enforceActiveEmergencySessionPermission() {
3698 mApp.enforceCallingOrSelfPermission(
3699 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3700 }
3701
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003702 /**
3703 * Make sure the caller has the CALL_PHONE permission.
3704 *
3705 * @throws SecurityException if the caller does not have the required permission
3706 */
3707 private void enforceCallPermission() {
3708 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3709 }
3710
paulhu5a773602019-08-23 19:17:33 +08003711 private void enforceSettingsPermission() {
3712 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003713 }
3714
Michele Berionne5e411512020-11-13 02:36:59 +00003715 private void enforceRebootPermission() {
3716 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3717 }
3718
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003719 private String createTelUrl(String number) {
3720 if (TextUtils.isEmpty(number)) {
3721 return null;
3722 }
3723
Jake Hambye994d462014-02-03 13:10:13 -08003724 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003725 }
3726
Ihab Awadf9e92732013-12-05 18:02:52 -08003727 private static void log(String msg) {
Ling Ma83dc5ea2023-01-12 15:06:04 -08003728 Log.d(LOG_TAG, msg);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003729 }
3730
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003731 private static void logv(String msg) {
3732 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3733 }
3734
Ihab Awadf9e92732013-12-05 18:02:52 -08003735 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003736 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3737 }
3738
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003739 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003740 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003741 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003742 }
3743
Sanket Padawe356d7632015-06-22 14:03:32 -07003744 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003745 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003746 final long identity = Binder.clearCallingIdentity();
3747 try {
3748 final Phone phone = PhoneFactory.getPhone(slotIndex);
3749 if (phone == null) {
3750 return PhoneConstants.PHONE_TYPE_NONE;
3751 } else {
3752 return phone.getPhoneType();
3753 }
3754 } finally {
3755 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003756 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003757 }
3758
3759 /**
3760 * Returns the CDMA ERI icon index to display
3761 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003762 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003763 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3764 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3765 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003766 }
3767
Sanket Padawe356d7632015-06-22 14:03:32 -07003768 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003769 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3770 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003771 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003772 mApp, subId, callingPackage, callingFeatureId,
3773 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003774 return -1;
3775 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003776
3777 final long identity = Binder.clearCallingIdentity();
3778 try {
3779 final Phone phone = getPhone(subId);
3780 if (phone != null) {
3781 return phone.getCdmaEriIconIndex();
3782 } else {
3783 return -1;
3784 }
3785 } finally {
3786 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003787 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003788 }
3789
3790 /**
3791 * Returns the CDMA ERI icon mode,
3792 * 0 - ON
3793 * 1 - FLASHING
3794 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003795 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003796 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3797 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3798 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003799 }
3800
Sanket Padawe356d7632015-06-22 14:03:32 -07003801 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003802 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3803 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003804 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003805 mApp, subId, callingPackage, callingFeatureId,
3806 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003807 return -1;
3808 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003809
3810 final long identity = Binder.clearCallingIdentity();
3811 try {
3812 final Phone phone = getPhone(subId);
3813 if (phone != null) {
3814 return phone.getCdmaEriIconMode();
3815 } else {
3816 return -1;
3817 }
3818 } finally {
3819 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003820 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003821 }
3822
3823 /**
3824 * Returns the CDMA ERI text,
3825 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003826 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003827 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3828 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3829 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003830 }
3831
Sanket Padawe356d7632015-06-22 14:03:32 -07003832 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003833 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3834 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003835 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003836 mApp, subId, callingPackage, callingFeatureId,
3837 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003838 return null;
3839 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003840
3841 final long identity = Binder.clearCallingIdentity();
3842 try {
3843 final Phone phone = getPhone(subId);
3844 if (phone != null) {
3845 return phone.getCdmaEriText();
3846 } else {
3847 return null;
3848 }
3849 } finally {
3850 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003851 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003852 }
3853
3854 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003855 * Returns the CDMA MDN.
3856 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003857 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003858 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003859 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3860 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003861
3862 final long identity = Binder.clearCallingIdentity();
3863 try {
3864 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003865 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003866 return phone.getLine1Number();
3867 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003868 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003869 return null;
3870 }
3871 } finally {
3872 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003873 }
3874 }
3875
3876 /**
3877 * Returns the CDMA MIN.
3878 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003879 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003880 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003881 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3882 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003883
3884 final long identity = Binder.clearCallingIdentity();
3885 try {
3886 final Phone phone = getPhone(subId);
3887 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3888 return phone.getCdmaMin();
3889 } else {
3890 return null;
3891 }
3892 } finally {
3893 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003894 }
3895 }
3896
Hall Liud892bec2018-11-30 14:51:45 -08003897 @Override
3898 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3899 INumberVerificationCallback callback, String callingPackage) {
3900 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3901 != PERMISSION_GRANTED) {
3902 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3903 }
3904 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3905
3906 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3907 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003908 throw new SecurityException("Calling package must be configured in the device config: "
3909 + "calling package: " + callingPackage
3910 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003911 }
3912
3913 if (range == null) {
3914 throw new NullPointerException("Range must be non-null");
3915 }
3916
3917 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003918 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003919
3920 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3921 }
3922
Junda Liuca05d5d2014-08-14 22:36:34 -07003923 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003924 * Returns true if CDMA provisioning needs to run.
3925 */
3926 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003927 final long identity = Binder.clearCallingIdentity();
3928 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003929 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003930 } finally {
3931 Binder.restoreCallingIdentity(identity);
3932 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003933 }
3934
3935 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003936 * Sets the voice mail number of a given subId.
3937 */
3938 @Override
3939 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003940 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3941 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003942
3943 final long identity = Binder.clearCallingIdentity();
3944 try {
3945 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3946 new Pair<String, String>(alphaTag, number), new Integer(subId));
3947 return success;
3948 } finally {
3949 Binder.restoreCallingIdentity(identity);
3950 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003951 }
3952
Ta-wei Yen87c49842016-05-13 21:19:52 -07003953 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003954 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3955 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003956 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3957 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003958 if (!TextUtils.equals(callingPackage, systemDialer)) {
3959 throw new SecurityException("caller must be system dialer");
3960 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003961
3962 final long identity = Binder.clearCallingIdentity();
3963 try {
3964 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3965 if (phoneAccountHandle == null) {
3966 return null;
3967 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003968 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003969 } finally {
3970 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003971 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003972 }
3973
3974 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003975 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3976 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003977 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003978 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003979 mApp, subId, callingPackage, callingFeatureId,
3980 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003981 return null;
3982 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003983
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003984 final long identity = Binder.clearCallingIdentity();
3985 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003986 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003987 } finally {
3988 Binder.restoreCallingIdentity(identity);
3989 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003990 }
3991
3992 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003993 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3994 VisualVoicemailSmsFilterSettings settings) {
3995 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003996
3997 final long identity = Binder.clearCallingIdentity();
3998 try {
3999 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004000 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004001 } finally {
4002 Binder.restoreCallingIdentity(identity);
4003 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004004 }
4005
4006 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004007 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4008 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004009
4010 final long identity = Binder.clearCallingIdentity();
4011 try {
4012 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004013 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004014 } finally {
4015 Binder.restoreCallingIdentity(identity);
4016 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004017 }
4018
4019 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004020 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4021 String callingPackage, int subId) {
4022 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004023
4024 final long identity = Binder.clearCallingIdentity();
4025 try {
4026 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004027 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004028 } finally {
4029 Binder.restoreCallingIdentity(identity);
4030 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004031 }
4032
4033 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004034 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004035 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004036
4037 final long identity = Binder.clearCallingIdentity();
4038 try {
4039 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004040 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004041 } finally {
4042 Binder.restoreCallingIdentity(identity);
4043 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004044 }
4045
4046 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004047 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4048 String callingAttributionTag, int subId, String number, int port, String text,
4049 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004050 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004051 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004052 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004053 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004054 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4055 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004056 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004057
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004058 /**
fionaxu0152e512016-11-14 13:36:14 -08004059 * Sets the voice activation state of a given subId.
4060 */
4061 @Override
4062 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004063 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4064 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004065
4066 final long identity = Binder.clearCallingIdentity();
4067 try {
4068 final Phone phone = getPhone(subId);
4069 if (phone != null) {
4070 phone.setVoiceActivationState(activationState);
4071 } else {
4072 loge("setVoiceActivationState fails with invalid subId: " + subId);
4073 }
4074 } finally {
4075 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004076 }
4077 }
4078
4079 /**
4080 * Sets the data activation state of a given subId.
4081 */
4082 @Override
4083 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004084 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4085 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004086
4087 final long identity = Binder.clearCallingIdentity();
4088 try {
4089 final Phone phone = getPhone(subId);
4090 if (phone != null) {
4091 phone.setDataActivationState(activationState);
4092 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004093 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004094 }
4095 } finally {
4096 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004097 }
4098 }
4099
4100 /**
4101 * Returns the voice activation state of a given subId.
4102 */
4103 @Override
4104 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004105 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004106
fionaxu0152e512016-11-14 13:36:14 -08004107 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004108 final long identity = Binder.clearCallingIdentity();
4109 try {
4110 if (phone != null) {
4111 return phone.getVoiceActivationState();
4112 } else {
4113 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4114 }
4115 } finally {
4116 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004117 }
4118 }
4119
4120 /**
4121 * Returns the data activation state of a given subId.
4122 */
4123 @Override
4124 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004125 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004126
fionaxu0152e512016-11-14 13:36:14 -08004127 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004128 final long identity = Binder.clearCallingIdentity();
4129 try {
4130 if (phone != null) {
4131 return phone.getDataActivationState();
4132 } else {
4133 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4134 }
4135 } finally {
4136 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004137 }
4138 }
4139
4140 /**
Wink Saville36469e72014-06-11 15:17:00 -07004141 * Returns the unread count of voicemails for a subId
4142 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004143 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004144 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4145 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004146 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004147 mApp, subId, callingPackage, callingFeatureId,
4148 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004149 return 0;
4150 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004151 final long identity = Binder.clearCallingIdentity();
4152 try {
4153 final Phone phone = getPhone(subId);
4154 if (phone != null) {
4155 return phone.getVoiceMessageCount();
4156 } else {
4157 return 0;
4158 }
4159 } finally {
4160 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004161 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004162 }
4163
4164 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004165 * returns true, if the device is in a state where both voice and data
4166 * are supported simultaneously. This can change based on location or network condition.
4167 */
4168 @Override
4169 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004170 final long identity = Binder.clearCallingIdentity();
4171 try {
4172 final Phone phone = getPhone(subId);
4173 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4174 } finally {
4175 Binder.restoreCallingIdentity(identity);
4176 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004177 }
4178
4179 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004180 * Send the dialer code if called from the current default dialer or the caller has
4181 * carrier privilege.
4182 * @param inputCode The dialer code to send
4183 */
4184 @Override
4185 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004186 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004187 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004188 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4189 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004190 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004191 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004192 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004193 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004194
4195 final long identity = Binder.clearCallingIdentity();
4196 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004197 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004198 } finally {
4199 Binder.restoreCallingIdentity(identity);
4200 }
fionaxu235cc5e2017-03-06 22:25:57 -08004201 }
4202
Pengquan Menga1bb6272018-09-06 09:59:22 -07004203 @Override
4204 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004205 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004206 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004207 mApp, subId, "getNetworkSelectionMode");
4208 final long identity = Binder.clearCallingIdentity();
4209 try {
4210 if (!isActiveSubscription(subId)) {
4211 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4212 }
4213 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4214 } finally {
4215 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004216 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004217 }
4218
Brad Ebinger35c841c2018-10-01 10:40:55 -07004219 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004220 public boolean isInEmergencySmsMode() {
4221 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4222 final long identity = Binder.clearCallingIdentity();
4223 try {
4224 for (Phone phone : PhoneFactory.getPhones()) {
4225 if (phone.isInEmergencySmsMode()) {
4226 return true;
4227 }
4228 }
4229 } finally {
4230 Binder.restoreCallingIdentity(identity);
4231 }
4232 return false;
4233 }
4234
shilu366312e2019-12-17 09:28:10 -08004235 /**
4236 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4237 * @param subId The subscription to use to check the configuration.
4238 * @param c The callback that will be used to send the result.
4239 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004240 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004241 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4242 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004243 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004244 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004245
4246 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4247 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4248 "IMS not available on device.");
4249 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004250 final long token = Binder.clearCallingIdentity();
4251 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004252 int slotId = getSlotIndexOrException(subId);
4253 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004254
4255 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4256 if (controller != null) {
4257 ImsManager imsManager = controller.getImsManager(subId);
4258 if (imsManager != null) {
4259 imsManager.addRegistrationCallbackForSubscription(c, subId);
4260 } else {
4261 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4262 }
4263 } else {
4264 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4265 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004266 } catch (ImsException e) {
4267 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004268 } finally {
4269 Binder.restoreCallingIdentity(token);
4270 }
4271 }
4272
shilu366312e2019-12-17 09:28:10 -08004273 /**
4274 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4275 * @param subId The subscription to use to check the configuration.
4276 * @param c The callback that will be used to send the result.
4277 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004278 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004279 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004280 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004281 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004282 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4283 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4284 }
Meng Wangafbc5852019-09-19 17:37:13 -07004285 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004286
Meng Wangafbc5852019-09-19 17:37:13 -07004287 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004288 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4289 if (controller != null) {
4290 ImsManager imsManager = controller.getImsManager(subId);
4291 if (imsManager != null) {
4292 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4293 } else {
4294 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4295 + "is inactive, ignoring unregister.");
4296 // If the ImsManager is not valid, just return, since the callback
4297 // will already have been removed internally.
4298 }
4299 }
Meng Wangafbc5852019-09-19 17:37:13 -07004300 } finally {
4301 Binder.restoreCallingIdentity(token);
4302 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004303 }
4304
Brad Ebingera34a6c22019-10-22 17:36:18 -07004305 /**
4306 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4307 */
4308 @Override
4309 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4310 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4311 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4312 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4313 "IMS not available on device.");
4314 }
4315 final long token = Binder.clearCallingIdentity();
4316 try {
4317 Phone phone = getPhone(subId);
4318 if (phone == null) {
4319 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4320 + subId + "'");
4321 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4322 }
4323 phone.getImsRegistrationState(regState -> {
4324 try {
4325 consumer.accept((regState == null)
4326 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4327 } catch (RemoteException e) {
4328 // Ignore if the remote process is no longer available to call back.
4329 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4330 }
4331 });
4332 } finally {
4333 Binder.restoreCallingIdentity(token);
4334 }
4335 }
4336
4337 /**
4338 * Get the transport type for the IMS service registration state.
4339 */
4340 @Override
4341 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004342 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004343 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004344 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4345 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4346 "IMS not available on device.");
4347 }
4348 final long token = Binder.clearCallingIdentity();
4349 try {
4350 Phone phone = getPhone(subId);
4351 if (phone == null) {
4352 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4353 + subId + "'");
4354 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4355 }
4356 phone.getImsRegistrationTech(regTech -> {
4357 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4358 int regTechConverted = (regTech == null)
4359 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4360 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4361 regTechConverted);
4362 try {
4363 consumer.accept(regTechConverted);
4364 } catch (RemoteException e) {
4365 // Ignore if the remote process is no longer available to call back.
4366 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4367 }
4368 });
4369 } finally {
4370 Binder.restoreCallingIdentity(token);
4371 }
4372 }
4373
shilu366312e2019-12-17 09:28:10 -08004374 /**
4375 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4376 * @param subId The subscription to use to check the configuration.
4377 * @param c The callback that will be used to send the result.
4378 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004379 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004380 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4381 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004382 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004383 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004384 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4385 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4386 "IMS not available on device.");
4387 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004388 final long token = Binder.clearCallingIdentity();
4389 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004390 int slotId = getSlotIndexOrException(subId);
4391 verifyImsMmTelConfiguredOrThrow(slotId);
4392 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004393 } catch (ImsException e) {
4394 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004395 } finally {
4396 Binder.restoreCallingIdentity(token);
4397 }
4398 }
4399
shilu366312e2019-12-17 09:28:10 -08004400 /**
4401 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4402 * @param subId The subscription to use to check the configuration.
4403 * @param c The callback that will be used to send the result.
4404 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004405 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004406 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004407 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004408 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004409 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4410 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4411 }
Meng Wangafbc5852019-09-19 17:37:13 -07004412
4413 final long token = Binder.clearCallingIdentity();
4414 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004415 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004416 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004417 } catch (ImsException e) {
4418 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4419 + "is inactive, ignoring unregister.");
4420 // If the subscription is no longer active, just return, since the callback
4421 // will already have been removed internally.
4422 } finally {
4423 Binder.restoreCallingIdentity(token);
4424 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004425 }
4426
4427 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004428 public boolean isCapable(int subId, int capability, int regTech) {
4429 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004430 final long token = Binder.clearCallingIdentity();
4431 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004432 int slotId = getSlotIndexOrException(subId);
4433 verifyImsMmTelConfiguredOrThrow(slotId);
4434 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4435 } catch (com.android.ims.ImsException e) {
4436 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4437 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004438 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004439 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4440 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004441 } finally {
4442 Binder.restoreCallingIdentity(token);
4443 }
4444 }
4445
4446 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004447 public boolean isAvailable(int subId, int capability, int regTech) {
4448 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004449 final long token = Binder.clearCallingIdentity();
4450 try {
4451 Phone phone = getPhone(subId);
4452 if (phone == null) return false;
4453 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004454 } catch (com.android.ims.ImsException e) {
4455 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4456 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004457 } finally {
4458 Binder.restoreCallingIdentity(token);
4459 }
4460 }
4461
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004462 /**
4463 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4464 * subscription.
4465 * @param subId The subscription to use to check the configuration.
4466 * @param callback The callback that will be used to send the result.
4467 * @param capability The MmTelFeature capability that will be used to send the result.
4468 * @param transportType The transport type of the MmTelFeature capability.
4469 */
4470 @Override
4471 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4472 int transportType) {
4473 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004474 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4475 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4476 "IMS not available on device.");
4477 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004478 final long token = Binder.clearCallingIdentity();
4479 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004480 int slotId = getSlotIndex(subId);
4481 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4482 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4483 + subId + "'");
4484 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4485 }
4486 verifyImsMmTelConfiguredOrThrow(slotId);
4487 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4488 transportType, aBoolean -> {
4489 try {
4490 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4491 } catch (RemoteException e) {
4492 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4493 + "running. Ignore");
4494 }
4495 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004496 } catch (ImsException e) {
4497 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004498 } finally {
4499 Binder.restoreCallingIdentity(token);
4500 }
4501 }
4502
shilu366312e2019-12-17 09:28:10 -08004503 /**
4504 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4505 * @param subId The subscription to use to check the configuration.
4506 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004507 @Override
4508 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004509 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004510 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004511
Brad Ebinger35c841c2018-10-01 10:40:55 -07004512 final long token = Binder.clearCallingIdentity();
4513 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004514 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004515 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004516 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004517 } catch (ImsException e) {
4518 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004519 } finally {
4520 Binder.restoreCallingIdentity(token);
4521 }
4522 }
4523
4524 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004525 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004526 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004527 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004528 final long identity = Binder.clearCallingIdentity();
4529 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004530 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004531 // This setting doesn't require an active ImsService connection, so do not verify. The
4532 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004533 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004534 } catch (ImsException e) {
4535 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004536 } finally {
4537 Binder.restoreCallingIdentity(identity);
4538 }
4539 }
4540
shilu366312e2019-12-17 09:28:10 -08004541 /**
4542 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4543 * @param subId The subscription to use to check the configuration.
4544 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004545 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004546 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004547 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004548 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004549 final long identity = Binder.clearCallingIdentity();
4550 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004551 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004552 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004553 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004554 } catch (ImsException e) {
4555 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004556 } finally {
4557 Binder.restoreCallingIdentity(identity);
4558 }
4559 }
4560
4561 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004562 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004563 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004564 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004565 final long identity = Binder.clearCallingIdentity();
4566 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004567 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004568 // This setting doesn't require an active ImsService connection, so do not verify. The
4569 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004570 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004571 } catch (ImsException e) {
4572 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004573 } finally {
4574 Binder.restoreCallingIdentity(identity);
4575 }
4576 }
4577
shilu366312e2019-12-17 09:28:10 -08004578 /**
4579 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4580 * @param subId The subscription to use to check the configuration.
4581 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004582 @Override
4583 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004584 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004585 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004586 final long identity = Binder.clearCallingIdentity();
4587 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004588 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004589 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004590 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004591 } catch (ImsException e) {
4592 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004593 } finally {
4594 Binder.restoreCallingIdentity(identity);
4595 }
4596 }
4597
4598 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004599 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004600 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004601 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004602 final long identity = Binder.clearCallingIdentity();
4603 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004604 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004605 // This setting doesn't require an active ImsService connection, so do not verify. The
4606 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004607 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004608 } catch (ImsException e) {
4609 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004610 } finally {
4611 Binder.restoreCallingIdentity(identity);
4612 }
4613 }
4614
shilu366312e2019-12-17 09:28:10 -08004615 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004616 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4617 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4618 * @param subId The subscription to use to check the configuration.
4619 */
4620 @Override
4621 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004622 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004623 mApp, subId, "isCrossSimCallingEnabledByUser");
4624 final long identity = Binder.clearCallingIdentity();
4625 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004626 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004627 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004628 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004629 } catch (ImsException e) {
4630 throw new ServiceSpecificException(e.getCode());
4631 } finally {
4632 Binder.restoreCallingIdentity(identity);
4633 }
4634 }
4635
4636 /**
4637 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4638 * Requires MODIFY_PHONE_STATE permission.
4639 * @param subId The subscription to use to check the configuration.
4640 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4641 * false otherwise
4642 */
4643 @Override
4644 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4645 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4646 "setCrossSimCallingEnabled");
4647 final long identity = Binder.clearCallingIdentity();
4648 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004649 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004650 // This setting doesn't require an active ImsService connection, so do not verify. The
4651 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004652 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004653 } catch (ImsException e) {
4654 throw new ServiceSpecificException(e.getCode());
4655 } finally {
4656 Binder.restoreCallingIdentity(identity);
4657 }
4658 }
4659
4660 /**
shilu366312e2019-12-17 09:28:10 -08004661 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4662 * @param subId The subscription to use to check the configuration.
4663 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004664 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004665
Brad Ebinger35c841c2018-10-01 10:40:55 -07004666 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004667 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004668 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004669 final long identity = Binder.clearCallingIdentity();
4670 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004671 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004672 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004673 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004674 } catch (ImsException e) {
4675 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004676 } finally {
4677 Binder.restoreCallingIdentity(identity);
4678 }
4679 }
4680
4681 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004682 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004683 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004684 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004685 final long identity = Binder.clearCallingIdentity();
4686 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004687 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004688 // This setting doesn't require an active ImsService connection, so do not verify. The
4689 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004690 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004691 } catch (ImsException e) {
4692 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004693 } finally {
4694 Binder.restoreCallingIdentity(identity);
4695 }
4696 }
4697
4698 @Override
4699 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4700 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4701 "setVoWiFiNonPersistent");
4702 final long identity = Binder.clearCallingIdentity();
4703 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004704 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004705 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004706 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004707 } catch (ImsException e) {
4708 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004709 } finally {
4710 Binder.restoreCallingIdentity(identity);
4711 }
4712 }
4713
shilu366312e2019-12-17 09:28:10 -08004714 /**
4715 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4716 * @param subId The subscription to use to check the configuration.
4717 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004718 @Override
4719 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004720 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004721 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004722 final long identity = Binder.clearCallingIdentity();
4723 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004724 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004725 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004726 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004727 } catch (ImsException e) {
4728 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004729 } finally {
4730 Binder.restoreCallingIdentity(identity);
4731 }
4732 }
4733
4734 @Override
4735 public void setVoWiFiModeSetting(int subId, int mode) {
4736 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4737 "setVoWiFiModeSetting");
4738 final long identity = Binder.clearCallingIdentity();
4739 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004740 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004741 // This setting doesn't require an active ImsService connection, so do not verify. The
4742 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004743 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004744 } catch (ImsException e) {
4745 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004746 } finally {
4747 Binder.restoreCallingIdentity(identity);
4748 }
4749 }
4750
4751 @Override
4752 public int getVoWiFiRoamingModeSetting(int subId) {
4753 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4754 final long identity = Binder.clearCallingIdentity();
4755 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004756 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004757 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004758 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004759 } catch (ImsException e) {
4760 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004761 } finally {
4762 Binder.restoreCallingIdentity(identity);
4763 }
4764 }
4765
4766 @Override
4767 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4768 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4769 "setVoWiFiRoamingModeSetting");
4770 final long identity = Binder.clearCallingIdentity();
4771 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004772 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004773 // This setting doesn't require an active ImsService connection, so do not verify. The
4774 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004775 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004776 } catch (ImsException e) {
4777 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004778 } finally {
4779 Binder.restoreCallingIdentity(identity);
4780 }
4781 }
4782
4783 @Override
4784 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4785 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4786 "setRttCapabilityEnabled");
4787 final long identity = Binder.clearCallingIdentity();
4788 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004789 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004790 // This setting doesn't require an active ImsService connection, so do not verify. The
4791 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004792 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004793 } catch (ImsException e) {
4794 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004795 } finally {
4796 Binder.restoreCallingIdentity(identity);
4797 }
4798 }
4799
shilu366312e2019-12-17 09:28:10 -08004800 /**
4801 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4802 * @param subId The subscription to use to check the configuration.
4803 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004804 @Override
4805 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004806 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004807 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004808 final long identity = Binder.clearCallingIdentity();
4809 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004810 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004811 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004812 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004813 } catch (ImsException e) {
4814 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004815 } finally {
4816 Binder.restoreCallingIdentity(identity);
4817 }
4818 }
4819
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004820 @Override
4821 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4822 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004823
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004824 final long identity = Binder.clearCallingIdentity();
4825 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004826 if (!isImsAvailableOnDevice()) {
4827 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4828 "IMS not available on device.");
4829 }
4830 int slotId = getSlotIndexOrException(subId);
4831 verifyImsMmTelConfiguredOrThrow(slotId);
4832 ImsManager.getInstance(mApp, slotId)
4833 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004834 } catch (ImsException e) {
4835 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004836 } finally {
4837 Binder.restoreCallingIdentity(identity);
4838 }
4839 }
4840
4841 @Override
4842 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4843 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004844
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004845 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004846 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4847 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4848 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004849 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004850 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004851 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004852 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004853 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4854 + "is inactive, ignoring unregister.");
4855 // If the subscription is no longer active, just return, since the callback will already
4856 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004857 } finally {
4858 Binder.restoreCallingIdentity(identity);
4859 }
4860 }
4861
joonhunshincffb7fc2021-11-28 07:32:01 +00004862 @Override
4863 public void registerFeatureProvisioningChangedCallback(int subId,
4864 IFeatureProvisioningCallback callback) {
4865 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4866 mApp, subId, "registerFeatureProvisioningChangedCallback");
4867
4868 final long identity = Binder.clearCallingIdentity();
4869 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4870 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4871 }
4872
joonhunshin91bc1952022-04-29 08:47:15 +00004873 try {
4874 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4875 if (controller == null) {
4876 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4877 "Device does not support IMS");
4878 }
4879 controller.addFeatureProvisioningChangedCallback(subId, callback);
4880 } finally {
4881 Binder.restoreCallingIdentity(identity);
4882 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004883 }
4884
4885 @Override
4886 public void unregisterFeatureProvisioningChangedCallback(int subId,
4887 IFeatureProvisioningCallback callback) {
4888 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4889 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4890
4891 final long identity = Binder.clearCallingIdentity();
4892 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4893 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4894 }
4895
joonhunshin91bc1952022-04-29 08:47:15 +00004896 try {
4897 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4898 if (controller == null) {
4899 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4900 return;
4901 }
4902 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4903 } finally {
4904 Binder.restoreCallingIdentity(identity);
4905 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004906 }
allenwtsu99c623b2020-01-03 18:24:23 +08004907
4908 private void checkModifyPhoneStatePermission(int subId, String message) {
4909 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4910 message);
4911 }
4912
allenwtsu99c623b2020-01-03 18:24:23 +08004913 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004914 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004915 boolean isProvisioned) {
4916 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4917
4918 final long identity = Binder.clearCallingIdentity();
4919 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004920 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4921 if (controller == null) {
4922 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4923 return;
4924 }
4925 controller.setRcsProvisioningStatusForCapability(
4926 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004927 } finally {
4928 Binder.restoreCallingIdentity(identity);
4929 }
allenwtsu99c623b2020-01-03 18:24:23 +08004930 }
4931
4932
4933 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004934 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4935 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4936 mApp, subId, "getRcsProvisioningStatusForCapability");
4937
allenwtsu99c623b2020-01-03 18:24:23 +08004938 final long identity = Binder.clearCallingIdentity();
4939 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004940 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4941 if (controller == null) {
4942 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4943
4944 // device does not support IMS, this method will return true always.
4945 return true;
4946 }
4947 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004948 } finally {
4949 Binder.restoreCallingIdentity(identity);
4950 }
4951 }
4952
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004953 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004954 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4955 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004956 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004957
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004958 final long identity = Binder.clearCallingIdentity();
4959 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004960 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4961 if (controller == null) {
4962 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4963 return;
4964 }
4965 controller.setImsProvisioningStatusForCapability(
4966 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004967 } finally {
4968 Binder.restoreCallingIdentity(identity);
4969 }
4970 }
4971
4972 @Override
4973 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004974 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4975 mApp, subId, "getProvisioningStatusForCapability");
4976
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004977 final long identity = Binder.clearCallingIdentity();
4978 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004979 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4980 if (controller == null) {
4981 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004982
joonhunshin91bc1952022-04-29 08:47:15 +00004983 // device does not support IMS, this method will return true always.
4984 return true;
4985 }
4986 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004987 } finally {
4988 Binder.restoreCallingIdentity(identity);
4989 }
4990 }
4991
4992 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004993 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4994 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4995 mApp, subId, "isProvisioningRequiredForCapability");
4996
4997 final long identity = Binder.clearCallingIdentity();
4998 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004999 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5000 if (controller == null) {
5001 loge("isProvisioningRequiredForCapability: Device does not support IMS");
5002
5003 // device does not support IMS, this method will return false
5004 return false;
5005 }
5006 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005007 } finally {
5008 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005009 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005010 }
5011
5012 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005013 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5014 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5015 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005016
joonhunshincffb7fc2021-11-28 07:32:01 +00005017 final long identity = Binder.clearCallingIdentity();
5018 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005019 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5020 if (controller == null) {
5021 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5022
5023 // device does not support IMS, this method will return false
5024 return false;
5025 }
5026 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005027 } finally {
5028 Binder.restoreCallingIdentity(identity);
5029 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005030 }
5031
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005032 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005033 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005034 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5035 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5036 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005037 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5038 mApp, subId, "getImsProvisioningInt");
5039
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005040 final long identity = Binder.clearCallingIdentity();
5041 try {
5042 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005043 int slotId = getSlotIndex(subId);
5044 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5045 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5046 + subId + "' for key:" + key);
5047 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5048 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005049
joonhunshin91bc1952022-04-29 08:47:15 +00005050 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5051 if (controller == null) {
5052 loge("getImsProvisioningInt: Device does not support IMS");
5053
5054 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5055 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5056 }
5057 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005058 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5059 return retVal;
5060 }
5061
calvinpanb5a34062021-02-08 19:59:36 +08005062 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005063 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005064 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5065 + subId + "' for key:" + key);
5066 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005067 } finally {
5068 Binder.restoreCallingIdentity(identity);
5069 }
5070 }
5071
5072 @Override
5073 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005074 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5075 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5076 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005077 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5078 mApp, subId, "getImsProvisioningString");
5079
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005080 final long identity = Binder.clearCallingIdentity();
5081 try {
5082 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005083 int slotId = getSlotIndex(subId);
5084 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5085 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5086 + subId + "' for key:" + key);
5087 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5088 }
calvinpanb5a34062021-02-08 19:59:36 +08005089 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005090 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005091 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5092 + subId + "' for key:" + key);
5093 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005094 } finally {
5095 Binder.restoreCallingIdentity(identity);
5096 }
5097 }
5098
5099 @Override
5100 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005101 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5102 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5103 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005104 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5105 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005106
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005107 final long identity = Binder.clearCallingIdentity();
5108 try {
5109 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005110 int slotId = getSlotIndex(subId);
5111 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5112 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5113 + subId + "' for key:" + key);
5114 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5115 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005116
joonhunshin91bc1952022-04-29 08:47:15 +00005117 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5118 if (controller == null) {
5119 loge("setImsProvisioningInt: Device does not support IMS");
5120
5121 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5122 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5123 }
5124 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005125 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5126 return retVal;
5127 }
5128
calvinpanb5a34062021-02-08 19:59:36 +08005129 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5130 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005131 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005132 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005133 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005134 } finally {
5135 Binder.restoreCallingIdentity(identity);
5136 }
5137 }
5138
5139 @Override
5140 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005141 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5142 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5143 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005144 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5145 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005146 final long identity = Binder.clearCallingIdentity();
5147 try {
5148 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005149 int slotId = getSlotIndex(subId);
5150 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5151 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5152 + subId + "' for key:" + key);
5153 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5154 }
calvinpanb5a34062021-02-08 19:59:36 +08005155 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5156 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005157 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005158 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005159 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005160 } finally {
5161 Binder.restoreCallingIdentity(identity);
5162 }
5163 }
5164
Brad Ebinger919631e2021-06-02 17:46:35 -07005165 /**
5166 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5167 * for the given slot ID or no ImsResolver instance has been created.
5168 * @param slotId The slot ID that the IMS service is created for.
5169 * @throws ImsException If there is no ImsService configured for this slot.
5170 */
5171 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5172 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5173 ImsFeature.FEATURE_MMTEL)) {
5174 throw new ImsException("This subscription does not support MMTEL over IMS",
5175 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5176 }
5177 }
5178
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005179 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005180 int slotId = SubscriptionManager.getSlotIndex(subId);
5181 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005182 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5183 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005184 }
5185 return slotId;
5186 }
5187
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005188 private int getSlotIndex(int subId) {
5189 int slotId = SubscriptionManager.getSlotIndex(subId);
5190 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5191 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5192 }
5193 return slotId;
5194 }
5195
Wink Saville36469e72014-06-11 15:17:00 -07005196 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005197 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005198 */
5199 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005200 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5201 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005202 try {
5203 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5204 } catch (SecurityException se) {
5205 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5206 throw new SecurityException("Package " + callingPackage + " does not belong to "
5207 + Binder.getCallingUid());
5208 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005209 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005210 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005211 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005212 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005213 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005214 mApp, subId, callingPackage, callingFeatureId,
5215 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005216 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5217 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005218
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005219 final long identity = Binder.clearCallingIdentity();
5220 try {
5221 final Phone phone = getPhone(subId);
5222 if (phone != null) {
5223 return phone.getServiceState().getDataNetworkType();
5224 } else {
5225 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5226 }
5227 } finally {
5228 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005229 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005230 }
5231
5232 /**
5233 * Returns the data network type
5234 */
5235 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005236 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005237 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005238 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005239 }
5240
5241 /**
5242 * Returns the data network type for a subId
5243 */
5244 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005245 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5246 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005247 String functionName = "getDataNetworkTypeForSubscriber";
5248 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5249 mApp, functionName)) {
5250 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5251 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5252 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5253 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005254 }
5255
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005256 final long identity = Binder.clearCallingIdentity();
5257 try {
5258 final Phone phone = getPhone(subId);
5259 if (phone != null) {
5260 return phone.getServiceState().getDataNetworkType();
5261 } else {
5262 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5263 }
5264 } finally {
5265 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005266 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005267 }
5268
5269 /**
Wink Saville36469e72014-06-11 15:17:00 -07005270 * Returns the Voice network type for a subId
5271 */
5272 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005273 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5274 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005275 String functionName = "getVoiceNetworkTypeForSubscriber";
5276 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5277 mApp, functionName)) {
5278 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5279 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5280 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5281 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005282 }
5283
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005284 final long identity = Binder.clearCallingIdentity();
5285 try {
5286 final Phone phone = getPhone(subId);
5287 if (phone != null) {
5288 return phone.getServiceState().getVoiceNetworkType();
5289 } else {
5290 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5291 }
5292 } finally {
5293 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005294 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005295 }
5296
5297 /**
5298 * @return true if a ICC card is present
5299 */
5300 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005301 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005302 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005303 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005304 }
5305
5306 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005307 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005308 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005309 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005310 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005311 final long identity = Binder.clearCallingIdentity();
5312 try {
5313 final Phone phone = PhoneFactory.getPhone(slotIndex);
5314 if (phone != null) {
5315 return phone.getIccCard().hasIccCard();
5316 } else {
5317 return false;
5318 }
5319 } finally {
5320 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005321 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005322 }
5323
5324 /**
5325 * Return if the current radio is LTE on CDMA. This
5326 * is a tri-state return value as for a period of time
5327 * the mode may be unknown.
5328 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005329 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005330 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005331 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005332 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005333 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005334 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5335 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5336 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005337 }
5338
Sanket Padawe356d7632015-06-22 14:03:32 -07005339 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005340 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5341 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005342 try {
5343 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5344 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005345 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5346 }
5347
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005348 final long identity = Binder.clearCallingIdentity();
5349 try {
5350 final Phone phone = getPhone(subId);
5351 if (phone == null) {
5352 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5353 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005354 return TelephonyProperties.lte_on_cdma_device()
5355 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005356 }
5357 } finally {
5358 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005359 }
Wink Saville36469e72014-06-11 15:17:00 -07005360 }
5361
Wink Saville36469e72014-06-11 15:17:00 -07005362 /**
5363 * {@hide}
5364 * Returns Default subId, 0 in the case of single standby.
5365 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005366 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005367 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005368 }
5369
Shishir Agrawala9f32182016-04-12 12:00:16 -07005370 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005371 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005372 }
5373
Wink Savilleb564aae2014-10-23 10:18:09 -07005374 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005375 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005376 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005377
Pengquan Menge92a50d2018-09-21 15:54:48 -07005378 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005379 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5380 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5381 mApp.getOpPackageName(), mApp.getFeatureId());
5382 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005383 return mSubscriptionController.isActiveSubId(subId);
5384 }
5385
Ihab Awadf2177b72013-11-25 13:33:23 -08005386 /**
5387 * @see android.telephony.TelephonyManager.WifiCallingChoices
5388 */
5389 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005390 final long identity = Binder.clearCallingIdentity();
5391 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005392 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005393 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5394 getWhenToMakeWifiCallsDefaultPreference());
5395 } finally {
5396 Binder.restoreCallingIdentity(identity);
5397 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005398 }
5399
5400 /**
5401 * @see android.telephony.TelephonyManager.WifiCallingChoices
5402 */
5403 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005404 final long identity = Binder.clearCallingIdentity();
5405 try {
5406 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005407 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005408 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5409 } finally {
5410 Binder.restoreCallingIdentity(identity);
5411 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005412 }
5413
Sailesh Nepald1e68152013-12-12 19:08:02 -08005414 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005415 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005416 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005417 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005418
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005419 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5420 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5421 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005422 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005423 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5424 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005425 }
5426 return PhoneFactory.getPhone(phoneId);
5427 }
5428
Shishir Agrawal566b7612013-10-28 14:41:00 -07005429 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005430 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005431 @NonNull IccLogicalChannelRequest request) {
5432 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5433 /*message=*/ "iccOpenLogicalChannel");
5434
5435 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5436 // Verify that the callingPackage in the request belongs to the calling UID
5437 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5438
5439 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005440 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005441
Rambo Wanga1782702021-11-10 20:15:19 -08005442 private Phone getPhoneFromValidIccLogicalChannelRequest(
5443 @NonNull IccLogicalChannelRequest request, String message) {
5444 Phone phone;
5445 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5446 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5447 mApp, request.subId, message);
5448 phone = getPhoneFromSubId(request.subId);
5449 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5450 enforceModifyPermission();
5451 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5452 } else {
5453 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005454 }
Rambo Wanga1782702021-11-10 20:15:19 -08005455 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005456 }
5457
5458 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005459 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005460 final long identity = Binder.clearCallingIdentity();
5461 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005462 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005463 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005464 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5465 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005466 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5467 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005468 loge("The calling package is not allowed to access ISD-R.");
5469 throw new SecurityException(
5470 "The calling package is not allowed to access ISD-R.");
5471 }
Derek Tan740e1672017-06-27 14:56:27 -07005472 }
Derek Tan740e1672017-06-27 14:56:27 -07005473
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005474 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005475 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5476 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005477 return response;
5478 } finally {
5479 Binder.restoreCallingIdentity(identity);
5480 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005481 }
5482
5483 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005484 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5485 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5486 /*message=*/"iccCloseLogicalChannel");
5487
5488 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5489
5490 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005491 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005492
Rambo Wanga1782702021-11-10 20:15:19 -08005493 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5494 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005495 // before this feature is enabled, this API should only return false if
5496 // the operation fails instead of throwing runtime exception for
5497 // backward-compatibility.
5498 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5499 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005500 final long identity = Binder.clearCallingIdentity();
5501 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005502 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005503 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005504 }
Chen Xue9d737e2022-01-01 23:41:31 -08005505 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005506 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005507 Boolean success = false;
5508 if (result instanceof RuntimeException) {
5509 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005510 if (shouldThrowExceptionOnFailure) {
5511 throw (RuntimeException) result;
5512 } else {
5513 return false;
5514 }
Chen Xue9d737e2022-01-01 23:41:31 -08005515 } else if (result instanceof Boolean) {
5516 success = (Boolean) result;
5517 } else {
5518 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5519 }
Rambo Wanga1782702021-11-10 20:15:19 -08005520 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005521 return success;
5522 } finally {
5523 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005524 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005525 }
5526
5527 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005528 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005529 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005530 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5531 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005532 if (DBG) {
5533 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5534 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5535 + p3 + " data=" + data);
5536 }
5537 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5538 command, p1, p2, p3, data);
5539 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005540
Jordan Liu4c733742019-02-28 12:03:40 -08005541 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005542 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5543 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005544 enforceModifyPermission();
5545 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005546 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5547 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5548 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005549 }
5550 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005551 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5552 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005553 }
5554
5555 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5556 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005557 final long identity = Binder.clearCallingIdentity();
5558 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005559 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005560 return "";
5561 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005562
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005563 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005564 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5565 null /* workSource */);
5566 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005567
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005568 // Append the returned status code to the end of the response payload.
5569 String s = Integer.toHexString(
5570 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5571 if (response.payload != null) {
5572 s = IccUtils.bytesToHexString(response.payload) + s;
5573 }
5574 return s;
5575 } finally {
5576 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005577 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005578 }
Jake Hambye994d462014-02-03 13:10:13 -08005579
Evan Charltonc66da362014-05-16 14:06:40 -07005580 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005581 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5582 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005583 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5584 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005585 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005586 if (DBG) {
5587 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5588 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5589 }
5590 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5591 cla, command, p1, p2, p3, data);
5592 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005593
Jordan Liu4c733742019-02-28 12:03:40 -08005594 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005595 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5596 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005597 enforceModifyPermission();
5598 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5599 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005600 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5601 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5602 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005603 }
5604
5605 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005606 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5607 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005608 }
5609
5610 // open APDU basic channel assuming the caller has sufficient permissions
5611 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5612 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005613 final long identity = Binder.clearCallingIdentity();
5614 try {
5615 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5616 && TextUtils.equals(ISDR_AID, data)) {
5617 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005618 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5619 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005620 if (bestComponent == null
5621 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5622 loge("The calling package is not allowed to select ISD-R.");
5623 throw new SecurityException(
5624 "The calling package is not allowed to select ISD-R.");
5625 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005626 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005627
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005628 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005629 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5630 null /* workSource */);
5631 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005632
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005633 // Append the returned status code to the end of the response payload.
5634 String s = Integer.toHexString(
5635 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5636 if (response.payload != null) {
5637 s = IccUtils.bytesToHexString(response.payload) + s;
5638 }
5639 return s;
5640 } finally {
5641 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005642 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005643 }
5644
5645 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005646 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005647 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005648 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5649 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005650
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005651 final long identity = Binder.clearCallingIdentity();
5652 try {
5653 if (DBG) {
5654 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5655 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5656 }
5657
5658 IccIoResult response =
5659 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5660 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5661 subId);
5662
5663 if (DBG) {
5664 log("Exchange SIM_IO [R]" + response);
5665 }
5666
5667 byte[] result = null;
5668 int length = 2;
5669 if (response.payload != null) {
5670 length = 2 + response.payload.length;
5671 result = new byte[length];
5672 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5673 } else {
5674 result = new byte[length];
5675 }
5676
5677 result[length - 1] = (byte) response.sw2;
5678 result[length - 2] = (byte) response.sw1;
5679 return result;
5680 } finally {
5681 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005682 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005683 }
5684
Nathan Haroldb3014052017-01-25 15:57:32 -08005685 /**
5686 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5687 * on a particular subscription
5688 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005689 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5690 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005691 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005692 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005693 return null;
5694 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005695
5696 final long identity = Binder.clearCallingIdentity();
5697 try {
5698 if (appType != TelephonyManager.APPTYPE_USIM
5699 && appType != TelephonyManager.APPTYPE_SIM) {
5700 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5701 return null;
5702 }
5703 Object response = sendRequest(
5704 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5705 if (response instanceof String[]) {
5706 return (String[]) response;
5707 }
yincheng zhao2737e882019-09-06 17:06:54 -07005708 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005709 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005710 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005711 } finally {
5712 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005713 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005714 }
5715
yincheng zhao2737e882019-09-06 17:06:54 -07005716 /**
5717 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5718 * subscription.
5719 *
5720 * @param subId the id of the subscription.
5721 * @param appType the uicc app type, must be USIM or SIM.
5722 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5723 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005724 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005725 * @return number of fplmns that is successfully written to the SIM.
5726 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005727 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5728 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005729 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5730 mApp, subId, "setForbiddenPlmns");
5731
yincheng zhao2737e882019-09-06 17:06:54 -07005732 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5733 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5734 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5735 }
5736 if (fplmns == null) {
5737 throw new IllegalArgumentException("Fplmn List provided is null");
5738 }
5739 for (String fplmn : fplmns) {
5740 if (!CellIdentity.isValidPlmn(fplmn)) {
5741 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5742 }
5743 }
5744 final long identity = Binder.clearCallingIdentity();
5745 try {
5746 Object response = sendRequest(
5747 CMD_SET_FORBIDDEN_PLMNS,
5748 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5749 subId);
5750 return (int) response;
5751 } finally {
5752 Binder.restoreCallingIdentity(identity);
5753 }
5754 }
5755
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005756 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005757 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005758 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5759 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005760
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005761 final long identity = Binder.clearCallingIdentity();
5762 try {
5763 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5764 if (response.payload == null) {
5765 return "";
5766 }
Evan Charltonc66da362014-05-16 14:06:40 -07005767
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005768 // Append the returned status code to the end of the response payload.
5769 String s = Integer.toHexString(
5770 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5771 s = IccUtils.bytesToHexString(response.payload) + s;
5772 return s;
5773 } finally {
5774 Binder.restoreCallingIdentity(identity);
5775 }
Evan Charltonc66da362014-05-16 14:06:40 -07005776 }
5777
Jake Hambye994d462014-02-03 13:10:13 -08005778 /**
5779 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5780 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5781 *
5782 * @param itemID the ID of the item to read
5783 * @return the NV item as a String, or null on error.
5784 */
5785 @Override
5786 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005787 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005788 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5789 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005790
5791 final long identity = Binder.clearCallingIdentity();
5792 try {
5793 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005794 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005795 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5796 return value;
5797 } finally {
5798 Binder.restoreCallingIdentity(identity);
5799 }
Jake Hambye994d462014-02-03 13:10:13 -08005800 }
5801
5802 /**
5803 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5804 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5805 *
5806 * @param itemID the ID of the item to read
5807 * @param itemValue the value to write, as a String
5808 * @return true on success; false on any failure
5809 */
5810 @Override
5811 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005812 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005813 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5814 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005815
5816 final long identity = Binder.clearCallingIdentity();
5817 try {
5818 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5819 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005820 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005821 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5822 return success;
5823 } finally {
5824 Binder.restoreCallingIdentity(identity);
5825 }
Jake Hambye994d462014-02-03 13:10:13 -08005826 }
5827
5828 /**
5829 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5830 * Used for device configuration by some CDMA operators.
5831 *
5832 * @param preferredRoamingList byte array containing the new PRL
5833 * @return true on success; false on any failure
5834 */
5835 @Override
5836 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005837 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5838 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005839
5840 final long identity = Binder.clearCallingIdentity();
5841 try {
5842 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5843 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5844 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5845 return success;
5846 } finally {
5847 Binder.restoreCallingIdentity(identity);
5848 }
Jake Hambye994d462014-02-03 13:10:13 -08005849 }
5850
5851 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005852 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005853 * Used for device configuration by some CDMA operators.
5854 *
chen xu6dac5ab2018-10-26 17:39:23 -07005855 * @param slotIndex - device slot.
5856 *
Jake Hambye994d462014-02-03 13:10:13 -08005857 * @return true on success; false on any failure
5858 */
5859 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005860 public boolean resetModemConfig(int slotIndex) {
5861 Phone phone = PhoneFactory.getPhone(slotIndex);
5862 if (phone != null) {
5863 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5864 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005865
chen xu6dac5ab2018-10-26 17:39:23 -07005866 final long identity = Binder.clearCallingIdentity();
5867 try {
5868 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5869 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5870 return success;
5871 } finally {
5872 Binder.restoreCallingIdentity(identity);
5873 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005874 }
chen xu6dac5ab2018-10-26 17:39:23 -07005875 return false;
5876 }
5877
5878 /**
5879 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5880 *
5881 * @param slotIndex - device slot.
5882 *
5883 * @return true on success; false on any failure
5884 */
5885 @Override
5886 public boolean rebootModem(int slotIndex) {
5887 Phone phone = PhoneFactory.getPhone(slotIndex);
5888 if (phone != null) {
5889 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5890 mApp, phone.getSubId(), "rebootModem");
5891
5892 final long identity = Binder.clearCallingIdentity();
5893 try {
5894 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5895 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5896 return success;
5897 } finally {
5898 Binder.restoreCallingIdentity(identity);
5899 }
5900 }
5901 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005902 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005903
Brad Ebinger51f743a2017-01-23 13:50:20 -08005904 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005905 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5906 * {@link #disableIms(int)}.
5907 * @param slotIndex device slot.
5908 */
5909 public void resetIms(int slotIndex) {
5910 enforceModifyPermission();
5911
5912 final long identity = Binder.clearCallingIdentity();
5913 try {
5914 if (mImsResolver == null) {
5915 // may happen if the does not support IMS.
5916 return;
5917 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005918 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005919 } finally {
5920 Binder.restoreCallingIdentity(identity);
5921 }
5922 }
5923
5924 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005925 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5926 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005927 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005928 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005929 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005930
5931 final long identity = Binder.clearCallingIdentity();
5932 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005933 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005934 // may happen if the device does not support IMS.
5935 return;
5936 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005937 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005938 } finally {
5939 Binder.restoreCallingIdentity(identity);
5940 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005941 }
5942
5943 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005944 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5945 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005946 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005947 public void disableIms(int slotId) {
5948 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005949
5950 final long identity = Binder.clearCallingIdentity();
5951 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005952 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005953 // may happen if the device does not support IMS.
5954 return;
5955 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005956 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005957 } finally {
5958 Binder.restoreCallingIdentity(identity);
5959 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005960 }
5961
5962 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005963 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5964 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005965 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005966 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005967 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005968 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005969
5970 final long identity = Binder.clearCallingIdentity();
5971 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005972 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005973 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5974 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005975 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005976 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005977 } finally {
5978 Binder.restoreCallingIdentity(identity);
5979 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005980 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005981 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005982 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5983 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005984 @Override
5985 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005986 enforceModifyPermission();
5987
5988 final long identity = Binder.clearCallingIdentity();
5989 try {
5990 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005991 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005992 } finally {
5993 Binder.restoreCallingIdentity(identity);
5994 }
5995 }
5996
5997 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005998 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005999 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08006000 */
6001 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
6002 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006003
6004 final long identity = Binder.clearCallingIdentity();
6005 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006006 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006007 // may happen if the device does not support IMS.
6008 return null;
6009 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006010 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006011 } finally {
6012 Binder.restoreCallingIdentity(identity);
6013 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006014 }
6015
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006016 /**
6017 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006018 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006019 */
6020 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6021 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006022
6023 final long identity = Binder.clearCallingIdentity();
6024 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006025 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006026 // may happen if the device does not support IMS.
6027 return null;
6028 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006029 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006030 } finally {
6031 Binder.restoreCallingIdentity(identity);
6032 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006033 }
6034
Brad Ebinger884c07b2018-02-15 16:17:40 -08006035 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006036 * Sets the ImsService Package Name that Telephony will bind to.
6037 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006038 * @param slotIndex the slot ID that the ImsService should bind for.
6039 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006040 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006041 * @param featureTypes An integer array of feature types associated with a packageName.
6042 * @param packageName The name of the package that the current configuration will be replaced
6043 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006044 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006045 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006046 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6047 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006048 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006049 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006050 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006051
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006052 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 false;
6057 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006058 Map<Integer, String> featureConfig = new HashMap<>();
6059 for (int featureType : featureTypes) {
6060 featureConfig.put(featureType, packageName);
6061 }
6062 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6063 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006064 } finally {
6065 Binder.restoreCallingIdentity(identity);
6066 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006067 }
6068
6069 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006070 * Clears any carrier ImsService overrides for the slot index specified that were previously
6071 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6072 *
6073 * This should only be used for testing.
6074 *
6075 * @param slotIndex the slot ID that the ImsService should bind for.
6076 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6077 */
6078 @Override
6079 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006080 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6081 "clearCarrierImsServiceOverride");
6082 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006083 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006084
6085 final long identity = Binder.clearCallingIdentity();
6086 try {
6087 if (mImsResolver == null) {
6088 // may happen if the device does not support IMS.
6089 return false;
6090 }
6091 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6092 } finally {
6093 Binder.restoreCallingIdentity(identity);
6094 }
6095 }
6096
6097 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006098 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006099 *
6100 * @param slotId The slot that the ImsService is associated with.
6101 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6102 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006103 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006104 * @return the package name of the ImsService configuration.
6105 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006106 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6107 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006108 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006109 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6110 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006111
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006112 final long identity = Binder.clearCallingIdentity();
6113 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006114 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006115 // may happen if the device does not support IMS.
6116 return "";
6117 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006118 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006119 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6120 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006121 } finally {
6122 Binder.restoreCallingIdentity(identity);
6123 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006124 }
6125
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006126 /**
6127 * Get the MmTelFeature state associated with the requested subscription id.
6128 * @param subId The subscription that the MmTelFeature is associated with.
6129 * @param callback A callback with an integer containing the
6130 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6131 */
6132 @Override
6133 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6134 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006135 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6136 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6137 "IMS not available on device.");
6138 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006139 final long token = Binder.clearCallingIdentity();
6140 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006141 int slotId = getSlotIndex(subId);
6142 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6143 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6144 + subId + "'");
6145 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6146 }
6147 verifyImsMmTelConfiguredOrThrow(slotId);
6148 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6149 try {
6150 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6151 } catch (RemoteException e) {
6152 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6153 + "Ignore");
6154 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006155 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006156 } catch (ImsException e) {
6157 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006158 } finally {
6159 Binder.restoreCallingIdentity(token);
6160 }
6161 }
6162
Daniel Brightbb5840b2021-01-12 15:48:18 -08006163 /**
6164 * Sets the ims registration state on all valid {@link Phone}s.
6165 */
6166 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006167 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006168
6169 final long identity = Binder.clearCallingIdentity();
6170 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006171 // NOTE: Before S, this method only set the default phone.
6172 for (final Phone phone : PhoneFactory.getPhones()) {
6173 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6174 phone.setImsRegistrationState(registered);
6175 }
6176 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006177 } finally {
6178 Binder.restoreCallingIdentity(identity);
6179 }
Wink Saville36469e72014-06-11 15:17:00 -07006180 }
6181
6182 /**
Stuart Scott54788802015-03-30 13:18:01 -07006183 * Set the network selection mode to automatic.
6184 *
6185 */
6186 @Override
6187 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006188 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6189 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006190
6191 final long identity = Binder.clearCallingIdentity();
6192 try {
shilufc958392020-01-20 11:36:01 -08006193 if (!isActiveSubscription(subId)) {
6194 return;
6195 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006196 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006197 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6198 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006199 } finally {
6200 Binder.restoreCallingIdentity(identity);
6201 }
Stuart Scott54788802015-03-30 13:18:01 -07006202 }
6203
Jack Yud10cdd42020-09-28 20:28:01 -07006204 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006205 * Ask the radio to connect to the input network and change selection mode to manual.
6206 *
6207 * @param subId the id of the subscription.
6208 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6209 * the operator to attach to.
6210 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6211 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6212 * normal network selection next time.
6213 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006214 */
6215 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006216 public boolean setNetworkSelectionModeManual(
6217 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006218 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6219 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006220
Jack Yu285100e2022-12-02 22:48:35 -08006221 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006222 if (!isActiveSubscription(subId)) {
6223 return false;
6224 }
6225
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006226 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006227 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006228 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006229 if (DBG) {
6230 log("setNetworkSelectionModeManual: subId: " + subId
6231 + " operator: " + operatorInfo);
6232 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006233 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6234 } finally {
6235 Binder.restoreCallingIdentity(identity);
6236 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006237 }
shilu84f6e8b2019-12-19 13:58:01 -08006238 /**
6239 * Get the manual network selection
6240 *
6241 * @param subId the id of the subscription.
6242 *
6243 * @return the previously saved user selected PLMN
6244 */
6245 @Override
6246 public String getManualNetworkSelectionPlmn(int subId) {
6247 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006248 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006249 mApp, subId, "getManualNetworkSelectionPlmn");
6250
6251 final long identity = Binder.clearCallingIdentity();
6252 try {
6253 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006254 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006255 }
6256
6257 final Phone phone = getPhone(subId);
6258 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006259 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006260 }
6261 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6262 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6263 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6264 } finally {
6265 Binder.restoreCallingIdentity(identity);
6266 }
6267 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006268
6269 /**
6270 * Scans for available networks.
6271 */
6272 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006273 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6274 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006275 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6276 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006277 LocationAccessPolicy.LocationPermissionResult locationResult =
6278 LocationAccessPolicy.checkLocationPermission(mApp,
6279 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6280 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006281 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006282 .setCallingPid(Binder.getCallingPid())
6283 .setCallingUid(Binder.getCallingUid())
6284 .setMethod("getCellNetworkScanResults")
6285 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006286 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6287 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006288 .build());
6289 switch (locationResult) {
6290 case DENIED_HARD:
6291 throw new SecurityException("Not allowed to access scan results -- location");
6292 case DENIED_SOFT:
6293 return null;
6294 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006295
Pengquan Menga1bb6272018-09-06 09:59:22 -07006296 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006297 try {
6298 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006299 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006300 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006301 } finally {
6302 Binder.restoreCallingIdentity(identity);
6303 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006304 }
6305
6306 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006307 * Get the call forwarding info, given the call forwarding reason.
6308 */
6309 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006310 public void getCallForwarding(int subId, int callForwardingReason,
6311 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006312 enforceReadPrivilegedPermission("getCallForwarding");
6313 long identity = Binder.clearCallingIdentity();
6314 try {
6315 if (DBG) {
6316 log("getCallForwarding: subId " + subId
6317 + " callForwardingReason" + callForwardingReason);
6318 }
Hall Liu27d24262020-09-18 19:04:59 -07006319
6320 Phone phone = getPhone(subId);
6321 if (phone == null) {
6322 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006323 callback.onError(
6324 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006325 } catch (RemoteException e) {
6326 // ignore
6327 }
6328 return;
6329 }
6330
6331 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6332 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6333 @Override
6334 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6335 try {
6336 callback.onCallForwardingInfoAvailable(info);
6337 } catch (RemoteException e) {
6338 // ignore
6339 }
6340 }
6341
6342 @Override
6343 public void onError(int error) {
6344 try {
6345 callback.onError(error);
6346 } catch (RemoteException e) {
6347 // ignore
6348 }
6349 }
6350 });
6351 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006352 } finally {
6353 Binder.restoreCallingIdentity(identity);
6354 }
6355 }
6356
6357 /**
6358 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6359 * reason, the number to forward, and the timeout before the forwarding is attempted.
6360 */
6361 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006362 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6363 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006364 enforceModifyPermission();
6365 long identity = Binder.clearCallingIdentity();
6366 try {
6367 if (DBG) {
6368 log("setCallForwarding: subId " + subId
6369 + " callForwardingInfo" + callForwardingInfo);
6370 }
Hall Liu27d24262020-09-18 19:04:59 -07006371
6372 Phone phone = getPhone(subId);
6373 if (phone == null) {
6374 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006375 callback.accept(
6376 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006377 } catch (RemoteException e) {
6378 // ignore
6379 }
6380 return;
6381 }
6382
6383 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6384 FunctionalUtils.ignoreRemoteException(callback::accept));
6385
6386 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006387 } finally {
6388 Binder.restoreCallingIdentity(identity);
6389 }
6390 }
6391
6392 /**
Hall Liu27d24262020-09-18 19:04:59 -07006393 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006394 */
6395 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006396 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006397 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006398 long identity = Binder.clearCallingIdentity();
6399 try {
Hall Liu27d24262020-09-18 19:04:59 -07006400 Phone phone = getPhone(subId);
6401 if (phone == null) {
6402 try {
6403 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6404 } catch (RemoteException e) {
6405 // ignore
6406 }
6407 return;
6408 }
SongFerngWang0e767992021-03-31 22:08:45 +08006409 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6410 PersistableBundle c = configManager.getConfigForSubId(subId);
6411 boolean requireUssd = c.getBoolean(
6412 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006413
Shuo Qian4a594052020-01-23 11:59:30 -08006414 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006415 if (requireUssd) {
6416 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6417 getSubscriptionCarrierId(subId));
6418 String newUssdCommand = "";
6419 try {
6420 newUssdCommand = carrierXmlParser.getFeature(
6421 CarrierXmlParser.FEATURE_CALL_WAITING)
6422 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6423 } catch (NullPointerException e) {
6424 loge("Failed to generate USSD number" + e);
6425 }
6426 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6427 mMainThreadHandler, callback, carrierXmlParser,
6428 CarrierXmlParser.SsEntry.SSAction.QUERY);
6429 final String ussdCommand = newUssdCommand;
6430 Executors.newSingleThreadExecutor().execute(() -> {
6431 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6432 });
6433 } else {
6434 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6435 callback::accept);
6436 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6437 }
Shuo Qian4a594052020-01-23 11:59:30 -08006438 } finally {
6439 Binder.restoreCallingIdentity(identity);
6440 }
6441 }
6442
6443 /**
Hall Liu27d24262020-09-18 19:04:59 -07006444 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006445 */
6446 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006447 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006448 enforceModifyPermission();
6449 long identity = Binder.clearCallingIdentity();
6450 try {
Hall Liu27d24262020-09-18 19:04:59 -07006451 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6452
6453 Phone phone = getPhone(subId);
6454 if (phone == null) {
6455 try {
6456 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6457 } catch (RemoteException e) {
6458 // ignore
6459 }
6460 return;
6461 }
6462
SongFerngWang0e767992021-03-31 22:08:45 +08006463 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6464 PersistableBundle c = configManager.getConfigForSubId(subId);
6465 boolean requireUssd = c.getBoolean(
6466 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006467
SongFerngWang0e767992021-03-31 22:08:45 +08006468 if (DBG) log("getCallWaitingStatus: subId " + subId);
6469 if (requireUssd) {
6470 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6471 getSubscriptionCarrierId(subId));
6472 CarrierXmlParser.SsEntry.SSAction ssAction =
6473 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6474 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6475 String newUssdCommand = "";
6476 try {
6477 newUssdCommand = carrierXmlParser.getFeature(
6478 CarrierXmlParser.FEATURE_CALL_WAITING)
6479 .makeCommand(ssAction, null);
6480 } catch (NullPointerException e) {
6481 loge("Failed to generate USSD number" + e);
6482 }
6483 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6484 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6485 final String ussdCommand = newUssdCommand;
6486 Executors.newSingleThreadExecutor().execute(() -> {
6487 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6488 });
6489 } else {
6490 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6491 FunctionalUtils.ignoreRemoteException(callback::accept));
6492
6493 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6494 }
Shuo Qian4a594052020-01-23 11:59:30 -08006495 } finally {
6496 Binder.restoreCallingIdentity(identity);
6497 }
6498 }
6499
6500 /**
yinxub1bed742017-04-17 11:45:04 -07006501 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006502 *
yinxub1bed742017-04-17 11:45:04 -07006503 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006504 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6505 * location related information which will be sent if the caller already possess
6506 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006507 * @param request contains the radio access networks with bands/channels to scan
6508 * @param messenger callback messenger for scan results or errors
6509 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006510 * @return the id of the requested scan which can be used to stop the scan.
6511 */
6512 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006513 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6514 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006515 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006516 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6517 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006518 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006519 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6520 if (!renounceFineLocationAccess) {
6521 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6522 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6523 .setCallingPackage(callingPackage)
6524 .setCallingFeatureId(callingFeatureId)
6525 .setCallingPid(Binder.getCallingPid())
6526 .setCallingUid(Binder.getCallingUid())
6527 .setMethod("requestNetworkScan")
6528 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6529 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6530 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6531 .build());
6532 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006533 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006534 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6535 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006536 if (e != null) {
6537 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6538 throw e;
6539 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006540 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006541 return TelephonyScanManager.INVALID_SCAN_ID;
6542 }
6543 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006544 }
Hall Liu912dfd32019-04-25 14:02:26 -07006545 int callingUid = Binder.getCallingUid();
6546 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006547 final long identity = Binder.clearCallingIdentity();
6548 try {
6549 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006550 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006551 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006552 } finally {
6553 Binder.restoreCallingIdentity(identity);
6554 }
yinxu504e1392017-04-12 16:03:22 -07006555 }
6556
Hall Liub2ac8ef2019-02-28 15:56:23 -08006557 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006558 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006559 boolean hasCarrierPriv;
6560 final long identity = Binder.clearCallingIdentity();
6561 try {
6562 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6563 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6564 } finally {
6565 Binder.restoreCallingIdentity(identity);
6566 }
Hall Liu558027f2019-05-15 19:14:05 -07006567 boolean hasNetworkScanPermission =
6568 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6569 == PERMISSION_GRANTED;
6570
6571 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6572 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6573 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006574 }
6575
6576 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6577 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006578 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6579 return new SecurityException("Specific channels must not be"
6580 + " scanned without location access.");
6581 }
6582 }
6583 }
6584
Hall Liub2ac8ef2019-02-28 15:56:23 -08006585 return null;
6586 }
6587
yinxu504e1392017-04-12 16:03:22 -07006588 /**
6589 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006590 *
6591 * @param subId id of the subscription
6592 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006593 */
6594 @Override
6595 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006596 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6597 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006598
Hall Liu912dfd32019-04-25 14:02:26 -07006599 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006600 final long identity = Binder.clearCallingIdentity();
6601 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006602 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006603 } finally {
6604 Binder.restoreCallingIdentity(identity);
6605 }
yinxu504e1392017-04-12 16:03:22 -07006606 }
6607
6608 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006609 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006610 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006611 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006612 */
6613 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006614 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006615 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006616 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006617 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006618
6619 final long identity = Binder.clearCallingIdentity();
6620 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006621 if (DBG) log("getAllowedNetworkTypesBitmask");
6622 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6623 int networkTypesBitmask = (result != null ? result[0] : -1);
6624 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6625 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006626 } finally {
6627 Binder.restoreCallingIdentity(identity);
6628 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006629 }
6630
6631 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006632 * Get the allowed network types for certain reason.
6633 *
6634 * @param subId the id of the subscription.
6635 * @param reason the reason the allowed network type change is taking place
6636 * @return the allowed network types.
6637 */
6638 @Override
6639 public long getAllowedNetworkTypesForReason(int subId,
6640 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006641 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006642 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006643 final long identity = Binder.clearCallingIdentity();
6644 try {
6645 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6646 } finally {
6647 Binder.restoreCallingIdentity(identity);
6648 }
6649 }
6650
6651 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006652 * Enable/Disable E-UTRA-NR Dual Connectivity
6653 * @param subId subscription id of the sim card
6654 * @param nrDualConnectivityState expected NR dual connectivity state
6655 * This can be passed following states
6656 * <ol>
6657 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6658 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6659 * <li>Disable NR dual connectivity and force secondary cell to be released
6660 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6661 * </ol>
6662 * @return operation result.
6663 */
6664 @Override
6665 public int setNrDualConnectivityState(int subId,
6666 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6667 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6668 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006669 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006670 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6671 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6672 }
6673
Sooraj Sasindran37444802020-08-11 10:40:43 -07006674 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6675 final long identity = Binder.clearCallingIdentity();
6676 try {
6677 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6678 nrDualConnectivityState, subId,
6679 workSource);
6680 if (DBG) log("enableNRDualConnectivity result: " + result);
6681 return result;
6682 } finally {
6683 Binder.restoreCallingIdentity(identity);
6684 }
6685 }
6686
6687 /**
6688 * Is E-UTRA-NR Dual Connectivity enabled
6689 * @return true if dual connectivity is enabled else false
6690 */
6691 @Override
6692 public boolean isNrDualConnectivityEnabled(int subId) {
6693 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006694 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006695 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006696 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006697 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6698 return false;
6699 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006700 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6701 final long identity = Binder.clearCallingIdentity();
6702 try {
6703 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6704 null, subId, workSource);
6705 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6706 return isEnabled;
6707 } finally {
6708 Binder.restoreCallingIdentity(identity);
6709 }
6710 }
6711
6712 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006713 * Set the allowed network types of the device and
6714 * provide the reason triggering the allowed network change.
6715 *
6716 * @param subId the id of the subscription.
6717 * @param reason the reason the allowed network type change is taking place
6718 * @param allowedNetworkTypes the allowed network types.
6719 * @return true on success; false on any failure.
6720 */
6721 @Override
6722 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006723 @TelephonyManager.AllowedNetworkTypesReason int reason,
6724 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006725 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6726 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006727 // If the caller only has carrier privileges, then they should not be able to override
6728 // any network types which were set for security reasons.
6729 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6730 != PERMISSION_GRANTED
6731 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6732 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6733 throw new SecurityException(
6734 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6735 + " reason "
6736 + reason);
6737 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006738 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006739 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6740 return false;
6741 }
6742 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6743 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006744 return false;
6745 }
6746
Aishwarya Mallampati32336e82023-01-04 23:02:56 +00006747 log("setAllowedNetworkTypesForReason: " + reason + " value: "
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006748 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6749
6750
Aishwarya Mallampati32336e82023-01-04 23:02:56 +00006751 if (allowedNetworkTypes == getPhoneFromSubId(subId).getAllowedNetworkTypes(reason)) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006752 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6753 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006754 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006755
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006756 final long identity = Binder.clearCallingIdentity();
6757 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006758 Boolean success = (Boolean) sendRequest(
6759 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6760 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6761
6762 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6763 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006764 } finally {
6765 Binder.restoreCallingIdentity(identity);
6766 }
6767 }
6768
6769 /**
Miaoa84611c2019-03-15 09:21:10 +08006770 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006771 *
Miaoa84611c2019-03-15 09:21:10 +08006772 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006773 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006774 * @hide
6775 */
6776 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006777 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006778 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006779 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006780 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006781 try {
Miaoa84611c2019-03-15 09:21:10 +08006782 if (phone != null) {
6783 return phone.hasMatchedTetherApnSetting();
6784 } else {
6785 return false;
6786 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006787 } finally {
6788 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006789 }
Junda Liu475951f2014-11-07 16:45:03 -08006790 }
6791
6792 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006793 * Get the user enabled state of Mobile Data.
6794 *
6795 * TODO: remove and use isUserDataEnabled.
6796 * This can't be removed now because some vendor codes
6797 * calls through ITelephony directly while they should
6798 * use TelephonyManager.
6799 *
6800 * @return true on enabled
6801 */
6802 @Override
6803 public boolean getDataEnabled(int subId) {
6804 return isUserDataEnabled(subId);
6805 }
6806
6807 /**
6808 * Get whether mobile data is enabled per user setting.
6809 *
6810 * There are other factors deciding whether mobile data is actually enabled, but they are
6811 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006812 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006813 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6814 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006815 *
6816 * @return {@code true} if data is enabled else {@code false}
6817 */
6818 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006819 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006820 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006821 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006822 try {
6823 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6824 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006825 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006826 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6827 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006828 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006829 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006830 mApp, subId, functionName);
6831
Robert Greenwalt646120a2014-05-23 11:54:03 -07006832 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006833
6834 final long identity = Binder.clearCallingIdentity();
6835 try {
Jack Yu285100e2022-12-02 22:48:35 -08006836 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006837 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6838 Phone phone = PhoneFactory.getPhone(phoneId);
6839 if (phone != null) {
6840 boolean retVal = phone.isUserDataEnabled();
6841 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6842 return retVal;
6843 } else {
6844 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6845 return false;
6846 }
6847 } finally {
6848 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006849 }
6850 }
6851
6852 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006853 * Checks if the device is capable of mobile data by considering whether whether the
6854 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6855 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006856 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006857 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006858 */
6859 @Override
6860 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006861 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006862 try {
6863 try {
6864 mApp.enforceCallingOrSelfPermission(
6865 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006866 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006867 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006868 try {
6869 mApp.enforceCallingOrSelfPermission(
6870 android.Manifest.permission.READ_PHONE_STATE,
6871 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006872 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006873 mApp.enforceCallingOrSelfPermission(
6874 permission.READ_BASIC_PHONE_STATE, functionName);
6875 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006876 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006877 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006878 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006879 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006880
6881 final long identity = Binder.clearCallingIdentity();
6882 try {
Jack Yu285100e2022-12-02 22:48:35 -08006883 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006884 Phone phone = PhoneFactory.getPhone(phoneId);
6885 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006886 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006887 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006888 return retVal;
6889 } else {
6890 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6891 return false;
6892 }
6893 } finally {
6894 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006895 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006896 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006897
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006898 /**
6899 * Check if data is enabled for a specific reason
6900 * @param subId Subscription index
6901 * @param reason the reason the data enable change is taking place
6902 * @return {@code true} if the overall data is enabled; {@code false} if not.
6903 */
6904 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006905 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006906 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006907 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006908 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006909 try {
6910 mApp.enforceCallingOrSelfPermission(
6911 android.Manifest.permission.ACCESS_NETWORK_STATE,
6912 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006913 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006914 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6915 functionName);
6916 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006917 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006918 try {
6919 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006920 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006921 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006922 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006923 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006924 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006925 }
6926
6927
6928 final long identity = Binder.clearCallingIdentity();
6929 try {
Jack Yu285100e2022-12-02 22:48:35 -08006930 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006931 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006932 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006933 + " reason=" + reason);
6934 }
6935 Phone phone = PhoneFactory.getPhone(phoneId);
6936 if (phone != null) {
6937 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006938 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006939 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006940 return retVal;
6941 } else {
6942 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006943 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006944 + subId + " retVal=false");
6945 }
6946 return false;
6947 }
6948 } finally {
6949 Binder.restoreCallingIdentity(identity);
6950 }
6951 }
6952
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006953 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006954 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006955 // No permission needed; this only lets the caller inspect their own status.
6956 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006957 }
Junda Liu29340342014-07-10 15:23:27 -07006958
6959 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006960 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006961 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006962 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6963 }
6964
6965 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6966 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006967 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006968 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006969 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6970 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006971 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6972 if (cpt == null) {
6973 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006974 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6975 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006976 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006977 }
6978
6979 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006980 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006981 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006982 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006983 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006984 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006985 Phone phone = getPhone(subId);
6986 if (phone == null) {
6987 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6988 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6989 }
6990 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6991 if (cpt == null) {
6992 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006993 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6994 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006995 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006996 }
6997
6998 @Override
6999 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007000 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007001 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7002 }
7003
7004 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007005 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007006 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007007 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007008 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007009 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7010 Phone phone = PhoneFactory.getPhone(phoneId);
7011 if (phone == null) {
7012 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007013 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007014 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7015 if (cpt == null) {
7016 continue;
7017 }
7018 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007019 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7020 break;
7021 }
7022 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007023 return result;
Junda Liu29340342014-07-10 15:23:27 -07007024 }
Derek Tan89e89d42014-07-08 17:00:10 -07007025
7026 @Override
Junda Liue64de782015-04-16 17:19:16 -07007027 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007028 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007029 Phone phone = PhoneFactory.getPhone(phoneId);
7030 if (phone == null) {
7031 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007032 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007033 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7034 if (cpt == null) {
7035 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007036 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007037 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007038 }
7039
Amith Yamasani6e118872016-02-19 12:53:51 -08007040 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007041 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007042 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007043 Phone phone = PhoneFactory.getPhone(phoneId);
7044 if (phone == null) {
7045 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007046 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007047 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7048 if (cpt == null) {
7049 return Collections.emptyList();
7050 }
7051 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007052 }
7053
chen xuf7e9fe82019-05-09 19:31:02 -07007054 @Override
7055 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007056 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007057 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007058 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007059 try {
7060 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7061 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7062 }
7063 } finally {
7064 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007065 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007066 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007067 }
7068
Rambo Wang6812ffb2022-03-15 16:54:17 -07007069 @Override
7070 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7071 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7072
7073 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7074 if (phone == null) {
7075 return null;
7076 }
7077 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7078 if (cpt == null) {
7079 return null;
7080 }
7081 return cpt.getCarrierServicePackageName();
7082 }
7083
Wink Savilleb564aae2014-10-23 10:18:09 -07007084 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007085 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007086 UiccPort port = phone == null ? null : phone.getUiccPort();
7087 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007088 return null;
7089 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007090 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007091 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007092 return null;
7093 }
7094 return iccId;
7095 }
7096
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007097 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007098 public void setCallComposerStatus(int subId, int status) {
7099 enforceModifyPermission();
7100
7101 final long identity = Binder.clearCallingIdentity();
7102 try {
7103 Phone phone = getPhone(subId);
7104 if (phone != null) {
7105 Phone defaultPhone = phone.getImsPhone();
7106 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7107 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7108 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007109 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7110 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007111 }
7112 }
Shuo Qian284ae752020-12-22 19:10:14 -08007113 } catch (ImsException e) {
7114 throw new ServiceSpecificException(e.getCode());
7115 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007116 Binder.restoreCallingIdentity(identity);
7117 }
7118 }
7119
7120 @Override
7121 public int getCallComposerStatus(int subId) {
7122 enforceReadPrivilegedPermission("getCallComposerStatus");
7123
7124 final long identity = Binder.clearCallingIdentity();
7125 try {
7126 Phone phone = getPhone(subId);
7127 if (phone != null) {
7128 Phone defaultPhone = phone.getImsPhone();
7129 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7130 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7131 return imsPhone.getCallComposerStatus();
7132 }
7133 }
7134 } finally {
7135 Binder.restoreCallingIdentity(identity);
7136 }
7137 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7138 }
7139
7140 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007141 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7142 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007143 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007144 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007145
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007146 final long identity = Binder.clearCallingIdentity();
7147 try {
7148 final String iccId = getIccId(subId);
7149 final Phone phone = getPhone(subId);
7150 if (phone == null) {
7151 return false;
7152 }
7153 final String subscriberId = phone.getSubscriberId();
7154
7155 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007156 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007157 + subscriberId + " to " + number);
7158 }
7159
7160 if (TextUtils.isEmpty(iccId)) {
7161 return false;
7162 }
7163
7164 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7165
7166 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7167 if (alphaTag == null) {
7168 editor.remove(alphaTagPrefKey);
7169 } else {
7170 editor.putString(alphaTagPrefKey, alphaTag);
7171 }
7172
7173 // Record both the line number and IMSI for this ICCID, since we need to
7174 // track all merged IMSIs based on line number
7175 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7176 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7177 if (number == null) {
7178 editor.remove(numberPrefKey);
7179 editor.remove(subscriberPrefKey);
7180 } else {
7181 editor.putString(numberPrefKey, number);
7182 editor.putString(subscriberPrefKey, subscriberId);
7183 }
7184
7185 editor.commit();
7186 return true;
7187 } finally {
7188 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007189 }
Derek Tan7226c842014-07-02 17:42:23 -07007190 }
7191
7192 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007193 public String getLine1NumberForDisplay(int subId, String callingPackage,
7194 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007195 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007196 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007197 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007198 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007199 return null;
7200 }
Derek Tan97ebb422014-09-05 16:55:38 -07007201
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007202 final long identity = Binder.clearCallingIdentity();
7203 try {
7204 String iccId = getIccId(subId);
7205 if (iccId != null) {
7206 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7207 if (DBG_MERGE) {
7208 log("getLine1NumberForDisplay returning "
7209 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7210 }
7211 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007212 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007213 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7214 return null;
7215 } finally {
7216 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007217 }
Derek Tan7226c842014-07-02 17:42:23 -07007218 }
7219
7220 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007221 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7222 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007223 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007224 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007225 return null;
7226 }
Derek Tan97ebb422014-09-05 16:55:38 -07007227
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007228 final long identity = Binder.clearCallingIdentity();
7229 try {
7230 String iccId = getIccId(subId);
7231 if (iccId != null) {
7232 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7233 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7234 }
7235 return null;
7236 } finally {
7237 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007238 }
Derek Tan7226c842014-07-02 17:42:23 -07007239 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007240
7241 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007242 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7243 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007244 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7245 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007246 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007247 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007248 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007249 return null;
7250 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007251
Jordan Liub49b04b2019-05-06 14:45:15 -07007252 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7253 // the process, where TelephonyManager was instantiated.
7254 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007255 final long identity = Binder.clearCallingIdentity();
7256 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007257 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007258 final TelephonyManager tele = TelephonyManager.from(context);
7259 final SubscriptionManager sub = SubscriptionManager.from(context);
7260
7261 // Figure out what subscribers are currently active
7262 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007263
Jordan Liub49b04b2019-05-06 14:45:15 -07007264 // Only consider subs which match the current subId
7265 // This logic can be simplified. See b/131189269 for progress.
7266 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007267 activeSubscriberIds.add(tele.getSubscriberId(subId));
7268 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007269
7270 // First pass, find a number override for an active subscriber
7271 String mergeNumber = null;
7272 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7273 for (String key : prefs.keySet()) {
7274 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7275 final String subscriberId = (String) prefs.get(key);
7276 if (activeSubscriberIds.contains(subscriberId)) {
7277 final String iccId = key.substring(
7278 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7279 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7280 mergeNumber = (String) prefs.get(numberKey);
7281 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007282 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007283 + " for active subscriber " + subscriberId);
7284 }
7285 if (!TextUtils.isEmpty(mergeNumber)) {
7286 break;
7287 }
7288 }
7289 }
7290 }
7291
7292 // Shortcut when no active merged subscribers
7293 if (TextUtils.isEmpty(mergeNumber)) {
7294 return null;
7295 }
7296
7297 // Second pass, find all subscribers under that line override
7298 final ArraySet<String> result = new ArraySet<>();
7299 for (String key : prefs.keySet()) {
7300 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7301 final String number = (String) prefs.get(key);
7302 if (mergeNumber.equals(number)) {
7303 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7304 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7305 final String subscriberId = (String) prefs.get(subscriberKey);
7306 if (!TextUtils.isEmpty(subscriberId)) {
7307 result.add(subscriberId);
7308 }
7309 }
7310 }
7311 }
7312
7313 final String[] resultArray = result.toArray(new String[result.size()]);
7314 Arrays.sort(resultArray);
7315 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007316 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007317 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7318 }
7319 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007320 } finally {
7321 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007322 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007323 }
7324
7325 @Override
zoey chen38003472019-12-13 17:16:31 +08007326 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7327 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007328
7329 final long identity = Binder.clearCallingIdentity();
7330 try {
7331 final TelephonyManager telephonyManager = mApp.getSystemService(
7332 TelephonyManager.class);
7333 String subscriberId = telephonyManager.getSubscriberId(subId);
7334 if (subscriberId == null) {
7335 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007336 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007337 + subId);
7338 }
7339 return null;
7340 }
7341
Jack Yu285100e2022-12-02 22:48:35 -08007342 ParcelUuid groupUuid;
7343 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7344 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7345 .getSubscriptionInfo(subId);
7346 groupUuid = info.getGroupUuid();
7347 } else {
7348 final SubscriptionInfo info = mSubscriptionController
7349 .getSubscriptionInfo(subId);
7350 groupUuid = info.getGroupUuid();
7351 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007352 // If it doesn't belong to any group, return just subscriberId of itself.
7353 if (groupUuid == null) {
7354 return new String[]{subscriberId};
7355 }
7356
7357 // Get all subscriberIds from the group.
7358 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007359 List<SubscriptionInfo> groupInfos;
7360 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7361 groupInfos = SubscriptionManagerService.getInstance()
7362 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7363 mApp.getAttributionTag());
7364 } else {
7365 groupInfos = mSubscriptionController
7366 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7367 mApp.getAttributionTag());
7368 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007369 for (SubscriptionInfo subInfo : groupInfos) {
7370 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7371 if (subscriberId != null) {
7372 mergedSubscriberIds.add(subscriberId);
7373 }
7374 }
7375
7376 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7377 } finally {
7378 Binder.restoreCallingIdentity(identity);
7379
7380 }
7381 }
7382
7383 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007384 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007385 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007386 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007387
7388 final long identity = Binder.clearCallingIdentity();
7389 try {
7390 final Phone phone = getPhone(subId);
7391 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7392 } finally {
7393 Binder.restoreCallingIdentity(identity);
7394 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007395 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007396
7397 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007398 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007399 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7400 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007401 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7402 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007403
7404 final long identity = Binder.clearCallingIdentity();
7405 try {
7406 final Phone phone = getPhone(subId);
7407 if (phone == null) {
7408 return false;
7409 }
7410 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7411 cdmaNonRoamingList);
7412 } finally {
7413 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007414 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007415 }
7416
7417 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007418 @Deprecated
7419 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7420 enforceModifyPermission();
7421
7422 int returnValue = 0;
7423 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007424 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007425 if(result.exception == null) {
7426 if (result.result != null) {
7427 byte[] responseData = (byte[])(result.result);
7428 if(responseData.length > oemResp.length) {
7429 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7430 responseData.length + "bytes. Buffer Size is " +
7431 oemResp.length + "bytes.");
7432 }
7433 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7434 returnValue = responseData.length;
7435 }
7436 } else {
7437 CommandException ex = (CommandException) result.exception;
7438 returnValue = ex.getCommandError().ordinal();
7439 if(returnValue > 0) returnValue *= -1;
7440 }
7441 } catch (RuntimeException e) {
7442 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7443 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7444 if(returnValue > 0) returnValue *= -1;
7445 }
7446
7447 return returnValue;
7448 }
7449
7450 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007451 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007452 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007453 try {
7454 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007455 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007456 mApp, phone.getSubId(), "getRadioAccessFamily");
7457 } catch (SecurityException e) {
7458 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7459 throw e;
7460 }
chen xub97461a2018-10-26 14:17:57 -07007461 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007462 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007463 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007464 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007465 final long identity = Binder.clearCallingIdentity();
7466 try {
chen xub97461a2018-10-26 14:17:57 -07007467 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007468 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007469 mApp, phone.getSubId(), "getRadioAccessFamily");
7470 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007471 } finally {
7472 Binder.restoreCallingIdentity(identity);
7473 }
chen xub97461a2018-10-26 14:17:57 -07007474 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007475 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007476
7477 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007478 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007479 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007480 try {
7481 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7482 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007483 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007484 }
7485 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007486 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007487 }
7488 RoleManager rm = mApp.getSystemService(RoleManager.class);
7489 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7490 if (!dialerRoleHolders.contains(callingPackage)) {
7491 throw new SecurityException("App must be the dialer role holder to"
7492 + " upload a call composer pic");
7493 }
7494
7495 Executors.newSingleThreadExecutor().execute(() -> {
7496 ByteArrayOutputStream output = new ByteArrayOutputStream(
7497 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7498 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7499 boolean readUntilEnd = false;
7500 int totalBytesRead = 0;
7501 byte[] buffer = new byte[16 * 1024];
7502 while (true) {
7503 int numRead;
7504 try {
7505 numRead = input.read(buffer);
7506 } catch (IOException e) {
7507 try {
7508 fd.checkError();
7509 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7510 null);
7511 } catch (IOException e1) {
7512 // This means that the other side closed explicitly with an error. If this
7513 // happens, log and ignore.
7514 loge("Remote end of call composer picture pipe closed: " + e1);
7515 }
7516 break;
7517 }
7518 if (numRead == -1) {
7519 readUntilEnd = true;
7520 break;
7521 }
7522 totalBytesRead += numRead;
7523 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7524 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7525 try {
7526 input.close();
7527 } catch (IOException e) {
7528 // ignore
7529 }
7530 break;
7531 }
7532 output.write(buffer, 0, numRead);
7533 }
7534 // Generally, the remote end will close the file descriptors. The only case where we
7535 // close is above, where the picture size is too big.
7536
7537 try {
7538 fd.checkError();
7539 } catch (IOException e) {
7540 loge("Remote end for call composer closed with an error: " + e);
7541 return;
7542 }
7543
Hall Liuaa4211e2021-01-20 15:43:39 -08007544 if (!readUntilEnd) {
7545 loge("Did not finish reading entire image; aborting");
7546 return;
7547 }
Hall Liu82694d52020-12-11 18:22:04 -08007548
Hall Liuaa4211e2021-01-20 15:43:39 -08007549 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7550 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7551 new CallComposerPictureTransfer.Factory() {},
7552 imageData,
7553 (result) -> {
7554 if (result.first != null) {
7555 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7556 Bundle outputResult = new Bundle();
7557 outputResult.putParcelable(
7558 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7559 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7560 outputResult);
7561 } else {
7562 callback.send(result.second, null);
7563 }
7564 }
7565 );
Hall Liu82694d52020-12-11 18:22:04 -08007566 });
7567 }
7568
7569 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007570 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007571 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007572 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007573
7574 final long identity = Binder.clearCallingIdentity();
7575 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007576 ImsManager.getInstance(defaultPhone.getContext(),
7577 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007578 } finally {
7579 Binder.restoreCallingIdentity(identity);
7580 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007581 }
7582
7583 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007584 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007585 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007586 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7587 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007588 return false;
7589 }
Svet Ganovb320e182015-04-16 12:30:10 -07007590
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007591 final long identity = Binder.clearCallingIdentity();
7592 try {
7593 // Check the user preference and the system-level IMS setting. Even if the user has
7594 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7595 // In the long run, we may instead need to check if there exists a connection service
7596 // which can support video calling.
7597 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007598 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007599 return imsManager.isVtEnabledByPlatform()
7600 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7601 && imsManager.isVtEnabledByUser();
7602 } finally {
7603 Binder.restoreCallingIdentity(identity);
7604 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007605 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007606
Andrew Leea1239f22015-03-02 17:44:07 -08007607 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007608 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7609 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007610 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007611 mApp, subId, callingPackage, callingFeatureId,
7612 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007613 return false;
7614 }
7615
7616 final long identity = Binder.clearCallingIdentity();
7617 try {
7618 CarrierConfigManager configManager =
7619 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007620 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007621 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7622 } finally {
7623 Binder.restoreCallingIdentity(identity);
7624 }
Andrew Leea1239f22015-03-02 17:44:07 -08007625 }
7626
7627 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007628 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007629 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007630 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007631 return false;
7632 }
7633
7634 final long identity = Binder.clearCallingIdentity();
7635 try {
7636 CarrierConfigManager configManager =
7637 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007638 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007639 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7640 } finally {
7641 Binder.restoreCallingIdentity(identity);
7642 }
Andrew Leea1239f22015-03-02 17:44:07 -08007643 }
7644
Andrew Lee9431b832015-03-09 18:46:45 -07007645 @Override
7646 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007647 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007648 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007649 }
7650
7651 @Override
7652 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007653 final long identity = Binder.clearCallingIdentity();
7654 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007655 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007656 } finally {
7657 Binder.restoreCallingIdentity(identity);
7658 }
Andrew Lee9431b832015-03-09 18:46:45 -07007659 }
7660
Hall Liuf6668912018-10-31 17:05:23 -07007661 /**
7662 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7663 * support for the feature and device firmware support.
7664 *
7665 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7666 */
7667 @Override
7668 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007669 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007670 final Phone phone = getPhone(subscriptionId);
7671 if (phone == null) {
7672 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7673 return false;
7674 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007675 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007676 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007677 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7678 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007679 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007680 return isCarrierSupported && isDeviceSupported;
7681 } finally {
7682 Binder.restoreCallingIdentity(identity);
7683 }
Hall Liu98187582018-01-22 19:15:32 -08007684 }
7685
Hall Liuf6668912018-10-31 17:05:23 -07007686 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007687 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7688 * RTT setting, will return true if the device and carrier both support RTT.
7689 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007690 */
7691 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007692 final long identity = Binder.clearCallingIdentity();
7693 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007694 boolean isRttSupported = isRttSupported(subscriptionId);
7695 boolean isUserRttSettingOn = Settings.Secure.getInt(
7696 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7697 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7698 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7699 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007700 } finally {
7701 Binder.restoreCallingIdentity(identity);
7702 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007703 }
7704
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007705 @Deprecated
7706 @Override
7707 public String getDeviceId(String callingPackage) {
7708 return getDeviceIdWithFeature(callingPackage, null);
7709 }
7710
Sanket Padawe7310cc72015-01-14 09:53:20 -08007711 /**
7712 * Returns the unique device ID of phone, for example, the IMEI for
7713 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7714 *
7715 * <p>Requires Permission:
7716 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7717 */
7718 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007719 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007720 try {
7721 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7722 } catch (SecurityException se) {
7723 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7724 throw new SecurityException("Package " + callingPackage + " does not belong to "
7725 + Binder.getCallingUid());
7726 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007727 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007728 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007729 return null;
7730 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007731 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007732 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007733 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007734 return null;
7735 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007736
7737 final long identity = Binder.clearCallingIdentity();
7738 try {
7739 return phone.getDeviceId();
7740 } finally {
7741 Binder.restoreCallingIdentity(identity);
7742 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007743 }
7744
Ping Sunc67b7c22016-03-02 19:16:45 +08007745 /**
7746 * {@hide}
7747 * Returns the IMS Registration Status on a particular subid
7748 *
7749 * @param subId
7750 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007751 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007752 Phone phone = getPhone(subId);
7753 if (phone != null) {
7754 return phone.isImsRegistered();
7755 } else {
7756 return false;
7757 }
7758 }
7759
Santos Cordon7a1885b2015-02-03 11:15:19 -08007760 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007761 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007762 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007763 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007764 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007765 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7766 }
7767 final long identity = Binder.clearCallingIdentity();
7768 try {
7769 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7770 } finally {
7771 Binder.restoreCallingIdentity(identity);
7772 }
7773 }
7774
7775 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007776 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007777 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007778 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007779 mApp,
7780 subscriptionId,
7781 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007782 final long identity = Binder.clearCallingIdentity();
7783 try {
7784 Phone phone = getPhone(subscriptionId);
7785 if (phone == null) {
7786 return null;
7787 }
7788 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7789 } finally {
7790 Binder.restoreCallingIdentity(identity);
7791 }
7792 }
7793
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007794 /**
7795 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007796 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007797 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007798 final long identity = Binder.clearCallingIdentity();
7799 try {
7800 Phone phone = getPhone(subId);
7801 if (phone != null) {
7802 return phone.isWifiCallingEnabled();
7803 } else {
7804 return false;
7805 }
7806 } finally {
7807 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007808 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007809 }
7810
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007811 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007812 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007813 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007814 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007815 final long identity = Binder.clearCallingIdentity();
7816 try {
7817 Phone phone = getPhone(subId);
7818 if (phone != null) {
7819 return phone.isVideoEnabled();
7820 } else {
7821 return false;
7822 }
7823 } finally {
7824 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007825 }
7826 }
7827
7828 /**
7829 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7830 * defined in {@link ImsRegistrationImplBase}.
7831 */
7832 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007833 final long identity = Binder.clearCallingIdentity();
7834 try {
7835 Phone phone = getPhone(subId);
7836 if (phone != null) {
7837 return phone.getImsRegistrationTech();
7838 } else {
7839 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7840 }
7841 } finally {
7842 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007843 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007844 }
7845
Stuart Scott8eef64f2015-04-08 15:13:54 -07007846 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007847 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007848 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007849 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7850 return;
7851 }
Kai Shif70f46f2021-03-03 13:59:46 -08007852 Phone defaultPhone = getDefaultPhone();
7853 if (defaultPhone != null) {
7854 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7855 mApp, getDefaultPhone().getSubId(), "factoryReset");
7856 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007857 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007858
Svet Ganovcc087f82015-05-12 20:35:54 -07007859 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007860 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7861 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007862 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007863 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007864 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007865 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007866 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007867 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007868 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007869 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007870 // There has been issues when Sms raw table somehow stores orphan
7871 // fragments. They lead to garbled message when new fragments come
7872 // in and combined with those stale ones. In case this happens again,
7873 // user can reset all network settings which will clean up this table.
7874 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007875 // Clean up IMS settings as well here.
7876 int slotId = getSlotIndex(subId);
7877 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7878 ImsManager.getInstance(mApp, slotId).factoryReset();
7879 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007880
Kai Shif70f46f2021-03-03 13:59:46 -08007881 if (defaultPhone == null) {
7882 return;
7883 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007884 // Erase modem config if erase modem on network setting is enabled.
7885 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7886 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7887 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007888 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007889 }
Kai Shif70f46f2021-03-03 13:59:46 -08007890
7891 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007892 } finally {
7893 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007894 }
7895 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007896
SongFerngWangfd89b102021-05-27 22:44:54 +08007897 @VisibleForTesting
7898 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7899 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7900 return;
7901 }
7902 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7903 RILConstants.PREFERRED_NETWORK_MODE);
7904 SubscriptionManager.setSubscriptionProperty(subId,
7905 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7906 "user=" + defaultNetworkType);
7907 phone.loadAllowedNetworksFromSubscriptionDatabase();
7908 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7909 defaultNetworkType, null);
7910 }
7911
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007912 private void cleanUpSmsRawTable(Context context) {
7913 ContentResolver resolver = context.getContentResolver();
7914 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7915 resolver.delete(uri, null, null);
7916 }
7917
Narayan Kamath1c496c22015-04-16 14:40:19 +01007918 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007919 public String getSimLocaleForSubscriber(int subId) {
7920 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7921 final Phone phone = getPhone(subId);
7922 if (phone == null) {
7923 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007924 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007925 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007926 final long identity = Binder.clearCallingIdentity();
7927 try {
Jack Yu285100e2022-12-02 22:48:35 -08007928 SubscriptionInfo info;
7929 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7930 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7931 phone.getContext().getOpPackageName(),
7932 phone.getContext().getAttributionTag());
7933 if (info == null) {
7934 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7935 return null;
7936 }
7937 } else {
7938 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7939 phone.getContext().getOpPackageName(),
7940 phone.getContext().getAttributionTag());
7941 if (info == null) {
7942 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7943 return null;
7944 }
chen xu6291c472019-02-04 12:55:53 -08007945 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007946 // Try and fetch the locale from the carrier properties or from the SIM language
7947 // preferences (EF-PL and EF-LI)...
7948 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007949 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007950 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7951 if (localeFromDefaultSim != null) {
7952 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7953 if (DBG) log("Using locale from subId: " + subId + " locale: "
7954 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007955 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007956 } else {
7957 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007958 }
7959 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007960
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007961 // The SIM language preferences only store a language (e.g. fr = French), not an
7962 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7963 // the SIM and carrier preferences does not include a country we add the country
7964 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007965 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007966 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007967 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08007968 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007969 }
7970
7971 if (DBG) log("No locale found - returning null");
7972 return null;
7973 } finally {
7974 Binder.restoreCallingIdentity(identity);
7975 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007976 }
7977
tom hsu0b59d292022-09-29 23:49:21 +08007978 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08007979 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08007980 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08007981 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08007982 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08007983 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
7984 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08007985 Locale.forLanguageTag(localeTag).getCountry())) {
7986 return localeTag;
7987 }
7988 }
7989 return inputLocale.toLanguageTag();
7990 }
7991
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007992 /**
7993 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7994 */
7995 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08007996 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7997 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
7998 mApp.getOpPackageName(), mApp.getAttributionTag());
7999 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008000 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008001 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008002 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008003
Gary Jian3aa9a762022-01-24 16:41:19 +08008004 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8005 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008006
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008007 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008008 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8009 * representing the state of the modem.
8010 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008011 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8012 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008013 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008014 */
8015 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008016 public void requestModemActivityInfo(ResultReceiver result) {
8017 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008018 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008019
8020 final long identity = Binder.clearCallingIdentity();
8021 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008022 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008023 } finally {
8024 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008025 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008026 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008027
Siddharth Rayb8114062018-06-17 15:02:38 -07008028 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
8029 // less than total activity duration.
8030 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8031 if (info == null) {
8032 return false;
8033 }
8034 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008035 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
8036 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8037
Siddharth Rayb8114062018-06-17 15:02:38 -07008038 return (info.isValid()
8039 && (info.getSleepTimeMillis() <= activityDurationMs)
8040 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07008041 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07008042 && (totalTxTimeMs <= activityDurationMs));
8043 }
8044
Gary Jian3aa9a762022-01-24 16:41:19 +08008045 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8046 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8047 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8048 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8049
8050 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8051 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8052 }
8053
8054 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8055 mLastModemActivityInfo.setReceiveTimeMillis(
8056 rat,
8057 freq,
8058 info.getReceiveTimeMillis(rat, freq)
8059 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8060 }
8061
8062 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8063 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8064 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8065 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8066
8067 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8068 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8069 }
8070 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8071 mLastModemActivityInfo.setReceiveTimeMillis(
8072 rat,
8073 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8074 }
8075
8076 /**
8077 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8078 * @param info recent ModemActivityInfo
8079 */
8080 private void mergeModemActivityInfo(ModemActivityInfo info) {
8081 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008082 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008083 boolean matched;
8084 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8085 matched = false;
8086 int rat = info.getSpecificInfoRat(i);
8087 int freq = info.getSpecificInfoFrequencyRange(i);
8088 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8089 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8090 //if it already exists
8091 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8092 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8093 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8094 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8095 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8096 updateLastModemActivityInfo(info, rat, freq);
8097 matched = true;
8098 }
8099 } else {
8100 updateLastModemActivityInfo(info, rat);
8101 matched = true;
8102 }
8103 }
8104 }
8105
8106 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008107 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008108 new ActivityStatsTechSpecificInfo(
8109 rat,
8110 freq,
8111 info.getTransmitTimeMillis(rat, freq),
8112 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008113 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008114 }
8115 }
8116 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8117 mLastModemActivitySpecificInfo =
8118 new ActivityStatsTechSpecificInfo[merged.size()];
8119 merged.toArray(mLastModemActivitySpecificInfo);
8120
8121 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8122 mLastModemActivityInfo.setSleepTimeMillis(
8123 info.getSleepTimeMillis()
8124 + mLastModemActivityInfo.getSleepTimeMillis());
8125 mLastModemActivityInfo.setIdleTimeMillis(
8126 info.getIdleTimeMillis()
8127 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008128
8129 mLastModemActivityInfo =
8130 new ModemActivityInfo(
8131 mLastModemActivityInfo.getTimestampMillis(),
8132 mLastModemActivityInfo.getSleepTimeMillis(),
8133 mLastModemActivityInfo.getIdleTimeMillis(),
8134 mLastModemActivitySpecificInfo);
8135 }
8136
8137 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8138 ActivityStatsTechSpecificInfo[] info) {
8139 int infoSize = info.length;
8140 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8141 for (int i = 0; i < infoSize; i++) {
8142 ret[i] = new ActivityStatsTechSpecificInfo(
8143 info[i].getRat(), info[i].getFrequencyRange(),
8144 info[i].getTransmitTimeMillis(),
8145 (int) info[i].getReceiveTimeMillis());
8146 }
8147 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008148 }
8149
Jack Yu85bd38a2015-11-09 11:34:32 -08008150 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008151 * Returns the service state information on specified subscription.
8152 */
8153 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008154 public ServiceState getServiceStateForSubscriber(int subId,
8155 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8156 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008157 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008158 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008159 return null;
8160 }
8161
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008162 boolean hasFinePermission = false;
8163 boolean hasCoarsePermission = false;
8164 if (!renounceFineLocationAccess) {
8165 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8166 LocationAccessPolicy.checkLocationPermission(mApp,
8167 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8168 .setCallingPackage(callingPackage)
8169 .setCallingFeatureId(callingFeatureId)
8170 .setCallingPid(Binder.getCallingPid())
8171 .setCallingUid(Binder.getCallingUid())
8172 .setMethod("getServiceStateForSubscriber")
8173 .setLogAsInfo(true)
8174 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8175 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8176 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8177 .build());
8178 hasFinePermission =
8179 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8180 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008181
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008182 if (!renounceCoarseLocationAccess) {
8183 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8184 LocationAccessPolicy.checkLocationPermission(mApp,
8185 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8186 .setCallingPackage(callingPackage)
8187 .setCallingFeatureId(callingFeatureId)
8188 .setCallingPid(Binder.getCallingPid())
8189 .setCallingUid(Binder.getCallingUid())
8190 .setMethod("getServiceStateForSubscriber")
8191 .setLogAsInfo(true)
8192 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8193 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8194 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8195 .build());
8196 hasCoarsePermission =
8197 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8198 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008199
Jack Yu479f40e2020-10-27 21:29:25 -07008200 final Phone phone = getPhone(subId);
8201 if (phone == null) {
8202 return null;
8203 }
8204
Jordan Liu0f2bc442020-11-18 16:47:37 -08008205 final long identity = Binder.clearCallingIdentity();
8206
Jack Yu479f40e2020-10-27 21:29:25 -07008207 boolean isCallingPackageDataService = phone.getDataServicePackages()
8208 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008209 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008210 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008211 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8212 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8213 .getSubscriptionInfoInternal(subId);
8214 if (subInfo == null || !subInfo.isActive()) {
8215 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8216 + "subId=" + subId);
8217 return null;
8218 }
8219 } else {
8220 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8221 callingFeatureId)) {
8222 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8223 + "subId=" + subId);
8224 return null;
8225 }
Jordan Liuc437b192020-08-17 10:59:12 -07008226 }
8227
Hall Liuf19c44f2018-11-27 14:38:17 -08008228 ServiceState ss = phone.getServiceState();
8229
8230 // Scrub out the location info in ServiceState depending on what level of access
8231 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008232 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008233 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8234 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008235 } finally {
8236 Binder.restoreCallingIdentity(identity);
8237 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008238 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008239
8240 /**
8241 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8242 *
8243 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8244 * voicemail ringtone.
8245 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8246 * PhoneAccount.
8247 */
8248 @Override
8249 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008250 final long identity = Binder.clearCallingIdentity();
8251 try {
8252 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8253 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008254 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008255 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008256
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008257 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8258 } finally {
8259 Binder.restoreCallingIdentity(identity);
8260 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008261 }
8262
8263 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008264 * Sets the per-account voicemail ringtone.
8265 *
8266 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8267 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8268 *
8269 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8270 * voicemail ringtone.
8271 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8272 * PhoneAccount.
8273 */
8274 @Override
8275 public void setVoicemailRingtoneUri(String callingPackage,
8276 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008277 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008278 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008279 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8280 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008281 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8282 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8283 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008284 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008285
8286 final long identity = Binder.clearCallingIdentity();
8287 try {
8288 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8289 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008290 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008291 }
8292 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8293 } finally {
8294 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008295 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008296 }
8297
8298 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008299 * Returns whether vibration is set for voicemail notification in Phone settings.
8300 *
8301 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8302 * voicemail vibration setting.
8303 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8304 */
8305 @Override
8306 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008307 final long identity = Binder.clearCallingIdentity();
8308 try {
8309 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8310 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008311 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008312 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008313
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008314 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8315 } finally {
8316 Binder.restoreCallingIdentity(identity);
8317 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008318 }
8319
Youhan Wange64578a2016-05-02 15:32:42 -07008320 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008321 * Sets the per-account voicemail vibration.
8322 *
8323 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8324 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8325 *
8326 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8327 * voicemail vibration setting.
8328 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8329 * specific PhoneAccount.
8330 */
8331 @Override
8332 public void setVoicemailVibrationEnabled(String callingPackage,
8333 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008334 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008335 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008336 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8337 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008338 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8339 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8340 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008341 }
8342
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008343 final long identity = Binder.clearCallingIdentity();
8344 try {
8345 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8346 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008347 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008348 }
8349 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8350 } finally {
8351 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008352 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008353 }
8354
8355 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008356 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8357 *
8358 * @throws SecurityException if the caller does not have the required permission
8359 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008360 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008361 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008362 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008363 }
8364
8365 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008366 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8367 * permission.
8368 *
8369 * @throws SecurityException if the caller does not have the required permission
8370 */
8371 private void enforceSendSmsPermission() {
8372 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8373 }
8374
8375 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008376 * Make sure either called from same process as self (phone) or IPC caller has interact across
8377 * users permission.
8378 *
8379 * @throws SecurityException if the caller does not have the required permission
8380 */
8381 private void enforceInteractAcrossUsersPermission(String message) {
8382 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8383 }
8384
8385 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008386 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008387 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008388 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008389 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008390 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008391 final long identity = Binder.clearCallingIdentity();
8392 try {
8393 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008394 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008395 if (componentName == null) {
8396 throw new SecurityException(
8397 "Caller not current active visual voicemail package[null]");
8398 }
8399 String vvmPackage = componentName.getPackageName();
8400 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008401 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008402 }
8403 } finally {
8404 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008405 }
8406 }
8407
8408 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008409 * Return the application ID for the app type.
8410 *
8411 * @param subId the subscription ID that this request applies to.
8412 * @param appType the uicc app type.
8413 * @return Application ID for specificied app type, or null if no uicc.
8414 */
8415 @Override
8416 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008417 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008418 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008419
8420 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008421 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008422 if (phone == null) {
8423 return null;
8424 }
8425 String aid = null;
8426 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008427 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008428 .getApplicationByType(appType).getAid();
8429 } catch (Exception e) {
8430 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8431 }
8432 return aid;
8433 } finally {
8434 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008435 }
Youhan Wange64578a2016-05-02 15:32:42 -07008436 }
8437
Youhan Wang4001d252016-05-11 10:29:41 -07008438 /**
8439 * Return the Electronic Serial Number.
8440 *
8441 * @param subId the subscription ID that this request applies to.
8442 * @return ESN or null if error.
8443 */
8444 @Override
8445 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008446 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008447 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008448
8449 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008450 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008451 if (phone == null) {
8452 return null;
8453 }
8454 String esn = null;
8455 try {
8456 esn = phone.getEsn();
8457 } catch (Exception e) {
8458 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8459 }
8460 return esn;
8461 } finally {
8462 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008463 }
Youhan Wang4001d252016-05-11 10:29:41 -07008464 }
8465
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008466 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008467 * Return the Preferred Roaming List Version.
8468 *
8469 * @param subId the subscription ID that this request applies to.
8470 * @return PRLVersion or null if error.
8471 */
8472 @Override
8473 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008474 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008475 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008476
8477 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008478 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008479 if (phone == null) {
8480 return null;
8481 }
8482 String cdmaPrlVersion = null;
8483 try {
8484 cdmaPrlVersion = phone.getCdmaPrlVersion();
8485 } catch (Exception e) {
8486 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8487 }
8488 return cdmaPrlVersion;
8489 } finally {
8490 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008491 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008492 }
8493
8494 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008495 * Get snapshot of Telephony histograms
8496 * @return List of Telephony histograms
8497 * @hide
8498 */
8499 @Override
8500 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008501 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8502 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008503
8504 final long identity = Binder.clearCallingIdentity();
8505 try {
8506 return RIL.getTelephonyRILTimingHistograms();
8507 } finally {
8508 Binder.restoreCallingIdentity(identity);
8509 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008510 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008511
8512 /**
8513 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008514 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8515 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008516 * Require system privileges. In the future we may add this to carrier APIs.
8517 *
Michele Berionne482f8202018-11-27 18:57:59 -08008518 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008519 */
8520 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008521 @TelephonyManager.SetCarrierRestrictionResult
8522 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008523 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008524 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008525
Michele Berionne482f8202018-11-27 18:57:59 -08008526 if (carrierRestrictionRules == null) {
8527 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008528 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008529
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008530 final long identity = Binder.clearCallingIdentity();
8531 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008532 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008533 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008534 } finally {
8535 Binder.restoreCallingIdentity(identity);
8536 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008537 }
8538
8539 /**
8540 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008541 * Get the allowed carrier list and the excluded carrier list, including the priority between
8542 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008543 * Require system privileges. In the future we may add this to carrier APIs.
8544 *
Michele Berionne482f8202018-11-27 18:57:59 -08008545 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008546 */
8547 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008548 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008549 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008550 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008551
8552 final long identity = Binder.clearCallingIdentity();
8553 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008554 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8555 if (response instanceof CarrierRestrictionRules) {
8556 return (CarrierRestrictionRules) response;
8557 }
8558 // Response is an Exception of some kind,
8559 // which is signalled to the user as a NULL retval
8560 return null;
8561 } catch (Exception e) {
8562 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8563 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008564 } finally {
8565 Binder.restoreCallingIdentity(identity);
8566 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008567 }
8568
fionaxu59545b42016-05-25 15:53:37 -07008569 /**
fionaxu59545b42016-05-25 15:53:37 -07008570 * Action set from carrier signalling broadcast receivers to enable/disable radio
8571 * @param subId the subscription ID that this action applies to.
8572 * @param enabled control enable or disable radio.
8573 * {@hide}
8574 */
8575 @Override
8576 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8577 enforceModifyPermission();
8578 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008579
8580 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008581 if (phone == null) {
8582 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8583 return;
8584 }
8585 try {
8586 phone.carrierActionSetRadioEnabled(enabled);
8587 } catch (Exception e) {
8588 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008589 } finally {
8590 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008591 }
8592 }
8593
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008594 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008595 * Enable or disable Voice over NR (VoNR)
8596 * @param subId the subscription ID that this action applies to.
8597 * @param enabled enable or disable VoNR.
8598 * @return operation result.
8599 */
8600 @Override
8601 public int setVoNrEnabled(int subId, boolean enabled) {
8602 enforceModifyPermission();
8603 final Phone phone = getPhone(subId);
8604
8605 final long identity = Binder.clearCallingIdentity();
8606 if (phone == null) {
8607 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8608 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8609 }
8610
8611 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8612 try {
8613 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8614 workSource);
8615 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008616
8617 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8618 if (DBG) {
8619 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8620 }
8621 SubscriptionManager.setSubscriptionProperty(
8622 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8623 (enabled ? "1" : "0"));
8624 }
8625
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008626 return result;
8627 } finally {
8628 Binder.restoreCallingIdentity(identity);
8629 }
8630 }
8631
8632 /**
8633 * Is voice over NR enabled
8634 * @return true if VoNR is enabled else false
8635 */
8636 @Override
8637 public boolean isVoNrEnabled(int subId) {
8638 enforceReadPrivilegedPermission("isVoNrEnabled");
8639 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8640 final long identity = Binder.clearCallingIdentity();
8641 try {
8642 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8643 null, subId, workSource);
8644 if (DBG) log("isVoNrEnabled: " + isEnabled);
8645 return isEnabled;
8646 } finally {
8647 Binder.restoreCallingIdentity(identity);
8648 }
8649 }
8650
8651 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008652 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8653 * network status based on which carrier apps could apply actions accordingly,
8654 * enable/disable default url handler for example.
8655 *
8656 * @param subId the subscription ID that this action applies to.
8657 * @param report control start/stop reporting the default network status.
8658 * {@hide}
8659 */
8660 @Override
8661 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8662 enforceModifyPermission();
8663 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008664
8665 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008666 if (phone == null) {
8667 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8668 return;
8669 }
8670 try {
8671 phone.carrierActionReportDefaultNetworkStatus(report);
8672 } catch (Exception e) {
8673 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008674 } finally {
8675 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008676 }
8677 }
8678
8679 /**
fionaxud9622282017-07-17 17:51:30 -07008680 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8681 * @param subId the subscription ID that this action applies to.
8682 * {@hide}
8683 */
8684 @Override
8685 public void carrierActionResetAll(int subId) {
8686 enforceModifyPermission();
8687 final Phone phone = getPhone(subId);
8688 if (phone == null) {
8689 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8690 return;
8691 }
8692 try {
8693 phone.carrierActionResetAll();
8694 } catch (Exception e) {
8695 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8696 }
8697 }
8698
8699 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008700 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8701 * bug report is being generated.
8702 */
8703 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008704 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008705 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8706 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008707 writer.println("Permission Denial: can't dump Phone from pid="
8708 + Binder.getCallingPid()
8709 + ", uid=" + Binder.getCallingUid()
8710 + "without permission "
8711 + android.Manifest.permission.DUMP);
8712 return;
8713 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008714 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008715 }
Jack Yueb89b242016-06-22 13:27:47 -07008716
Brad Ebingerdac2f002018-04-03 15:17:52 -07008717 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008718 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8719 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8720 @NonNull String[] args) {
8721 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8722 this, in.getFileDescriptor(), out.getFileDescriptor(),
8723 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008724 }
8725
Jack Yueb89b242016-06-22 13:27:47 -07008726 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008727 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008728 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008729 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008730 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008731 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008732 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008733 */
8734 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008735 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008736 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008737 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8738 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8739 try {
8740 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008741 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008742 } catch (SecurityException se) {
8743 enforceModifyPermission();
8744 }
8745 } else {
8746 enforceModifyPermission();
8747 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008748
8749 final long identity = Binder.clearCallingIdentity();
8750 try {
8751 Phone phone = getPhone(subId);
8752 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008753 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8754 phone.carrierActionSetMeteredApnsEnabled(enabled);
8755 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008756 phone.getDataSettingsManager().setDataEnabled(
8757 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008758 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008759 }
8760 } finally {
8761 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008762 }
8763 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008764
8765 /**
8766 * Get Client request stats
8767 * @return List of Client Request Stats
8768 * @hide
8769 */
8770 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008771 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8772 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008773 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008774 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008775 return null;
8776 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008777 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008778
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008779 final long identity = Binder.clearCallingIdentity();
8780 try {
8781 if (phone != null) {
8782 return phone.getClientRequestStats();
8783 }
8784
8785 return null;
8786 } finally {
8787 Binder.restoreCallingIdentity(identity);
8788 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008789 }
8790
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008791 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008792 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008793 if (uid == Process.ROOT_UID && packageName == null) {
8794 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8795 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8796 // exception. ROOT_UID seems not to have a valid package name returned by
8797 // PackageManager, so just fake it here to avoid issues when running telephony shell
8798 // commands that plumb through the RIL as root, like so:
8799 // $ adb root
8800 // $ adb shell cmd phone ...
8801 packageName = "root";
8802 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008803 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008804 }
Jack Yueb4124c2017-02-16 15:32:43 -08008805
8806 /**
Grace Chen70990072017-03-24 17:21:30 -07008807 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008808 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008809 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008810 * @param state State of SIM (power down, power up, pass through)
8811 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8812 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8813 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008814 *
8815 **/
8816 @Override
Grace Chen70990072017-03-24 17:21:30 -07008817 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008818 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008819 Phone phone = PhoneFactory.getPhone(slotIndex);
8820
vagdeviaf9a5b92018-08-15 16:01:53 -07008821 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8822
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008823 final long identity = Binder.clearCallingIdentity();
8824 try {
8825 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008826 phone.setSimPowerState(state, null, workSource);
8827 }
8828 } finally {
8829 Binder.restoreCallingIdentity(identity);
8830 }
8831 }
8832
8833 /**
8834 * Set SIM card power state.
8835 *
8836 * @param slotIndex SIM slot id.
8837 * @param state State of SIM (power down, power up, pass through)
8838 * @param callback callback to trigger after success or failure
8839 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8840 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8841 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8842 *
8843 **/
8844 @Override
8845 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8846 IIntegerConsumer callback) {
8847 enforceModifyPermission();
8848 Phone phone = PhoneFactory.getPhone(slotIndex);
8849
8850 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8851
8852 final long identity = Binder.clearCallingIdentity();
8853 try {
8854 if (phone != null) {
8855 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8856 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008857 }
8858 } finally {
8859 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008860 }
8861 }
Shuo Qiandd210312017-04-12 22:11:33 +00008862
Tyler Gunn65d45c22017-06-05 11:22:26 -07008863 private boolean isUssdApiAllowed(int subId) {
8864 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008865 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008866 if (configManager == null) {
8867 return false;
8868 }
8869 PersistableBundle pb = configManager.getConfigForSubId(subId);
8870 if (pb == null) {
8871 return false;
8872 }
8873 return pb.getBoolean(
8874 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8875 }
8876
Shuo Qiandd210312017-04-12 22:11:33 +00008877 /**
8878 * Check if phone is in emergency callback mode
8879 * @return true if phone is in emergency callback mode
8880 * @param subId sub id
8881 */
goneil9c5f4872017-12-05 14:07:56 -08008882 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008883 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008884 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008885 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008886
8887 final long identity = Binder.clearCallingIdentity();
8888 try {
8889 if (phone != null) {
8890 return phone.isInEcm();
8891 } else {
8892 return false;
8893 }
8894 } finally {
8895 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008896 }
8897 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008898
8899 /**
8900 * Get the current signal strength information for the given subscription.
8901 * Because this information is not updated when the device is in a low power state
8902 * it should not be relied-upon to be current.
8903 * @param subId Subscription index
8904 * @return the most recent cached signal strength info from the modem
8905 */
8906 @Override
8907 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008908 final long identity = Binder.clearCallingIdentity();
8909 try {
8910 Phone p = getPhone(subId);
8911 if (p == null) {
8912 return null;
8913 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008914
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008915 return p.getSignalStrength();
8916 } finally {
8917 Binder.restoreCallingIdentity(identity);
8918 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008919 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008920
Pengquan Meng77b7f132018-08-22 14:49:57 -07008921 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008922 * Get the current modem radio state for the given slot.
8923 * @param slotIndex slot index.
8924 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008925 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008926 * @return the current radio power state from the modem
8927 */
8928 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008929 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008930 Phone phone = PhoneFactory.getPhone(slotIndex);
8931 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008932 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8933 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008934 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8935 }
8936
8937 final long identity = Binder.clearCallingIdentity();
8938 try {
8939 return phone.getRadioPowerState();
8940 } finally {
8941 Binder.restoreCallingIdentity(identity);
8942 }
8943 }
8944 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8945 }
8946
8947 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008948 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8949 *
8950 * <p>Requires one of the following permissions:
8951 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008952 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008953 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8954 * privileges.
8955 *
8956 * @param subId subscription id
8957 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8958 * {@code false}.
8959 */
8960 @Override
8961 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008962 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008963 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008964 try {
8965 mApp.enforceCallingOrSelfPermission(
8966 android.Manifest.permission.ACCESS_NETWORK_STATE,
8967 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008968 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008969 mApp.enforceCallingOrSelfPermission(
8970 permission.READ_BASIC_PHONE_STATE, functionName);
8971 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008972 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008973 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008974 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008975 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008976
Pengquan Menga1bb6272018-09-06 09:59:22 -07008977 boolean isEnabled = false;
8978 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008979 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008980 Phone phone = getPhone(subId);
8981 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008982 } finally {
8983 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008984 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008985 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008986 }
8987
8988
8989 /**
8990 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8991 *
8992 * <p> Requires permission:
8993 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8994 * privileges.
8995 *
8996 * @param subId subscription id
8997 * @param isEnabled {@code true} means enable, {@code false} means disable.
8998 */
8999 @Override
9000 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009001 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9002 mApp, subId, "setDataRoamingEnabled");
9003
Pengquan Menga1bb6272018-09-06 09:59:22 -07009004 final long identity = Binder.clearCallingIdentity();
9005 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009006 Phone phone = getPhone(subId);
9007 if (phone != null) {
9008 phone.setDataRoamingEnabled(isEnabled);
9009 }
9010 } finally {
9011 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009012 }
9013 }
9014
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009015 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009016 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009017 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009018 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009019 mApp, subId, "isManualNetworkSelectionAllowed");
9020
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009021 boolean isAllowed = true;
9022 final long identity = Binder.clearCallingIdentity();
9023 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009024 Phone phone = getPhone(subId);
9025 if (phone != null) {
9026 isAllowed = phone.isCspPlmnEnabled();
9027 }
9028 } finally {
9029 Binder.restoreCallingIdentity(identity);
9030 }
9031 return isAllowed;
9032 }
9033
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009034 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9035 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009036 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009037 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009038 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009039 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9040 if (phone == null) {
9041 return false;
9042 }
9043 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9044 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9045 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009046 }
9047
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009048 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009049 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009050 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009051 mApp.getSystemService(AppOpsManager.class)
9052 .checkPackage(Binder.getCallingUid(), callingPackage);
9053
Jordan Liu1e142fc2019-04-22 15:10:43 -07009054 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009055 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009056 try {
9057 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009058 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009059 } catch (SecurityException e) {
9060 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9061 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009062 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009063 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009064 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009065 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009066 }
sandeepjsb6c87872021-09-27 15:34:44 +00009067 // checking compatibility, if calling app's target SDK is T and beyond.
9068 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9069 Binder.getCallingUid())) {
9070 isIccIdAccessRestricted = true;
9071 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009072 final long identity = Binder.clearCallingIdentity();
9073 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009074 UiccController uiccController = UiccController.getInstance();
9075 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009076 if (hasReadPermission) {
9077 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009078 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009079
9080 // Remove private info if the caller doesn't have access
9081 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9082 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009083 //setting the value after compatibility check
9084 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009085 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9086 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009087 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009088 if (card == null) {
9089 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009090 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009091 continue;
9092 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009093 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9094 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009095 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009096 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009097 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009098 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9099 for (UiccPortInfo portInfo : portInfos) {
9100 UiccPort port = uiccController.getUiccPortForSlot(
9101 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9102 if (port == null) {
9103 // assume no access if port is null
9104 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9105 continue;
9106 }
9107 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9108 uiccPortInfos.add(portInfo);
9109 } else {
9110 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9111 }
9112 }
9113 filteredInfos.add(new UiccCardInfo(
9114 cardInfo.isEuicc(),
9115 cardInfo.getCardId(),
9116 null,
9117 cardInfo.getPhysicalSlotIndex(),
9118 cardInfo.isRemovable(),
9119 cardInfo.isMultipleEnabledProfilesSupported(),
9120 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009121 }
9122 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009123 } finally {
9124 Binder.restoreCallingIdentity(identity);
9125 }
9126 }
9127
sandeepjsb6c87872021-09-27 15:34:44 +00009128 /**
9129 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9130 * generally private and require carrier privileges to view.
9131 *
9132 * @hide
9133 */
9134 @NonNull
9135 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9136 List<UiccPortInfo> portinfo = new ArrayList<>();
9137 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9138 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9139 }
9140 return new UiccCardInfo(
9141 cardInfo.isEuicc(),
9142 cardInfo.getCardId(),
9143 null,
9144 cardInfo.getPhysicalSlotIndex(),
9145 cardInfo.isRemovable(),
9146 cardInfo.isMultipleEnabledProfilesSupported(),
9147 portinfo
9148 );
9149 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009150
sandeepjsb6c87872021-09-27 15:34:44 +00009151 /**
9152 * @hide
9153 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9154 * These values are generally private and require carrier privileges to view.
9155 */
9156 @NonNull
9157 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9158 return new UiccPortInfo(
9159 UiccPortInfo.ICCID_REDACTED,
9160 portInfo.getPortIndex(),
9161 portInfo.getLogicalSlotIndex(),
9162 portInfo.isActive()
9163 );
9164 }
9165 @Override
9166 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009167 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009168 mApp.getSystemService(AppOpsManager.class)
9169 .checkPackage(Binder.getCallingUid(), callingPackage);
9170
sandeepjsb6c87872021-09-27 15:34:44 +00009171 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009172
Aman Guptaf3c90b32022-03-17 04:54:16 +00009173 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9174 // we are reading iccId which is PII data.
9175 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009176
9177 // checking compatibility, if calling app's target SDK is T and beyond.
9178 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9179 Binder.getCallingUid())) {
9180 isLogicalSlotAccessRestricted = true;
9181 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009182 final long identity = Binder.clearCallingIdentity();
9183 try {
9184 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009185 if (slots == null || slots.length == 0) {
9186 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009187 return null;
9188 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009189 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9190 for (int i = 0; i < slots.length; i++) {
9191 UiccSlot slot = slots[i];
9192 if (slot == null) {
9193 continue;
9194 }
9195
Jordan Liu7be7e652019-05-06 18:55:02 +00009196 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009197 UiccCard card = slot.getUiccCard();
9198 if (card != null) {
9199 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009200 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009201 cardId = slot.getEid();
9202 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009203 // If cardId is null, use iccId of default port as cardId.
9204 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009205 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009206 }
9207
Jordan Liu857451f2019-05-09 16:35:35 -07009208 if (cardId != null) {
9209 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9210 // if cardId is an EID, it's all digits so this is fine
9211 cardId = IccUtils.stripTrailingFs(cardId);
9212 }
9213
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009214 int cardState = 0;
9215 switch (slot.getCardState()) {
9216 case CARDSTATE_ABSENT:
9217 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9218 break;
9219 case CARDSTATE_PRESENT:
9220 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9221 break;
9222 case CARDSTATE_ERROR:
9223 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9224 break;
9225 case CARDSTATE_RESTRICTED:
9226 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9227 break;
9228 default:
9229 break;
9230
9231 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009232 List<UiccPortInfo> portInfos = new ArrayList<>();
9233 int[] portIndexes = slot.getPortList();
9234 for (int portIdx : portIndexes) {
9235 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009236 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009237 portInfos.add(new UiccPortInfo(iccId, portIdx,
9238 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009239 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009240 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009241 slot.isEuicc(),
9242 cardId,
9243 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009244 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009245 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009246 //setting the value after compatibility check
9247 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009248 }
9249 return infos;
9250 } finally {
9251 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009252 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009253 }
9254
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009255 /* Returns null if doesn't have read permission or carrier privilege access. */
9256 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9257 boolean hasReadPermission) {
9258 String iccId = slot.getIccId(portIndex);
9259 if (hasReadPermission) { // if has read permission
9260 return iccId;
9261 } else {
9262 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9263 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9264 // if no read permission, checking carrier privilege access
9265 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9266 return iccId;
9267 }
9268 }
9269 }
9270 // No read permission or carrier privilege access.
9271 return UiccPortInfo.ICCID_REDACTED;
9272 }
9273
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009274 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009275 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009276 public boolean switchSlots(int[] physicalSlots) {
9277 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009278
9279 final long identity = Binder.clearCallingIdentity();
9280 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009281 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9282 for (int i = 0; i < physicalSlots.length; i++) {
9283 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9284 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9285 physicalSlots[i], i));
9286 }
9287 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009288 } finally {
9289 Binder.restoreCallingIdentity(identity);
9290 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009291 }
Jack Yu4c988042018-02-27 15:30:01 -08009292
9293 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009294 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9295 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9296 enforceModifyPermission();
9297
9298 final long identity = Binder.clearCallingIdentity();
9299 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009300 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009301 } finally {
9302 Binder.restoreCallingIdentity(identity);
9303 }
9304 }
9305
9306 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009307 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009308 final long identity = Binder.clearCallingIdentity();
9309 try {
9310 return UiccController.getInstance().getCardIdForDefaultEuicc();
9311 } finally {
9312 Binder.restoreCallingIdentity(identity);
9313 }
9314 }
9315
Pengquan Meng85728fb2018-03-12 16:31:21 -07009316 /**
goneil47ffb6e2018-04-06 15:40:58 -07009317 * A test API to reload the UICC profile.
9318 *
9319 * <p>Requires that the calling app has permission
9320 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9321 * @hide
9322 */
9323 @Override
9324 public void refreshUiccProfile(int subId) {
9325 enforceModifyPermission();
9326
9327 final long identity = Binder.clearCallingIdentity();
9328 try {
9329 Phone phone = getPhone(subId);
9330 if (phone == null) {
9331 return;
9332 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009333 UiccPort uiccPort = phone.getUiccPort();
9334 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009335 return;
9336 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009337 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009338 if (uiccProfile == null) {
9339 return;
9340 }
9341 uiccProfile.refresh();
9342 } finally {
9343 Binder.restoreCallingIdentity(identity);
9344 }
9345 }
9346
9347 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009348 * Returns false if the mobile data is disabled by default, otherwise return true.
9349 */
9350 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009351 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009352 }
9353
9354 /**
9355 * Returns true if the data roaming is enabled by default, i.e the system property
9356 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9357 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9358 */
9359 private boolean getDefaultDataRoamingEnabled(int subId) {
9360 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009361 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009362 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009363 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9364 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9365 return isDataRoamingEnabled;
9366 }
9367
9368 /**
9369 * Returns the default network type for the given {@code subId}, if the default network type is
9370 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9371 */
9372 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009373 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009374 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009375 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9376 return list.get(phoneId);
9377 }
9378 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009379 }
fionaxua13278b2018-03-21 00:08:13 -07009380
9381 @Override
9382 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009383 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009384 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009385
9386 final long identity = Binder.clearCallingIdentity();
9387 try {
9388 final Phone phone = getPhone(subId);
9389 if (phone == null) {
9390 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9391 return;
9392 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009393 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9394 if (cpt != null) {
9395 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9396 }
9397 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009398 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9399 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009400 if (carrierPrivilegeRules == null) {
9401 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9402 } else {
9403 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9404 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009405 } finally {
9406 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009407 }
fionaxua13278b2018-03-21 00:08:13 -07009408 }
9409
9410 @Override
9411 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009412 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009413
9414 final long identity = Binder.clearCallingIdentity();
9415 try {
9416 final Phone phone = getPhone(subId);
9417 if (phone == null) {
9418 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9419 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9420 }
9421 return phone.getCarrierIdListVersion();
9422 } finally {
9423 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009424 }
fionaxua13278b2018-03-21 00:08:13 -07009425 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009426
9427 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009428 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9429 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009430 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009431 mApp, subId, callingPackage, callingFeatureId,
9432 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009433 return -1;
9434 }
9435
9436 final long identity = Binder.clearCallingIdentity();
9437 try {
9438 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9439 } finally {
9440 Binder.restoreCallingIdentity(identity);
9441 }
9442 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009443
9444 @Override
9445 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009446 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009447 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009448 mApp, subId, "getCdmaRoamingMode");
9449
9450 final long identity = Binder.clearCallingIdentity();
9451 try {
9452 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9453 } finally {
9454 Binder.restoreCallingIdentity(identity);
9455 }
9456 }
9457
9458 @Override
9459 public boolean setCdmaRoamingMode(int subId, int mode) {
9460 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9461 mApp, subId, "setCdmaRoamingMode");
9462
9463 final long identity = Binder.clearCallingIdentity();
9464 try {
9465 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9466 } finally {
9467 Binder.restoreCallingIdentity(identity);
9468 }
9469 }
9470
9471 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009472 public int getCdmaSubscriptionMode(int subId) {
9473 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009474 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009475 mApp, subId, "getCdmaSubscriptionMode");
9476
9477 final long identity = Binder.clearCallingIdentity();
9478 try {
9479 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9480 } finally {
9481 Binder.restoreCallingIdentity(identity);
9482 }
9483 }
9484
9485 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009486 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9487 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9488 mApp, subId, "setCdmaSubscriptionMode");
9489
9490 final long identity = Binder.clearCallingIdentity();
9491 try {
9492 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9493 } finally {
9494 Binder.restoreCallingIdentity(identity);
9495 }
9496 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009497
sqianc5eccab2018-10-19 18:46:41 -07009498 @Override
sqian8c685422019-02-22 15:55:18 -08009499 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009500 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009501 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009502 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9503 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009504 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9505 }
9506 final long identity = Binder.clearCallingIdentity();
9507 try {
sqian854d44b2018-12-12 16:48:18 -08009508 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9509 for (Phone phone: PhoneFactory.getPhones()) {
9510 if (phone.getEmergencyNumberTracker() != null
9511 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9512 emergencyNumberListInternal.put(
9513 phone.getSubId(),
9514 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9515 }
sqian11b7a0e2018-12-05 18:48:28 -08009516 }
sqian854d44b2018-12-12 16:48:18 -08009517 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009518 } finally {
9519 Binder.restoreCallingIdentity(identity);
9520 }
sqianc5eccab2018-10-19 18:46:41 -07009521 }
9522
9523 @Override
sqian8c685422019-02-22 15:55:18 -08009524 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009525 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009526 if (!exactMatch) {
9527 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009528 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009529 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009530 }
9531 final long identity = Binder.clearCallingIdentity();
9532 try {
sqian854d44b2018-12-12 16:48:18 -08009533 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009534 //Note: we ignore passed in param exactMatch. We can remove it once
9535 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009536 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009537 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009538 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009539 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009540 }
sqian11b7a0e2018-12-05 18:48:28 -08009541 }
9542 return false;
9543 } finally {
9544 Binder.restoreCallingIdentity(identity);
9545 }
9546 }
9547
sqianf4ca7ed2019-01-15 18:32:07 -08009548 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009549 * Start emergency callback mode for GsmCdmaPhone for testing.
9550 */
9551 @Override
9552 public void startEmergencyCallbackMode() {
9553 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9554 "startEmergencyCallbackMode");
9555 enforceModifyPermission();
9556 final long identity = Binder.clearCallingIdentity();
9557 try {
9558 for (Phone phone : PhoneFactory.getPhones()) {
9559 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9560 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9561 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9562 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9563 gsmCdmaPhone.obtainMessage(
9564 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9565 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9566 }
9567 }
9568 } finally {
9569 Binder.restoreCallingIdentity(identity);
9570 }
9571 }
9572
9573 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009574 * Update emergency number list for test mode.
9575 */
9576 @Override
9577 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9578 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9579 "updateEmergencyNumberListTestMode");
9580
9581 final long identity = Binder.clearCallingIdentity();
9582 try {
9583 for (Phone phone: PhoneFactory.getPhones()) {
9584 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9585 if (tracker != null) {
9586 tracker.executeEmergencyNumberTestModeCommand(action, num);
9587 }
9588 }
9589 } finally {
9590 Binder.restoreCallingIdentity(identity);
9591 }
9592 }
9593
9594 /**
9595 * Get the full emergency number list for test mode.
9596 */
9597 @Override
9598 public List<String> getEmergencyNumberListTestMode() {
9599 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9600 "getEmergencyNumberListTestMode");
9601
9602 final long identity = Binder.clearCallingIdentity();
9603 try {
9604 Set<String> emergencyNumbers = new HashSet<>();
9605 for (Phone phone: PhoneFactory.getPhones()) {
9606 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9607 if (tracker != null) {
9608 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9609 emergencyNumbers.add(num.getNumber());
9610 }
9611 }
9612 }
9613 return new ArrayList<>(emergencyNumbers);
9614 } finally {
9615 Binder.restoreCallingIdentity(identity);
9616 }
9617 }
9618
chen xud6b45bd2018-10-30 22:27:10 -07009619 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009620 public int getEmergencyNumberDbVersion(int subId) {
9621 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9622
9623 final long identity = Binder.clearCallingIdentity();
9624 try {
9625 final Phone phone = getPhone(subId);
9626 if (phone == null) {
9627 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9628 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9629 }
9630 return phone.getEmergencyNumberDbVersion();
9631 } finally {
9632 Binder.restoreCallingIdentity(identity);
9633 }
9634 }
9635
9636 @Override
9637 public void notifyOtaEmergencyNumberDbInstalled() {
9638 enforceModifyPermission();
9639
9640 final long identity = Binder.clearCallingIdentity();
9641 try {
9642 for (Phone phone: PhoneFactory.getPhones()) {
9643 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9644 if (tracker != null) {
9645 tracker.updateOtaEmergencyNumberDatabase();
9646 }
9647 }
9648 } finally {
9649 Binder.restoreCallingIdentity(identity);
9650 }
9651 }
9652
9653 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009654 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009655 enforceActiveEmergencySessionPermission();
9656
9657 final long identity = Binder.clearCallingIdentity();
9658 try {
9659 for (Phone phone: PhoneFactory.getPhones()) {
9660 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9661 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009662 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9663 }
9664 }
9665 } finally {
9666 Binder.restoreCallingIdentity(identity);
9667 }
9668 }
9669
9670 @Override
9671 public void resetOtaEmergencyNumberDbFilePath() {
9672 enforceActiveEmergencySessionPermission();
9673
9674 final long identity = Binder.clearCallingIdentity();
9675 try {
9676 for (Phone phone: PhoneFactory.getPhones()) {
9677 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9678 if (tracker != null) {
9679 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009680 }
9681 }
9682 } finally {
9683 Binder.restoreCallingIdentity(identity);
9684 }
9685 }
9686
9687 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009688 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9689 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9690 Phone phone = getPhone(subId);
9691 if (phone == null) {
9692 return null;
9693 }
9694 final long identity = Binder.clearCallingIdentity();
9695 try {
9696 UiccProfile profile = UiccController.getInstance()
9697 .getUiccProfileForPhone(phone.getPhoneId());
9698 if (profile != null) {
9699 return profile.getCertsFromCarrierPrivilegeAccessRules();
9700 }
9701 } finally {
9702 Binder.restoreCallingIdentity(identity);
9703 }
9704 return null;
9705 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009706
9707 /**
9708 * Enable or disable a modem stack.
9709 */
9710 @Override
9711 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9712 enforceModifyPermission();
9713
9714 final long identity = Binder.clearCallingIdentity();
9715 try {
9716 Phone phone = PhoneFactory.getPhone(slotIndex);
9717 if (phone == null) {
9718 return false;
9719 } else {
9720 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9721 }
9722 } finally {
9723 Binder.restoreCallingIdentity(identity);
9724 }
9725 }
Michelecea4cf22018-12-21 15:00:11 -08009726
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009727 /**
9728 * Whether a modem stack is enabled or not.
9729 */
9730 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009731 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9732 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009733 Phone phone = PhoneFactory.getPhone(slotIndex);
9734 if (phone == null) return false;
9735
9736 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009737 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9738 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009739 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9740 }
9741
9742 final long identity = Binder.clearCallingIdentity();
9743 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009744 try {
9745 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9746 } catch (NoSuchElementException ex) {
9747 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9748 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009749 } finally {
9750 Binder.restoreCallingIdentity(identity);
9751 }
9752 }
9753
Michelecea4cf22018-12-21 15:00:11 -08009754 @Override
Michele0ea7d782019-03-19 14:58:42 -07009755 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009756 enforceModifyPermission();
9757
9758 final long identity = Binder.clearCallingIdentity();
9759 try {
9760 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009761 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009762 .commit();
9763 } finally {
9764 Binder.restoreCallingIdentity(identity);
9765 }
9766 }
9767
9768 @Override
Michele0ea7d782019-03-19 14:58:42 -07009769 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009770 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009771 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009772 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9773 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009774 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009775 }
Michelecea4cf22018-12-21 15:00:11 -08009776
9777 final long identity = Binder.clearCallingIdentity();
9778 try {
Michele0ea7d782019-03-19 14:58:42 -07009779 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009780 } finally {
9781 Binder.restoreCallingIdentity(identity);
9782 }
9783 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009784
Michele0ea7d782019-03-19 14:58:42 -07009785 @TelephonyManager.IsMultiSimSupportedResult
9786 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009787 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9788 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9789 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009790 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9791 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009792 }
9793 // Check if the hardware supports multisim functionality. If usage of multisim is not
9794 // supported by the modem, indicate that it is restricted.
9795 PhoneCapability staticCapability =
9796 mPhoneConfigurationManager.getStaticPhoneCapability();
9797 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009798 loge("isMultiSimSupportedInternal: no static configuration available");
9799 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009800 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009801 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009802 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9803 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009804 }
9805 // Check if support of multiple SIMs is restricted by carrier
9806 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009807 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009808 }
9809
Michele0ea7d782019-03-19 14:58:42 -07009810 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009811 }
9812
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009813 /**
9814 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009815 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9816 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9817 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009818 * @param numOfSims number of active sims we want to switch to
9819 */
9820 @Override
9821 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009822 if (numOfSims == 1) {
9823 enforceModifyPermission();
9824 } else {
9825 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9826 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9827 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009828 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009829
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009830 try {
Michele30b57b22019-03-01 12:01:14 -08009831 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009832 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009833 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9834 return;
9835 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009836 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9837 } finally {
9838 Binder.restoreCallingIdentity(identity);
9839 }
9840 }
9841
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009842 @Override
9843 public boolean isApplicationOnUicc(int subId, int appType) {
9844 enforceReadPrivilegedPermission("isApplicationOnUicc");
9845 Phone phone = getPhone(subId);
9846 if (phone == null) {
9847 return false;
9848 }
9849 final long identity = Binder.clearCallingIdentity();
9850 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009851 UiccPort uiccPort = phone.getUiccPort();
9852 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009853 return false;
9854 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009855 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009856 if (uiccProfile == null) {
9857 return false;
9858 }
9859 if (TelephonyManager.APPTYPE_SIM <= appType
9860 && appType <= TelephonyManager.APPTYPE_ISIM) {
9861 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9862 }
9863 return false;
9864 } finally {
9865 Binder.restoreCallingIdentity(identity);
9866 }
9867 }
9868
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009869 /**
chen xub4baa772019-04-03 10:23:41 -07009870 * Get whether making changes to modem configurations will trigger reboot.
9871 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009872 */
9873 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009874 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9875 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009876 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009877 mApp, subId, callingPackage, callingFeatureId,
9878 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009879 return false;
9880 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009881 final long identity = Binder.clearCallingIdentity();
9882 try {
9883 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9884 } finally {
9885 Binder.restoreCallingIdentity(identity);
9886 }
9887 }
9888
Nathan Harold29f5f052019-02-15 13:41:57 -08009889 private void updateModemStateMetrics() {
9890 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9891 // TODO: check the state for each modem if the api is ready.
9892 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9893 }
9894
Pengquan Meng3889a572019-01-23 11:16:29 -08009895 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009896 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009897 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009898 // Verify that the callingPackage belongs to the calling UID
9899 mApp.getSystemService(AppOpsManager.class)
9900 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009901 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009902 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009903 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009904 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9905 if (slotInfos != null) {
9906 for (int i = 0; i < slotInfos.length; i++) {
9907 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9908 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9909 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9910 portInfo.getLogicalSlotIndex()));
9911 }
9912 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009913 }
9914 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009915 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009916 } finally {
9917 Binder.restoreCallingIdentity(identity);
9918 }
9919 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009920
9921 /**
9922 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009923 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009924 */
jimsunf9ec1622022-09-13 21:18:43 +08009925 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009926 @Override
9927 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009928 return getHalVersion(HAL_SERVICE_RADIO);
9929 }
9930
9931 /**
9932 * Get the HAL Version of a specific service
9933 */
9934 @Override
9935 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009936 Phone phone = getDefaultPhone();
9937 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009938 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009939 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9940 return hv.major * 100 + hv.minor;
9941 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009942
9943 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009944 * Get the current calling package name.
9945 * @return the current calling package name
9946 */
9947 @Override
9948 public String getCurrentPackageName() {
9949 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9950 }
9951
9952 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009953 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9954 * corresponding network requests on a subId.
9955 *
9956 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009957 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009958 * 2) APN is un-metered for this subscription, or
9959 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009960 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009961 *
9962 * @return whether data is allowed for a apn type.
9963 *
9964 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009965 */
9966 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009967 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009968 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9969 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009970
9971 // Now that all security checks passes, perform the operation as ourselves.
9972 final long identity = Binder.clearCallingIdentity();
9973 try {
9974 Phone phone = getPhone(subId);
9975 if (phone == null) return false;
9976
Jack Yu27422a52022-03-21 10:38:05 -07009977 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009978 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009979 isMetered = phone.getDataNetworkController().getDataConfigManager()
9980 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9981 phone.getServiceState().getDataRoaming());
9982 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009983 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009984 } finally {
9985 Binder.restoreCallingIdentity(identity);
9986 }
9987 }
9988
9989 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009990 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009991 enforceReadPrivilegedPermission("isApnMetered");
9992
9993 // Now that all security checks passes, perform the operation as ourselves.
9994 final long identity = Binder.clearCallingIdentity();
9995 try {
9996 Phone phone = getPhone(subId);
9997 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -07009998 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
9999 DataUtils.apnTypeToNetworkCapability(apnType),
10000 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010001 } finally {
10002 Binder.restoreCallingIdentity(identity);
10003 }
10004 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010005
10006 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010007 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10008 int subscriptionId, IBooleanConsumer resultCallback) {
10009 enforceModifyPermission();
10010 long token = Binder.clearCallingIdentity();
10011 try {
10012 Phone phone = getPhone(subscriptionId);
10013 if (phone == null) {
10014 try {
10015 if (resultCallback != null) {
10016 resultCallback.accept(false);
10017 }
10018 } catch (RemoteException e) {
10019 // ignore
10020 }
10021 return;
10022 }
10023 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10024 Pair.create(specifiers, (x) -> {
10025 try {
10026 if (resultCallback != null) {
10027 resultCallback.accept(x);
10028 }
10029 } catch (RemoteException e) {
10030 // ignore
10031 }
10032 });
10033 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10034 } finally {
10035 Binder.restoreCallingIdentity(token);
10036 }
10037 }
10038
10039 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010040 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10041 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010042 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010043 mApp, subId, "getSystemSelectionChannels");
10044 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10045 final long identity = Binder.clearCallingIdentity();
10046 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010047 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10048 if (result instanceof IllegalStateException) {
10049 throw (IllegalStateException) result;
10050 }
10051 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010052 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10053 return specifiers;
10054 } finally {
10055 Binder.restoreCallingIdentity(identity);
10056 }
10057 }
10058
10059 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010060 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010061 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010062 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010063 }
10064
10065 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010066 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10067 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010068 if (callingPackage == null) {
10069 callingPackage = getCurrentPackageName();
10070 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010071 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10072 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010073 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10074 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010075 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10076 }
10077 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10078 Intent intent = new Intent();
10079 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10080 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10081 // Bring up choose default SMS subscription dialog right now
10082 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10083 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10084 mApp.startActivity(intent);
10085 }
chen xud5ca2d52019-05-28 15:20:57 -070010086
10087 @Override
10088 public String getMmsUAProfUrl(int subId) {
10089 //TODO investigate if this API should require proper permission check in R b/133791609
10090 final long identity = Binder.clearCallingIdentity();
10091 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010092 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10093 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10094 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10095 return carrierUAProfUrl;
10096 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010097 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10098 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010099 } finally {
10100 Binder.restoreCallingIdentity(identity);
10101 }
10102 }
10103
10104 @Override
10105 public String getMmsUserAgent(int subId) {
10106 //TODO investigate if this API should require proper permission check in R b/133791609
10107 final long identity = Binder.clearCallingIdentity();
10108 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010109 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10110 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10111 if (!TextUtils.isEmpty(carrierUserAgent)) {
10112 return carrierUserAgent;
10113 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010114 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10115 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010116 } finally {
10117 Binder.restoreCallingIdentity(identity);
10118 }
10119 }
Jack Yub07d4972019-05-28 16:12:25 -070010120
10121 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010122 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10123 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010124
Jack Yub07d4972019-05-28 16:12:25 -070010125 final long identity = Binder.clearCallingIdentity();
10126 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010127 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010128 if (phone == null) return false;
10129
Ling Maf188d502022-09-16 15:22:36 -070010130 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010131 } finally {
10132 Binder.restoreCallingIdentity(identity);
10133 }
10134 }
10135
10136 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010137 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010138 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010139 enforceModifyPermission();
10140
changbettyd5c246e2019-12-24 15:40:37 +080010141 final long identity = Binder.clearCallingIdentity();
10142 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010143 Phone phone = getPhone(subscriptionId);
10144 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010145
Ling Maf188d502022-09-16 15:22:36 -070010146 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010147 } finally {
10148 Binder.restoreCallingIdentity(identity);
10149 }
10150 }
10151
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010152 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010153 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010154 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10155 * otherwise.
10156 */
10157 @Override
10158 public void setCepEnabled(boolean isCepEnabled) {
10159 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10160
10161 final long identity = Binder.clearCallingIdentity();
10162 try {
10163 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10164 for (Phone phone : PhoneFactory.getPhones()) {
10165 Phone defaultPhone = phone.getImsPhone();
10166 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10167 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10168 ImsPhoneCallTracker imsPhoneCallTracker =
10169 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10170 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10171 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10172 + imsPhone.getMsisdn());
10173 }
10174 }
10175 } finally {
10176 Binder.restoreCallingIdentity(identity);
10177 }
10178 }
allenwtsu46dcc572020-01-08 18:24:03 +080010179
10180 /**
10181 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10182 *
10183 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10184 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10185 * before being read.
10186 */
10187 @Override
10188 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10189 isCompressed) {
10190 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10191 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010192 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10193 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10194 }
10195 if (!isImsAvailableOnDevice()) {
10196 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10197 "IMS not available on device.");
10198 }
10199
10200 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010201 try {
Hui Wang761a6682020-10-31 05:12:53 +000010202 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10203 } finally {
10204 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010205 }
10206 }
zoey chene02881a2019-12-30 16:11:23 +080010207
10208 @Override
10209 public boolean isIccLockEnabled(int subId) {
10210 enforceReadPrivilegedPermission("isIccLockEnabled");
10211
10212 // Now that all security checks passes, perform the operation as ourselves.
10213 final long identity = Binder.clearCallingIdentity();
10214 try {
10215 Phone phone = getPhone(subId);
10216 if (phone != null && phone.getIccCard() != null) {
10217 return phone.getIccCard().getIccLockEnabled();
10218 } else {
10219 return false;
10220 }
10221 } finally {
10222 Binder.restoreCallingIdentity(identity);
10223 }
10224 }
10225
10226 /**
10227 * Set the ICC pin lock enabled or disabled.
10228 *
10229 * @return an integer representing the status of IccLock enabled or disabled in the following
10230 * three cases:
10231 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10232 * successfully.
10233 * - Positive number and zero for remaining password attempts.
10234 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10235 *
10236 */
10237 @Override
10238 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10239 enforceModifyPermission();
10240
10241 Phone phone = getPhone(subId);
10242 if (phone == null) {
10243 return 0;
10244 }
10245 // Now that all security checks passes, perform the operation as ourselves.
10246 final long identity = Binder.clearCallingIdentity();
10247 try {
10248 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10249 new Pair<Boolean, String>(enabled, password), phone, null);
10250 return attemptsRemaining;
10251
10252 } catch (Exception e) {
10253 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10254 } finally {
10255 Binder.restoreCallingIdentity(identity);
10256 }
10257 return 0;
10258 }
10259
10260 /**
10261 * Change the ICC password used in ICC pin lock.
10262 *
10263 * @return an integer representing the status of IccLock changed in the following three cases:
10264 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10265 * - Positive number and zero for remaining password attempts.
10266 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10267 *
10268 */
10269 @Override
10270 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10271 enforceModifyPermission();
10272
10273 Phone phone = getPhone(subId);
10274 if (phone == null) {
10275 return 0;
10276 }
10277 // Now that all security checks passes, perform the operation as ourselves.
10278 final long identity = Binder.clearCallingIdentity();
10279 try {
10280 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10281 new Pair<String, String>(oldPassword, newPassword), phone, null);
10282 return attemptsRemaining;
10283
10284 } catch (Exception e) {
10285 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10286 } finally {
10287 Binder.restoreCallingIdentity(identity);
10288 }
10289 return 0;
10290 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010291
10292 /**
10293 * Request for receiving user activity notification
10294 */
10295 @Override
10296 public void requestUserActivityNotification() {
10297 if (!mNotifyUserActivity.get()
10298 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10299 mNotifyUserActivity.set(true);
10300 }
10301 }
10302
10303 /**
10304 * Called when userActivity is signalled in the power manager.
10305 * This is safe to call from any thread, with any window manager locks held or not.
10306 */
10307 @Override
10308 public void userActivity() {
10309 // ***************************************
10310 // * Inherited from PhoneWindowManager *
10311 // ***************************************
10312 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10313 // WITH ITS LOCKS HELD.
10314 //
10315 // This code must be VERY careful about the locks
10316 // it acquires.
10317 // In fact, the current code acquires way too many,
10318 // and probably has lurking deadlocks.
10319
10320 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10321 throw new SecurityException("Only the OS may call notifyUserActivity()");
10322 }
10323
10324 if (mNotifyUserActivity.getAndSet(false)) {
10325 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10326 USER_ACTIVITY_NOTIFICATION_DELAY);
10327 }
10328 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010329
10330 @Override
10331 public boolean canConnectTo5GInDsdsMode() {
10332 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10333 }
Jack Yud10cdd42020-09-28 20:28:01 -070010334
10335 @Override
10336 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10337 String callingFeatureId) {
10338 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10339 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10340 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10341 }
10342
10343 Phone phone = getPhone(subId);
10344 if (phone == null) {
10345 throw new RuntimeException("phone is not available");
10346 }
10347 // Now that all security checks passes, perform the operation as ourselves.
10348 final long identity = Binder.clearCallingIdentity();
10349 try {
10350 return phone.getEquivalentHomePlmns();
10351 } finally {
10352 Binder.restoreCallingIdentity(identity);
10353 }
10354 }
Daniel Bright59e67312020-11-13 11:49:37 -080010355
10356 @Override
10357 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010358 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10359 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010360 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010361 if (radioInterfaceCapabilities == null) {
10362 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010363 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010364 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010365 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010366
Hui Wang641e81c2020-10-12 12:14:23 -070010367 @Override
10368 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10369 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010370 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10371 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10372 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10373 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10374 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010375 if (DBG) {
10376 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10377 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10378 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10379 }
10380
10381 if (!SubscriptionManager.isValidSubscriptionId(subId)
10382 || appType < TelephonyManager.APPTYPE_UNKNOWN
10383 || appType > TelephonyManager.APPTYPE_ISIM
10384 || nafUrl == null || securityProtocol == null || callback == null) {
10385 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10386 if (callback != null) {
10387 try {
10388 callback.onAuthenticationFailure(
10389 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10390 } catch (RemoteException exception) {
10391 log("Fail to notify onAuthenticationFailure due to " + exception);
10392 }
10393 return;
10394 }
10395 }
10396
10397 final long token = Binder.clearCallingIdentity();
10398 try {
10399 getGbaManager(subId).bootstrapAuthenticationRequest(
10400 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10401 forceBootStrapping, callback));
10402 } finally {
10403 Binder.restoreCallingIdentity(token);
10404 }
10405 }
10406
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010407 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010408 * Attempts to set the radio power state for all phones for thermal reason.
10409 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010410 * requested radio power state will actually be set. See {@link
10411 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10412 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010413 * @param enable {@code true} if trying to turn radio on.
10414 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10415 * false}.
10416 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010417 private boolean setRadioPowerForThermal(boolean enable) {
10418 boolean isPhoneAvailable = false;
10419 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10420 Phone phone = PhoneFactory.getPhone(i);
10421 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010422 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010423 isPhoneAvailable = true;
10424 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010425 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010426
10427 // return true if successfully informed the phone object about the thermal radio power
10428 // request.
10429 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010430 }
10431
10432 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010433 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010434 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10435 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10436 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10437 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10438 throw new IllegalArgumentException("modem does not support data throttling");
10439 }
10440
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010441 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10442 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010443 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010444 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10445 }
10446
Sarah Chinecc78c42022-03-31 21:16:48 -070010447 setDataEnabledForReason(
10448 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010449
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010450 if (isDataThrottlingSupported) {
10451 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010452 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010453 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10454 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10455 } else if (thermalMitigationResult
10456 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010457 log("Modem likely does not support data throttling on secondary carrier. Data " +
10458 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10459 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010460 }
10461 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010462 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010463
10464 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010465 }
10466
Jack Nudelman644b91a2021-03-12 14:09:48 -080010467 private static List<String> getThermalMitigationAllowlist(Context context) {
10468 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10469 for (String pckg : context.getResources()
10470 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10471 sThermalMitigationAllowlistedPackages.add(pckg);
10472 }
10473 }
10474
10475 return sThermalMitigationAllowlistedPackages;
10476 }
10477
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010478 private boolean isAnyPhoneInEmergencyState() {
10479 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10480 if (tm.isInEmergencyCall()) {
10481 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10482 return true;
10483 }
10484 for (Phone phone : PhoneFactory.getPhones()) {
10485 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10486 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10487 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10488 + phone.isInEcm());
10489 return true;
10490 }
10491 }
10492
10493 return false;
10494 }
10495
Jack Nudelman644b91a2021-03-12 14:09:48 -080010496 /**
10497 * Used by shell commands to add an authorized package name for thermal mitigation.
10498 * @param packageName name of package to be allowlisted
10499 * @param context
10500 */
10501 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10502 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10503 sThermalMitigationAllowlistedPackages.add(packageName);
10504 }
10505
10506 /**
10507 * Used by shell commands to remove an authorized package name for thermal mitigation.
10508 * @param packageName name of package to remove from allowlist
10509 * @param context
10510 */
10511 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10512 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10513 sThermalMitigationAllowlistedPackages.remove(packageName);
10514 }
10515
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010516 /**
10517 * Thermal mitigation request to control functionalities at modem.
10518 *
10519 * @param subId the id of the subscription.
10520 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010521 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010522 *
10523 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10524 */
10525 @Override
10526 @ThermalMitigationResult
10527 public int sendThermalMitigationRequest(
10528 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010529 ThermalMitigationRequest thermalMitigationRequest,
10530 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010531 enforceModifyPermission();
10532
Jack Nudelman644b91a2021-03-12 14:09:48 -080010533 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10534 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10535 .contains(callingPackage)) {
10536 throw new SecurityException("Calling package must be configured in the device config. "
10537 + "calling package: " + callingPackage);
10538 }
10539
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010540 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10541 final long identity = Binder.clearCallingIdentity();
10542
10543 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10544 try {
10545 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10546 switch (thermalMitigationAction) {
10547 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10548 thermalMitigationResult =
10549 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010550 thermalMitigationRequest.getDataThrottlingRequest(),
10551 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010552 break;
10553 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10554 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10555 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10556 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10557 }
10558
10559 // Ensure that radio is on. If not able to power on due to phone being
10560 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010561 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010562 thermalMitigationResult =
10563 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10564 break;
10565 }
10566
10567 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010568 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010569 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10570 break;
10571 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10572 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10573 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10574 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10575 }
10576
10577 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10578 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010579 Phone phone = getPhone(subId);
10580 if (phone == null) {
10581 thermalMitigationResult =
10582 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10583 break;
10584 }
10585
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010586 TelephonyConnectionService service =
10587 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010588 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010589 Log.e(LOG_TAG, "An emergency call is pending");
10590 thermalMitigationResult =
10591 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10592 break;
10593 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010594 thermalMitigationResult =
10595 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10596 break;
10597 }
10598 } else {
10599 thermalMitigationResult =
10600 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10601 break;
10602 }
10603
10604 // Turn radio off. If not able to power off due to phone being unavailable,
10605 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010606 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010607 thermalMitigationResult =
10608 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10609 break;
10610 }
10611 thermalMitigationResult =
10612 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10613 break;
10614 default:
10615 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10616 + "not exist. Requested action: " + thermalMitigationAction);
10617 }
10618 } catch (IllegalArgumentException e) {
10619 throw e;
10620 } catch (Exception e) {
10621 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10622 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10623 } finally {
10624 Binder.restoreCallingIdentity(identity);
10625 }
10626
10627 if (DBG) {
10628 log("thermalMitigationRequest returning with thermalMitigationResult: "
10629 + thermalMitigationResult);
10630 }
10631
10632 return thermalMitigationResult;
10633 }
Hui Wang641e81c2020-10-12 12:14:23 -070010634
10635 /**
10636 * Set the GbaService Package Name that Telephony will bind to.
10637 *
10638 * @param subId The sim that the GbaService is associated with.
10639 * @param packageName The name of the package to be replaced with.
10640 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10641 */
10642 @Override
10643 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10644 enforceModifyPermission();
10645
10646 final long identity = Binder.clearCallingIdentity();
10647 try {
10648 return getGbaManager(subId).overrideServicePackage(packageName);
10649 } finally {
10650 Binder.restoreCallingIdentity(identity);
10651 }
10652 }
10653
10654 /**
10655 * Return the package name of the currently bound GbaService.
10656 *
10657 * @param subId The sim that the GbaService is associated with.
10658 * @return the package name of the GbaService configuration, null if GBA is not supported.
10659 */
10660 @Override
10661 public String getBoundGbaService(int subId) {
10662 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10663
10664 final long identity = Binder.clearCallingIdentity();
10665 try {
10666 return getGbaManager(subId).getServicePackage();
10667 } finally {
10668 Binder.restoreCallingIdentity(identity);
10669 }
10670 }
10671
10672 /**
10673 * Set the release time for telephony to unbind GbaService.
10674 *
10675 * @param subId The sim that the GbaService is associated with.
10676 * @param interval The release time to unbind GbaService by millisecond.
10677 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10678 */
10679 @Override
10680 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10681 enforceModifyPermission();
10682
10683 final long identity = Binder.clearCallingIdentity();
10684 try {
10685 return getGbaManager(subId).overrideReleaseTime(interval);
10686 } finally {
10687 Binder.restoreCallingIdentity(identity);
10688 }
10689 }
10690
10691 /**
10692 * Return the release time for telephony to unbind GbaService.
10693 *
10694 * @param subId The sim that the GbaService is associated with.
10695 * @return The release time to unbind GbaService by millisecond.
10696 */
10697 @Override
10698 public int getGbaReleaseTime(int subId) {
10699 enforceReadPrivilegedPermission("getGbaReleaseTime");
10700
10701 final long identity = Binder.clearCallingIdentity();
10702 try {
10703 return getGbaManager(subId).getReleaseTime();
10704 } finally {
10705 Binder.restoreCallingIdentity(identity);
10706 }
10707 }
10708
10709 private GbaManager getGbaManager(int subId) {
10710 GbaManager instance = GbaManager.getInstance(subId);
10711 if (instance == null) {
10712 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10713 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10714 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10715 }
10716 return instance;
10717 }
Hui Wang761a6682020-10-31 05:12:53 +000010718
10719 /**
10720 * indicate whether the device and the carrier can support
10721 * RCS VoLTE single registration.
10722 */
10723 @Override
10724 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010725 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10726 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10727 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10728 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010729
10730 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10731 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10732 }
10733
10734 final long identity = Binder.clearCallingIdentity();
10735 try {
10736 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10737 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010738 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10739 if (isCapable != null) {
10740 return isCapable;
10741 }
Hui Wang761a6682020-10-31 05:12:53 +000010742 }
Hui Wang67af90e2021-06-04 16:57:15 -070010743 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10744 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010745 } finally {
10746 Binder.restoreCallingIdentity(identity);
10747 }
10748 }
10749
10750 /**
10751 * Register RCS provisioning callback.
10752 */
10753 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010754 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010755 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010756 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010757 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010758 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10759 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010760
10761 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10762 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10763 }
10764 if (!isImsAvailableOnDevice()) {
10765 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10766 "IMS not available on device.");
10767 }
10768
10769 final long identity = Binder.clearCallingIdentity();
10770 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010771 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010772 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010773 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10774 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010775 }
Hui Wang761a6682020-10-31 05:12:53 +000010776 } finally {
10777 Binder.restoreCallingIdentity(identity);
10778 }
10779 }
10780
10781 /**
10782 * Unregister RCS provisioning callback.
10783 */
10784 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010785 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010786 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010787 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010788 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010789 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10790 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010791
10792 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10793 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10794 }
10795 if (!isImsAvailableOnDevice()) {
10796 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10797 "IMS not available on device.");
10798 }
10799
10800 final long identity = Binder.clearCallingIdentity();
10801 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010802 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010803 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010804 } finally {
10805 Binder.restoreCallingIdentity(identity);
10806 }
10807 }
10808
10809 /**
10810 * trigger RCS reconfiguration.
10811 */
10812 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010813 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10814 "triggerRcsReconfiguration",
10815 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010816
10817 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10818 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10819 }
10820 if (!isImsAvailableOnDevice()) {
10821 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10822 "IMS not available on device.");
10823 }
10824
10825 final long identity = Binder.clearCallingIdentity();
10826 try {
10827 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10828 } finally {
10829 Binder.restoreCallingIdentity(identity);
10830 }
10831 }
10832
10833 /**
10834 * Provide the client configuration parameters of the RCS application.
10835 */
10836 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010837 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10838 "setRcsClientConfiguration",
10839 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010840
10841 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10842 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10843 }
10844 if (!isImsAvailableOnDevice()) {
10845 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10846 "IMS not available on device.");
10847 }
10848
10849 final long identity = Binder.clearCallingIdentity();
10850
10851 try {
10852 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10853 if (configBinder == null) {
10854 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010855 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10856 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010857 } else {
10858 configBinder.setRcsClientConfiguration(rcc);
10859 }
joonhunshin3e154242021-09-17 06:33:39 +000010860
10861 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10862 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010863 } catch (RemoteException e) {
10864 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010865 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10866 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010867 } finally {
10868 Binder.restoreCallingIdentity(identity);
10869 }
10870 }
10871
10872 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010873 * Enables or disables the test mode for RCS VoLTE single registration.
10874 */
10875 @Override
10876 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10877 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10878 "setRcsSingleRegistrationTestModeEnabled");
10879
10880 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10881 }
10882
10883 /**
10884 * Gets the test mode for RCS VoLTE single registration.
10885 */
10886 @Override
10887 public boolean getRcsSingleRegistrationTestModeEnabled() {
10888 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10889 "getRcsSingleRegistrationTestModeEnabled");
10890
10891 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10892 }
10893
10894 /**
Hui Wang761a6682020-10-31 05:12:53 +000010895 * Overrides the config of RCS VoLTE single registration enabled for the device.
10896 */
10897 @Override
10898 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10899 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10900 "setDeviceSingleRegistrationEnabledOverride");
10901 enforceModifyPermission();
10902
10903 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10904 : Boolean.parseBoolean(enabledStr);
10905 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010906 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010907 }
10908
10909 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010910 * Sends a device to device communication message. Only usable via shell.
10911 * @param message message to send.
10912 * @param value message value.
10913 */
10914 @Override
10915 public void sendDeviceToDeviceMessage(int message, int value) {
10916 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010917 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010918 enforceModifyPermission();
10919
10920 final long identity = Binder.clearCallingIdentity();
10921 try {
10922 TelephonyConnectionService service =
10923 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10924 if (service == null) {
10925 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10926 return;
10927 }
10928 service.sendTestDeviceToDeviceMessage(message, value);
10929 } finally {
10930 Binder.restoreCallingIdentity(identity);
10931 }
10932 }
10933
Tyler Gunnbabbda02021-02-10 11:05:02 -080010934 /**
10935 * Sets the specified device to device transport active.
10936 * @param transport The transport to set active.
10937 */
10938 @Override
10939 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10940 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10941 "setActiveDeviceToDeviceTransport");
10942 enforceModifyPermission();
10943
10944 final long identity = Binder.clearCallingIdentity();
10945 try {
10946 TelephonyConnectionService service =
10947 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10948 if (service == null) {
10949 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10950 return;
10951 }
10952 service.setActiveDeviceToDeviceTransport(transport);
10953 } finally {
10954 Binder.restoreCallingIdentity(identity);
10955 }
10956 }
Tyler Gunn92479152021-01-20 16:30:10 -080010957
Tyler Gunnd4339262021-05-03 14:46:49 -070010958 @Override
10959 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10960 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10961 "setDeviceToDeviceForceEnabled");
10962
10963 final long identity = Binder.clearCallingIdentity();
10964 try {
10965 Arrays.stream(PhoneFactory.getPhones()).forEach(
10966 p -> {
10967 Phone thePhone = p.getImsPhone();
10968 if (thePhone != null && thePhone instanceof ImsPhone) {
10969 ImsPhone imsPhone = (ImsPhone) thePhone;
10970 CallTracker tracker = imsPhone.getCallTracker();
10971 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10972 ImsPhoneCallTracker imsPhoneCallTracker =
10973 (ImsPhoneCallTracker) tracker;
10974 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10975 }
10976 }
10977 }
10978 );
10979 } finally {
10980 Binder.restoreCallingIdentity(identity);
10981 }
10982 }
10983
Tyler Gunn92479152021-01-20 16:30:10 -080010984 /**
Hui Wang761a6682020-10-31 05:12:53 +000010985 * Gets the config of RCS VoLTE single registration enabled for the device.
10986 */
10987 @Override
10988 public boolean getDeviceSingleRegistrationEnabled() {
10989 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10990 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10991 }
10992
10993 /**
10994 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10995 */
10996 @Override
10997 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
10998 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10999 "setCarrierSingleRegistrationEnabledOverride");
11000 enforceModifyPermission();
11001
11002 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11003 : Boolean.parseBoolean(enabledStr);
11004 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11005 subId, enabled);
11006 }
11007
11008 /**
11009 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11010 */
11011 @Override
11012 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11013 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11014 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11015 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011016
11017 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011018 * Overrides the ims feature validation result
11019 */
11020 @Override
11021 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11022 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11023 "setImsFeatureValidationOverride");
11024
11025 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11026 : Boolean.parseBoolean(enabledStr);
11027 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11028 subId, enabled);
11029 }
11030
11031 /**
11032 * Gets the ims feature validation override value
11033 */
11034 @Override
11035 public boolean getImsFeatureValidationOverride(int subId) {
11036 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11037 "getImsFeatureValidationOverride");
11038 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11039 }
11040
11041 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011042 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11043 * their mobile plan.
11044 */
11045 @Override
11046 public String getMobileProvisioningUrl() {
11047 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11048 final long identity = Binder.clearCallingIdentity();
11049 try {
11050 return getDefaultPhone().getMobileProvisioningUrl();
11051 } finally {
11052 Binder.restoreCallingIdentity(identity);
11053 }
11054 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011055
James.cf Linbcdf8b32021-01-14 16:44:13 +080011056 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011057 * Get the EAB contact from the EAB database.
11058 */
11059 @Override
11060 public String getContactFromEab(String contact) {
11061 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11062 enforceModifyPermission();
11063 final long identity = Binder.clearCallingIdentity();
11064 try {
11065 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11066 } finally {
11067 Binder.restoreCallingIdentity(identity);
11068 }
11069 }
11070
11071 /**
Calvin Pana1434322021-07-01 19:27:01 +080011072 * Get the EAB capability from the EAB database.
11073 */
11074 @Override
11075 public String getCapabilityFromEab(String contact) {
11076 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11077 enforceModifyPermission();
11078 final long identity = Binder.clearCallingIdentity();
11079 try {
11080 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11081 } finally {
11082 Binder.restoreCallingIdentity(identity);
11083 }
11084 }
11085
11086 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011087 * Remove the EAB contacts from the EAB database.
11088 */
11089 @Override
11090 public int removeContactFromEab(int subId, String contacts) {
11091 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11092 enforceModifyPermission();
11093 final long identity = Binder.clearCallingIdentity();
11094 try {
11095 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11096 } finally {
11097 Binder.restoreCallingIdentity(identity);
11098 }
11099 }
11100
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011101 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011102 public boolean getDeviceUceEnabled() {
11103 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11104 final long identity = Binder.clearCallingIdentity();
11105 try {
11106 return mApp.getDeviceUceEnabled();
11107 } finally {
11108 Binder.restoreCallingIdentity(identity);
11109 }
11110 }
11111
11112 @Override
11113 public void setDeviceUceEnabled(boolean isEnabled) {
11114 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11115 final long identity = Binder.clearCallingIdentity();
11116 try {
11117 mApp.setDeviceUceEnabled(isEnabled);
11118 } finally {
11119 Binder.restoreCallingIdentity(identity);
11120 }
11121 }
11122
Brad Ebinger14d467f2021-02-12 06:18:28 +000011123 /**
11124 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11125 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11126 */
11127 // Used for SHELL command only right now.
11128 @Override
11129 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11130 List<String> featureTags) {
11131 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11132 "addUceRegistrationOverrideShell");
11133 final long identity = Binder.clearCallingIdentity();
11134 try {
11135 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11136 new ArraySet<>(featureTags));
11137 } catch (ImsException e) {
11138 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11139 } finally {
11140 Binder.restoreCallingIdentity(identity);
11141 }
11142 }
11143
11144 /**
11145 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11146 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11147 */
11148 // Used for SHELL command only right now.
11149 @Override
11150 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11151 List<String> featureTags) {
11152 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11153 "removeUceRegistrationOverrideShell");
11154 final long identity = Binder.clearCallingIdentity();
11155 try {
11156 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11157 new ArraySet<>(featureTags));
11158 } catch (ImsException e) {
11159 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11160 } finally {
11161 Binder.restoreCallingIdentity(identity);
11162 }
11163 }
11164
11165 /**
11166 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11167 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11168 */
11169 // Used for SHELL command only right now.
11170 @Override
11171 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11172 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11173 "clearUceRegistrationOverrideShell");
11174 final long identity = Binder.clearCallingIdentity();
11175 try {
11176 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11177 } catch (ImsException e) {
11178 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11179 } finally {
11180 Binder.restoreCallingIdentity(identity);
11181 }
11182 }
11183
11184 /**
11185 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11186 */
11187 // Used for SHELL command only right now.
11188 @Override
11189 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11190 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11191 "getLatestRcsContactUceCapabilityShell");
11192 final long identity = Binder.clearCallingIdentity();
11193 try {
11194 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11195 } catch (ImsException e) {
11196 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11197 } finally {
11198 Binder.restoreCallingIdentity(identity);
11199 }
11200 }
11201
11202 /**
11203 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11204 * device does not have an active PUBLISH.
11205 */
11206 // Used for SHELL command only right now.
11207 @Override
11208 public String getLastUcePidfXmlShell(int subId) {
11209 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11210 final long identity = Binder.clearCallingIdentity();
11211 try {
11212 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11213 } catch (ImsException e) {
11214 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11215 } finally {
11216 Binder.restoreCallingIdentity(identity);
11217 }
11218 }
11219
James.cf Line8713a42021-04-29 16:04:26 +080011220 /**
11221 * Remove UCE requests cannot be sent to the network status.
11222 */
11223 // Used for SHELL command only right now.
11224 @Override
11225 public boolean removeUceRequestDisallowedStatus(int subId) {
11226 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11227 final long identity = Binder.clearCallingIdentity();
11228 try {
11229 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11230 } catch (ImsException e) {
11231 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11232 } finally {
11233 Binder.restoreCallingIdentity(identity);
11234 }
11235 }
11236
James.cf Lin18bb9002021-05-25 01:37:38 +080011237 /**
11238 * Remove UCE requests cannot be sent to the network status.
11239 */
11240 // Used for SHELL command only.
11241 @Override
11242 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11243 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11244 final long identity = Binder.clearCallingIdentity();
11245 try {
11246 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11247 } catch (ImsException e) {
11248 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11249 } finally {
11250 Binder.restoreCallingIdentity(identity);
11251 }
11252 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011253
James.cf Lin4b784aa2021-01-31 03:25:15 +080011254 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011255 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11256 String callingPackage) {
11257 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11258 mApp, subId, "setSignalStrengthUpdateRequest");
11259
11260 final int callingUid = Binder.getCallingUid();
11261 // Verify that tha callingPackage belongs to the calling UID
11262 mApp.getSystemService(AppOpsManager.class)
11263 .checkPackage(callingUid, callingPackage);
11264
Rambo Wang3607f502021-02-01 21:51:40 -080011265 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011266
11267 final long identity = Binder.clearCallingIdentity();
11268 try {
11269 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11270 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11271
11272 if (result instanceof IllegalStateException) {
11273 throw (IllegalStateException) result;
11274 }
11275 } finally {
11276 Binder.restoreCallingIdentity(identity);
11277 }
11278 }
11279
11280 @Override
11281 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11282 String callingPackage) {
11283 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11284 mApp, subId, "clearSignalStrengthUpdateRequest");
11285
11286 final int callingUid = Binder.getCallingUid();
11287 // Verify that tha callingPackage belongs to the calling UID
11288 mApp.getSystemService(AppOpsManager.class)
11289 .checkPackage(callingUid, callingPackage);
11290
11291 final long identity = Binder.clearCallingIdentity();
11292 try {
11293 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11294 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11295
11296 if (result instanceof IllegalStateException) {
11297 throw (IllegalStateException) result;
11298 }
11299 } finally {
11300 Binder.restoreCallingIdentity(identity);
11301 }
11302 }
11303
Rambo Wang3607f502021-02-01 21:51:40 -080011304 private static void validateSignalStrengthUpdateRequest(Context context,
11305 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011306 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11307 // phone/system process do not have further restriction on request
11308 return;
11309 }
11310
11311 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011312 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011313 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011314 context.enforceCallingOrSelfPermission(
11315 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11316 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011317 }
11318
11319 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011320 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011321 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011322 || info.isEnabled()) {
11323 throw new IllegalArgumentException(
11324 "Only system can set hide fields in SignalThresholdInfo");
11325 }
11326
11327 // Thresholds length for each RAN need in range. This has been validated in
11328 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11329 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11330 final int[] thresholds = info.getThresholds();
11331 Objects.requireNonNull(thresholds);
11332 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11333 || thresholds.length
11334 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11335 throw new IllegalArgumentException(
11336 "thresholds length is out of range: " + thresholds.length);
11337 }
11338 }
11339 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011340
11341 /**
11342 * Gets the current phone capability.
11343 *
11344 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11345 * @return the PhoneCapability which describes the data connection capability of modem.
11346 * It's used to evaluate possible phone config change, for example from single
11347 * SIM device to multi-SIM device.
11348 */
11349 @Override
11350 public PhoneCapability getPhoneCapability() {
11351 enforceReadPrivilegedPermission("getPhoneCapability");
11352 final long identity = Binder.clearCallingIdentity();
11353 try {
11354 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11355 } finally {
11356 Binder.restoreCallingIdentity(identity);
11357 }
11358 }
Michele Berionne5e411512020-11-13 02:36:59 +000011359
11360 /**
11361 * Prepare TelephonyManager for an unattended reboot. The reboot is
11362 * required to be done shortly after the API is invoked.
11363 */
11364 @Override
11365 @TelephonyManager.PrepareUnattendedRebootResult
11366 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011367 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011368 enforceRebootPermission();
11369
11370 final long identity = Binder.clearCallingIdentity();
11371 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011372 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011373 } finally {
11374 Binder.restoreCallingIdentity(identity);
11375 }
11376 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011377
11378 /**
11379 * Request to get the current slicing configuration including URSP rules and
11380 * NSSAIs (configured, allowed and rejected).
11381 *
11382 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11383 */
11384 @Override
11385 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011386 TelephonyPermissions
11387 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11388 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011389
11390 final long identity = Binder.clearCallingIdentity();
11391 try {
11392 Phone phone = getDefaultPhone();
11393 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11394 } finally {
11395 Binder.restoreCallingIdentity(identity);
11396 }
11397 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011398
11399 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011400 * Check whether the given premium capability is available for purchase from the carrier.
11401 *
11402 * @param capability The premium capability to check.
11403 * @param subId The subId to check the premium capability for.
11404 *
11405 * @return Whether the given premium capability is available to purchase.
11406 */
11407 @Override
11408 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11409 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11410 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11411 log("Premium capability "
11412 + TelephonyManager.convertPremiumCapabilityToString(capability)
11413 + " is not available for purchase due to missing permissions.");
11414 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11415 + "permission READ_BASIC_PHONE_STATE.");
11416 }
11417
11418 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011419 if (phone == null) {
11420 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11421 return false;
11422 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011423 final long identity = Binder.clearCallingIdentity();
11424 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011425 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011426 .isPremiumCapabilityAvailableForPurchase(capability);
11427 } finally {
11428 Binder.restoreCallingIdentity(identity);
11429 }
11430 }
11431
11432 /**
11433 * Purchase the given premium capability from the carrier.
11434 *
11435 * @param capability The premium capability to purchase.
11436 * @param callback The result of the purchase request.
11437 * @param subId The subId to purchase the premium capability for.
11438 */
11439 @Override
11440 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11441 log("purchasePremiumCapability: capability="
11442 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11443 + getCurrentPackageName());
11444
11445 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11446 mApp, "purchasePremiumCapability")) {
11447 log("purchasePremiumCapability "
11448 + TelephonyManager.convertPremiumCapabilityToString(capability)
11449 + " failed due to missing permissions.");
11450 throw new SecurityException("purchasePremiumCapability requires permission "
11451 + "READ_BASIC_PHONE_STATE.");
11452 }
11453
11454 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011455 if (phone == null) {
11456 try {
11457 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11458 callback.accept(result);
11459 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11460 } catch (RemoteException e) {
11461 String logStr = "Purchase premium capability "
11462 + TelephonyManager.convertPremiumCapabilityToString(capability)
11463 + " failed due to RemoteException handling null phone: " + e;
11464 if (DBG) log(logStr);
11465 AnomalyReporter.reportAnomaly(
11466 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11467 }
11468 return;
11469 }
Sarah Chin71b3a852022-09-28 15:54:19 -070011470 String appName;
11471 try {
11472 appName = mApp.getPackageManager().getApplicationLabel(mApp.getPackageManager()
11473 .getApplicationInfo(getCurrentPackageName(), 0)).toString();
11474 } catch (PackageManager.NameNotFoundException e) {
11475 appName = "An application";
11476 }
11477 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
11478 new PurchasePremiumCapabilityArgument(capability, appName, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011479 }
11480
11481 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011482 * Register an IMS connection state callback
11483 */
11484 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011485 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11486 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011487 if (feature == ImsFeature.FEATURE_MMTEL) {
11488 // ImsMmTelManager
11489 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11490 TelephonyPermissions
11491 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11492 mApp, subId, "registerImsStateCallback");
11493 } else if (feature == ImsFeature.FEATURE_RCS) {
11494 // ImsRcsManager or SipDelegateManager
11495 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11496 Binder.getCallingUid(), "registerImsStateCallback",
11497 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11498 Manifest.permission.READ_PRECISE_PHONE_STATE,
11499 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11500 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11501 }
11502
11503 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11504 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11505 "IMS not available on device.");
11506 }
11507
11508 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11509 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11510 }
11511
11512 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11513 if (controller == null) {
11514 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11515 "IMS not available on device.");
11516 }
11517
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011518 if (callingPackage == null) {
11519 callingPackage = getCurrentPackageName();
11520 }
11521
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011522 final long token = Binder.clearCallingIdentity();
11523 try {
11524 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011525 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011526 } catch (ImsException e) {
11527 throw new ServiceSpecificException(e.getCode());
11528 } finally {
11529 Binder.restoreCallingIdentity(token);
11530 }
11531 }
11532
11533 /**
11534 * Unregister an IMS connection state callback
11535 */
11536 @Override
11537 public void unregisterImsStateCallback(IImsStateCallback cb) {
11538 final long token = Binder.clearCallingIdentity();
11539 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11540 if (controller == null) {
11541 return;
11542 }
11543 try {
11544 controller.unregisterImsStateCallback(cb);
11545 } finally {
11546 Binder.restoreCallingIdentity(token);
11547 }
11548 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011549
11550 /**
11551 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11552 *
11553 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11554 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11555 * SecurityException.
11556 * If there is current registered network this value will be same as the registered cell
11557 * identity. If the device goes out of service the previous cell identity is cached and
11558 * will be returned. If the cache age of the Cell identity is more than 24 hours
11559 * it will be cleared and null will be returned.
11560 *
11561 */
11562 @Override
11563 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11564 String callingFeatureId) {
11565 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11566 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11567 LocationAccessPolicy.checkLocationPermission(mApp,
11568 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11569 .setCallingPackage(callingPackage)
11570 .setCallingFeatureId(callingFeatureId)
11571 .setCallingPid(Binder.getCallingPid())
11572 .setCallingUid(Binder.getCallingUid())
11573 .setMethod("getLastKnownCellIdentity")
11574 .setLogAsInfo(true)
11575 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11576 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11577 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11578 .build());
11579
11580 boolean hasFinePermission =
11581 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11582 if (!hasFinePermission
11583 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11584 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011585 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011586 }
11587
11588 final long identity = Binder.clearCallingIdentity();
11589 try {
11590 Phone phone = getPhone(subId);
11591 if (phone == null) return null;
11592 ServiceStateTracker sst = phone.getServiceStateTracker();
11593 if (sst == null) return null;
11594 return sst.getLastKnownCellIdentity();
11595 } finally {
11596 Binder.restoreCallingIdentity(identity);
11597 }
11598 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011599
jimsun3b9ccac2021-10-26 15:01:23 +080011600 /**
11601 * Sets the modem service class Name that Telephony will bind to.
11602 *
11603 * @param serviceName The class name of the modem service.
11604 * @return true if the operation is succeed, otherwise false.
11605 */
11606 public boolean setModemService(String serviceName) {
11607 Log.d(LOG_TAG, "setModemService - " + serviceName);
11608 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11609 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11610 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11611 "setModemService");
11612 return mPhoneConfigurationManager.setModemService(serviceName);
11613 }
11614
11615 /**
11616 * Return the class name of the currently bounded modem service.
11617 *
11618 * @return the class name of the modem service.
11619 */
11620 public String getModemService() {
11621 String result;
11622 Log.d(LOG_TAG, "getModemService");
11623 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11624 TelephonyPermissions
11625 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11626 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11627 "getModemService");
11628 result = mPhoneConfigurationManager.getModemService();
11629 Log.d(LOG_TAG, "result = " + result);
11630 return result;
11631 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011632
11633 @Override
11634 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11635 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11636 mApp.enforceCallingOrSelfPermission(
11637 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11638 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11639
11640 final long identity = Binder.clearCallingIdentity();
11641 try {
11642 Phone phone = getPhone(subId);
11643 if (phone == null) return;
11644 phone.setVoiceServiceStateOverride(hasService);
11645 } finally {
11646 Binder.restoreCallingIdentity(identity);
11647 }
11648 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011649
11650 /**
11651 * set removable eSIM as default eUICC.
11652 *
11653 * @hide
11654 */
11655 @Override
11656 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11657 enforceModifyPermission();
11658 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11659
11660 final long identity = Binder.clearCallingIdentity();
11661 try {
11662 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11663 } finally {
11664 Binder.restoreCallingIdentity(identity);
11665 }
11666 }
11667
11668 /**
11669 * Returns whether the removable eSIM is default eUICC or not.
11670 *
11671 * @hide
11672 */
11673 @Override
11674 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11675 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11676 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11677
11678 final long identity = Binder.clearCallingIdentity();
11679 try {
11680 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11681 } finally {
11682 Binder.restoreCallingIdentity(identity);
11683 }
11684 }
11685
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011686 /**
11687 * Get the component name of the default app to direct respond-via-message intent for the
11688 * user associated with this subscription, update the cache if there is no respond-via-message
11689 * application currently configured for this user.
11690 * @return component name of the app and class to direct Respond Via Message intent to, or
11691 * {@code null} if the functionality is not supported.
11692 * @hide
11693 */
11694 @Override
11695 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11696 boolean updateIfNeeded) {
11697 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011698
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011699 Context context = getPhone(subId).getContext();
11700 UserHandle userHandle = null;
11701 final long identity = Binder.clearCallingIdentity();
11702 try {
11703 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11704 } finally {
11705 Binder.restoreCallingIdentity(identity);
11706 }
11707 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11708 updateIfNeeded, userHandle);
11709 }
Jack Yuf5badd92022-12-08 00:50:53 -080011710
11711 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011712 * Set whether the device is able to connect with null ciphering or integrity
11713 * algorithms. This is a global setting and will apply to all active subscriptions
11714 * and all new subscriptions after this.
11715 *
11716 * @param enabled when true, null cipher and integrity algorithms are allowed.
11717 * @hide
11718 */
11719 @Override
11720 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11721 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11722 enforceModifyPermission();
11723 checkForNullCipherAndIntegritySupport();
11724
11725 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11726 // for listening to these preference changes and applying them immediately.
11727 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11728 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11729 editor.apply();
11730
11731 for (Phone phone: PhoneFactory.getPhones()) {
11732 phone.handleNullCipherEnabledChange();
11733 }
11734 }
11735
11736
11737 /**
11738 * Get whether the device is able to connect with null ciphering or integrity
11739 * algorithms. Note that this retrieves the phone-global preference and not
11740 * the state of the radio.
11741 *
11742 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11743 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11744 * version for this feature.
11745 * @hide
11746 */
11747 @Override
11748 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11749 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11750 enforceReadPermission();
11751 checkForNullCipherAndIntegritySupport();
11752 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11753 }
11754
11755 private void checkForNullCipherAndIntegritySupport() {
11756 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11757 throw new UnsupportedOperationException(
11758 "Null cipher and integrity operations require HAL 2.1 or above");
11759 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011760 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11761 throw new UnsupportedOperationException(
11762 "Null cipher and integrity operations unsupported by modem");
11763 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011764 }
11765
11766 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011767 * Get the SIM state for the slot index.
11768 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11769 *
11770 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11771 */
11772 @Override
11773 @SimState
11774 public int getSimStateForSlotIndex(int slotIndex) {
11775 IccCardConstants.State simState;
11776 if (slotIndex < 0) {
11777 simState = IccCardConstants.State.UNKNOWN;
11778 } else {
11779 Phone phone = null;
11780 try {
11781 phone = PhoneFactory.getPhone(slotIndex);
11782 } catch (IllegalStateException e) {
11783 // ignore
11784 }
11785 if (phone == null) {
11786 simState = IccCardConstants.State.UNKNOWN;
11787 } else {
11788 IccCard icc = phone.getIccCard();
11789 if (icc == null) {
11790 simState = IccCardConstants.State.UNKNOWN;
11791 } else {
11792 simState = icc.getState();
11793 }
11794 }
11795 }
11796 return simState.ordinal();
11797 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011798
11799 /**
11800 * Get current cell broadcast ranges.
11801 */
11802 @Override
11803 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11804 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11805 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11806 "getCellBroadcastIdRanges");
11807 final long identity = Binder.clearCallingIdentity();
11808 try {
11809 return getPhone(subId).getCellBroadcastIdRanges();
11810 } finally {
11811 Binder.restoreCallingIdentity(identity);
11812 }
11813 }
11814
11815 /**
11816 * Set reception of cell broadcast messages with the list of the given ranges
11817 *
11818 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11819 */
11820 @Override
11821 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11822 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11823 @Nullable IIntegerConsumer callback) {
11824 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11825 "setCellBroadcastIdRanges");
11826 final long identity = Binder.clearCallingIdentity();
11827 try {
11828 Phone phone = getPhoneFromSubId(subId);
11829 if (DBG) {
11830 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11831 }
11832 phone.setCellBroadcastIdRanges(ranges, result -> {
11833 if (callback != null) {
11834 try {
11835 callback.accept(result);
11836 } catch (RemoteException e) {
11837 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11838 }
11839 }
11840 });
11841 } finally {
11842 Binder.restoreCallingIdentity(identity);
11843 }
11844 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011845
11846 /**
11847 * Returns whether the device supports the domain selection service.
11848 *
11849 * @return {@code true} if the device supports the domain selection service.
11850 */
11851 @Override
11852 public boolean isDomainSelectionSupported() {
11853 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11854 "isDomainSelectionSupported");
11855
11856 final long identity = Binder.clearCallingIdentity();
11857 try {
11858 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11859 } finally {
11860 Binder.restoreCallingIdentity(identity);
11861 }
11862 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011863
11864 /**
11865 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11866 *
11867 * <p>This method behaves in one of the following ways:
11868 * <ul>
11869 * <li>return true : if the calling package has the appop permission {@link
11870 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11871 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11872 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11873 * Owner device identifier access check, or the calling package has carrier privileges</>
11874 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11875 * </ul>
11876 */
11877 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11878 String callingPackage, @Nullable String callingFeatureId, String message) {
11879 for (Phone phone : PhoneFactory.getPhones()) {
11880 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11881 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11882 return true;
11883 }
11884 }
11885 return false;
11886 }
11887}