blob: 4d467abecd7423f9fa498b1292ef211cb2c1c794 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Hall Liud892bec2018-11-30 14:51:45 -080019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Gil Cukierman1c0eb932022-12-06 22:28:24 +000020import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
jimsunf9ec1622022-09-13 21:18:43 +080021import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
Hall Liud892bec2018-11-30 14:51:45 -080022
Shuo Qianccbaf742021-02-22 18:32:21 -080023import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA;
24import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_GSM;
Tyler Gunn7bcdc742019-10-04 15:56:59 -070025import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070026import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
joonhunshin3e154242021-09-17 06:33:39 +000027import static com.android.internal.telephony.TelephonyStatsLog.RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT;
Ta-wei Yen87c49842016-05-13 21:19:52 -070028
Brad Ebinger34c09a52021-02-17 23:23:21 +000029import android.Manifest;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080030import android.Manifest.permission;
Hall Liua1548bd2019-12-24 14:14:12 -080031import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070032import android.annotation.Nullable;
sandeepjsb6c87872021-09-27 15:34:44 +000033import android.annotation.RequiresPermission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080035import android.app.PendingIntent;
Tyler Gunn64144d92022-03-17 14:16:41 -070036import android.app.PropertyInvalidatedCache;
Brad Ebinger4f6208e2021-03-23 21:04:45 +000037import android.app.compat.CompatChanges;
Hall Liu82694d52020-12-11 18:22:04 -080038import android.app.role.RoleManager;
sandeepjsb6c87872021-09-27 15:34:44 +000039import android.compat.annotation.ChangeId;
40import android.compat.annotation.EnabledSince;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070041import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070042import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.content.Context;
44import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070046import android.content.pm.ComponentInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070047import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.net.Uri;
49import android.os.AsyncResult;
50import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080051import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.os.Bundle;
53import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070054import android.os.IBinder;
tom hsu0b59d292022-09-29 23:49:21 +080055import android.os.LocaleList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.os.Looper;
57import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070058import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080059import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070060import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070061import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080062import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080063import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070064import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070065import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080066import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070068import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070069import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070070import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070071import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080072import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070073import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090074import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080075import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080076import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070077import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080078import android.telephony.AccessNetworkConstants;
79import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070080import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070081import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080082import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070083import android.telephony.AnomalyReporter;
Shuo Qian4a594052020-01-23 11:59:30 -080084import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070085import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080086import android.telephony.CarrierRestrictionRules;
Hui Wang9b5793a2022-12-05 14:38:06 -060087import android.telephony.CellBroadcastIdRange;
yincheng zhao2737e882019-09-06 17:06:54 -070088import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080089import android.telephony.CellIdentityCdma;
90import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070092import android.telephony.CellInfoGsm;
93import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070094import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080095import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070096import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070097import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070098import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080099import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700100import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -0800101import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -0700102import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -0800103import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800104import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700105import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800106import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800108import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800109import android.telephony.SignalStrengthUpdateRequest;
110import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800111import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800112import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800113import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700114import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700115import android.telephony.TelephonyManager;
Jack Yuf5badd92022-12-08 00:50:53 -0800116import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800117import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800118import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800119import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000120import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000121import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000122import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700123import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700124import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800125import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800126import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700127import android.telephony.gba.GbaAuthRequest;
128import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700129import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800130import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000131import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000132import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700133import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000134import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700135import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800136import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700137import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800138import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700139import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000140import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700141import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800142import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800143import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800145import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700146import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800148import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800149
Andrew Lee312e8172014-10-23 17:01:36 -0700150import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800151import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800152import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800153import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800154import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700156import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700157import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800158import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800159import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700160import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700161import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800162import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700163import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800164import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800165import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800166import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700167import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000168import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700169import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800170import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800172import com.android.internal.telephony.IccCard;
Jack Yuf5badd92022-12-08 00:50:53 -0800173import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800174import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700175import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700176import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700177import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700179import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800180import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700181import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700182import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700183import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700184import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800185import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800186import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700187import com.android.internal.telephony.ServiceStateTracker;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000188import com.android.internal.telephony.SmsApplication;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700189import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700190import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800191import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800192import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800193import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700194import com.android.internal.telephony.data.DataUtils;
Hunsuk Choi42cc62a2022-10-16 06:03:40 +0000195import com.android.internal.telephony.domainselection.DomainSelectionResolver;
sqianf4ca7ed2019-01-15 18:32:07 -0800196import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700197import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800198import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700199import com.android.internal.telephony.imsphone.ImsPhone;
200import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000201import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800202import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yu285100e2022-12-02 22:48:35 -0800203import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
204import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700205import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700206import com.android.internal.telephony.uicc.IccIoResult;
207import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800208import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700209import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800210import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700211import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000212import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800213import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000214import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800215import com.android.internal.telephony.util.LocaleUtils;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000216import com.android.internal.telephony.util.TelephonyUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700217import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700218import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800219import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800220import com.android.phone.callcomposer.CallComposerPictureManager;
221import com.android.phone.callcomposer.CallComposerPictureTransfer;
222import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700223import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin46355ba2022-11-01 23:51:16 -0700224import com.android.phone.slice.SlicePurchaseController;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700225import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800226import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700227import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700228import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800229import com.android.services.telephony.TelecomAccountRegistry;
230import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800231import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800232
Hall Liu82694d52020-12-11 18:22:04 -0800233import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700234import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800235import java.io.IOException;
236import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700237import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800239import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000240import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800241import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800242import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800243import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800244import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100245import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800246import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700247import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800248import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800249import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700250import java.util.UUID;
Hall Liu82694d52020-12-11 18:22:04 -0800251import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800252import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800253import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254
255/**
256 * Implementation of the ITelephony interface.
257 */
Santos Cordon117fee72014-05-16 17:56:12 -0700258public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 private static final String LOG_TAG = "PhoneInterfaceManager";
260 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
261 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800262 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263
264 // Message codes used with mMainThreadHandler
265 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700266 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
267 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700268 private static final int CMD_OPEN_CHANNEL = 9;
269 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
270 private static final int CMD_CLOSE_CHANNEL = 11;
271 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800272 private static final int CMD_NV_READ_ITEM = 13;
273 private static final int EVENT_NV_READ_ITEM_DONE = 14;
274 private static final int CMD_NV_WRITE_ITEM = 15;
275 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
276 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
277 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700278 private static final int CMD_RESET_MODEM_CONFIG = 19;
279 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800280 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
281 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800282 private static final int CMD_SEND_ENVELOPE = 25;
283 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000284 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
285 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700286 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
287 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
288 private static final int CMD_EXCHANGE_SIM_IO = 31;
289 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800290 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
291 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700292 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
293 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700294 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
295 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700296 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
297 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
298 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
299 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700300 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
301 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
302 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
303 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700304 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800305 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
306 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000307 private static final int CMD_SWITCH_SLOTS = 50;
308 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700309 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
310 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
311 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
312 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
313 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
314 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
315 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
316 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700317 private static final int CMD_GET_ALL_CELL_INFO = 60;
318 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
319 private static final int CMD_GET_CELL_LOCATION = 62;
320 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700321 private static final int CMD_MODEM_REBOOT = 64;
322 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700323 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
324 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800325 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
326 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700327 private static final int CMD_GET_MODEM_STATUS = 70;
328 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700329 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
330 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700331 private static final int CMD_ERASE_MODEM_CONFIG = 74;
332 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800333 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
334 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
335 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
336 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800337 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
338 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800339 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800340 private static final int CMD_GET_CALL_FORWARDING = 83;
341 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
342 private static final int CMD_SET_CALL_FORWARDING = 85;
343 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
344 private static final int CMD_GET_CALL_WAITING = 87;
345 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
346 private static final int CMD_SET_CALL_WAITING = 89;
347 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700348 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
349 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
350 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
351 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700352 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
353 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800354 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
355 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800356 private static final int CMD_SET_DATA_THROTTLING = 99;
357 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800358 private static final int CMD_SET_SIM_POWER = 101;
359 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800360 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
361 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
362 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
363 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800364 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
365 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000366 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800367 private static final int CMD_GET_SLICING_CONFIG = 110;
368 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800369 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700370 private static final int CMD_ENABLE_VONR = 113;
371 private static final int EVENT_ENABLE_VONR_DONE = 114;
372 private static final int CMD_IS_VONR_ENABLED = 115;
373 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700374 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
375 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800377 // Parameters of select command.
378 private static final int SELECT_COMMAND = 0xA4;
379 private static final int SELECT_P1 = 0x04;
380 private static final int SELECT_P2 = 0;
381 private static final int SELECT_P3 = 0x10;
382
Gil Cukierman1c0eb932022-12-06 22:28:24 +0000383 // Toggling null cipher and integrity support was added in IRadioNetwork 2.1
384 private static final int MIN_NULL_CIPHER_AND_INTEGRITY_VERSION = 201;
385
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 /** The singleton instance. */
387 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800388 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389
Wink Saville3ab207e2014-11-20 13:07:20 -0800390 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800391 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800392 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700393 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800394 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700395 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800396 private MainThreadHandler mMainThreadHandler;
Jack Yue37dd262022-12-16 11:53:37 -0800397 private final SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800398 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700399 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800400 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Gil Cukierman6dac5eb2022-09-19 16:09:04 +0000401 private final Telephony2gUpdater mTelephony2gUpdater;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402
Peter Wangdafb9ac2020-01-15 14:13:38 -0800403 /** User Activity */
404 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800405 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
406
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700407 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
408
Derek Tan97ebb422014-09-05 16:55:38 -0700409 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
410 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800411 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800412 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700413
Michelecea4cf22018-12-21 15:00:11 -0800414 // String to store multi SIM allowed
415 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
416
Derek Tan740e1672017-06-27 14:56:27 -0700417 // The AID of ISD-R.
418 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
419
yinxub1bed742017-04-17 11:45:04 -0700420 private NetworkScanRequestTracker mNetworkScanRequestTracker;
421
David Kelly5e06a7f2018-03-12 14:10:59 +0000422 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
423 private static final int MANUFACTURER_CODE_LENGTH = 8;
424
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800425 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800426 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800427
Sarah Chin2ec39f62022-08-31 17:03:26 -0700428 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
429 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
430
Derek Tan89e89d42014-07-08 17:00:10 -0700431 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700432 * Experiment flag to enable erase modem config on reset network, default value is false
433 */
434 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
435 "reset_network_erase_modem_config_enabled";
436
Rambo Wang0f050d82021-02-12 11:43:36 -0800437 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800438
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();
Jack Yue37dd262022-12-16 11:53:37 -08002413 if (!PhoneFactory.isSubscriptionManagerServiceEnabled()) {
2414 mSubscriptionController = SubscriptionController.getInstance();
2415 } else {
2416 mSubscriptionController = null;
2417 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002418 mTelephonySharedPreferences =
2419 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002420 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002421 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002422 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002423 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002424 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002425 mTelephony2gUpdater = new Telephony2gUpdater(
2426 Executors.newSingleThreadExecutor(), mApp);
2427 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002428 publish();
2429 }
2430
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002431 @VisibleForTesting
2432 public SharedPreferences getSharedPreferences() {
2433 return mTelephonySharedPreferences;
2434 }
2435
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002436 /**
2437 * Get the default phone for this device.
2438 */
2439 @VisibleForTesting
2440 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002441 Phone thePhone = getPhone(getDefaultSubscription());
2442 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2443 }
2444
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002445 private void publish() {
2446 if (DBG) log("publish: " + this);
2447
Peter Wangc035ce42020-01-08 21:00:22 -08002448 TelephonyFrameworkInitializer
2449 .getTelephonyServiceManager()
2450 .getTelephonyServiceRegisterer()
2451 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002452 }
2453
Stuart Scott584921c2015-01-15 17:10:34 -08002454 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002455 if (request.phone != null) {
2456 return request.phone;
2457 } else {
2458 return getPhoneFromSubId(request.subId);
2459 }
2460 }
2461
2462 private Phone getPhoneFromSubId(int subId) {
2463 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2464 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002465 }
2466
Rambo Wange53e07d2022-05-10 13:01:13 -07002467 @Nullable
2468 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002469 Phone phone = getPhoneFromRequest(request);
2470 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002471 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002472 }
2473
Wink Saville36469e72014-06-11 15:17:00 -07002474 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002475 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002476 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002477 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002478
Kai Shif70f46f2021-03-03 13:59:46 -08002479 private void sendEraseModemConfig(@NonNull Phone phone) {
2480 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2481 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2482 }
2483
2484 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2485 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2486 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002487 }
2488
Peter Wang44b186e2020-01-13 23:33:09 -08002489 private boolean isImsAvailableOnDevice() {
2490 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2491 if (pm == null) {
2492 // For some reason package manger is not available.. This will fail internally anyway,
2493 // so do not throw error and allow.
2494 return true;
2495 }
2496 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2497 }
2498
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002499 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002500 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002501 }
2502
Wink Savilleb564aae2014-10-23 10:18:09 -07002503 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002504 if (DBG) log("dial: " + number);
2505 // No permission check needed here: This is just a wrapper around the
2506 // ACTION_DIAL intent, which is available to any app since it puts up
2507 // the UI before it does anything.
2508
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002509 final long identity = Binder.clearCallingIdentity();
2510 try {
2511 String url = createTelUrl(number);
2512 if (url == null) {
2513 return;
2514 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002515
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002516 // PENDING: should we just silently fail if phone is offhook or ringing?
2517 PhoneConstants.State state = mCM.getState(subId);
2518 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2519 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2520 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2521 mApp.startActivity(intent);
2522 }
2523 } finally {
2524 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002525 }
2526 }
2527
2528 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002529 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002530 }
2531
Wink Savilleb564aae2014-10-23 10:18:09 -07002532 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002533 if (DBG) log("call: " + number);
2534
2535 // This is just a wrapper around the ACTION_CALL intent, but we still
2536 // need to do a permission check since we're calling startActivity()
2537 // from the context of the phone app.
2538 enforceCallPermission();
2539
Jordan Liu1617b712019-07-10 15:06:26 -07002540 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002541 != AppOpsManager.MODE_ALLOWED) {
2542 return;
2543 }
2544
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002545 final long identity = Binder.clearCallingIdentity();
2546 try {
2547 String url = createTelUrl(number);
2548 if (url == null) {
2549 return;
2550 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002551
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002552 boolean isValid = false;
2553 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2554 if (slist != null) {
2555 for (SubscriptionInfo subInfoRecord : slist) {
2556 if (subInfoRecord.getSubscriptionId() == subId) {
2557 isValid = true;
2558 break;
2559 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002560 }
Wink Saville08874612014-08-31 19:19:58 -07002561 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002562 if (!isValid) {
2563 return;
2564 }
Wink Saville08874612014-08-31 19:19:58 -07002565
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002566 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2567 intent.putExtra(SUBSCRIPTION_KEY, subId);
2568 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2569 mApp.startActivity(intent);
2570 } finally {
2571 Binder.restoreCallingIdentity(identity);
2572 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002573 }
2574
Wink Savilleb564aae2014-10-23 10:18:09 -07002575 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002576 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002577 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2578 }
2579
Wink Savilleb564aae2014-10-23 10:18:09 -07002580 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002581 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002582 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2583 }
2584
Wink Savilleb564aae2014-10-23 10:18:09 -07002585 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002586 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002587
2588 final long identity = Binder.clearCallingIdentity();
2589 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002590 Phone phone = getPhone(subId);
2591 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002592 checkSimPin.start();
2593 return checkSimPin.unlockSim(null, pin);
2594 } finally {
2595 Binder.restoreCallingIdentity(identity);
2596 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002597 }
2598
Wink Savilleb564aae2014-10-23 10:18:09 -07002599 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002600 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002601
2602 final long identity = Binder.clearCallingIdentity();
2603 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002604 Phone phone = getPhone(subId);
2605 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002606 checkSimPuk.start();
2607 return checkSimPuk.unlockSim(puk, pin);
2608 } finally {
2609 Binder.restoreCallingIdentity(identity);
2610 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002611 }
2612
2613 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002614 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002615 * a synchronous one.
2616 */
2617 private static class UnlockSim extends Thread {
2618
2619 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002620 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002621
2622 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002623 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2624 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002625
2626 // For replies from SimCard interface
2627 private Handler mHandler;
2628
2629 // For async handler to identify request type
2630 private static final int SUPPLY_PIN_COMPLETE = 100;
2631
Michele Berionne5e411512020-11-13 02:36:59 +00002632 UnlockSim(int phoneId, IccCard simCard) {
2633 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002634 mSimCard = simCard;
2635 }
2636
2637 @Override
2638 public void run() {
2639 Looper.prepare();
2640 synchronized (UnlockSim.this) {
2641 mHandler = new Handler() {
2642 @Override
2643 public void handleMessage(Message msg) {
2644 AsyncResult ar = (AsyncResult) msg.obj;
2645 switch (msg.what) {
2646 case SUPPLY_PIN_COMPLETE:
2647 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2648 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002649 mRetryCount = msg.arg1;
2650 if (ar.exception != null) {
2651 if (ar.exception instanceof CommandException &&
2652 ((CommandException)(ar.exception)).getCommandError()
2653 == CommandException.Error.PASSWORD_INCORRECT) {
2654 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002655 } //When UiccCardApp dispose,handle message and return exception
2656 else if (ar.exception instanceof CommandException &&
2657 ((CommandException) (ar.exception)).getCommandError()
2658 == CommandException.Error.ABORTED) {
2659 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002660 } else {
2661 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2662 }
2663 } else {
2664 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2665 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002666 mDone = true;
2667 UnlockSim.this.notifyAll();
2668 }
2669 break;
2670 }
2671 }
2672 };
2673 UnlockSim.this.notifyAll();
2674 }
2675 Looper.loop();
2676 }
2677
2678 /*
2679 * Use PIN or PUK to unlock SIM card
2680 *
2681 * If PUK is null, unlock SIM card with PIN
2682 *
2683 * If PUK is not null, unlock SIM card with PUK and set PIN code
2684 */
Wink Saville9de0f752013-10-22 19:04:03 -07002685 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002686
2687 while (mHandler == null) {
2688 try {
2689 wait();
2690 } catch (InterruptedException e) {
2691 Thread.currentThread().interrupt();
2692 }
2693 }
2694 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2695
2696 if (puk == null) {
2697 mSimCard.supplyPin(pin, callback);
2698 } else {
2699 mSimCard.supplyPuk(puk, pin, callback);
2700 }
2701
2702 while (!mDone) {
2703 try {
2704 Log.d(LOG_TAG, "wait for done");
2705 wait();
2706 } catch (InterruptedException e) {
2707 // Restore the interrupted status
2708 Thread.currentThread().interrupt();
2709 }
2710 }
2711 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002712 int[] resultArray = new int[2];
2713 resultArray[0] = mResult;
2714 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002715
2716 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002717 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002718 }
2719
Wink Saville9de0f752013-10-22 19:04:03 -07002720 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002721 }
2722 }
2723
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002724 /**
2725 * This method has been removed due to privacy and stability concerns.
2726 */
2727 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002728 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002729 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2730 return;
Wink Saville36469e72014-06-11 15:17:00 -07002731 }
2732
Nathan Harold1f889d82020-06-04 17:05:26 -07002733 @Override
2734 public void updateServiceLocationWithPackageName(String callingPackage) {
2735 mApp.getSystemService(AppOpsManager.class)
2736 .checkPackage(Binder.getCallingUid(), callingPackage);
2737
Nathan Haroldf096d982020-11-18 17:18:06 -08002738 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002739 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2740 // Callers targeting S have no business invoking this method.
2741 return;
2742 }
2743
2744 LocationAccessPolicy.LocationPermissionResult locationResult =
2745 LocationAccessPolicy.checkLocationPermission(mApp,
2746 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2747 .setCallingPackage(callingPackage)
2748 .setCallingFeatureId(null)
2749 .setCallingPid(Binder.getCallingPid())
2750 .setCallingUid(Binder.getCallingUid())
2751 .setMethod("updateServiceLocation")
2752 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2753 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2754 .build());
2755 // Apps that lack location permission have no business calling this method;
2756 // however, because no permission was declared in the public API, denials must
2757 // all be "soft".
2758 switch (locationResult) {
2759 case DENIED_HARD: /* fall through */
2760 case DENIED_SOFT:
2761 return;
2762 }
2763
2764 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002765 final long identity = Binder.clearCallingIdentity();
2766 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002767 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002768 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002769 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002770 }
2771 } finally {
2772 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002773 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002774 }
2775
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002776 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002777 @Override
2778 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002779 return isRadioOnWithFeature(callingPackage, null);
2780 }
2781
2782
2783 @Override
2784 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2785 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2786 callingFeatureId);
2787 }
2788
2789 @Deprecated
2790 @Override
2791 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2792 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002793 }
2794
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002795 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002796 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2797 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002798 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002799 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002800 return false;
2801 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002802
2803 final long identity = Binder.clearCallingIdentity();
2804 try {
2805 return isRadioOnForSubscriber(subId);
2806 } finally {
2807 Binder.restoreCallingIdentity(identity);
2808 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002809 }
2810
2811 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002812 final long identity = Binder.clearCallingIdentity();
2813 try {
2814 final Phone phone = getPhone(subId);
2815 if (phone != null) {
2816 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2817 } else {
2818 return false;
2819 }
2820 } finally {
2821 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002822 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002823 }
2824
2825 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002826 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002827 }
Wink Saville36469e72014-06-11 15:17:00 -07002828
Wink Savilleb564aae2014-10-23 10:18:09 -07002829 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002830 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002831
2832 final long identity = Binder.clearCallingIdentity();
2833 try {
2834 final Phone phone = getPhone(subId);
2835 if (phone != null) {
2836 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2837 }
2838 } finally {
2839 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002840 }
Wink Saville36469e72014-06-11 15:17:00 -07002841 }
2842
2843 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002844 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002845 }
2846
Wink Savilleb564aae2014-10-23 10:18:09 -07002847 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002848 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002849
2850 final long identity = Binder.clearCallingIdentity();
2851 try {
2852 final Phone phone = getPhone(subId);
2853 if (phone == null) {
2854 return false;
2855 }
2856 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2857 toggleRadioOnOffForSubscriber(subId);
2858 }
2859 return true;
2860 } finally {
2861 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002862 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002863 }
Wink Saville36469e72014-06-11 15:17:00 -07002864
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002865 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002866 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002867 /*
2868 * If any of the Radios are available, it will need to be
2869 * shutdown. So return true if any Radio is available.
2870 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002871 final long identity = Binder.clearCallingIdentity();
2872 try {
2873 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2874 Phone phone = PhoneFactory.getPhone(i);
2875 if (phone != null && phone.isRadioAvailable()) return true;
2876 }
2877 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2878 return false;
2879 } finally {
2880 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002881 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002882 }
2883
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002884 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002885 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002886 enforceModifyPermission();
2887
2888 final long identity = Binder.clearCallingIdentity();
2889 try {
2890 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2891 logv("Shutting down Phone " + i);
2892 shutdownRadioUsingPhoneId(i);
2893 }
2894 } finally {
2895 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002896 }
2897 }
2898
2899 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002900 Phone phone = PhoneFactory.getPhone(phoneId);
2901 if (phone != null && phone.isRadioAvailable()) {
2902 phone.shutdownRadio();
2903 }
2904 }
2905
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002906 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002907 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002908
2909 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
2927 final long identity = Binder.clearCallingIdentity();
2928 try {
2929 final Phone phone = getPhone(subId);
2930 if (phone != null) {
2931 phone.setRadioPower(turnOn);
2932 return true;
2933 } else {
2934 return false;
2935 }
2936 } finally {
2937 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002938 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002939 }
2940
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002941 /**
2942 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
2943 * no reason to power it off. When any of the voters want to power it off, it will be turned
2944 * off. In case of emergency, the radio will be turned on even if there are some reasons for
2945 * powering it off, and these radio off votes will be cleared.
2946 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
2947 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
2948 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
2949 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
2950 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
2951 * check its vote.
2952 *
2953 * @param subId The subscription ID.
2954 * @param reason The reason for powering off radio.
2955 * @return true on success and false on failure.
2956 */
2957 public boolean requestRadioPowerOffForReason(int subId,
2958 @TelephonyManager.RadioPowerReason int reason) {
2959 enforceModifyPermission();
2960
2961 final long identity = Binder.clearCallingIdentity();
2962 try {
2963 final Phone phone = getPhone(subId);
2964 if (phone != null) {
2965 phone.setRadioPowerForReason(false, reason);
2966 return true;
2967 } else {
2968 return false;
2969 }
2970 } finally {
2971 Binder.restoreCallingIdentity(identity);
2972 }
2973 }
2974
2975 /**
2976 * Remove the vote on powering off the radio for a reason, as requested by
2977 * {@link requestRadioPowerOffForReason}.
2978 *
2979 * @param subId The subscription ID.
2980 * @param reason The reason for powering off radio.
2981 * @return true on success and false on failure.
2982 */
2983 public boolean clearRadioPowerOffForReason(int subId,
2984 @TelephonyManager.RadioPowerReason int reason) {
2985 enforceModifyPermission();
2986
2987 final long identity = Binder.clearCallingIdentity();
2988 try {
2989 final Phone phone = getPhone(subId);
2990 if (phone != null) {
2991 phone.setRadioPowerForReason(true, reason);
2992 return true;
2993 } else {
2994 return false;
2995 }
2996 } finally {
2997 Binder.restoreCallingIdentity(identity);
2998 }
2999 }
3000
3001 /**
3002 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3003 *
3004 * @param subId The subscription ID.
3005 * @param callingPackage The package making the call.
3006 * @param callingFeatureId The feature in the package.
3007 * @return List of reasons for powering off radio.
3008 */
3009 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3010 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3011
3012 final long identity = Binder.clearCallingIdentity();
3013 List result = new ArrayList();
3014 try {
3015 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3016 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3017 return result;
3018 }
3019
3020 final Phone phone = getPhone(subId);
3021 if (phone != null) {
3022 result.addAll(phone.getRadioPowerOffReasons());
3023 }
3024 } finally {
3025 Binder.restoreCallingIdentity(identity);
3026 }
3027 return result;
3028 }
3029
Wink Saville36469e72014-06-11 15:17:00 -07003030 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003031 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003032 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003033 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003034
3035 final long identity = Binder.clearCallingIdentity();
3036 try {
Jack Yu285100e2022-12-02 22:48:35 -08003037 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003038 final Phone phone = getPhone(subId);
3039 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003040 phone.getDataSettingsManager().setDataEnabled(
3041 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003042 return true;
3043 } else {
3044 return false;
3045 }
3046 } finally {
3047 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003048 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003049 }
3050
Wink Saville36469e72014-06-11 15:17:00 -07003051 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003052 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003053 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003054 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003055
3056 final long identity = Binder.clearCallingIdentity();
3057 try {
Jack Yu285100e2022-12-02 22:48:35 -08003058 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003059 final Phone phone = getPhone(subId);
3060 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003061 phone.getDataSettingsManager().setDataEnabled(
3062 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003063 return true;
3064 } else {
3065 return false;
3066 }
3067 } finally {
3068 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003069 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003070 }
3071
Sanket Padawe356d7632015-06-22 14:03:32 -07003072 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003073 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003074 final long identity = Binder.clearCallingIdentity();
3075 try {
3076 final Phone phone = getPhone(subId);
3077 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003078 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003079 } else {
3080 return false;
3081 }
3082 } finally {
3083 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003084 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003085 }
3086
3087 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003088 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003089 }
3090
pkanwarae03a6b2016-11-06 20:37:09 -08003091 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003092 enforceCallPermission();
3093
3094 final long identity = Binder.clearCallingIdentity();
3095 try {
3096 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3097 return;
3098 }
3099 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3100 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3101 } finally {
3102 Binder.restoreCallingIdentity(identity);
3103 }
pkanwar32d516d2016-10-14 19:37:38 -07003104 };
3105
Wink Savilleb564aae2014-10-23 10:18:09 -07003106 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003107 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003108
3109 final long identity = Binder.clearCallingIdentity();
3110 try {
3111 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3112 return false;
3113 }
3114 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3115 } finally {
3116 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003117 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003118 }
3119
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003120 /**
3121 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3122 * tag on getCallState Binder call.
3123 */
3124 @Deprecated
3125 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003126 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003127 if (CompatChanges.isChangeEnabled(
3128 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3129 Binder.getCallingUid())) {
3130 // Do not allow this API to be called on API version 31+, it should only be
3131 // called on old apps using this Binder call directly.
3132 throw new SecurityException("This method can only be used for applications "
3133 + "targeting API version 30 or less.");
3134 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003135 final long identity = Binder.clearCallingIdentity();
3136 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003137 Phone phone = getPhone(getDefaultSubscription());
3138 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3139 PhoneConstantConversions.convertCallState(phone.getState());
3140 } finally {
3141 Binder.restoreCallingIdentity(identity);
3142 }
3143 }
3144
3145 @Override
3146 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3147 if (CompatChanges.isChangeEnabled(
3148 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3149 Binder.getCallingUid())) {
3150 // Check READ_PHONE_STATE for API version 31+
3151 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3152 featureId, "getCallStateForSubscription")) {
3153 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3154 + "targeting API level 31+.");
3155 }
3156 }
3157 final long identity = Binder.clearCallingIdentity();
3158 try {
3159 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003160 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3161 PhoneConstantConversions.convertCallState(phone.getState());
3162 } finally {
3163 Binder.restoreCallingIdentity(identity);
3164 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003165 }
3166
Sanket Padawe356d7632015-06-22 14:03:32 -07003167 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003168 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003169 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003170 }
3171
3172 @Override
3173 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003174 final long identity = Binder.clearCallingIdentity();
3175 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003176 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003177 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003178 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003179 } else {
3180 return PhoneConstantConversions.convertDataState(
3181 PhoneConstants.DataState.DISCONNECTED);
3182 }
3183 } finally {
3184 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003185 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003186 }
3187
Sanket Padawe356d7632015-06-22 14:03:32 -07003188 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003189 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003190 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003191 }
3192
3193 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003194 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003195 final long identity = Binder.clearCallingIdentity();
3196 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003197 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003198 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003199 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003200 } else {
3201 return TelephonyManager.DATA_ACTIVITY_NONE;
3202 }
3203 } finally {
3204 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003205 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003206 }
3207
3208 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003209 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003210 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003211 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003212
3213 LocationAccessPolicy.LocationPermissionResult locationResult =
3214 LocationAccessPolicy.checkLocationPermission(mApp,
3215 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3216 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003217 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003218 .setCallingPid(Binder.getCallingPid())
3219 .setCallingUid(Binder.getCallingUid())
3220 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003221 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003222 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3223 .build());
3224 switch (locationResult) {
3225 case DENIED_HARD:
3226 throw new SecurityException("Not allowed to access cell location");
3227 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003228 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3229 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003230 }
3231
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003232 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003233 final long identity = Binder.clearCallingIdentity();
3234 try {
3235 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003236 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003237 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003238 } finally {
3239 Binder.restoreCallingIdentity(identity);
3240 }
Svetoslav64fad262015-04-14 14:35:21 -07003241 }
3242
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003243 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003244 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003245 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3246 // registered cell info, so return a NULL country instead.
3247 final long identity = Binder.clearCallingIdentity();
3248 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003249 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3250 // Get default phone in this case.
3251 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3252 }
Jack Yu285100e2022-12-02 22:48:35 -08003253 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003254 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003255 if (phone == null) return "";
3256 ServiceStateTracker sst = phone.getServiceStateTracker();
3257 if (sst == null) return "";
3258 LocaleTracker lt = sst.getLocaleTracker();
3259 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003260 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003261 } finally {
3262 Binder.restoreCallingIdentity(identity);
3263 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003264 }
3265
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003266 /**
3267 * This method was removed due to potential issues caused by performing partial
3268 * updates of service state, and lack of a credible use case.
3269 *
3270 * This has the ability to break the telephony implementation by disabling notification of
3271 * changes in device connectivity. DO NOT USE THIS!
3272 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003273 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003274 public void enableLocationUpdates() {
3275 mApp.enforceCallingOrSelfPermission(
3276 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003277 }
3278
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003279 /**
3280 * This method was removed due to potential issues caused by performing partial
3281 * updates of service state, and lack of a credible use case.
3282 *
3283 * This has the ability to break the telephony implementation by disabling notification of
3284 * changes in device connectivity. DO NOT USE THIS!
3285 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003286 @Override
3287 public void disableLocationUpdates() {
3288 mApp.enforceCallingOrSelfPermission(
3289 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003290 }
3291
3292 @Override
3293 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003294 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3295 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003296 try {
3297 mApp.getSystemService(AppOpsManager.class)
3298 .checkPackage(Binder.getCallingUid(), callingPackage);
3299 } catch (SecurityException e) {
3300 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3301 throw e;
3302 }
3303
Nathan Haroldf096d982020-11-18 17:18:06 -08003304 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003305 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3306 throw new SecurityException(
3307 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3308 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003309
Jordan Liu1617b712019-07-10 15:06:26 -07003310 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003311 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3312 return null;
3313 }
Svetoslav64fad262015-04-14 14:35:21 -07003314
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003315 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003316
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003317 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003318 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003319
Nathan Haroldf180aac2018-06-01 18:43:55 -07003320 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3321 for (CellInfo ci : info) {
3322 if (ci instanceof CellInfoGsm) {
3323 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3324 } else if (ci instanceof CellInfoWcdma) {
3325 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3326 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003327 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003328 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003329 }
3330
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003331 private List<CellInfo> getCachedCellInfo() {
3332 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3333 for (Phone phone : PhoneFactory.getPhones()) {
3334 List<CellInfo> info = phone.getAllCellInfo();
3335 if (info != null) cellInfos.addAll(info);
3336 }
3337 return cellInfos;
3338 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003339
3340 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003341 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003342 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003343 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003344
3345 LocationAccessPolicy.LocationPermissionResult locationResult =
3346 LocationAccessPolicy.checkLocationPermission(mApp,
3347 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3348 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003349 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003350 .setCallingPid(Binder.getCallingPid())
3351 .setCallingUid(Binder.getCallingUid())
3352 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003353 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003354 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3355 .build());
3356 switch (locationResult) {
3357 case DENIED_HARD:
3358 throw new SecurityException("Not allowed to access cell info");
3359 case DENIED_SOFT:
3360 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003361 }
3362
Nathan Haroldf096d982020-11-18 17:18:06 -08003363 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003364 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3365 return getCachedCellInfo();
3366 }
3367
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003368 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003369 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003370 final long identity = Binder.clearCallingIdentity();
3371 try {
3372 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3373 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003374 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003375 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003376 if (info != null) cellInfos.addAll(info);
3377 }
3378 return cellInfos;
3379 } finally {
3380 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003381 }
3382 }
3383
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003384 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003385 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3386 String callingFeatureId) {
3387 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3388 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003389 }
3390
3391 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003392 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3393 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003394 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003395 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003396 }
3397
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003398 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3399 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003400 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003401 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003402
3403 LocationAccessPolicy.LocationPermissionResult locationResult =
3404 LocationAccessPolicy.checkLocationPermission(mApp,
3405 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3406 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003407 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003408 .setCallingPid(Binder.getCallingPid())
3409 .setCallingUid(Binder.getCallingUid())
3410 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003411 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3412 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003413 .build());
3414 switch (locationResult) {
3415 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003416 if (TelephonyPermissions
3417 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003418 // Safetynet logging for b/154934934
3419 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3420 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003421 throw new SecurityException("Not allowed to access cell info");
3422 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003423 if (TelephonyPermissions
3424 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003425 // Safetynet logging for b/154934934
3426 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3427 }
Nathan Harold5320c422019-05-09 10:26:08 -07003428 try {
3429 cb.onCellInfo(new ArrayList<CellInfo>());
3430 } catch (RemoteException re) {
3431 // Drop without consequences
3432 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003433 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003434 }
3435
Nathan Harolda939a962019-05-09 10:13:47 -07003436
3437 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003438 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3439
3440 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3441 }
3442
3443 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003444 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003445 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003446 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003447
3448 final long identity = Binder.clearCallingIdentity();
3449 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003450 Phone phone = getPhone(subId);
3451 if (phone == null) {
3452 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3453 } else {
3454 phone.setCellInfoListRate(rateInMillis, workSource);
3455 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003456 } finally {
3457 Binder.restoreCallingIdentity(identity);
3458 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003459 }
3460
Shishir Agrawala9f32182016-04-12 12:00:16 -07003461 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003462 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003463 Phone phone = PhoneFactory.getPhone(slotIndex);
3464 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003465 return null;
3466 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003467 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003468 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003469 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003470 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003471 return null;
3472 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003473
3474 final long identity = Binder.clearCallingIdentity();
3475 try {
3476 return phone.getImei();
3477 } finally {
3478 Binder.restoreCallingIdentity(identity);
3479 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003480 }
3481
3482 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003483 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3484 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3485 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3486 callingFeatureId, "getPrimaryImei")) {
3487 throw new SecurityException("Caller does not have permission");
3488 }
3489 final long identity = Binder.clearCallingIdentity();
3490 try {
3491 for (Phone phone : PhoneFactory.getPhones()) {
3492 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3493 return phone.getImei();
3494 }
3495 }
3496 throw new UnsupportedOperationException("Operation not supported");
3497 } finally {
3498 Binder.restoreCallingIdentity(identity);
3499 }
3500 }
3501
3502 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003503 public String getTypeAllocationCodeForSlot(int slotIndex) {
3504 Phone phone = PhoneFactory.getPhone(slotIndex);
3505 String tac = null;
3506 if (phone != null) {
3507 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003508 try {
3509 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3510 } catch (IndexOutOfBoundsException e) {
3511 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3512 return null;
3513 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003514 }
3515 return tac;
3516 }
3517
3518 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003519 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003520 try {
3521 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3522 } catch (SecurityException se) {
3523 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3524 throw new SecurityException("Package " + callingPackage + " does not belong to "
3525 + Binder.getCallingUid());
3526 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003527 Phone phone = PhoneFactory.getPhone(slotIndex);
3528 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003529 return null;
3530 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003531
Jeff Davidson913390f2018-02-23 17:11:49 -08003532 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003533 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003534 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003535 return null;
3536 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003537
3538 final long identity = Binder.clearCallingIdentity();
3539 try {
3540 return phone.getMeid();
3541 } finally {
3542 Binder.restoreCallingIdentity(identity);
3543 }
Jack Yu2af8d712017-03-15 17:14:14 -07003544 }
3545
3546 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003547 public String getManufacturerCodeForSlot(int slotIndex) {
3548 Phone phone = PhoneFactory.getPhone(slotIndex);
3549 String manufacturerCode = null;
3550 if (phone != null) {
3551 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003552 try {
3553 manufacturerCode =
3554 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3555 } catch (IndexOutOfBoundsException e) {
3556 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3557 return null;
3558 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003559 }
3560 return manufacturerCode;
3561 }
3562
3563 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003564 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3565 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003566 Phone phone = PhoneFactory.getPhone(slotIndex);
3567 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003568 return null;
3569 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003570 int subId = phone.getSubId();
3571 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003572 mApp, subId, callingPackage, callingFeatureId,
3573 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003574 return null;
3575 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003576
3577 final long identity = Binder.clearCallingIdentity();
3578 try {
3579 return phone.getDeviceSvn();
3580 } finally {
3581 Binder.restoreCallingIdentity(identity);
3582 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003583 }
3584
fionaxu43304da2017-11-27 22:51:16 -08003585 @Override
3586 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003587 final long identity = Binder.clearCallingIdentity();
3588 try {
3589 final Phone phone = getPhone(subId);
3590 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3591 } finally {
3592 Binder.restoreCallingIdentity(identity);
3593 }
fionaxu43304da2017-11-27 22:51:16 -08003594 }
3595
3596 @Override
3597 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003598 final long identity = Binder.clearCallingIdentity();
3599 try {
3600 final Phone phone = getPhone(subId);
3601 return phone == null ? null : phone.getCarrierName();
3602 } finally {
3603 Binder.restoreCallingIdentity(identity);
3604 }
fionaxu43304da2017-11-27 22:51:16 -08003605 }
3606
calvinpanffe225e2018-11-01 19:43:06 +08003607 @Override
chen xu0026ca62019-03-06 15:28:50 -08003608 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003609 final long identity = Binder.clearCallingIdentity();
3610 try {
3611 final Phone phone = getPhone(subId);
3612 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003613 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003614 } finally {
3615 Binder.restoreCallingIdentity(identity);
3616 }
3617 }
3618
3619 @Override
chen xu0026ca62019-03-06 15:28:50 -08003620 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003621 final long identity = Binder.clearCallingIdentity();
3622 try {
3623 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003624 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003625 } finally {
3626 Binder.restoreCallingIdentity(identity);
3627 }
3628 }
3629
chen xu651eec72018-11-11 19:03:44 -08003630 @Override
chen xu864e11c2018-12-06 22:10:03 -08003631 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3632 if (!isSubscriptionMccMnc) {
3633 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3634 }
chen xu651eec72018-11-11 19:03:44 -08003635 final Phone phone = PhoneFactory.getPhone(slotIndex);
3636 if (phone == null) {
3637 return TelephonyManager.UNKNOWN_CARRIER_ID;
3638 }
3639 final long identity = Binder.clearCallingIdentity();
3640 try {
3641 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3642 } finally {
3643 Binder.restoreCallingIdentity(identity);
3644 }
3645 }
3646
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003647 //
3648 // Internal helper methods.
3649 //
3650
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003651 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003652 * Make sure the caller is the calling package itself
3653 *
3654 * @throws SecurityException if the caller is not the calling package
3655 */
3656 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3657 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003658 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3659 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003660 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003661 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003662 } catch (PackageManager.NameNotFoundException e) {
3663 // packageUid is -1
3664 }
3665 if (packageUid != callingUid) {
3666 throw new SecurityException(message + ": Package " + callingPackage
3667 + " does not belong to " + callingUid);
3668 }
3669 }
3670
3671 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003672 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3673 *
3674 * @throws SecurityException if the caller does not have the required permission
3675 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003676 @VisibleForTesting
3677 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003678 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3679 }
3680
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003681 /**
3682 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3683 *
3684 * @throws SecurityException if the caller does not have the required permission
3685 */
3686 @VisibleForTesting
3687 public void enforceReadPermission() {
3688 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
3689 }
3690
Shuo Qian3b6ee772019-11-13 17:43:31 -08003691 private void enforceActiveEmergencySessionPermission() {
3692 mApp.enforceCallingOrSelfPermission(
3693 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3694 }
3695
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003696 /**
3697 * Make sure the caller has the CALL_PHONE permission.
3698 *
3699 * @throws SecurityException if the caller does not have the required permission
3700 */
3701 private void enforceCallPermission() {
3702 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3703 }
3704
paulhu5a773602019-08-23 19:17:33 +08003705 private void enforceSettingsPermission() {
3706 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003707 }
3708
Michele Berionne5e411512020-11-13 02:36:59 +00003709 private void enforceRebootPermission() {
3710 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3711 }
3712
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003713 private String createTelUrl(String number) {
3714 if (TextUtils.isEmpty(number)) {
3715 return null;
3716 }
3717
Jake Hambye994d462014-02-03 13:10:13 -08003718 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003719 }
3720
Ihab Awadf9e92732013-12-05 18:02:52 -08003721 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003722 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3723 }
3724
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003725 private static void logv(String msg) {
3726 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3727 }
3728
Ihab Awadf9e92732013-12-05 18:02:52 -08003729 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003730 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3731 }
3732
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003733 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003734 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003735 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003736 }
3737
Sanket Padawe356d7632015-06-22 14:03:32 -07003738 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003739 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003740 final long identity = Binder.clearCallingIdentity();
3741 try {
3742 final Phone phone = PhoneFactory.getPhone(slotIndex);
3743 if (phone == null) {
3744 return PhoneConstants.PHONE_TYPE_NONE;
3745 } else {
3746 return phone.getPhoneType();
3747 }
3748 } finally {
3749 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003750 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003751 }
3752
3753 /**
3754 * Returns the CDMA ERI icon index to display
3755 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003756 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003757 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3758 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3759 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003760 }
3761
Sanket Padawe356d7632015-06-22 14:03:32 -07003762 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003763 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3764 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003765 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003766 mApp, subId, callingPackage, callingFeatureId,
3767 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003768 return -1;
3769 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003770
3771 final long identity = Binder.clearCallingIdentity();
3772 try {
3773 final Phone phone = getPhone(subId);
3774 if (phone != null) {
3775 return phone.getCdmaEriIconIndex();
3776 } else {
3777 return -1;
3778 }
3779 } finally {
3780 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003781 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003782 }
3783
3784 /**
3785 * Returns the CDMA ERI icon mode,
3786 * 0 - ON
3787 * 1 - FLASHING
3788 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003789 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003790 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3791 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3792 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003793 }
3794
Sanket Padawe356d7632015-06-22 14:03:32 -07003795 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003796 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3797 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003798 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003799 mApp, subId, callingPackage, callingFeatureId,
3800 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003801 return -1;
3802 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003803
3804 final long identity = Binder.clearCallingIdentity();
3805 try {
3806 final Phone phone = getPhone(subId);
3807 if (phone != null) {
3808 return phone.getCdmaEriIconMode();
3809 } else {
3810 return -1;
3811 }
3812 } finally {
3813 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003814 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003815 }
3816
3817 /**
3818 * Returns the CDMA ERI text,
3819 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003820 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003821 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3822 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3823 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003824 }
3825
Sanket Padawe356d7632015-06-22 14:03:32 -07003826 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003827 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3828 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003829 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003830 mApp, subId, callingPackage, callingFeatureId,
3831 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003832 return null;
3833 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003834
3835 final long identity = Binder.clearCallingIdentity();
3836 try {
3837 final Phone phone = getPhone(subId);
3838 if (phone != null) {
3839 return phone.getCdmaEriText();
3840 } else {
3841 return null;
3842 }
3843 } finally {
3844 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003845 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003846 }
3847
3848 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003849 * Returns the CDMA MDN.
3850 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003851 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003852 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003853 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3854 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003855
3856 final long identity = Binder.clearCallingIdentity();
3857 try {
3858 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003859 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003860 return phone.getLine1Number();
3861 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003862 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003863 return null;
3864 }
3865 } finally {
3866 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003867 }
3868 }
3869
3870 /**
3871 * Returns the CDMA MIN.
3872 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003873 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003874 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003875 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3876 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003877
3878 final long identity = Binder.clearCallingIdentity();
3879 try {
3880 final Phone phone = getPhone(subId);
3881 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3882 return phone.getCdmaMin();
3883 } else {
3884 return null;
3885 }
3886 } finally {
3887 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003888 }
3889 }
3890
Hall Liud892bec2018-11-30 14:51:45 -08003891 @Override
3892 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3893 INumberVerificationCallback callback, String callingPackage) {
3894 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3895 != PERMISSION_GRANTED) {
3896 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3897 }
3898 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3899
3900 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3901 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003902 throw new SecurityException("Calling package must be configured in the device config: "
3903 + "calling package: " + callingPackage
3904 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003905 }
3906
3907 if (range == null) {
3908 throw new NullPointerException("Range must be non-null");
3909 }
3910
3911 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003912 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003913
3914 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3915 }
3916
Junda Liuca05d5d2014-08-14 22:36:34 -07003917 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003918 * Returns true if CDMA provisioning needs to run.
3919 */
3920 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003921 final long identity = Binder.clearCallingIdentity();
3922 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003923 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003924 } finally {
3925 Binder.restoreCallingIdentity(identity);
3926 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003927 }
3928
3929 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003930 * Sets the voice mail number of a given subId.
3931 */
3932 @Override
3933 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003934 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3935 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003936
3937 final long identity = Binder.clearCallingIdentity();
3938 try {
3939 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3940 new Pair<String, String>(alphaTag, number), new Integer(subId));
3941 return success;
3942 } finally {
3943 Binder.restoreCallingIdentity(identity);
3944 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003945 }
3946
Ta-wei Yen87c49842016-05-13 21:19:52 -07003947 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003948 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3949 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003950 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3951 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003952 if (!TextUtils.equals(callingPackage, systemDialer)) {
3953 throw new SecurityException("caller must be system dialer");
3954 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003955
3956 final long identity = Binder.clearCallingIdentity();
3957 try {
3958 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3959 if (phoneAccountHandle == null) {
3960 return null;
3961 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003962 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003963 } finally {
3964 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003965 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003966 }
3967
3968 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003969 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3970 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003971 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003972 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003973 mApp, subId, callingPackage, callingFeatureId,
3974 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003975 return null;
3976 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003977
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003978 final long identity = Binder.clearCallingIdentity();
3979 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003980 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003981 } finally {
3982 Binder.restoreCallingIdentity(identity);
3983 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003984 }
3985
3986 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003987 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3988 VisualVoicemailSmsFilterSettings settings) {
3989 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003990
3991 final long identity = Binder.clearCallingIdentity();
3992 try {
3993 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003994 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003995 } finally {
3996 Binder.restoreCallingIdentity(identity);
3997 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003998 }
3999
4000 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004001 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4002 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004003
4004 final long identity = Binder.clearCallingIdentity();
4005 try {
4006 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004007 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004008 } finally {
4009 Binder.restoreCallingIdentity(identity);
4010 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004011 }
4012
4013 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004014 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4015 String callingPackage, int subId) {
4016 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004017
4018 final long identity = Binder.clearCallingIdentity();
4019 try {
4020 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004021 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004022 } finally {
4023 Binder.restoreCallingIdentity(identity);
4024 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004025 }
4026
4027 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004028 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004029 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004030
4031 final long identity = Binder.clearCallingIdentity();
4032 try {
4033 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004034 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004035 } finally {
4036 Binder.restoreCallingIdentity(identity);
4037 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004038 }
4039
4040 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004041 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4042 String callingAttributionTag, int subId, String number, int port, String text,
4043 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004044 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004045 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004046 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004047 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004048 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4049 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004050 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004051
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004052 /**
fionaxu0152e512016-11-14 13:36:14 -08004053 * Sets the voice activation state of a given subId.
4054 */
4055 @Override
4056 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004057 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4058 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004059
4060 final long identity = Binder.clearCallingIdentity();
4061 try {
4062 final Phone phone = getPhone(subId);
4063 if (phone != null) {
4064 phone.setVoiceActivationState(activationState);
4065 } else {
4066 loge("setVoiceActivationState fails with invalid subId: " + subId);
4067 }
4068 } finally {
4069 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004070 }
4071 }
4072
4073 /**
4074 * Sets the data activation state of a given subId.
4075 */
4076 @Override
4077 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004078 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4079 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004080
4081 final long identity = Binder.clearCallingIdentity();
4082 try {
4083 final Phone phone = getPhone(subId);
4084 if (phone != null) {
4085 phone.setDataActivationState(activationState);
4086 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004087 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004088 }
4089 } finally {
4090 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004091 }
4092 }
4093
4094 /**
4095 * Returns the voice activation state of a given subId.
4096 */
4097 @Override
4098 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004099 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004100
fionaxu0152e512016-11-14 13:36:14 -08004101 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004102 final long identity = Binder.clearCallingIdentity();
4103 try {
4104 if (phone != null) {
4105 return phone.getVoiceActivationState();
4106 } else {
4107 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4108 }
4109 } finally {
4110 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004111 }
4112 }
4113
4114 /**
4115 * Returns the data activation state of a given subId.
4116 */
4117 @Override
4118 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004119 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004120
fionaxu0152e512016-11-14 13:36:14 -08004121 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004122 final long identity = Binder.clearCallingIdentity();
4123 try {
4124 if (phone != null) {
4125 return phone.getDataActivationState();
4126 } else {
4127 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4128 }
4129 } finally {
4130 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004131 }
4132 }
4133
4134 /**
Wink Saville36469e72014-06-11 15:17:00 -07004135 * Returns the unread count of voicemails for a subId
4136 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004137 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004138 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4139 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004140 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004141 mApp, subId, callingPackage, callingFeatureId,
4142 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004143 return 0;
4144 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004145 final long identity = Binder.clearCallingIdentity();
4146 try {
4147 final Phone phone = getPhone(subId);
4148 if (phone != null) {
4149 return phone.getVoiceMessageCount();
4150 } else {
4151 return 0;
4152 }
4153 } finally {
4154 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004155 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004156 }
4157
4158 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004159 * returns true, if the device is in a state where both voice and data
4160 * are supported simultaneously. This can change based on location or network condition.
4161 */
4162 @Override
4163 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004164 final long identity = Binder.clearCallingIdentity();
4165 try {
4166 final Phone phone = getPhone(subId);
4167 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4168 } finally {
4169 Binder.restoreCallingIdentity(identity);
4170 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004171 }
4172
4173 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004174 * Send the dialer code if called from the current default dialer or the caller has
4175 * carrier privilege.
4176 * @param inputCode The dialer code to send
4177 */
4178 @Override
4179 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004180 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004181 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004182 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4183 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004184 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004185 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004186 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004187 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004188
4189 final long identity = Binder.clearCallingIdentity();
4190 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004191 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004192 } finally {
4193 Binder.restoreCallingIdentity(identity);
4194 }
fionaxu235cc5e2017-03-06 22:25:57 -08004195 }
4196
Pengquan Menga1bb6272018-09-06 09:59:22 -07004197 @Override
4198 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004199 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004200 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004201 mApp, subId, "getNetworkSelectionMode");
4202 final long identity = Binder.clearCallingIdentity();
4203 try {
4204 if (!isActiveSubscription(subId)) {
4205 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4206 }
4207 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4208 } finally {
4209 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004210 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004211 }
4212
Brad Ebinger35c841c2018-10-01 10:40:55 -07004213 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004214 public boolean isInEmergencySmsMode() {
4215 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4216 final long identity = Binder.clearCallingIdentity();
4217 try {
4218 for (Phone phone : PhoneFactory.getPhones()) {
4219 if (phone.isInEmergencySmsMode()) {
4220 return true;
4221 }
4222 }
4223 } finally {
4224 Binder.restoreCallingIdentity(identity);
4225 }
4226 return false;
4227 }
4228
shilu366312e2019-12-17 09:28:10 -08004229 /**
4230 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4231 * @param subId The subscription to use to check the configuration.
4232 * @param c The callback that will be used to send the result.
4233 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004234 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004235 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4236 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004237 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004238 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004239
4240 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4241 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4242 "IMS not available on device.");
4243 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004244 final long token = Binder.clearCallingIdentity();
4245 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004246 int slotId = getSlotIndexOrException(subId);
4247 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004248
4249 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4250 if (controller != null) {
4251 ImsManager imsManager = controller.getImsManager(subId);
4252 if (imsManager != null) {
4253 imsManager.addRegistrationCallbackForSubscription(c, subId);
4254 } else {
4255 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4256 }
4257 } else {
4258 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4259 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004260 } catch (ImsException e) {
4261 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004262 } finally {
4263 Binder.restoreCallingIdentity(token);
4264 }
4265 }
4266
shilu366312e2019-12-17 09:28:10 -08004267 /**
4268 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4269 * @param subId The subscription to use to check the configuration.
4270 * @param c The callback that will be used to send the result.
4271 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004272 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004273 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004274 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004275 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004276 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4277 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4278 }
Meng Wangafbc5852019-09-19 17:37:13 -07004279 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004280
Meng Wangafbc5852019-09-19 17:37:13 -07004281 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004282 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4283 if (controller != null) {
4284 ImsManager imsManager = controller.getImsManager(subId);
4285 if (imsManager != null) {
4286 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4287 } else {
4288 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4289 + "is inactive, ignoring unregister.");
4290 // If the ImsManager is not valid, just return, since the callback
4291 // will already have been removed internally.
4292 }
4293 }
Meng Wangafbc5852019-09-19 17:37:13 -07004294 } finally {
4295 Binder.restoreCallingIdentity(token);
4296 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004297 }
4298
Brad Ebingera34a6c22019-10-22 17:36:18 -07004299 /**
4300 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4301 */
4302 @Override
4303 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4304 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4305 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4306 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4307 "IMS not available on device.");
4308 }
4309 final long token = Binder.clearCallingIdentity();
4310 try {
4311 Phone phone = getPhone(subId);
4312 if (phone == null) {
4313 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4314 + subId + "'");
4315 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4316 }
4317 phone.getImsRegistrationState(regState -> {
4318 try {
4319 consumer.accept((regState == null)
4320 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4321 } catch (RemoteException e) {
4322 // Ignore if the remote process is no longer available to call back.
4323 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4324 }
4325 });
4326 } finally {
4327 Binder.restoreCallingIdentity(token);
4328 }
4329 }
4330
4331 /**
4332 * Get the transport type for the IMS service registration state.
4333 */
4334 @Override
4335 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004336 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004337 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004338 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4339 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4340 "IMS not available on device.");
4341 }
4342 final long token = Binder.clearCallingIdentity();
4343 try {
4344 Phone phone = getPhone(subId);
4345 if (phone == null) {
4346 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4347 + subId + "'");
4348 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4349 }
4350 phone.getImsRegistrationTech(regTech -> {
4351 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4352 int regTechConverted = (regTech == null)
4353 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4354 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4355 regTechConverted);
4356 try {
4357 consumer.accept(regTechConverted);
4358 } catch (RemoteException e) {
4359 // Ignore if the remote process is no longer available to call back.
4360 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4361 }
4362 });
4363 } finally {
4364 Binder.restoreCallingIdentity(token);
4365 }
4366 }
4367
shilu366312e2019-12-17 09:28:10 -08004368 /**
4369 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4370 * @param subId The subscription to use to check the configuration.
4371 * @param c The callback that will be used to send the result.
4372 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004373 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004374 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4375 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004376 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004377 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004378 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4379 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4380 "IMS not available on device.");
4381 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004382 final long token = Binder.clearCallingIdentity();
4383 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004384 int slotId = getSlotIndexOrException(subId);
4385 verifyImsMmTelConfiguredOrThrow(slotId);
4386 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004387 } catch (ImsException e) {
4388 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004389 } finally {
4390 Binder.restoreCallingIdentity(token);
4391 }
4392 }
4393
shilu366312e2019-12-17 09:28:10 -08004394 /**
4395 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4396 * @param subId The subscription to use to check the configuration.
4397 * @param c The callback that will be used to send the result.
4398 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004399 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004400 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004401 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004402 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004403 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4404 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4405 }
Meng Wangafbc5852019-09-19 17:37:13 -07004406
4407 final long token = Binder.clearCallingIdentity();
4408 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004409 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004410 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004411 } catch (ImsException e) {
4412 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4413 + "is inactive, ignoring unregister.");
4414 // If the subscription is no longer active, just return, since the callback
4415 // will already have been removed internally.
4416 } finally {
4417 Binder.restoreCallingIdentity(token);
4418 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004419 }
4420
4421 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004422 public boolean isCapable(int subId, int capability, int regTech) {
4423 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004424 final long token = Binder.clearCallingIdentity();
4425 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004426 int slotId = getSlotIndexOrException(subId);
4427 verifyImsMmTelConfiguredOrThrow(slotId);
4428 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4429 } catch (com.android.ims.ImsException e) {
4430 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4431 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004432 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004433 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4434 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004435 } finally {
4436 Binder.restoreCallingIdentity(token);
4437 }
4438 }
4439
4440 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004441 public boolean isAvailable(int subId, int capability, int regTech) {
4442 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004443 final long token = Binder.clearCallingIdentity();
4444 try {
4445 Phone phone = getPhone(subId);
4446 if (phone == null) return false;
4447 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004448 } catch (com.android.ims.ImsException e) {
4449 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4450 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004451 } finally {
4452 Binder.restoreCallingIdentity(token);
4453 }
4454 }
4455
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004456 /**
4457 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4458 * subscription.
4459 * @param subId The subscription to use to check the configuration.
4460 * @param callback The callback that will be used to send the result.
4461 * @param capability The MmTelFeature capability that will be used to send the result.
4462 * @param transportType The transport type of the MmTelFeature capability.
4463 */
4464 @Override
4465 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4466 int transportType) {
4467 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004468 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4469 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4470 "IMS not available on device.");
4471 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004472 final long token = Binder.clearCallingIdentity();
4473 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004474 int slotId = getSlotIndex(subId);
4475 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4476 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4477 + subId + "'");
4478 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4479 }
4480 verifyImsMmTelConfiguredOrThrow(slotId);
4481 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4482 transportType, aBoolean -> {
4483 try {
4484 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4485 } catch (RemoteException e) {
4486 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4487 + "running. Ignore");
4488 }
4489 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004490 } catch (ImsException e) {
4491 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004492 } finally {
4493 Binder.restoreCallingIdentity(token);
4494 }
4495 }
4496
shilu366312e2019-12-17 09:28:10 -08004497 /**
4498 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4499 * @param subId The subscription to use to check the configuration.
4500 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004501 @Override
4502 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004503 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004504 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004505
Brad Ebinger35c841c2018-10-01 10:40:55 -07004506 final long token = Binder.clearCallingIdentity();
4507 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004508 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004509 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004510 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004511 } catch (ImsException e) {
4512 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004513 } finally {
4514 Binder.restoreCallingIdentity(token);
4515 }
4516 }
4517
4518 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004519 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004520 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004521 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004522 final long identity = Binder.clearCallingIdentity();
4523 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004524 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004525 // This setting doesn't require an active ImsService connection, so do not verify. The
4526 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004527 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004528 } catch (ImsException e) {
4529 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004530 } finally {
4531 Binder.restoreCallingIdentity(identity);
4532 }
4533 }
4534
shilu366312e2019-12-17 09:28:10 -08004535 /**
4536 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4537 * @param subId The subscription to use to check the configuration.
4538 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004539 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004540 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004541 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004542 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004543 final long identity = Binder.clearCallingIdentity();
4544 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004545 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004546 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004547 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004548 } catch (ImsException e) {
4549 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004550 } finally {
4551 Binder.restoreCallingIdentity(identity);
4552 }
4553 }
4554
4555 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004556 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004557 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004558 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004559 final long identity = Binder.clearCallingIdentity();
4560 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004561 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004562 // This setting doesn't require an active ImsService connection, so do not verify. The
4563 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004564 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004565 } catch (ImsException e) {
4566 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004567 } finally {
4568 Binder.restoreCallingIdentity(identity);
4569 }
4570 }
4571
shilu366312e2019-12-17 09:28:10 -08004572 /**
4573 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4574 * @param subId The subscription to use to check the configuration.
4575 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004576 @Override
4577 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004578 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004579 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004580 final long identity = Binder.clearCallingIdentity();
4581 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004582 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004583 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004584 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004585 } catch (ImsException e) {
4586 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004587 } finally {
4588 Binder.restoreCallingIdentity(identity);
4589 }
4590 }
4591
4592 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004593 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004594 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004595 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004596 final long identity = Binder.clearCallingIdentity();
4597 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004598 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004599 // This setting doesn't require an active ImsService connection, so do not verify. The
4600 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004601 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004602 } catch (ImsException e) {
4603 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004604 } finally {
4605 Binder.restoreCallingIdentity(identity);
4606 }
4607 }
4608
shilu366312e2019-12-17 09:28:10 -08004609 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004610 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4611 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4612 * @param subId The subscription to use to check the configuration.
4613 */
4614 @Override
4615 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004616 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004617 mApp, subId, "isCrossSimCallingEnabledByUser");
4618 final long identity = Binder.clearCallingIdentity();
4619 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004620 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004621 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004622 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004623 } catch (ImsException e) {
4624 throw new ServiceSpecificException(e.getCode());
4625 } finally {
4626 Binder.restoreCallingIdentity(identity);
4627 }
4628 }
4629
4630 /**
4631 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4632 * Requires MODIFY_PHONE_STATE permission.
4633 * @param subId The subscription to use to check the configuration.
4634 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4635 * false otherwise
4636 */
4637 @Override
4638 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4639 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4640 "setCrossSimCallingEnabled");
4641 final long identity = Binder.clearCallingIdentity();
4642 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004643 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004644 // This setting doesn't require an active ImsService connection, so do not verify. The
4645 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004646 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004647 } catch (ImsException e) {
4648 throw new ServiceSpecificException(e.getCode());
4649 } finally {
4650 Binder.restoreCallingIdentity(identity);
4651 }
4652 }
4653
4654 /**
shilu366312e2019-12-17 09:28:10 -08004655 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4656 * @param subId The subscription to use to check the configuration.
4657 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004658 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004659
Brad Ebinger35c841c2018-10-01 10:40:55 -07004660 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004661 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004662 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004663 final long identity = Binder.clearCallingIdentity();
4664 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004665 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004666 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004667 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004668 } catch (ImsException e) {
4669 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004670 } finally {
4671 Binder.restoreCallingIdentity(identity);
4672 }
4673 }
4674
4675 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004676 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004677 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004678 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004679 final long identity = Binder.clearCallingIdentity();
4680 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004681 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004682 // This setting doesn't require an active ImsService connection, so do not verify. The
4683 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004684 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004685 } catch (ImsException e) {
4686 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004687 } finally {
4688 Binder.restoreCallingIdentity(identity);
4689 }
4690 }
4691
4692 @Override
4693 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4694 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4695 "setVoWiFiNonPersistent");
4696 final long identity = Binder.clearCallingIdentity();
4697 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004698 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004699 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004700 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004701 } catch (ImsException e) {
4702 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004703 } finally {
4704 Binder.restoreCallingIdentity(identity);
4705 }
4706 }
4707
shilu366312e2019-12-17 09:28:10 -08004708 /**
4709 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4710 * @param subId The subscription to use to check the configuration.
4711 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004712 @Override
4713 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004714 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004715 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004716 final long identity = Binder.clearCallingIdentity();
4717 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004718 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004719 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004720 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004721 } catch (ImsException e) {
4722 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004723 } finally {
4724 Binder.restoreCallingIdentity(identity);
4725 }
4726 }
4727
4728 @Override
4729 public void setVoWiFiModeSetting(int subId, int mode) {
4730 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4731 "setVoWiFiModeSetting");
4732 final long identity = Binder.clearCallingIdentity();
4733 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004734 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004735 // This setting doesn't require an active ImsService connection, so do not verify. The
4736 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004737 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004738 } catch (ImsException e) {
4739 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004740 } finally {
4741 Binder.restoreCallingIdentity(identity);
4742 }
4743 }
4744
4745 @Override
4746 public int getVoWiFiRoamingModeSetting(int subId) {
4747 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4748 final long identity = Binder.clearCallingIdentity();
4749 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004750 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004751 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004752 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004753 } catch (ImsException e) {
4754 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004755 } finally {
4756 Binder.restoreCallingIdentity(identity);
4757 }
4758 }
4759
4760 @Override
4761 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4762 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4763 "setVoWiFiRoamingModeSetting");
4764 final long identity = Binder.clearCallingIdentity();
4765 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004766 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004767 // This setting doesn't require an active ImsService connection, so do not verify. The
4768 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004769 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004770 } catch (ImsException e) {
4771 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004772 } finally {
4773 Binder.restoreCallingIdentity(identity);
4774 }
4775 }
4776
4777 @Override
4778 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4779 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4780 "setRttCapabilityEnabled");
4781 final long identity = Binder.clearCallingIdentity();
4782 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004783 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004784 // This setting doesn't require an active ImsService connection, so do not verify. The
4785 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004786 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004787 } catch (ImsException e) {
4788 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004789 } finally {
4790 Binder.restoreCallingIdentity(identity);
4791 }
4792 }
4793
shilu366312e2019-12-17 09:28:10 -08004794 /**
4795 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4796 * @param subId The subscription to use to check the configuration.
4797 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004798 @Override
4799 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004800 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004801 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004802 final long identity = Binder.clearCallingIdentity();
4803 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004804 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004805 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004806 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004807 } catch (ImsException e) {
4808 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004809 } finally {
4810 Binder.restoreCallingIdentity(identity);
4811 }
4812 }
4813
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004814 @Override
4815 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4816 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004817
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004818 final long identity = Binder.clearCallingIdentity();
4819 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004820 if (!isImsAvailableOnDevice()) {
4821 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4822 "IMS not available on device.");
4823 }
4824 int slotId = getSlotIndexOrException(subId);
4825 verifyImsMmTelConfiguredOrThrow(slotId);
4826 ImsManager.getInstance(mApp, slotId)
4827 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004828 } catch (ImsException e) {
4829 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004830 } finally {
4831 Binder.restoreCallingIdentity(identity);
4832 }
4833 }
4834
4835 @Override
4836 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4837 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004838
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004839 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004840 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4841 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4842 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004843 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004844 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004845 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004846 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004847 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4848 + "is inactive, ignoring unregister.");
4849 // If the subscription is no longer active, just return, since the callback will already
4850 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004851 } finally {
4852 Binder.restoreCallingIdentity(identity);
4853 }
4854 }
4855
joonhunshincffb7fc2021-11-28 07:32:01 +00004856 @Override
4857 public void registerFeatureProvisioningChangedCallback(int subId,
4858 IFeatureProvisioningCallback callback) {
4859 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4860 mApp, subId, "registerFeatureProvisioningChangedCallback");
4861
4862 final long identity = Binder.clearCallingIdentity();
4863 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4864 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4865 }
4866
joonhunshin91bc1952022-04-29 08:47:15 +00004867 try {
4868 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4869 if (controller == null) {
4870 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4871 "Device does not support IMS");
4872 }
4873 controller.addFeatureProvisioningChangedCallback(subId, callback);
4874 } finally {
4875 Binder.restoreCallingIdentity(identity);
4876 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004877 }
4878
4879 @Override
4880 public void unregisterFeatureProvisioningChangedCallback(int subId,
4881 IFeatureProvisioningCallback callback) {
4882 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4883 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4884
4885 final long identity = Binder.clearCallingIdentity();
4886 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4887 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4888 }
4889
joonhunshin91bc1952022-04-29 08:47:15 +00004890 try {
4891 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4892 if (controller == null) {
4893 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4894 return;
4895 }
4896 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4897 } finally {
4898 Binder.restoreCallingIdentity(identity);
4899 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004900 }
allenwtsu99c623b2020-01-03 18:24:23 +08004901
4902 private void checkModifyPhoneStatePermission(int subId, String message) {
4903 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4904 message);
4905 }
4906
allenwtsu99c623b2020-01-03 18:24:23 +08004907 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004908 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004909 boolean isProvisioned) {
4910 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4911
4912 final long identity = Binder.clearCallingIdentity();
4913 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004914 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4915 if (controller == null) {
4916 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4917 return;
4918 }
4919 controller.setRcsProvisioningStatusForCapability(
4920 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004921 } finally {
4922 Binder.restoreCallingIdentity(identity);
4923 }
allenwtsu99c623b2020-01-03 18:24:23 +08004924 }
4925
4926
4927 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004928 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4929 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4930 mApp, subId, "getRcsProvisioningStatusForCapability");
4931
allenwtsu99c623b2020-01-03 18:24:23 +08004932 final long identity = Binder.clearCallingIdentity();
4933 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004934 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4935 if (controller == null) {
4936 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4937
4938 // device does not support IMS, this method will return true always.
4939 return true;
4940 }
4941 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004942 } finally {
4943 Binder.restoreCallingIdentity(identity);
4944 }
4945 }
4946
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004947 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004948 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4949 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004950 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004951
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004952 final long identity = Binder.clearCallingIdentity();
4953 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004954 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4955 if (controller == null) {
4956 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4957 return;
4958 }
4959 controller.setImsProvisioningStatusForCapability(
4960 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004961 } finally {
4962 Binder.restoreCallingIdentity(identity);
4963 }
4964 }
4965
4966 @Override
4967 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004968 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4969 mApp, subId, "getProvisioningStatusForCapability");
4970
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004971 final long identity = Binder.clearCallingIdentity();
4972 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004973 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4974 if (controller == null) {
4975 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004976
joonhunshin91bc1952022-04-29 08:47:15 +00004977 // device does not support IMS, this method will return true always.
4978 return true;
4979 }
4980 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004981 } finally {
4982 Binder.restoreCallingIdentity(identity);
4983 }
4984 }
4985
4986 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004987 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
4988 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4989 mApp, subId, "isProvisioningRequiredForCapability");
4990
4991 final long identity = Binder.clearCallingIdentity();
4992 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004993 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4994 if (controller == null) {
4995 loge("isProvisioningRequiredForCapability: Device does not support IMS");
4996
4997 // device does not support IMS, this method will return false
4998 return false;
4999 }
5000 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005001 } finally {
5002 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005003 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005004 }
5005
5006 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005007 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5008 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5009 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005010
joonhunshincffb7fc2021-11-28 07:32:01 +00005011 final long identity = Binder.clearCallingIdentity();
5012 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005013 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5014 if (controller == null) {
5015 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5016
5017 // device does not support IMS, this method will return false
5018 return false;
5019 }
5020 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005021 } finally {
5022 Binder.restoreCallingIdentity(identity);
5023 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005024 }
5025
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005026 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005027 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005028 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5029 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5030 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005031 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5032 mApp, subId, "getImsProvisioningInt");
5033
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005034 final long identity = Binder.clearCallingIdentity();
5035 try {
5036 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005037 int slotId = getSlotIndex(subId);
5038 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5039 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5040 + subId + "' for key:" + key);
5041 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5042 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005043
joonhunshin91bc1952022-04-29 08:47:15 +00005044 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5045 if (controller == null) {
5046 loge("getImsProvisioningInt: Device does not support IMS");
5047
5048 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5049 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5050 }
5051 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005052 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5053 return retVal;
5054 }
5055
calvinpanb5a34062021-02-08 19:59:36 +08005056 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005057 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005058 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5059 + subId + "' for key:" + key);
5060 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005061 } finally {
5062 Binder.restoreCallingIdentity(identity);
5063 }
5064 }
5065
5066 @Override
5067 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005068 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5069 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5070 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005071 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5072 mApp, subId, "getImsProvisioningString");
5073
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005074 final long identity = Binder.clearCallingIdentity();
5075 try {
5076 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005077 int slotId = getSlotIndex(subId);
5078 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5079 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5080 + subId + "' for key:" + key);
5081 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5082 }
calvinpanb5a34062021-02-08 19:59:36 +08005083 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005084 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005085 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5086 + subId + "' for key:" + key);
5087 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005088 } finally {
5089 Binder.restoreCallingIdentity(identity);
5090 }
5091 }
5092
5093 @Override
5094 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005095 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5096 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5097 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005098 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5099 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005100
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005101 final long identity = Binder.clearCallingIdentity();
5102 try {
5103 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005104 int slotId = getSlotIndex(subId);
5105 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5106 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5107 + subId + "' for key:" + key);
5108 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5109 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005110
joonhunshin91bc1952022-04-29 08:47:15 +00005111 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5112 if (controller == null) {
5113 loge("setImsProvisioningInt: Device does not support IMS");
5114
5115 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5116 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5117 }
5118 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005119 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5120 return retVal;
5121 }
5122
calvinpanb5a34062021-02-08 19:59:36 +08005123 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5124 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005125 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005126 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005127 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005128 } finally {
5129 Binder.restoreCallingIdentity(identity);
5130 }
5131 }
5132
5133 @Override
5134 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005135 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5136 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5137 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005138 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5139 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005140 final long identity = Binder.clearCallingIdentity();
5141 try {
5142 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005143 int slotId = getSlotIndex(subId);
5144 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5145 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5146 + subId + "' for key:" + key);
5147 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5148 }
calvinpanb5a34062021-02-08 19:59:36 +08005149 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5150 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005151 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005152 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005153 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005154 } finally {
5155 Binder.restoreCallingIdentity(identity);
5156 }
5157 }
5158
Brad Ebinger919631e2021-06-02 17:46:35 -07005159 /**
5160 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5161 * for the given slot ID or no ImsResolver instance has been created.
5162 * @param slotId The slot ID that the IMS service is created for.
5163 * @throws ImsException If there is no ImsService configured for this slot.
5164 */
5165 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5166 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5167 ImsFeature.FEATURE_MMTEL)) {
5168 throw new ImsException("This subscription does not support MMTEL over IMS",
5169 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5170 }
5171 }
5172
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005173 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005174 int slotId = SubscriptionManager.getSlotIndex(subId);
5175 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005176 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5177 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005178 }
5179 return slotId;
5180 }
5181
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005182 private int getSlotIndex(int subId) {
5183 int slotId = SubscriptionManager.getSlotIndex(subId);
5184 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5185 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5186 }
5187 return slotId;
5188 }
5189
Wink Saville36469e72014-06-11 15:17:00 -07005190 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005191 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005192 */
5193 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005194 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5195 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005196 try {
5197 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5198 } catch (SecurityException se) {
5199 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5200 throw new SecurityException("Package " + callingPackage + " does not belong to "
5201 + Binder.getCallingUid());
5202 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005203 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005204 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005205 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005206 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005207 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005208 mApp, subId, callingPackage, callingFeatureId,
5209 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005210 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5211 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005212
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005213 final long identity = Binder.clearCallingIdentity();
5214 try {
5215 final Phone phone = getPhone(subId);
5216 if (phone != null) {
5217 return phone.getServiceState().getDataNetworkType();
5218 } else {
5219 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5220 }
5221 } finally {
5222 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005224 }
5225
5226 /**
5227 * Returns the data network type
5228 */
5229 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005230 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005231 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005232 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005233 }
5234
5235 /**
5236 * Returns the data network type for a subId
5237 */
5238 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005239 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5240 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005241 String functionName = "getDataNetworkTypeForSubscriber";
5242 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5243 mApp, functionName)) {
5244 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5245 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5246 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5247 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005248 }
5249
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005250 final long identity = Binder.clearCallingIdentity();
5251 try {
5252 final Phone phone = getPhone(subId);
5253 if (phone != null) {
5254 return phone.getServiceState().getDataNetworkType();
5255 } else {
5256 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5257 }
5258 } finally {
5259 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005261 }
5262
5263 /**
Wink Saville36469e72014-06-11 15:17:00 -07005264 * Returns the Voice network type for a subId
5265 */
5266 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005267 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5268 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005269 String functionName = "getVoiceNetworkTypeForSubscriber";
5270 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5271 mApp, functionName)) {
5272 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5273 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5274 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5275 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005276 }
5277
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005278 final long identity = Binder.clearCallingIdentity();
5279 try {
5280 final Phone phone = getPhone(subId);
5281 if (phone != null) {
5282 return phone.getServiceState().getVoiceNetworkType();
5283 } else {
5284 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5285 }
5286 } finally {
5287 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005289 }
5290
5291 /**
5292 * @return true if a ICC card is present
5293 */
5294 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005295 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005296 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005297 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005298 }
5299
5300 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005301 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005302 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005303 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005304 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005305 final long identity = Binder.clearCallingIdentity();
5306 try {
5307 final Phone phone = PhoneFactory.getPhone(slotIndex);
5308 if (phone != null) {
5309 return phone.getIccCard().hasIccCard();
5310 } else {
5311 return false;
5312 }
5313 } finally {
5314 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005315 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005316 }
5317
5318 /**
5319 * Return if the current radio is LTE on CDMA. This
5320 * is a tri-state return value as for a period of time
5321 * the mode may be unknown.
5322 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005323 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005324 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005325 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005326 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005327 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005328 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5329 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5330 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005331 }
5332
Sanket Padawe356d7632015-06-22 14:03:32 -07005333 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005334 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5335 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005336 try {
5337 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5338 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005339 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5340 }
5341
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005342 final long identity = Binder.clearCallingIdentity();
5343 try {
5344 final Phone phone = getPhone(subId);
5345 if (phone == null) {
5346 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5347 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005348 return TelephonyProperties.lte_on_cdma_device()
5349 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005350 }
5351 } finally {
5352 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005353 }
Wink Saville36469e72014-06-11 15:17:00 -07005354 }
5355
Wink Saville36469e72014-06-11 15:17:00 -07005356 /**
5357 * {@hide}
5358 * Returns Default subId, 0 in the case of single standby.
5359 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005360 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005361 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005362 }
5363
Shishir Agrawala9f32182016-04-12 12:00:16 -07005364 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005365 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005366 }
5367
Wink Savilleb564aae2014-10-23 10:18:09 -07005368 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005369 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005370 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005371
Pengquan Menge92a50d2018-09-21 15:54:48 -07005372 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005373 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5374 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5375 mApp.getOpPackageName(), mApp.getFeatureId());
5376 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005377 return mSubscriptionController.isActiveSubId(subId);
5378 }
5379
Ihab Awadf2177b72013-11-25 13:33:23 -08005380 /**
5381 * @see android.telephony.TelephonyManager.WifiCallingChoices
5382 */
5383 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005384 final long identity = Binder.clearCallingIdentity();
5385 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005386 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005387 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5388 getWhenToMakeWifiCallsDefaultPreference());
5389 } finally {
5390 Binder.restoreCallingIdentity(identity);
5391 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005392 }
5393
5394 /**
5395 * @see android.telephony.TelephonyManager.WifiCallingChoices
5396 */
5397 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005398 final long identity = Binder.clearCallingIdentity();
5399 try {
5400 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005401 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005402 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5403 } finally {
5404 Binder.restoreCallingIdentity(identity);
5405 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005406 }
5407
Sailesh Nepald1e68152013-12-12 19:08:02 -08005408 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005409 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005410 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005411 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005412
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005413 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5414 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5415 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005416 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005417 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5418 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005419 }
5420 return PhoneFactory.getPhone(phoneId);
5421 }
5422
Shishir Agrawal566b7612013-10-28 14:41:00 -07005423 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005424 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005425 @NonNull IccLogicalChannelRequest request) {
5426 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5427 /*message=*/ "iccOpenLogicalChannel");
5428
5429 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5430 // Verify that the callingPackage in the request belongs to the calling UID
5431 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5432
5433 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005434 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005435
Rambo Wanga1782702021-11-10 20:15:19 -08005436 private Phone getPhoneFromValidIccLogicalChannelRequest(
5437 @NonNull IccLogicalChannelRequest request, String message) {
5438 Phone phone;
5439 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5440 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5441 mApp, request.subId, message);
5442 phone = getPhoneFromSubId(request.subId);
5443 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5444 enforceModifyPermission();
5445 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5446 } else {
5447 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005448 }
Rambo Wanga1782702021-11-10 20:15:19 -08005449 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005450 }
5451
5452 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005453 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005454 final long identity = Binder.clearCallingIdentity();
5455 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005456 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005457 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005458 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5459 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005460 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5461 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005462 loge("The calling package is not allowed to access ISD-R.");
5463 throw new SecurityException(
5464 "The calling package is not allowed to access ISD-R.");
5465 }
Derek Tan740e1672017-06-27 14:56:27 -07005466 }
Derek Tan740e1672017-06-27 14:56:27 -07005467
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005468 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005469 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5470 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005471 return response;
5472 } finally {
5473 Binder.restoreCallingIdentity(identity);
5474 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005475 }
5476
5477 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005478 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5479 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5480 /*message=*/"iccCloseLogicalChannel");
5481
5482 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5483
5484 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005485 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005486
Rambo Wanga1782702021-11-10 20:15:19 -08005487 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5488 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005489 // before this feature is enabled, this API should only return false if
5490 // the operation fails instead of throwing runtime exception for
5491 // backward-compatibility.
5492 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5493 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005494 final long identity = Binder.clearCallingIdentity();
5495 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005496 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005497 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005498 }
Chen Xue9d737e2022-01-01 23:41:31 -08005499 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005500 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005501 Boolean success = false;
5502 if (result instanceof RuntimeException) {
5503 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005504 if (shouldThrowExceptionOnFailure) {
5505 throw (RuntimeException) result;
5506 } else {
5507 return false;
5508 }
Chen Xue9d737e2022-01-01 23:41:31 -08005509 } else if (result instanceof Boolean) {
5510 success = (Boolean) result;
5511 } else {
5512 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5513 }
Rambo Wanga1782702021-11-10 20:15:19 -08005514 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005515 return success;
5516 } finally {
5517 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005518 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005519 }
5520
5521 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005522 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005523 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005524 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5525 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005526 if (DBG) {
5527 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5528 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5529 + p3 + " data=" + data);
5530 }
5531 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5532 command, p1, p2, p3, data);
5533 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005534
Jordan Liu4c733742019-02-28 12:03:40 -08005535 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005536 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5537 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005538 enforceModifyPermission();
5539 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005540 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5541 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5542 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005543 }
5544 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005545 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5546 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005547 }
5548
5549 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5550 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005551 final long identity = Binder.clearCallingIdentity();
5552 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005553 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005554 return "";
5555 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005556
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005557 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005558 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5559 null /* workSource */);
5560 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005561
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005562 // Append the returned status code to the end of the response payload.
5563 String s = Integer.toHexString(
5564 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5565 if (response.payload != null) {
5566 s = IccUtils.bytesToHexString(response.payload) + s;
5567 }
5568 return s;
5569 } finally {
5570 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005571 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005572 }
Jake Hambye994d462014-02-03 13:10:13 -08005573
Evan Charltonc66da362014-05-16 14:06:40 -07005574 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005575 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5576 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005577 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5578 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005579 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005580 if (DBG) {
5581 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5582 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5583 }
5584 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5585 cla, command, p1, p2, p3, data);
5586 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005587
Jordan Liu4c733742019-02-28 12:03:40 -08005588 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005589 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5590 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005591 enforceModifyPermission();
5592 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5593 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005594 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5595 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5596 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005597 }
5598
5599 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005600 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5601 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005602 }
5603
5604 // open APDU basic channel assuming the caller has sufficient permissions
5605 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5606 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005607 final long identity = Binder.clearCallingIdentity();
5608 try {
5609 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5610 && TextUtils.equals(ISDR_AID, data)) {
5611 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005612 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5613 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005614 if (bestComponent == null
5615 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5616 loge("The calling package is not allowed to select ISD-R.");
5617 throw new SecurityException(
5618 "The calling package is not allowed to select ISD-R.");
5619 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005620 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005621
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005622 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005623 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5624 null /* workSource */);
5625 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005626
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005627 // Append the returned status code to the end of the response payload.
5628 String s = Integer.toHexString(
5629 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5630 if (response.payload != null) {
5631 s = IccUtils.bytesToHexString(response.payload) + s;
5632 }
5633 return s;
5634 } finally {
5635 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005636 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005637 }
5638
5639 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005640 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005641 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005642 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5643 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005644
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005645 final long identity = Binder.clearCallingIdentity();
5646 try {
5647 if (DBG) {
5648 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5649 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5650 }
5651
5652 IccIoResult response =
5653 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5654 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5655 subId);
5656
5657 if (DBG) {
5658 log("Exchange SIM_IO [R]" + response);
5659 }
5660
5661 byte[] result = null;
5662 int length = 2;
5663 if (response.payload != null) {
5664 length = 2 + response.payload.length;
5665 result = new byte[length];
5666 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5667 } else {
5668 result = new byte[length];
5669 }
5670
5671 result[length - 1] = (byte) response.sw2;
5672 result[length - 2] = (byte) response.sw1;
5673 return result;
5674 } finally {
5675 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005676 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005677 }
5678
Nathan Haroldb3014052017-01-25 15:57:32 -08005679 /**
5680 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5681 * on a particular subscription
5682 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005683 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5684 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005685 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005686 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005687 return null;
5688 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005689
5690 final long identity = Binder.clearCallingIdentity();
5691 try {
5692 if (appType != TelephonyManager.APPTYPE_USIM
5693 && appType != TelephonyManager.APPTYPE_SIM) {
5694 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5695 return null;
5696 }
5697 Object response = sendRequest(
5698 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5699 if (response instanceof String[]) {
5700 return (String[]) response;
5701 }
yincheng zhao2737e882019-09-06 17:06:54 -07005702 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005703 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005704 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005705 } finally {
5706 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005707 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005708 }
5709
yincheng zhao2737e882019-09-06 17:06:54 -07005710 /**
5711 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5712 * subscription.
5713 *
5714 * @param subId the id of the subscription.
5715 * @param appType the uicc app type, must be USIM or SIM.
5716 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5717 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005718 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005719 * @return number of fplmns that is successfully written to the SIM.
5720 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005721 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5722 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005723 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5724 mApp, subId, "setForbiddenPlmns");
5725
yincheng zhao2737e882019-09-06 17:06:54 -07005726 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5727 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5728 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5729 }
5730 if (fplmns == null) {
5731 throw new IllegalArgumentException("Fplmn List provided is null");
5732 }
5733 for (String fplmn : fplmns) {
5734 if (!CellIdentity.isValidPlmn(fplmn)) {
5735 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5736 }
5737 }
5738 final long identity = Binder.clearCallingIdentity();
5739 try {
5740 Object response = sendRequest(
5741 CMD_SET_FORBIDDEN_PLMNS,
5742 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5743 subId);
5744 return (int) response;
5745 } finally {
5746 Binder.restoreCallingIdentity(identity);
5747 }
5748 }
5749
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005750 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005751 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005752 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5753 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005754
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005755 final long identity = Binder.clearCallingIdentity();
5756 try {
5757 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5758 if (response.payload == null) {
5759 return "";
5760 }
Evan Charltonc66da362014-05-16 14:06:40 -07005761
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005762 // Append the returned status code to the end of the response payload.
5763 String s = Integer.toHexString(
5764 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5765 s = IccUtils.bytesToHexString(response.payload) + s;
5766 return s;
5767 } finally {
5768 Binder.restoreCallingIdentity(identity);
5769 }
Evan Charltonc66da362014-05-16 14:06:40 -07005770 }
5771
Jake Hambye994d462014-02-03 13:10:13 -08005772 /**
5773 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5774 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5775 *
5776 * @param itemID the ID of the item to read
5777 * @return the NV item as a String, or null on error.
5778 */
5779 @Override
5780 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005781 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005782 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5783 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005784
5785 final long identity = Binder.clearCallingIdentity();
5786 try {
5787 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005788 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005789 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5790 return value;
5791 } finally {
5792 Binder.restoreCallingIdentity(identity);
5793 }
Jake Hambye994d462014-02-03 13:10:13 -08005794 }
5795
5796 /**
5797 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5798 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5799 *
5800 * @param itemID the ID of the item to read
5801 * @param itemValue the value to write, as a String
5802 * @return true on success; false on any failure
5803 */
5804 @Override
5805 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005806 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005807 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5808 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005809
5810 final long identity = Binder.clearCallingIdentity();
5811 try {
5812 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5813 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005814 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005815 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5816 return success;
5817 } finally {
5818 Binder.restoreCallingIdentity(identity);
5819 }
Jake Hambye994d462014-02-03 13:10:13 -08005820 }
5821
5822 /**
5823 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5824 * Used for device configuration by some CDMA operators.
5825 *
5826 * @param preferredRoamingList byte array containing the new PRL
5827 * @return true on success; false on any failure
5828 */
5829 @Override
5830 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005831 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5832 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005833
5834 final long identity = Binder.clearCallingIdentity();
5835 try {
5836 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5837 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5838 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5839 return success;
5840 } finally {
5841 Binder.restoreCallingIdentity(identity);
5842 }
Jake Hambye994d462014-02-03 13:10:13 -08005843 }
5844
5845 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005846 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005847 * Used for device configuration by some CDMA operators.
5848 *
chen xu6dac5ab2018-10-26 17:39:23 -07005849 * @param slotIndex - device slot.
5850 *
Jake Hambye994d462014-02-03 13:10:13 -08005851 * @return true on success; false on any failure
5852 */
5853 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005854 public boolean resetModemConfig(int slotIndex) {
5855 Phone phone = PhoneFactory.getPhone(slotIndex);
5856 if (phone != null) {
5857 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5858 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005859
chen xu6dac5ab2018-10-26 17:39:23 -07005860 final long identity = Binder.clearCallingIdentity();
5861 try {
5862 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5863 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5864 return success;
5865 } finally {
5866 Binder.restoreCallingIdentity(identity);
5867 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005868 }
chen xu6dac5ab2018-10-26 17:39:23 -07005869 return false;
5870 }
5871
5872 /**
5873 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5874 *
5875 * @param slotIndex - device slot.
5876 *
5877 * @return true on success; false on any failure
5878 */
5879 @Override
5880 public boolean rebootModem(int slotIndex) {
5881 Phone phone = PhoneFactory.getPhone(slotIndex);
5882 if (phone != null) {
5883 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5884 mApp, phone.getSubId(), "rebootModem");
5885
5886 final long identity = Binder.clearCallingIdentity();
5887 try {
5888 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5889 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5890 return success;
5891 } finally {
5892 Binder.restoreCallingIdentity(identity);
5893 }
5894 }
5895 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005896 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005897
Brad Ebinger51f743a2017-01-23 13:50:20 -08005898 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005899 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5900 * {@link #disableIms(int)}.
5901 * @param slotIndex device slot.
5902 */
5903 public void resetIms(int slotIndex) {
5904 enforceModifyPermission();
5905
5906 final long identity = Binder.clearCallingIdentity();
5907 try {
5908 if (mImsResolver == null) {
5909 // may happen if the does not support IMS.
5910 return;
5911 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005912 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005913 } finally {
5914 Binder.restoreCallingIdentity(identity);
5915 }
5916 }
5917
5918 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005919 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5920 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005921 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005922 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005923 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005924
5925 final long identity = Binder.clearCallingIdentity();
5926 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005927 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005928 // may happen if the device does not support IMS.
5929 return;
5930 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005931 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005932 } finally {
5933 Binder.restoreCallingIdentity(identity);
5934 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005935 }
5936
5937 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005938 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5939 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005940 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005941 public void disableIms(int slotId) {
5942 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005943
5944 final long identity = Binder.clearCallingIdentity();
5945 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005946 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005947 // may happen if the device does not support IMS.
5948 return;
5949 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005950 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005951 } finally {
5952 Binder.restoreCallingIdentity(identity);
5953 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005954 }
5955
5956 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005957 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5958 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005959 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005960 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005961 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005962 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005963
5964 final long identity = Binder.clearCallingIdentity();
5965 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005966 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005967 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5968 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005969 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005970 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005971 } finally {
5972 Binder.restoreCallingIdentity(identity);
5973 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005974 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005975 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005976 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5977 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005978 @Override
5979 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005980 enforceModifyPermission();
5981
5982 final long identity = Binder.clearCallingIdentity();
5983 try {
5984 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005985 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005986 } finally {
5987 Binder.restoreCallingIdentity(identity);
5988 }
5989 }
5990
5991 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005992 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005993 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005994 */
5995 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5996 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005997
5998 final long identity = Binder.clearCallingIdentity();
5999 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006000 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006001 // may happen if the device does not support IMS.
6002 return null;
6003 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006004 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006005 } finally {
6006 Binder.restoreCallingIdentity(identity);
6007 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006008 }
6009
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006010 /**
6011 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006012 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006013 */
6014 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6015 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006016
6017 final long identity = Binder.clearCallingIdentity();
6018 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006019 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006020 // may happen if the device does not support IMS.
6021 return null;
6022 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006023 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006024 } finally {
6025 Binder.restoreCallingIdentity(identity);
6026 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006027 }
6028
Brad Ebinger884c07b2018-02-15 16:17:40 -08006029 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006030 * Sets the ImsService Package Name that Telephony will bind to.
6031 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006032 * @param slotIndex the slot ID that the ImsService should bind for.
6033 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006034 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006035 * @param featureTypes An integer array of feature types associated with a packageName.
6036 * @param packageName The name of the package that the current configuration will be replaced
6037 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006038 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006039 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006040 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6041 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006042 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006043 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006044 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006045
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006046 final long identity = Binder.clearCallingIdentity();
6047 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006048 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006049 // may happen if the device does not support IMS.
6050 return false;
6051 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006052 Map<Integer, String> featureConfig = new HashMap<>();
6053 for (int featureType : featureTypes) {
6054 featureConfig.put(featureType, packageName);
6055 }
6056 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6057 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006058 } finally {
6059 Binder.restoreCallingIdentity(identity);
6060 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006061 }
6062
6063 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006064 * Clears any carrier ImsService overrides for the slot index specified that were previously
6065 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6066 *
6067 * This should only be used for testing.
6068 *
6069 * @param slotIndex the slot ID that the ImsService should bind for.
6070 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6071 */
6072 @Override
6073 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006074 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6075 "clearCarrierImsServiceOverride");
6076 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006077 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006078
6079 final long identity = Binder.clearCallingIdentity();
6080 try {
6081 if (mImsResolver == null) {
6082 // may happen if the device does not support IMS.
6083 return false;
6084 }
6085 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6086 } finally {
6087 Binder.restoreCallingIdentity(identity);
6088 }
6089 }
6090
6091 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006092 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006093 *
6094 * @param slotId The slot that the ImsService is associated with.
6095 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6096 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006097 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006098 * @return the package name of the ImsService configuration.
6099 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006100 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6101 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006102 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006103 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6104 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006105
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006106 final long identity = Binder.clearCallingIdentity();
6107 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006108 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006109 // may happen if the device does not support IMS.
6110 return "";
6111 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006112 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006113 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6114 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006115 } finally {
6116 Binder.restoreCallingIdentity(identity);
6117 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006118 }
6119
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006120 /**
6121 * Get the MmTelFeature state associated with the requested subscription id.
6122 * @param subId The subscription that the MmTelFeature is associated with.
6123 * @param callback A callback with an integer containing the
6124 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6125 */
6126 @Override
6127 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6128 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006129 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6130 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6131 "IMS not available on device.");
6132 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006133 final long token = Binder.clearCallingIdentity();
6134 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006135 int slotId = getSlotIndex(subId);
6136 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6137 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6138 + subId + "'");
6139 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6140 }
6141 verifyImsMmTelConfiguredOrThrow(slotId);
6142 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6143 try {
6144 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6145 } catch (RemoteException e) {
6146 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6147 + "Ignore");
6148 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006149 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006150 } catch (ImsException e) {
6151 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006152 } finally {
6153 Binder.restoreCallingIdentity(token);
6154 }
6155 }
6156
Daniel Brightbb5840b2021-01-12 15:48:18 -08006157 /**
6158 * Sets the ims registration state on all valid {@link Phone}s.
6159 */
6160 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006161 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006162
6163 final long identity = Binder.clearCallingIdentity();
6164 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006165 // NOTE: Before S, this method only set the default phone.
6166 for (final Phone phone : PhoneFactory.getPhones()) {
6167 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6168 phone.setImsRegistrationState(registered);
6169 }
6170 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006171 } finally {
6172 Binder.restoreCallingIdentity(identity);
6173 }
Wink Saville36469e72014-06-11 15:17:00 -07006174 }
6175
6176 /**
Stuart Scott54788802015-03-30 13:18:01 -07006177 * Set the network selection mode to automatic.
6178 *
6179 */
6180 @Override
6181 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006182 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6183 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006184
6185 final long identity = Binder.clearCallingIdentity();
6186 try {
shilufc958392020-01-20 11:36:01 -08006187 if (!isActiveSubscription(subId)) {
6188 return;
6189 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006190 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006191 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6192 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006193 } finally {
6194 Binder.restoreCallingIdentity(identity);
6195 }
Stuart Scott54788802015-03-30 13:18:01 -07006196 }
6197
Jack Yud10cdd42020-09-28 20:28:01 -07006198 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006199 * Ask the radio to connect to the input network and change selection mode to manual.
6200 *
6201 * @param subId the id of the subscription.
6202 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6203 * the operator to attach to.
6204 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6205 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6206 * normal network selection next time.
6207 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006208 */
6209 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006210 public boolean setNetworkSelectionModeManual(
6211 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006212 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6213 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006214
Jack Yu285100e2022-12-02 22:48:35 -08006215 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006216 if (!isActiveSubscription(subId)) {
6217 return false;
6218 }
6219
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006220 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006221 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006222 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006223 if (DBG) {
6224 log("setNetworkSelectionModeManual: subId: " + subId
6225 + " operator: " + operatorInfo);
6226 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006227 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6228 } finally {
6229 Binder.restoreCallingIdentity(identity);
6230 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006231 }
shilu84f6e8b2019-12-19 13:58:01 -08006232 /**
6233 * Get the manual network selection
6234 *
6235 * @param subId the id of the subscription.
6236 *
6237 * @return the previously saved user selected PLMN
6238 */
6239 @Override
6240 public String getManualNetworkSelectionPlmn(int subId) {
6241 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006242 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006243 mApp, subId, "getManualNetworkSelectionPlmn");
6244
6245 final long identity = Binder.clearCallingIdentity();
6246 try {
6247 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006248 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006249 }
6250
6251 final Phone phone = getPhone(subId);
6252 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006253 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006254 }
6255 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6256 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6257 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6258 } finally {
6259 Binder.restoreCallingIdentity(identity);
6260 }
6261 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006262
6263 /**
6264 * Scans for available networks.
6265 */
6266 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006267 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6268 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006269 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6270 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006271 LocationAccessPolicy.LocationPermissionResult locationResult =
6272 LocationAccessPolicy.checkLocationPermission(mApp,
6273 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6274 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006275 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006276 .setCallingPid(Binder.getCallingPid())
6277 .setCallingUid(Binder.getCallingUid())
6278 .setMethod("getCellNetworkScanResults")
6279 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006280 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6281 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006282 .build());
6283 switch (locationResult) {
6284 case DENIED_HARD:
6285 throw new SecurityException("Not allowed to access scan results -- location");
6286 case DENIED_SOFT:
6287 return null;
6288 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006289
Pengquan Menga1bb6272018-09-06 09:59:22 -07006290 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006291 try {
6292 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006293 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006294 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006295 } finally {
6296 Binder.restoreCallingIdentity(identity);
6297 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006298 }
6299
6300 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006301 * Get the call forwarding info, given the call forwarding reason.
6302 */
6303 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006304 public void getCallForwarding(int subId, int callForwardingReason,
6305 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006306 enforceReadPrivilegedPermission("getCallForwarding");
6307 long identity = Binder.clearCallingIdentity();
6308 try {
6309 if (DBG) {
6310 log("getCallForwarding: subId " + subId
6311 + " callForwardingReason" + callForwardingReason);
6312 }
Hall Liu27d24262020-09-18 19:04:59 -07006313
6314 Phone phone = getPhone(subId);
6315 if (phone == null) {
6316 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006317 callback.onError(
6318 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006319 } catch (RemoteException e) {
6320 // ignore
6321 }
6322 return;
6323 }
6324
6325 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6326 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6327 @Override
6328 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6329 try {
6330 callback.onCallForwardingInfoAvailable(info);
6331 } catch (RemoteException e) {
6332 // ignore
6333 }
6334 }
6335
6336 @Override
6337 public void onError(int error) {
6338 try {
6339 callback.onError(error);
6340 } catch (RemoteException e) {
6341 // ignore
6342 }
6343 }
6344 });
6345 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006346 } finally {
6347 Binder.restoreCallingIdentity(identity);
6348 }
6349 }
6350
6351 /**
6352 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6353 * reason, the number to forward, and the timeout before the forwarding is attempted.
6354 */
6355 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006356 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6357 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006358 enforceModifyPermission();
6359 long identity = Binder.clearCallingIdentity();
6360 try {
6361 if (DBG) {
6362 log("setCallForwarding: subId " + subId
6363 + " callForwardingInfo" + callForwardingInfo);
6364 }
Hall Liu27d24262020-09-18 19:04:59 -07006365
6366 Phone phone = getPhone(subId);
6367 if (phone == null) {
6368 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006369 callback.accept(
6370 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006371 } catch (RemoteException e) {
6372 // ignore
6373 }
6374 return;
6375 }
6376
6377 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6378 FunctionalUtils.ignoreRemoteException(callback::accept));
6379
6380 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006381 } finally {
6382 Binder.restoreCallingIdentity(identity);
6383 }
6384 }
6385
6386 /**
Hall Liu27d24262020-09-18 19:04:59 -07006387 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006388 */
6389 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006390 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006391 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006392 long identity = Binder.clearCallingIdentity();
6393 try {
Hall Liu27d24262020-09-18 19:04:59 -07006394 Phone phone = getPhone(subId);
6395 if (phone == null) {
6396 try {
6397 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6398 } catch (RemoteException e) {
6399 // ignore
6400 }
6401 return;
6402 }
SongFerngWang0e767992021-03-31 22:08:45 +08006403 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6404 PersistableBundle c = configManager.getConfigForSubId(subId);
6405 boolean requireUssd = c.getBoolean(
6406 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006407
Shuo Qian4a594052020-01-23 11:59:30 -08006408 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006409 if (requireUssd) {
6410 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6411 getSubscriptionCarrierId(subId));
6412 String newUssdCommand = "";
6413 try {
6414 newUssdCommand = carrierXmlParser.getFeature(
6415 CarrierXmlParser.FEATURE_CALL_WAITING)
6416 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6417 } catch (NullPointerException e) {
6418 loge("Failed to generate USSD number" + e);
6419 }
6420 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6421 mMainThreadHandler, callback, carrierXmlParser,
6422 CarrierXmlParser.SsEntry.SSAction.QUERY);
6423 final String ussdCommand = newUssdCommand;
6424 Executors.newSingleThreadExecutor().execute(() -> {
6425 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6426 });
6427 } else {
6428 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6429 callback::accept);
6430 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6431 }
Shuo Qian4a594052020-01-23 11:59:30 -08006432 } finally {
6433 Binder.restoreCallingIdentity(identity);
6434 }
6435 }
6436
6437 /**
Hall Liu27d24262020-09-18 19:04:59 -07006438 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006439 */
6440 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006441 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006442 enforceModifyPermission();
6443 long identity = Binder.clearCallingIdentity();
6444 try {
Hall Liu27d24262020-09-18 19:04:59 -07006445 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6446
6447 Phone phone = getPhone(subId);
6448 if (phone == null) {
6449 try {
6450 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6451 } catch (RemoteException e) {
6452 // ignore
6453 }
6454 return;
6455 }
6456
SongFerngWang0e767992021-03-31 22:08:45 +08006457 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6458 PersistableBundle c = configManager.getConfigForSubId(subId);
6459 boolean requireUssd = c.getBoolean(
6460 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006461
SongFerngWang0e767992021-03-31 22:08:45 +08006462 if (DBG) log("getCallWaitingStatus: subId " + subId);
6463 if (requireUssd) {
6464 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6465 getSubscriptionCarrierId(subId));
6466 CarrierXmlParser.SsEntry.SSAction ssAction =
6467 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6468 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6469 String newUssdCommand = "";
6470 try {
6471 newUssdCommand = carrierXmlParser.getFeature(
6472 CarrierXmlParser.FEATURE_CALL_WAITING)
6473 .makeCommand(ssAction, null);
6474 } catch (NullPointerException e) {
6475 loge("Failed to generate USSD number" + e);
6476 }
6477 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6478 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6479 final String ussdCommand = newUssdCommand;
6480 Executors.newSingleThreadExecutor().execute(() -> {
6481 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6482 });
6483 } else {
6484 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6485 FunctionalUtils.ignoreRemoteException(callback::accept));
6486
6487 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6488 }
Shuo Qian4a594052020-01-23 11:59:30 -08006489 } finally {
6490 Binder.restoreCallingIdentity(identity);
6491 }
6492 }
6493
6494 /**
yinxub1bed742017-04-17 11:45:04 -07006495 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006496 *
yinxub1bed742017-04-17 11:45:04 -07006497 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006498 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6499 * location related information which will be sent if the caller already possess
6500 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006501 * @param request contains the radio access networks with bands/channels to scan
6502 * @param messenger callback messenger for scan results or errors
6503 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006504 * @return the id of the requested scan which can be used to stop the scan.
6505 */
6506 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006507 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6508 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006509 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006510 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6511 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006512 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006513 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6514 if (!renounceFineLocationAccess) {
6515 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6516 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6517 .setCallingPackage(callingPackage)
6518 .setCallingFeatureId(callingFeatureId)
6519 .setCallingPid(Binder.getCallingPid())
6520 .setCallingUid(Binder.getCallingUid())
6521 .setMethod("requestNetworkScan")
6522 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6523 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6524 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6525 .build());
6526 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006527 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006528 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6529 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006530 if (e != null) {
6531 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6532 throw e;
6533 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006534 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006535 return TelephonyScanManager.INVALID_SCAN_ID;
6536 }
6537 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006538 }
Hall Liu912dfd32019-04-25 14:02:26 -07006539 int callingUid = Binder.getCallingUid();
6540 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006541 final long identity = Binder.clearCallingIdentity();
6542 try {
6543 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006544 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006545 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006546 } finally {
6547 Binder.restoreCallingIdentity(identity);
6548 }
yinxu504e1392017-04-12 16:03:22 -07006549 }
6550
Hall Liub2ac8ef2019-02-28 15:56:23 -08006551 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006552 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006553 boolean hasCarrierPriv;
6554 final long identity = Binder.clearCallingIdentity();
6555 try {
6556 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6557 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6558 } finally {
6559 Binder.restoreCallingIdentity(identity);
6560 }
Hall Liu558027f2019-05-15 19:14:05 -07006561 boolean hasNetworkScanPermission =
6562 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6563 == PERMISSION_GRANTED;
6564
6565 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6566 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6567 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006568 }
6569
6570 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6571 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006572 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6573 return new SecurityException("Specific channels must not be"
6574 + " scanned without location access.");
6575 }
6576 }
6577 }
6578
Hall Liub2ac8ef2019-02-28 15:56:23 -08006579 return null;
6580 }
6581
yinxu504e1392017-04-12 16:03:22 -07006582 /**
6583 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006584 *
6585 * @param subId id of the subscription
6586 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006587 */
6588 @Override
6589 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006590 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6591 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006592
Hall Liu912dfd32019-04-25 14:02:26 -07006593 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006594 final long identity = Binder.clearCallingIdentity();
6595 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006596 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006597 } finally {
6598 Binder.restoreCallingIdentity(identity);
6599 }
yinxu504e1392017-04-12 16:03:22 -07006600 }
6601
6602 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006603 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006604 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006605 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006606 */
6607 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006608 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006609 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006610 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006611 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006612
6613 final long identity = Binder.clearCallingIdentity();
6614 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006615 if (DBG) log("getAllowedNetworkTypesBitmask");
6616 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6617 int networkTypesBitmask = (result != null ? result[0] : -1);
6618 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6619 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006620 } finally {
6621 Binder.restoreCallingIdentity(identity);
6622 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006623 }
6624
6625 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006626 * Get the allowed network types for certain reason.
6627 *
6628 * @param subId the id of the subscription.
6629 * @param reason the reason the allowed network type change is taking place
6630 * @return the allowed network types.
6631 */
6632 @Override
6633 public long getAllowedNetworkTypesForReason(int subId,
6634 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006635 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006636 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006637 final long identity = Binder.clearCallingIdentity();
6638 try {
6639 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6640 } finally {
6641 Binder.restoreCallingIdentity(identity);
6642 }
6643 }
6644
6645 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006646 * Enable/Disable E-UTRA-NR Dual Connectivity
6647 * @param subId subscription id of the sim card
6648 * @param nrDualConnectivityState expected NR dual connectivity state
6649 * This can be passed following states
6650 * <ol>
6651 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6652 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6653 * <li>Disable NR dual connectivity and force secondary cell to be released
6654 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6655 * </ol>
6656 * @return operation result.
6657 */
6658 @Override
6659 public int setNrDualConnectivityState(int subId,
6660 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6661 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6662 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006663 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006664 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6665 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6666 }
6667
Sooraj Sasindran37444802020-08-11 10:40:43 -07006668 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6669 final long identity = Binder.clearCallingIdentity();
6670 try {
6671 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6672 nrDualConnectivityState, subId,
6673 workSource);
6674 if (DBG) log("enableNRDualConnectivity result: " + result);
6675 return result;
6676 } finally {
6677 Binder.restoreCallingIdentity(identity);
6678 }
6679 }
6680
6681 /**
6682 * Is E-UTRA-NR Dual Connectivity enabled
6683 * @return true if dual connectivity is enabled else false
6684 */
6685 @Override
6686 public boolean isNrDualConnectivityEnabled(int subId) {
6687 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006688 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006689 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006690 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006691 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6692 return false;
6693 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006694 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6695 final long identity = Binder.clearCallingIdentity();
6696 try {
6697 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6698 null, subId, workSource);
6699 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6700 return isEnabled;
6701 } finally {
6702 Binder.restoreCallingIdentity(identity);
6703 }
6704 }
6705
6706 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006707 * Set the allowed network types of the device and
6708 * provide the reason triggering the allowed network change.
6709 *
6710 * @param subId the id of the subscription.
6711 * @param reason the reason the allowed network type change is taking place
6712 * @param allowedNetworkTypes the allowed network types.
6713 * @return true on success; false on any failure.
6714 */
6715 @Override
6716 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006717 @TelephonyManager.AllowedNetworkTypesReason int reason,
6718 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006719 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6720 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006721 // If the caller only has carrier privileges, then they should not be able to override
6722 // any network types which were set for security reasons.
6723 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6724 != PERMISSION_GRANTED
6725 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6726 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6727 throw new SecurityException(
6728 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6729 + " reason "
Jack Yu363462b2023-01-14 12:42:30 -08006730 + TelephonyManager.allowedNetworkTypesReasonToString(reason));
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006731 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006732 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
Jack Yu363462b2023-01-14 12:42:30 -08006733 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: "
6734 + TelephonyManager.allowedNetworkTypesReasonToString(reason));
SongFerngWang7ffc2732021-04-15 19:46:33 +08006735 return false;
6736 }
6737 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6738 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006739 return false;
6740 }
6741
Jack Yu363462b2023-01-14 12:42:30 -08006742 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason="
6743 + TelephonyManager.allowedNetworkTypesReasonToString(reason) + ", network types: "
6744 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes)
6745 + ", callingPackage=" + getCurrentPackageName());
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006746
Jack Yue37dd262022-12-16 11:53:37 -08006747 Phone phone = getPhone(subId);
6748 if (phone == null) {
6749 return false;
6750 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006751
Jack Yue37dd262022-12-16 11:53:37 -08006752 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
Jack Yu363462b2023-01-14 12:42:30 -08006753 log("setAllowedNetworkTypesForReason: "
6754 + TelephonyManager.allowedNetworkTypesReasonToString(reason)
6755 + " already has the specified network types.");
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006756 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006757 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006758
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006759 final long identity = Binder.clearCallingIdentity();
6760 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006761 Boolean success = (Boolean) sendRequest(
6762 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6763 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6764
6765 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6766 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006767 } finally {
6768 Binder.restoreCallingIdentity(identity);
6769 }
6770 }
6771
6772 /**
Miaoa84611c2019-03-15 09:21:10 +08006773 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006774 *
Miaoa84611c2019-03-15 09:21:10 +08006775 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006776 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006777 * @hide
6778 */
6779 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006780 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006781 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006782 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006783 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006784 try {
Miaoa84611c2019-03-15 09:21:10 +08006785 if (phone != null) {
6786 return phone.hasMatchedTetherApnSetting();
6787 } else {
6788 return false;
6789 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006790 } finally {
6791 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006792 }
Junda Liu475951f2014-11-07 16:45:03 -08006793 }
6794
6795 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006796 * Get the user enabled state of Mobile Data.
6797 *
6798 * TODO: remove and use isUserDataEnabled.
6799 * This can't be removed now because some vendor codes
6800 * calls through ITelephony directly while they should
6801 * use TelephonyManager.
6802 *
6803 * @return true on enabled
6804 */
6805 @Override
6806 public boolean getDataEnabled(int subId) {
6807 return isUserDataEnabled(subId);
6808 }
6809
6810 /**
6811 * Get whether mobile data is enabled per user setting.
6812 *
6813 * There are other factors deciding whether mobile data is actually enabled, but they are
6814 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006815 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006816 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6817 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006818 *
6819 * @return {@code true} if data is enabled else {@code false}
6820 */
6821 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006822 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006823 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006824 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006825 try {
6826 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6827 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006828 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006829 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6830 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006831 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006832 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006833 mApp, subId, functionName);
6834
Robert Greenwalt646120a2014-05-23 11:54:03 -07006835 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006836
6837 final long identity = Binder.clearCallingIdentity();
6838 try {
Jack Yu285100e2022-12-02 22:48:35 -08006839 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006840 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6841 Phone phone = PhoneFactory.getPhone(phoneId);
6842 if (phone != null) {
6843 boolean retVal = phone.isUserDataEnabled();
6844 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6845 return retVal;
6846 } else {
6847 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6848 return false;
6849 }
6850 } finally {
6851 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006852 }
6853 }
6854
6855 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006856 * Checks if the device is capable of mobile data by considering whether whether the
6857 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6858 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006859 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006860 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006861 */
6862 @Override
6863 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006864 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006865 try {
6866 try {
6867 mApp.enforceCallingOrSelfPermission(
6868 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006869 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006870 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006871 try {
6872 mApp.enforceCallingOrSelfPermission(
6873 android.Manifest.permission.READ_PHONE_STATE,
6874 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006875 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006876 mApp.enforceCallingOrSelfPermission(
6877 permission.READ_BASIC_PHONE_STATE, functionName);
6878 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006879 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006880 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006881 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006882 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006883
6884 final long identity = Binder.clearCallingIdentity();
6885 try {
Jack Yu285100e2022-12-02 22:48:35 -08006886 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006887 Phone phone = PhoneFactory.getPhone(phoneId);
6888 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006889 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006890 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006891 return retVal;
6892 } else {
6893 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6894 return false;
6895 }
6896 } finally {
6897 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006898 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006899 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006900
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006901 /**
6902 * Check if data is enabled for a specific reason
6903 * @param subId Subscription index
6904 * @param reason the reason the data enable change is taking place
6905 * @return {@code true} if the overall data is enabled; {@code false} if not.
6906 */
6907 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006908 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006909 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006910 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006911 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006912 try {
6913 mApp.enforceCallingOrSelfPermission(
6914 android.Manifest.permission.ACCESS_NETWORK_STATE,
6915 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006916 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006917 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6918 functionName);
6919 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006920 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006921 try {
6922 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006923 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006924 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006925 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006926 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006927 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006928 }
6929
6930
6931 final long identity = Binder.clearCallingIdentity();
6932 try {
Jack Yu285100e2022-12-02 22:48:35 -08006933 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006934 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006935 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006936 + " reason=" + reason);
6937 }
6938 Phone phone = PhoneFactory.getPhone(phoneId);
6939 if (phone != null) {
6940 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006941 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006942 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006943 return retVal;
6944 } else {
6945 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006946 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006947 + subId + " retVal=false");
6948 }
6949 return false;
6950 }
6951 } finally {
6952 Binder.restoreCallingIdentity(identity);
6953 }
6954 }
6955
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006956 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006957 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006958 // No permission needed; this only lets the caller inspect their own status.
6959 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006960 }
Junda Liu29340342014-07-10 15:23:27 -07006961
6962 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006963 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006964 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006965 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6966 }
6967
6968 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6969 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006970 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006971 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006972 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6973 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006974 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6975 if (cpt == null) {
6976 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006977 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6978 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006979 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006980 }
6981
6982 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006983 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006984 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006985 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006986 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006987 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006988 Phone phone = getPhone(subId);
6989 if (phone == null) {
6990 loge("checkCarrierPrivilegesForPackage: Invalid subId");
6991 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6992 }
6993 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6994 if (cpt == null) {
6995 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006996 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6997 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006998 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006999 }
7000
7001 @Override
7002 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007003 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007004 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7005 }
7006
7007 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007008 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007009 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007010 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007011 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007012 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7013 Phone phone = PhoneFactory.getPhone(phoneId);
7014 if (phone == null) {
7015 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007016 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007017 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7018 if (cpt == null) {
7019 continue;
7020 }
7021 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007022 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7023 break;
7024 }
7025 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007026 return result;
Junda Liu29340342014-07-10 15:23:27 -07007027 }
Derek Tan89e89d42014-07-08 17:00:10 -07007028
7029 @Override
Junda Liue64de782015-04-16 17:19:16 -07007030 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007031 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007032 Phone phone = PhoneFactory.getPhone(phoneId);
7033 if (phone == null) {
7034 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007035 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007036 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7037 if (cpt == null) {
7038 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007039 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007040 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007041 }
7042
Amith Yamasani6e118872016-02-19 12:53:51 -08007043 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007044 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007045 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007046 Phone phone = PhoneFactory.getPhone(phoneId);
7047 if (phone == null) {
7048 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007049 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007050 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7051 if (cpt == null) {
7052 return Collections.emptyList();
7053 }
7054 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007055 }
7056
chen xuf7e9fe82019-05-09 19:31:02 -07007057 @Override
7058 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007059 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007060 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007061 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007062 try {
7063 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7064 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7065 }
7066 } finally {
7067 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007068 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007069 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007070 }
7071
Rambo Wang6812ffb2022-03-15 16:54:17 -07007072 @Override
7073 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7074 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7075
7076 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7077 if (phone == null) {
7078 return null;
7079 }
7080 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7081 if (cpt == null) {
7082 return null;
7083 }
7084 return cpt.getCarrierServicePackageName();
7085 }
7086
Wink Savilleb564aae2014-10-23 10:18:09 -07007087 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007088 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007089 UiccPort port = phone == null ? null : phone.getUiccPort();
7090 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007091 return null;
7092 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007093 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007094 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007095 return null;
7096 }
7097 return iccId;
7098 }
7099
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007100 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007101 public void setCallComposerStatus(int subId, int status) {
7102 enforceModifyPermission();
7103
7104 final long identity = Binder.clearCallingIdentity();
7105 try {
7106 Phone phone = getPhone(subId);
7107 if (phone != null) {
7108 Phone defaultPhone = phone.getImsPhone();
7109 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7110 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7111 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007112 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7113 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007114 }
7115 }
Shuo Qian284ae752020-12-22 19:10:14 -08007116 } catch (ImsException e) {
7117 throw new ServiceSpecificException(e.getCode());
7118 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007119 Binder.restoreCallingIdentity(identity);
7120 }
7121 }
7122
7123 @Override
7124 public int getCallComposerStatus(int subId) {
7125 enforceReadPrivilegedPermission("getCallComposerStatus");
7126
7127 final long identity = Binder.clearCallingIdentity();
7128 try {
7129 Phone phone = getPhone(subId);
7130 if (phone != null) {
7131 Phone defaultPhone = phone.getImsPhone();
7132 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7133 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7134 return imsPhone.getCallComposerStatus();
7135 }
7136 }
7137 } finally {
7138 Binder.restoreCallingIdentity(identity);
7139 }
7140 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7141 }
7142
7143 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007144 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7145 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007146 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007147 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007148
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007149 final long identity = Binder.clearCallingIdentity();
7150 try {
7151 final String iccId = getIccId(subId);
7152 final Phone phone = getPhone(subId);
7153 if (phone == null) {
7154 return false;
7155 }
7156 final String subscriberId = phone.getSubscriberId();
7157
7158 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007159 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007160 + subscriberId + " to " + number);
7161 }
7162
7163 if (TextUtils.isEmpty(iccId)) {
7164 return false;
7165 }
7166
7167 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7168
7169 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7170 if (alphaTag == null) {
7171 editor.remove(alphaTagPrefKey);
7172 } else {
7173 editor.putString(alphaTagPrefKey, alphaTag);
7174 }
7175
7176 // Record both the line number and IMSI for this ICCID, since we need to
7177 // track all merged IMSIs based on line number
7178 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7179 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7180 if (number == null) {
7181 editor.remove(numberPrefKey);
7182 editor.remove(subscriberPrefKey);
7183 } else {
7184 editor.putString(numberPrefKey, number);
7185 editor.putString(subscriberPrefKey, subscriberId);
7186 }
7187
7188 editor.commit();
7189 return true;
7190 } finally {
7191 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007192 }
Derek Tan7226c842014-07-02 17:42:23 -07007193 }
7194
7195 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007196 public String getLine1NumberForDisplay(int subId, String callingPackage,
7197 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007198 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007199 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007200 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007201 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007202 return null;
7203 }
Derek Tan97ebb422014-09-05 16:55:38 -07007204
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007205 final long identity = Binder.clearCallingIdentity();
7206 try {
7207 String iccId = getIccId(subId);
7208 if (iccId != null) {
7209 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7210 if (DBG_MERGE) {
7211 log("getLine1NumberForDisplay returning "
7212 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7213 }
7214 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007215 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007216 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7217 return null;
7218 } finally {
7219 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007220 }
Derek Tan7226c842014-07-02 17:42:23 -07007221 }
7222
7223 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007224 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7225 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007226 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007227 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007228 return null;
7229 }
Derek Tan97ebb422014-09-05 16:55:38 -07007230
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007231 final long identity = Binder.clearCallingIdentity();
7232 try {
7233 String iccId = getIccId(subId);
7234 if (iccId != null) {
7235 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7236 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7237 }
7238 return null;
7239 } finally {
7240 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007241 }
Derek Tan7226c842014-07-02 17:42:23 -07007242 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007243
7244 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007245 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7246 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007247 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7248 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007249 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007250 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007251 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007252 return null;
7253 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007254
Jordan Liub49b04b2019-05-06 14:45:15 -07007255 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7256 // the process, where TelephonyManager was instantiated.
7257 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007258 final long identity = Binder.clearCallingIdentity();
7259 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007260 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007261 final TelephonyManager tele = TelephonyManager.from(context);
7262 final SubscriptionManager sub = SubscriptionManager.from(context);
7263
7264 // Figure out what subscribers are currently active
7265 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007266
Jordan Liub49b04b2019-05-06 14:45:15 -07007267 // Only consider subs which match the current subId
7268 // This logic can be simplified. See b/131189269 for progress.
7269 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007270 activeSubscriberIds.add(tele.getSubscriberId(subId));
7271 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007272
7273 // First pass, find a number override for an active subscriber
7274 String mergeNumber = null;
7275 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7276 for (String key : prefs.keySet()) {
7277 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7278 final String subscriberId = (String) prefs.get(key);
7279 if (activeSubscriberIds.contains(subscriberId)) {
7280 final String iccId = key.substring(
7281 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7282 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7283 mergeNumber = (String) prefs.get(numberKey);
7284 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007285 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007286 + " for active subscriber " + subscriberId);
7287 }
7288 if (!TextUtils.isEmpty(mergeNumber)) {
7289 break;
7290 }
7291 }
7292 }
7293 }
7294
7295 // Shortcut when no active merged subscribers
7296 if (TextUtils.isEmpty(mergeNumber)) {
7297 return null;
7298 }
7299
7300 // Second pass, find all subscribers under that line override
7301 final ArraySet<String> result = new ArraySet<>();
7302 for (String key : prefs.keySet()) {
7303 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7304 final String number = (String) prefs.get(key);
7305 if (mergeNumber.equals(number)) {
7306 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7307 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7308 final String subscriberId = (String) prefs.get(subscriberKey);
7309 if (!TextUtils.isEmpty(subscriberId)) {
7310 result.add(subscriberId);
7311 }
7312 }
7313 }
7314 }
7315
7316 final String[] resultArray = result.toArray(new String[result.size()]);
7317 Arrays.sort(resultArray);
7318 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007319 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007320 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7321 }
7322 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007323 } finally {
7324 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007325 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007326 }
7327
7328 @Override
zoey chen38003472019-12-13 17:16:31 +08007329 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7330 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007331
7332 final long identity = Binder.clearCallingIdentity();
7333 try {
7334 final TelephonyManager telephonyManager = mApp.getSystemService(
7335 TelephonyManager.class);
7336 String subscriberId = telephonyManager.getSubscriberId(subId);
7337 if (subscriberId == null) {
7338 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007339 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007340 + subId);
7341 }
7342 return null;
7343 }
7344
Jack Yu285100e2022-12-02 22:48:35 -08007345 ParcelUuid groupUuid;
7346 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7347 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7348 .getSubscriptionInfo(subId);
7349 groupUuid = info.getGroupUuid();
7350 } else {
7351 final SubscriptionInfo info = mSubscriptionController
7352 .getSubscriptionInfo(subId);
7353 groupUuid = info.getGroupUuid();
7354 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007355 // If it doesn't belong to any group, return just subscriberId of itself.
7356 if (groupUuid == null) {
7357 return new String[]{subscriberId};
7358 }
7359
7360 // Get all subscriberIds from the group.
7361 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007362 List<SubscriptionInfo> groupInfos;
7363 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7364 groupInfos = SubscriptionManagerService.getInstance()
7365 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7366 mApp.getAttributionTag());
7367 } else {
7368 groupInfos = mSubscriptionController
7369 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7370 mApp.getAttributionTag());
7371 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007372 for (SubscriptionInfo subInfo : groupInfos) {
7373 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7374 if (subscriberId != null) {
7375 mergedSubscriberIds.add(subscriberId);
7376 }
7377 }
7378
7379 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7380 } finally {
7381 Binder.restoreCallingIdentity(identity);
7382
7383 }
7384 }
7385
7386 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007387 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007388 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007389 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007390
7391 final long identity = Binder.clearCallingIdentity();
7392 try {
7393 final Phone phone = getPhone(subId);
7394 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7395 } finally {
7396 Binder.restoreCallingIdentity(identity);
7397 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007398 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007399
7400 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007401 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007402 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7403 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007404 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7405 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007406
7407 final long identity = Binder.clearCallingIdentity();
7408 try {
7409 final Phone phone = getPhone(subId);
7410 if (phone == null) {
7411 return false;
7412 }
7413 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7414 cdmaNonRoamingList);
7415 } finally {
7416 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007417 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007418 }
7419
7420 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007421 @Deprecated
7422 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7423 enforceModifyPermission();
7424
7425 int returnValue = 0;
7426 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007427 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007428 if(result.exception == null) {
7429 if (result.result != null) {
7430 byte[] responseData = (byte[])(result.result);
7431 if(responseData.length > oemResp.length) {
7432 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7433 responseData.length + "bytes. Buffer Size is " +
7434 oemResp.length + "bytes.");
7435 }
7436 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7437 returnValue = responseData.length;
7438 }
7439 } else {
7440 CommandException ex = (CommandException) result.exception;
7441 returnValue = ex.getCommandError().ordinal();
7442 if(returnValue > 0) returnValue *= -1;
7443 }
7444 } catch (RuntimeException e) {
7445 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7446 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7447 if(returnValue > 0) returnValue *= -1;
7448 }
7449
7450 return returnValue;
7451 }
7452
7453 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007454 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007455 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007456 try {
7457 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007458 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007459 mApp, phone.getSubId(), "getRadioAccessFamily");
7460 } catch (SecurityException e) {
7461 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7462 throw e;
7463 }
chen xub97461a2018-10-26 14:17:57 -07007464 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007465 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007466 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007467 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007468 final long identity = Binder.clearCallingIdentity();
7469 try {
chen xub97461a2018-10-26 14:17:57 -07007470 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007471 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007472 mApp, phone.getSubId(), "getRadioAccessFamily");
7473 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007474 } finally {
7475 Binder.restoreCallingIdentity(identity);
7476 }
chen xub97461a2018-10-26 14:17:57 -07007477 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007478 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007479
7480 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007481 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007482 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007483 try {
7484 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7485 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007486 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007487 }
7488 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007489 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007490 }
7491 RoleManager rm = mApp.getSystemService(RoleManager.class);
7492 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7493 if (!dialerRoleHolders.contains(callingPackage)) {
7494 throw new SecurityException("App must be the dialer role holder to"
7495 + " upload a call composer pic");
7496 }
7497
7498 Executors.newSingleThreadExecutor().execute(() -> {
7499 ByteArrayOutputStream output = new ByteArrayOutputStream(
7500 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7501 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7502 boolean readUntilEnd = false;
7503 int totalBytesRead = 0;
7504 byte[] buffer = new byte[16 * 1024];
7505 while (true) {
7506 int numRead;
7507 try {
7508 numRead = input.read(buffer);
7509 } catch (IOException e) {
7510 try {
7511 fd.checkError();
7512 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7513 null);
7514 } catch (IOException e1) {
7515 // This means that the other side closed explicitly with an error. If this
7516 // happens, log and ignore.
7517 loge("Remote end of call composer picture pipe closed: " + e1);
7518 }
7519 break;
7520 }
7521 if (numRead == -1) {
7522 readUntilEnd = true;
7523 break;
7524 }
7525 totalBytesRead += numRead;
7526 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7527 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7528 try {
7529 input.close();
7530 } catch (IOException e) {
7531 // ignore
7532 }
7533 break;
7534 }
7535 output.write(buffer, 0, numRead);
7536 }
7537 // Generally, the remote end will close the file descriptors. The only case where we
7538 // close is above, where the picture size is too big.
7539
7540 try {
7541 fd.checkError();
7542 } catch (IOException e) {
7543 loge("Remote end for call composer closed with an error: " + e);
7544 return;
7545 }
7546
Hall Liuaa4211e2021-01-20 15:43:39 -08007547 if (!readUntilEnd) {
7548 loge("Did not finish reading entire image; aborting");
7549 return;
7550 }
Hall Liu82694d52020-12-11 18:22:04 -08007551
Hall Liuaa4211e2021-01-20 15:43:39 -08007552 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7553 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7554 new CallComposerPictureTransfer.Factory() {},
7555 imageData,
7556 (result) -> {
7557 if (result.first != null) {
7558 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7559 Bundle outputResult = new Bundle();
7560 outputResult.putParcelable(
7561 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7562 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7563 outputResult);
7564 } else {
7565 callback.send(result.second, null);
7566 }
7567 }
7568 );
Hall Liu82694d52020-12-11 18:22:04 -08007569 });
7570 }
7571
7572 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007573 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007574 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007575 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007576
7577 final long identity = Binder.clearCallingIdentity();
7578 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007579 ImsManager.getInstance(defaultPhone.getContext(),
7580 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007581 } finally {
7582 Binder.restoreCallingIdentity(identity);
7583 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007584 }
7585
7586 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007587 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007588 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007589 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7590 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007591 return false;
7592 }
Svet Ganovb320e182015-04-16 12:30:10 -07007593
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007594 final long identity = Binder.clearCallingIdentity();
7595 try {
7596 // Check the user preference and the system-level IMS setting. Even if the user has
7597 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7598 // In the long run, we may instead need to check if there exists a connection service
7599 // which can support video calling.
7600 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007601 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007602 return imsManager.isVtEnabledByPlatform()
7603 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7604 && imsManager.isVtEnabledByUser();
7605 } finally {
7606 Binder.restoreCallingIdentity(identity);
7607 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007608 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007609
Andrew Leea1239f22015-03-02 17:44:07 -08007610 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007611 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7612 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007613 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007614 mApp, subId, callingPackage, callingFeatureId,
7615 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007616 return false;
7617 }
7618
7619 final long identity = Binder.clearCallingIdentity();
7620 try {
7621 CarrierConfigManager configManager =
7622 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007623 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007624 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7625 } finally {
7626 Binder.restoreCallingIdentity(identity);
7627 }
Andrew Leea1239f22015-03-02 17:44:07 -08007628 }
7629
7630 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007631 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007632 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007633 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007634 return false;
7635 }
7636
7637 final long identity = Binder.clearCallingIdentity();
7638 try {
7639 CarrierConfigManager configManager =
7640 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007641 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007642 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7643 } finally {
7644 Binder.restoreCallingIdentity(identity);
7645 }
Andrew Leea1239f22015-03-02 17:44:07 -08007646 }
7647
Andrew Lee9431b832015-03-09 18:46:45 -07007648 @Override
7649 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007650 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007651 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007652 }
7653
7654 @Override
7655 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007656 final long identity = Binder.clearCallingIdentity();
7657 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007658 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007659 } finally {
7660 Binder.restoreCallingIdentity(identity);
7661 }
Andrew Lee9431b832015-03-09 18:46:45 -07007662 }
7663
Hall Liuf6668912018-10-31 17:05:23 -07007664 /**
7665 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7666 * support for the feature and device firmware support.
7667 *
7668 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7669 */
7670 @Override
7671 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007672 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007673 final Phone phone = getPhone(subscriptionId);
7674 if (phone == null) {
7675 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7676 return false;
7677 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007678 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007679 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007680 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7681 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007682 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007683 return isCarrierSupported && isDeviceSupported;
7684 } finally {
7685 Binder.restoreCallingIdentity(identity);
7686 }
Hall Liu98187582018-01-22 19:15:32 -08007687 }
7688
Hall Liuf6668912018-10-31 17:05:23 -07007689 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007690 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7691 * RTT setting, will return true if the device and carrier both support RTT.
7692 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007693 */
7694 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007695 final long identity = Binder.clearCallingIdentity();
7696 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007697 boolean isRttSupported = isRttSupported(subscriptionId);
7698 boolean isUserRttSettingOn = Settings.Secure.getInt(
7699 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7700 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7701 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7702 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007703 } finally {
7704 Binder.restoreCallingIdentity(identity);
7705 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007706 }
7707
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007708 @Deprecated
7709 @Override
7710 public String getDeviceId(String callingPackage) {
7711 return getDeviceIdWithFeature(callingPackage, null);
7712 }
7713
Sanket Padawe7310cc72015-01-14 09:53:20 -08007714 /**
7715 * Returns the unique device ID of phone, for example, the IMEI for
7716 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7717 *
7718 * <p>Requires Permission:
7719 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7720 */
7721 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007722 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007723 try {
7724 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7725 } catch (SecurityException se) {
7726 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7727 throw new SecurityException("Package " + callingPackage + " does not belong to "
7728 + Binder.getCallingUid());
7729 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007730 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007731 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007732 return null;
7733 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007734 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007735 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007736 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007737 return null;
7738 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007739
7740 final long identity = Binder.clearCallingIdentity();
7741 try {
7742 return phone.getDeviceId();
7743 } finally {
7744 Binder.restoreCallingIdentity(identity);
7745 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007746 }
7747
Ping Sunc67b7c22016-03-02 19:16:45 +08007748 /**
7749 * {@hide}
7750 * Returns the IMS Registration Status on a particular subid
7751 *
7752 * @param subId
7753 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007754 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007755 Phone phone = getPhone(subId);
7756 if (phone != null) {
7757 return phone.isImsRegistered();
7758 } else {
7759 return false;
7760 }
7761 }
7762
Santos Cordon7a1885b2015-02-03 11:15:19 -08007763 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007764 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007765 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007766 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007767 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007768 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7769 }
7770 final long identity = Binder.clearCallingIdentity();
7771 try {
7772 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7773 } finally {
7774 Binder.restoreCallingIdentity(identity);
7775 }
7776 }
7777
7778 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007779 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007780 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007781 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007782 mApp,
7783 subscriptionId,
7784 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007785 final long identity = Binder.clearCallingIdentity();
7786 try {
7787 Phone phone = getPhone(subscriptionId);
7788 if (phone == null) {
7789 return null;
7790 }
7791 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7792 } finally {
7793 Binder.restoreCallingIdentity(identity);
7794 }
7795 }
7796
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007797 /**
7798 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007799 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007800 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007801 final long identity = Binder.clearCallingIdentity();
7802 try {
7803 Phone phone = getPhone(subId);
7804 if (phone != null) {
7805 return phone.isWifiCallingEnabled();
7806 } else {
7807 return false;
7808 }
7809 } finally {
7810 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007811 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007812 }
7813
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007814 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007815 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007816 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007817 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007818 final long identity = Binder.clearCallingIdentity();
7819 try {
7820 Phone phone = getPhone(subId);
7821 if (phone != null) {
7822 return phone.isVideoEnabled();
7823 } else {
7824 return false;
7825 }
7826 } finally {
7827 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007828 }
7829 }
7830
7831 /**
7832 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7833 * defined in {@link ImsRegistrationImplBase}.
7834 */
7835 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007836 final long identity = Binder.clearCallingIdentity();
7837 try {
7838 Phone phone = getPhone(subId);
7839 if (phone != null) {
7840 return phone.getImsRegistrationTech();
7841 } else {
7842 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7843 }
7844 } finally {
7845 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007846 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007847 }
7848
Stuart Scott8eef64f2015-04-08 15:13:54 -07007849 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007850 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007851 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007852 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7853 return;
7854 }
Kai Shif70f46f2021-03-03 13:59:46 -08007855 Phone defaultPhone = getDefaultPhone();
7856 if (defaultPhone != null) {
7857 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7858 mApp, getDefaultPhone().getSubId(), "factoryReset");
7859 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007860 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007861
Svet Ganovcc087f82015-05-12 20:35:54 -07007862 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007863 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7864 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007865 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007866 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007867 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007868 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007869 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007870 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007871 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007872 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007873 // There has been issues when Sms raw table somehow stores orphan
7874 // fragments. They lead to garbled message when new fragments come
7875 // in and combined with those stale ones. In case this happens again,
7876 // user can reset all network settings which will clean up this table.
7877 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007878 // Clean up IMS settings as well here.
7879 int slotId = getSlotIndex(subId);
7880 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7881 ImsManager.getInstance(mApp, slotId).factoryReset();
7882 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007883
Kai Shif70f46f2021-03-03 13:59:46 -08007884 if (defaultPhone == null) {
7885 return;
7886 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007887 // Erase modem config if erase modem on network setting is enabled.
7888 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7889 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7890 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007891 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007892 }
Kai Shif70f46f2021-03-03 13:59:46 -08007893
7894 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007895 } finally {
7896 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007897 }
7898 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007899
SongFerngWangfd89b102021-05-27 22:44:54 +08007900 @VisibleForTesting
7901 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7902 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7903 return;
7904 }
7905 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7906 RILConstants.PREFERRED_NETWORK_MODE);
7907 SubscriptionManager.setSubscriptionProperty(subId,
7908 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7909 "user=" + defaultNetworkType);
7910 phone.loadAllowedNetworksFromSubscriptionDatabase();
7911 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7912 defaultNetworkType, null);
7913 }
7914
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007915 private void cleanUpSmsRawTable(Context context) {
7916 ContentResolver resolver = context.getContentResolver();
7917 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7918 resolver.delete(uri, null, null);
7919 }
7920
Narayan Kamath1c496c22015-04-16 14:40:19 +01007921 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007922 public String getSimLocaleForSubscriber(int subId) {
7923 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7924 final Phone phone = getPhone(subId);
7925 if (phone == null) {
7926 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007927 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007928 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007929 final long identity = Binder.clearCallingIdentity();
7930 try {
Jack Yu285100e2022-12-02 22:48:35 -08007931 SubscriptionInfo info;
7932 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7933 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7934 phone.getContext().getOpPackageName(),
7935 phone.getContext().getAttributionTag());
7936 if (info == null) {
7937 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7938 return null;
7939 }
7940 } else {
7941 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7942 phone.getContext().getOpPackageName(),
7943 phone.getContext().getAttributionTag());
7944 if (info == null) {
7945 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7946 return null;
7947 }
chen xu6291c472019-02-04 12:55:53 -08007948 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007949 // Try and fetch the locale from the carrier properties or from the SIM language
7950 // preferences (EF-PL and EF-LI)...
7951 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007952 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007953 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7954 if (localeFromDefaultSim != null) {
7955 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7956 if (DBG) log("Using locale from subId: " + subId + " locale: "
7957 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007958 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007959 } else {
7960 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007961 }
7962 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007963
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007964 // The SIM language preferences only store a language (e.g. fr = French), not an
7965 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7966 // the SIM and carrier preferences does not include a country we add the country
7967 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007968 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007969 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007970 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08007971 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007972 }
7973
7974 if (DBG) log("No locale found - returning null");
7975 return null;
7976 } finally {
7977 Binder.restoreCallingIdentity(identity);
7978 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007979 }
7980
tom hsu0b59d292022-09-29 23:49:21 +08007981 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08007982 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08007983 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08007984 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08007985 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08007986 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
7987 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08007988 Locale.forLanguageTag(localeTag).getCountry())) {
7989 return localeTag;
7990 }
7991 }
7992 return inputLocale.toLanguageTag();
7993 }
7994
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007995 /**
7996 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
7997 */
7998 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08007999 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8000 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
8001 mApp.getOpPackageName(), mApp.getAttributionTag());
8002 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008003 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008004 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008005 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008006
Gary Jian3aa9a762022-01-24 16:41:19 +08008007 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8008 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008009
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008010 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008011 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8012 * representing the state of the modem.
8013 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008014 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8015 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008016 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008017 */
8018 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008019 public void requestModemActivityInfo(ResultReceiver result) {
8020 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008021 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008022
8023 final long identity = Binder.clearCallingIdentity();
8024 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008025 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008026 } finally {
8027 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008028 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008029 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008030
Siddharth Rayb8114062018-06-17 15:02:38 -07008031 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
8032 // less than total activity duration.
8033 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8034 if (info == null) {
8035 return false;
8036 }
8037 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008038 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
8039 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8040
Siddharth Rayb8114062018-06-17 15:02:38 -07008041 return (info.isValid()
8042 && (info.getSleepTimeMillis() <= activityDurationMs)
8043 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07008044 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07008045 && (totalTxTimeMs <= activityDurationMs));
8046 }
8047
Gary Jian3aa9a762022-01-24 16:41:19 +08008048 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8049 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8050 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8051 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8052
8053 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8054 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8055 }
8056
8057 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8058 mLastModemActivityInfo.setReceiveTimeMillis(
8059 rat,
8060 freq,
8061 info.getReceiveTimeMillis(rat, freq)
8062 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8063 }
8064
8065 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8066 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8067 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8068 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8069
8070 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8071 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8072 }
8073 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8074 mLastModemActivityInfo.setReceiveTimeMillis(
8075 rat,
8076 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8077 }
8078
8079 /**
8080 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8081 * @param info recent ModemActivityInfo
8082 */
8083 private void mergeModemActivityInfo(ModemActivityInfo info) {
8084 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008085 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008086 boolean matched;
8087 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8088 matched = false;
8089 int rat = info.getSpecificInfoRat(i);
8090 int freq = info.getSpecificInfoFrequencyRange(i);
8091 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8092 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8093 //if it already exists
8094 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8095 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8096 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8097 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8098 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8099 updateLastModemActivityInfo(info, rat, freq);
8100 matched = true;
8101 }
8102 } else {
8103 updateLastModemActivityInfo(info, rat);
8104 matched = true;
8105 }
8106 }
8107 }
8108
8109 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008110 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008111 new ActivityStatsTechSpecificInfo(
8112 rat,
8113 freq,
8114 info.getTransmitTimeMillis(rat, freq),
8115 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008116 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008117 }
8118 }
8119 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8120 mLastModemActivitySpecificInfo =
8121 new ActivityStatsTechSpecificInfo[merged.size()];
8122 merged.toArray(mLastModemActivitySpecificInfo);
8123
8124 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8125 mLastModemActivityInfo.setSleepTimeMillis(
8126 info.getSleepTimeMillis()
8127 + mLastModemActivityInfo.getSleepTimeMillis());
8128 mLastModemActivityInfo.setIdleTimeMillis(
8129 info.getIdleTimeMillis()
8130 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008131
8132 mLastModemActivityInfo =
8133 new ModemActivityInfo(
8134 mLastModemActivityInfo.getTimestampMillis(),
8135 mLastModemActivityInfo.getSleepTimeMillis(),
8136 mLastModemActivityInfo.getIdleTimeMillis(),
8137 mLastModemActivitySpecificInfo);
8138 }
8139
8140 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8141 ActivityStatsTechSpecificInfo[] info) {
8142 int infoSize = info.length;
8143 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8144 for (int i = 0; i < infoSize; i++) {
8145 ret[i] = new ActivityStatsTechSpecificInfo(
8146 info[i].getRat(), info[i].getFrequencyRange(),
8147 info[i].getTransmitTimeMillis(),
8148 (int) info[i].getReceiveTimeMillis());
8149 }
8150 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008151 }
8152
Jack Yu85bd38a2015-11-09 11:34:32 -08008153 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008154 * Returns the service state information on specified subscription.
8155 */
8156 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008157 public ServiceState getServiceStateForSubscriber(int subId,
8158 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8159 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008160 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008161 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008162 return null;
8163 }
8164
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008165 boolean hasFinePermission = false;
8166 boolean hasCoarsePermission = false;
8167 if (!renounceFineLocationAccess) {
8168 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8169 LocationAccessPolicy.checkLocationPermission(mApp,
8170 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8171 .setCallingPackage(callingPackage)
8172 .setCallingFeatureId(callingFeatureId)
8173 .setCallingPid(Binder.getCallingPid())
8174 .setCallingUid(Binder.getCallingUid())
8175 .setMethod("getServiceStateForSubscriber")
8176 .setLogAsInfo(true)
8177 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8178 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8179 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8180 .build());
8181 hasFinePermission =
8182 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8183 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008184
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008185 if (!renounceCoarseLocationAccess) {
8186 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8187 LocationAccessPolicy.checkLocationPermission(mApp,
8188 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8189 .setCallingPackage(callingPackage)
8190 .setCallingFeatureId(callingFeatureId)
8191 .setCallingPid(Binder.getCallingPid())
8192 .setCallingUid(Binder.getCallingUid())
8193 .setMethod("getServiceStateForSubscriber")
8194 .setLogAsInfo(true)
8195 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8196 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8197 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8198 .build());
8199 hasCoarsePermission =
8200 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8201 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008202
Jack Yu479f40e2020-10-27 21:29:25 -07008203 final Phone phone = getPhone(subId);
8204 if (phone == null) {
8205 return null;
8206 }
8207
Jordan Liu0f2bc442020-11-18 16:47:37 -08008208 final long identity = Binder.clearCallingIdentity();
8209
Jack Yu479f40e2020-10-27 21:29:25 -07008210 boolean isCallingPackageDataService = phone.getDataServicePackages()
8211 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008212 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008213 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008214 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8215 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8216 .getSubscriptionInfoInternal(subId);
8217 if (subInfo == null || !subInfo.isActive()) {
8218 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8219 + "subId=" + subId);
8220 return null;
8221 }
8222 } else {
8223 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8224 callingFeatureId)) {
8225 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8226 + "subId=" + subId);
8227 return null;
8228 }
Jordan Liuc437b192020-08-17 10:59:12 -07008229 }
8230
Hall Liuf19c44f2018-11-27 14:38:17 -08008231 ServiceState ss = phone.getServiceState();
8232
8233 // Scrub out the location info in ServiceState depending on what level of access
8234 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008235 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008236 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8237 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008238 } finally {
8239 Binder.restoreCallingIdentity(identity);
8240 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008241 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008242
8243 /**
8244 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8245 *
8246 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8247 * voicemail ringtone.
8248 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8249 * PhoneAccount.
8250 */
8251 @Override
8252 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008253 final long identity = Binder.clearCallingIdentity();
8254 try {
8255 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8256 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008257 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008258 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008259
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008260 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8261 } finally {
8262 Binder.restoreCallingIdentity(identity);
8263 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008264 }
8265
8266 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008267 * Sets the per-account voicemail ringtone.
8268 *
8269 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8270 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8271 *
8272 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8273 * voicemail ringtone.
8274 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8275 * PhoneAccount.
8276 */
8277 @Override
8278 public void setVoicemailRingtoneUri(String callingPackage,
8279 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008280 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008281 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008282 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8283 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008284 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8285 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8286 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008287 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008288
8289 final long identity = Binder.clearCallingIdentity();
8290 try {
8291 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8292 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008293 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008294 }
8295 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8296 } finally {
8297 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008298 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008299 }
8300
8301 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008302 * Returns whether vibration is set for voicemail notification in Phone settings.
8303 *
8304 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8305 * voicemail vibration setting.
8306 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8307 */
8308 @Override
8309 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008310 final long identity = Binder.clearCallingIdentity();
8311 try {
8312 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8313 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008314 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008315 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008316
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008317 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8318 } finally {
8319 Binder.restoreCallingIdentity(identity);
8320 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008321 }
8322
Youhan Wange64578a2016-05-02 15:32:42 -07008323 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008324 * Sets the per-account voicemail vibration.
8325 *
8326 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8327 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8328 *
8329 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8330 * voicemail vibration setting.
8331 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8332 * specific PhoneAccount.
8333 */
8334 @Override
8335 public void setVoicemailVibrationEnabled(String callingPackage,
8336 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008337 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008338 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008339 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8340 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008341 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8342 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8343 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008344 }
8345
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008346 final long identity = Binder.clearCallingIdentity();
8347 try {
8348 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8349 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008350 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008351 }
8352 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8353 } finally {
8354 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008355 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008356 }
8357
8358 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008359 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8360 *
8361 * @throws SecurityException if the caller does not have the required permission
8362 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008363 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008364 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008365 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008366 }
8367
8368 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008369 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8370 * permission.
8371 *
8372 * @throws SecurityException if the caller does not have the required permission
8373 */
8374 private void enforceSendSmsPermission() {
8375 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8376 }
8377
8378 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008379 * Make sure either called from same process as self (phone) or IPC caller has interact across
8380 * users permission.
8381 *
8382 * @throws SecurityException if the caller does not have the required permission
8383 */
8384 private void enforceInteractAcrossUsersPermission(String message) {
8385 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8386 }
8387
8388 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008389 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008390 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008391 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008392 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008393 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008394 final long identity = Binder.clearCallingIdentity();
8395 try {
8396 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008397 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008398 if (componentName == null) {
8399 throw new SecurityException(
8400 "Caller not current active visual voicemail package[null]");
8401 }
8402 String vvmPackage = componentName.getPackageName();
8403 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008404 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008405 }
8406 } finally {
8407 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008408 }
8409 }
8410
8411 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008412 * Return the application ID for the app type.
8413 *
8414 * @param subId the subscription ID that this request applies to.
8415 * @param appType the uicc app type.
8416 * @return Application ID for specificied app type, or null if no uicc.
8417 */
8418 @Override
8419 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008420 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008421 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008422
8423 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008424 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008425 if (phone == null) {
8426 return null;
8427 }
8428 String aid = null;
8429 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008430 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008431 .getApplicationByType(appType).getAid();
8432 } catch (Exception e) {
8433 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8434 }
8435 return aid;
8436 } finally {
8437 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008438 }
Youhan Wange64578a2016-05-02 15:32:42 -07008439 }
8440
Youhan Wang4001d252016-05-11 10:29:41 -07008441 /**
8442 * Return the Electronic Serial Number.
8443 *
8444 * @param subId the subscription ID that this request applies to.
8445 * @return ESN or null if error.
8446 */
8447 @Override
8448 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008449 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008450 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008451
8452 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008453 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008454 if (phone == null) {
8455 return null;
8456 }
8457 String esn = null;
8458 try {
8459 esn = phone.getEsn();
8460 } catch (Exception e) {
8461 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8462 }
8463 return esn;
8464 } finally {
8465 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008466 }
Youhan Wang4001d252016-05-11 10:29:41 -07008467 }
8468
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008469 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008470 * Return the Preferred Roaming List Version.
8471 *
8472 * @param subId the subscription ID that this request applies to.
8473 * @return PRLVersion or null if error.
8474 */
8475 @Override
8476 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008477 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008478 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008479
8480 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008481 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008482 if (phone == null) {
8483 return null;
8484 }
8485 String cdmaPrlVersion = null;
8486 try {
8487 cdmaPrlVersion = phone.getCdmaPrlVersion();
8488 } catch (Exception e) {
8489 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8490 }
8491 return cdmaPrlVersion;
8492 } finally {
8493 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008494 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008495 }
8496
8497 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008498 * Get snapshot of Telephony histograms
8499 * @return List of Telephony histograms
8500 * @hide
8501 */
8502 @Override
8503 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008504 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8505 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008506
8507 final long identity = Binder.clearCallingIdentity();
8508 try {
8509 return RIL.getTelephonyRILTimingHistograms();
8510 } finally {
8511 Binder.restoreCallingIdentity(identity);
8512 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008513 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008514
8515 /**
8516 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008517 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8518 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008519 * Require system privileges. In the future we may add this to carrier APIs.
8520 *
Michele Berionne482f8202018-11-27 18:57:59 -08008521 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008522 */
8523 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008524 @TelephonyManager.SetCarrierRestrictionResult
8525 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008526 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008527 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008528
Michele Berionne482f8202018-11-27 18:57:59 -08008529 if (carrierRestrictionRules == null) {
8530 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008531 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008532
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008533 final long identity = Binder.clearCallingIdentity();
8534 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008535 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008536 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008537 } finally {
8538 Binder.restoreCallingIdentity(identity);
8539 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008540 }
8541
8542 /**
8543 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008544 * Get the allowed carrier list and the excluded carrier list, including the priority between
8545 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008546 * Require system privileges. In the future we may add this to carrier APIs.
8547 *
Michele Berionne482f8202018-11-27 18:57:59 -08008548 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008549 */
8550 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008551 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008552 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008553 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008554
8555 final long identity = Binder.clearCallingIdentity();
8556 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008557 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8558 if (response instanceof CarrierRestrictionRules) {
8559 return (CarrierRestrictionRules) response;
8560 }
8561 // Response is an Exception of some kind,
8562 // which is signalled to the user as a NULL retval
8563 return null;
8564 } catch (Exception e) {
8565 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8566 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008567 } finally {
8568 Binder.restoreCallingIdentity(identity);
8569 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008570 }
8571
fionaxu59545b42016-05-25 15:53:37 -07008572 /**
fionaxu59545b42016-05-25 15:53:37 -07008573 * Action set from carrier signalling broadcast receivers to enable/disable radio
8574 * @param subId the subscription ID that this action applies to.
8575 * @param enabled control enable or disable radio.
8576 * {@hide}
8577 */
8578 @Override
8579 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8580 enforceModifyPermission();
8581 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008582
8583 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008584 if (phone == null) {
8585 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8586 return;
8587 }
8588 try {
8589 phone.carrierActionSetRadioEnabled(enabled);
8590 } catch (Exception e) {
8591 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008592 } finally {
8593 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008594 }
8595 }
8596
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008597 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008598 * Enable or disable Voice over NR (VoNR)
8599 * @param subId the subscription ID that this action applies to.
8600 * @param enabled enable or disable VoNR.
8601 * @return operation result.
8602 */
8603 @Override
8604 public int setVoNrEnabled(int subId, boolean enabled) {
8605 enforceModifyPermission();
8606 final Phone phone = getPhone(subId);
8607
8608 final long identity = Binder.clearCallingIdentity();
8609 if (phone == null) {
8610 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8611 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8612 }
8613
8614 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8615 try {
8616 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8617 workSource);
8618 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008619
8620 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8621 if (DBG) {
8622 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8623 }
8624 SubscriptionManager.setSubscriptionProperty(
8625 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8626 (enabled ? "1" : "0"));
8627 }
8628
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008629 return result;
8630 } finally {
8631 Binder.restoreCallingIdentity(identity);
8632 }
8633 }
8634
8635 /**
8636 * Is voice over NR enabled
8637 * @return true if VoNR is enabled else false
8638 */
8639 @Override
8640 public boolean isVoNrEnabled(int subId) {
8641 enforceReadPrivilegedPermission("isVoNrEnabled");
8642 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8643 final long identity = Binder.clearCallingIdentity();
8644 try {
8645 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8646 null, subId, workSource);
8647 if (DBG) log("isVoNrEnabled: " + isEnabled);
8648 return isEnabled;
8649 } finally {
8650 Binder.restoreCallingIdentity(identity);
8651 }
8652 }
8653
8654 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008655 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8656 * network status based on which carrier apps could apply actions accordingly,
8657 * enable/disable default url handler for example.
8658 *
8659 * @param subId the subscription ID that this action applies to.
8660 * @param report control start/stop reporting the default network status.
8661 * {@hide}
8662 */
8663 @Override
8664 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8665 enforceModifyPermission();
8666 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008667
8668 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008669 if (phone == null) {
8670 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8671 return;
8672 }
8673 try {
8674 phone.carrierActionReportDefaultNetworkStatus(report);
8675 } catch (Exception e) {
8676 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008677 } finally {
8678 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008679 }
8680 }
8681
8682 /**
fionaxud9622282017-07-17 17:51:30 -07008683 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8684 * @param subId the subscription ID that this action applies to.
8685 * {@hide}
8686 */
8687 @Override
8688 public void carrierActionResetAll(int subId) {
8689 enforceModifyPermission();
8690 final Phone phone = getPhone(subId);
8691 if (phone == null) {
8692 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8693 return;
8694 }
8695 try {
8696 phone.carrierActionResetAll();
8697 } catch (Exception e) {
8698 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8699 }
8700 }
8701
8702 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008703 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8704 * bug report is being generated.
8705 */
8706 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008707 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008708 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8709 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008710 writer.println("Permission Denial: can't dump Phone from pid="
8711 + Binder.getCallingPid()
8712 + ", uid=" + Binder.getCallingUid()
8713 + "without permission "
8714 + android.Manifest.permission.DUMP);
8715 return;
8716 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008717 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008718 }
Jack Yueb89b242016-06-22 13:27:47 -07008719
Brad Ebingerdac2f002018-04-03 15:17:52 -07008720 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008721 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8722 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8723 @NonNull String[] args) {
8724 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8725 this, in.getFileDescriptor(), out.getFileDescriptor(),
8726 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008727 }
8728
Jack Yueb89b242016-06-22 13:27:47 -07008729 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008730 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008731 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008732 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008733 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008734 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008735 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008736 */
8737 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008738 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008739 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008740 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8741 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8742 try {
8743 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008744 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008745 } catch (SecurityException se) {
8746 enforceModifyPermission();
8747 }
8748 } else {
8749 enforceModifyPermission();
8750 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008751
8752 final long identity = Binder.clearCallingIdentity();
8753 try {
8754 Phone phone = getPhone(subId);
8755 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008756 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8757 phone.carrierActionSetMeteredApnsEnabled(enabled);
8758 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008759 phone.getDataSettingsManager().setDataEnabled(
8760 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008761 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008762 }
8763 } finally {
8764 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008765 }
8766 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008767
8768 /**
8769 * Get Client request stats
8770 * @return List of Client Request Stats
8771 * @hide
8772 */
8773 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008774 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8775 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008776 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008777 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008778 return null;
8779 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008780 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008781
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008782 final long identity = Binder.clearCallingIdentity();
8783 try {
8784 if (phone != null) {
8785 return phone.getClientRequestStats();
8786 }
8787
8788 return null;
8789 } finally {
8790 Binder.restoreCallingIdentity(identity);
8791 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008792 }
8793
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008794 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008795 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008796 if (uid == Process.ROOT_UID && packageName == null) {
8797 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8798 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8799 // exception. ROOT_UID seems not to have a valid package name returned by
8800 // PackageManager, so just fake it here to avoid issues when running telephony shell
8801 // commands that plumb through the RIL as root, like so:
8802 // $ adb root
8803 // $ adb shell cmd phone ...
8804 packageName = "root";
8805 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008806 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008807 }
Jack Yueb4124c2017-02-16 15:32:43 -08008808
8809 /**
Grace Chen70990072017-03-24 17:21:30 -07008810 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008811 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008812 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008813 * @param state State of SIM (power down, power up, pass through)
8814 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8815 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8816 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008817 *
8818 **/
8819 @Override
Grace Chen70990072017-03-24 17:21:30 -07008820 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008821 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008822 Phone phone = PhoneFactory.getPhone(slotIndex);
8823
vagdeviaf9a5b92018-08-15 16:01:53 -07008824 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8825
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008826 final long identity = Binder.clearCallingIdentity();
8827 try {
8828 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008829 phone.setSimPowerState(state, null, workSource);
8830 }
8831 } finally {
8832 Binder.restoreCallingIdentity(identity);
8833 }
8834 }
8835
8836 /**
8837 * Set SIM card power state.
8838 *
8839 * @param slotIndex SIM slot id.
8840 * @param state State of SIM (power down, power up, pass through)
8841 * @param callback callback to trigger after success or failure
8842 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8843 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8844 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8845 *
8846 **/
8847 @Override
8848 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8849 IIntegerConsumer callback) {
8850 enforceModifyPermission();
8851 Phone phone = PhoneFactory.getPhone(slotIndex);
8852
8853 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8854
8855 final long identity = Binder.clearCallingIdentity();
8856 try {
8857 if (phone != null) {
8858 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8859 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008860 }
8861 } finally {
8862 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008863 }
8864 }
Shuo Qiandd210312017-04-12 22:11:33 +00008865
Tyler Gunn65d45c22017-06-05 11:22:26 -07008866 private boolean isUssdApiAllowed(int subId) {
8867 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008868 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008869 if (configManager == null) {
8870 return false;
8871 }
8872 PersistableBundle pb = configManager.getConfigForSubId(subId);
8873 if (pb == null) {
8874 return false;
8875 }
8876 return pb.getBoolean(
8877 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8878 }
8879
Shuo Qiandd210312017-04-12 22:11:33 +00008880 /**
8881 * Check if phone is in emergency callback mode
8882 * @return true if phone is in emergency callback mode
8883 * @param subId sub id
8884 */
goneil9c5f4872017-12-05 14:07:56 -08008885 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008886 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008887 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008888 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008889
8890 final long identity = Binder.clearCallingIdentity();
8891 try {
8892 if (phone != null) {
8893 return phone.isInEcm();
8894 } else {
8895 return false;
8896 }
8897 } finally {
8898 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008899 }
8900 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008901
8902 /**
8903 * Get the current signal strength information for the given subscription.
8904 * Because this information is not updated when the device is in a low power state
8905 * it should not be relied-upon to be current.
8906 * @param subId Subscription index
8907 * @return the most recent cached signal strength info from the modem
8908 */
8909 @Override
8910 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008911 final long identity = Binder.clearCallingIdentity();
8912 try {
8913 Phone p = getPhone(subId);
8914 if (p == null) {
8915 return null;
8916 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008917
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008918 return p.getSignalStrength();
8919 } finally {
8920 Binder.restoreCallingIdentity(identity);
8921 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008922 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008923
Pengquan Meng77b7f132018-08-22 14:49:57 -07008924 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008925 * Get the current modem radio state for the given slot.
8926 * @param slotIndex slot index.
8927 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008928 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008929 * @return the current radio power state from the modem
8930 */
8931 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008932 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008933 Phone phone = PhoneFactory.getPhone(slotIndex);
8934 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008935 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8936 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008937 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8938 }
8939
8940 final long identity = Binder.clearCallingIdentity();
8941 try {
8942 return phone.getRadioPowerState();
8943 } finally {
8944 Binder.restoreCallingIdentity(identity);
8945 }
8946 }
8947 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8948 }
8949
8950 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008951 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8952 *
8953 * <p>Requires one of the following permissions:
8954 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008955 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008956 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8957 * privileges.
8958 *
8959 * @param subId subscription id
8960 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8961 * {@code false}.
8962 */
8963 @Override
8964 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008965 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008966 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008967 try {
8968 mApp.enforceCallingOrSelfPermission(
8969 android.Manifest.permission.ACCESS_NETWORK_STATE,
8970 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008971 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008972 mApp.enforceCallingOrSelfPermission(
8973 permission.READ_BASIC_PHONE_STATE, functionName);
8974 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008975 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008976 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008977 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008978 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008979
Pengquan Menga1bb6272018-09-06 09:59:22 -07008980 boolean isEnabled = false;
8981 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008982 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008983 Phone phone = getPhone(subId);
8984 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008985 } finally {
8986 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07008987 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07008988 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07008989 }
8990
8991
8992 /**
8993 * Enables/Disables the data roaming on the subscription with id {@code subId}.
8994 *
8995 * <p> Requires permission:
8996 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
8997 * privileges.
8998 *
8999 * @param subId subscription id
9000 * @param isEnabled {@code true} means enable, {@code false} means disable.
9001 */
9002 @Override
9003 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009004 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9005 mApp, subId, "setDataRoamingEnabled");
9006
Pengquan Menga1bb6272018-09-06 09:59:22 -07009007 final long identity = Binder.clearCallingIdentity();
9008 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009009 Phone phone = getPhone(subId);
9010 if (phone != null) {
9011 phone.setDataRoamingEnabled(isEnabled);
9012 }
9013 } finally {
9014 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009015 }
9016 }
9017
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009018 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009019 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009020 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009021 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009022 mApp, subId, "isManualNetworkSelectionAllowed");
9023
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009024 boolean isAllowed = true;
9025 final long identity = Binder.clearCallingIdentity();
9026 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009027 Phone phone = getPhone(subId);
9028 if (phone != null) {
9029 isAllowed = phone.isCspPlmnEnabled();
9030 }
9031 } finally {
9032 Binder.restoreCallingIdentity(identity);
9033 }
9034 return isAllowed;
9035 }
9036
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009037 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9038 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009039 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009040 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009041 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009042 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9043 if (phone == null) {
9044 return false;
9045 }
9046 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9047 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9048 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009049 }
9050
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009051 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009052 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009053 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009054 mApp.getSystemService(AppOpsManager.class)
9055 .checkPackage(Binder.getCallingUid(), callingPackage);
9056
Jordan Liu1e142fc2019-04-22 15:10:43 -07009057 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009058 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009059 try {
9060 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009061 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009062 } catch (SecurityException e) {
9063 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9064 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009065 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009066 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009067 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009068 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009069 }
sandeepjsb6c87872021-09-27 15:34:44 +00009070 // checking compatibility, if calling app's target SDK is T and beyond.
9071 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9072 Binder.getCallingUid())) {
9073 isIccIdAccessRestricted = true;
9074 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009075 final long identity = Binder.clearCallingIdentity();
9076 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009077 UiccController uiccController = UiccController.getInstance();
9078 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009079 if (hasReadPermission) {
9080 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009081 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009082
9083 // Remove private info if the caller doesn't have access
9084 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9085 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009086 //setting the value after compatibility check
9087 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009088 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9089 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009090 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009091 if (card == null) {
9092 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009093 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009094 continue;
9095 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009096 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9097 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009098 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009099 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009100 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009101 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9102 for (UiccPortInfo portInfo : portInfos) {
9103 UiccPort port = uiccController.getUiccPortForSlot(
9104 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9105 if (port == null) {
9106 // assume no access if port is null
9107 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9108 continue;
9109 }
9110 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9111 uiccPortInfos.add(portInfo);
9112 } else {
9113 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9114 }
9115 }
9116 filteredInfos.add(new UiccCardInfo(
9117 cardInfo.isEuicc(),
9118 cardInfo.getCardId(),
9119 null,
9120 cardInfo.getPhysicalSlotIndex(),
9121 cardInfo.isRemovable(),
9122 cardInfo.isMultipleEnabledProfilesSupported(),
9123 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009124 }
9125 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009126 } finally {
9127 Binder.restoreCallingIdentity(identity);
9128 }
9129 }
9130
sandeepjsb6c87872021-09-27 15:34:44 +00009131 /**
9132 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9133 * generally private and require carrier privileges to view.
9134 *
9135 * @hide
9136 */
9137 @NonNull
9138 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9139 List<UiccPortInfo> portinfo = new ArrayList<>();
9140 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9141 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9142 }
9143 return new UiccCardInfo(
9144 cardInfo.isEuicc(),
9145 cardInfo.getCardId(),
9146 null,
9147 cardInfo.getPhysicalSlotIndex(),
9148 cardInfo.isRemovable(),
9149 cardInfo.isMultipleEnabledProfilesSupported(),
9150 portinfo
9151 );
9152 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009153
sandeepjsb6c87872021-09-27 15:34:44 +00009154 /**
9155 * @hide
9156 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9157 * These values are generally private and require carrier privileges to view.
9158 */
9159 @NonNull
9160 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9161 return new UiccPortInfo(
9162 UiccPortInfo.ICCID_REDACTED,
9163 portInfo.getPortIndex(),
9164 portInfo.getLogicalSlotIndex(),
9165 portInfo.isActive()
9166 );
9167 }
9168 @Override
9169 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009170 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009171 mApp.getSystemService(AppOpsManager.class)
9172 .checkPackage(Binder.getCallingUid(), callingPackage);
9173
sandeepjsb6c87872021-09-27 15:34:44 +00009174 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009175
Aman Guptaf3c90b32022-03-17 04:54:16 +00009176 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9177 // we are reading iccId which is PII data.
9178 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009179
9180 // checking compatibility, if calling app's target SDK is T and beyond.
9181 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9182 Binder.getCallingUid())) {
9183 isLogicalSlotAccessRestricted = true;
9184 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009185 final long identity = Binder.clearCallingIdentity();
9186 try {
9187 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009188 if (slots == null || slots.length == 0) {
9189 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009190 return null;
9191 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009192 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9193 for (int i = 0; i < slots.length; i++) {
9194 UiccSlot slot = slots[i];
9195 if (slot == null) {
9196 continue;
9197 }
9198
Jordan Liu7be7e652019-05-06 18:55:02 +00009199 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009200 UiccCard card = slot.getUiccCard();
9201 if (card != null) {
9202 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009203 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009204 cardId = slot.getEid();
9205 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009206 // If cardId is null, use iccId of default port as cardId.
9207 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009208 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009209 }
9210
Jordan Liu857451f2019-05-09 16:35:35 -07009211 if (cardId != null) {
9212 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9213 // if cardId is an EID, it's all digits so this is fine
9214 cardId = IccUtils.stripTrailingFs(cardId);
9215 }
9216
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009217 int cardState = 0;
9218 switch (slot.getCardState()) {
9219 case CARDSTATE_ABSENT:
9220 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9221 break;
9222 case CARDSTATE_PRESENT:
9223 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9224 break;
9225 case CARDSTATE_ERROR:
9226 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9227 break;
9228 case CARDSTATE_RESTRICTED:
9229 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9230 break;
9231 default:
9232 break;
9233
9234 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009235 List<UiccPortInfo> portInfos = new ArrayList<>();
9236 int[] portIndexes = slot.getPortList();
9237 for (int portIdx : portIndexes) {
9238 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009239 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009240 portInfos.add(new UiccPortInfo(iccId, portIdx,
9241 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009242 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009243 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009244 slot.isEuicc(),
9245 cardId,
9246 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009247 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009248 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009249 //setting the value after compatibility check
9250 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009251 }
9252 return infos;
9253 } finally {
9254 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009255 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009256 }
9257
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009258 /* Returns null if doesn't have read permission or carrier privilege access. */
9259 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9260 boolean hasReadPermission) {
9261 String iccId = slot.getIccId(portIndex);
9262 if (hasReadPermission) { // if has read permission
9263 return iccId;
9264 } else {
9265 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9266 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9267 // if no read permission, checking carrier privilege access
9268 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9269 return iccId;
9270 }
9271 }
9272 }
9273 // No read permission or carrier privilege access.
9274 return UiccPortInfo.ICCID_REDACTED;
9275 }
9276
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009277 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009278 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009279 public boolean switchSlots(int[] physicalSlots) {
9280 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009281
9282 final long identity = Binder.clearCallingIdentity();
9283 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009284 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9285 for (int i = 0; i < physicalSlots.length; i++) {
9286 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9287 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9288 physicalSlots[i], i));
9289 }
9290 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009291 } finally {
9292 Binder.restoreCallingIdentity(identity);
9293 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009294 }
Jack Yu4c988042018-02-27 15:30:01 -08009295
9296 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009297 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9298 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9299 enforceModifyPermission();
9300
9301 final long identity = Binder.clearCallingIdentity();
9302 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009303 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009304 } finally {
9305 Binder.restoreCallingIdentity(identity);
9306 }
9307 }
9308
9309 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009310 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009311 final long identity = Binder.clearCallingIdentity();
9312 try {
9313 return UiccController.getInstance().getCardIdForDefaultEuicc();
9314 } finally {
9315 Binder.restoreCallingIdentity(identity);
9316 }
9317 }
9318
Pengquan Meng85728fb2018-03-12 16:31:21 -07009319 /**
goneil47ffb6e2018-04-06 15:40:58 -07009320 * A test API to reload the UICC profile.
9321 *
9322 * <p>Requires that the calling app has permission
9323 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9324 * @hide
9325 */
9326 @Override
9327 public void refreshUiccProfile(int subId) {
9328 enforceModifyPermission();
9329
9330 final long identity = Binder.clearCallingIdentity();
9331 try {
9332 Phone phone = getPhone(subId);
9333 if (phone == null) {
9334 return;
9335 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009336 UiccPort uiccPort = phone.getUiccPort();
9337 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009338 return;
9339 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009340 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009341 if (uiccProfile == null) {
9342 return;
9343 }
9344 uiccProfile.refresh();
9345 } finally {
9346 Binder.restoreCallingIdentity(identity);
9347 }
9348 }
9349
9350 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009351 * Returns false if the mobile data is disabled by default, otherwise return true.
9352 */
9353 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009354 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009355 }
9356
9357 /**
9358 * Returns true if the data roaming is enabled by default, i.e the system property
9359 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9360 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9361 */
9362 private boolean getDefaultDataRoamingEnabled(int subId) {
9363 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009364 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009365 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009366 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9367 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9368 return isDataRoamingEnabled;
9369 }
9370
9371 /**
9372 * Returns the default network type for the given {@code subId}, if the default network type is
9373 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9374 */
9375 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009376 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009377 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009378 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9379 return list.get(phoneId);
9380 }
9381 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009382 }
fionaxua13278b2018-03-21 00:08:13 -07009383
9384 @Override
9385 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009386 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009387 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009388
9389 final long identity = Binder.clearCallingIdentity();
9390 try {
9391 final Phone phone = getPhone(subId);
9392 if (phone == null) {
9393 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9394 return;
9395 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009396 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9397 if (cpt != null) {
9398 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9399 }
9400 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009401 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9402 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009403 if (carrierPrivilegeRules == null) {
9404 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9405 } else {
9406 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9407 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009408 } finally {
9409 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009410 }
fionaxua13278b2018-03-21 00:08:13 -07009411 }
9412
9413 @Override
9414 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009415 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009416
9417 final long identity = Binder.clearCallingIdentity();
9418 try {
9419 final Phone phone = getPhone(subId);
9420 if (phone == null) {
9421 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9422 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9423 }
9424 return phone.getCarrierIdListVersion();
9425 } finally {
9426 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009427 }
fionaxua13278b2018-03-21 00:08:13 -07009428 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009429
9430 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009431 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9432 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009433 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009434 mApp, subId, callingPackage, callingFeatureId,
9435 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009436 return -1;
9437 }
9438
9439 final long identity = Binder.clearCallingIdentity();
9440 try {
9441 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9442 } finally {
9443 Binder.restoreCallingIdentity(identity);
9444 }
9445 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009446
9447 @Override
9448 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009449 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009450 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009451 mApp, subId, "getCdmaRoamingMode");
9452
9453 final long identity = Binder.clearCallingIdentity();
9454 try {
9455 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9456 } finally {
9457 Binder.restoreCallingIdentity(identity);
9458 }
9459 }
9460
9461 @Override
9462 public boolean setCdmaRoamingMode(int subId, int mode) {
9463 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9464 mApp, subId, "setCdmaRoamingMode");
9465
9466 final long identity = Binder.clearCallingIdentity();
9467 try {
9468 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9469 } finally {
9470 Binder.restoreCallingIdentity(identity);
9471 }
9472 }
9473
9474 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009475 public int getCdmaSubscriptionMode(int subId) {
9476 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009477 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009478 mApp, subId, "getCdmaSubscriptionMode");
9479
9480 final long identity = Binder.clearCallingIdentity();
9481 try {
9482 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9483 } finally {
9484 Binder.restoreCallingIdentity(identity);
9485 }
9486 }
9487
9488 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009489 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9490 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9491 mApp, subId, "setCdmaSubscriptionMode");
9492
9493 final long identity = Binder.clearCallingIdentity();
9494 try {
9495 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9496 } finally {
9497 Binder.restoreCallingIdentity(identity);
9498 }
9499 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009500
sqianc5eccab2018-10-19 18:46:41 -07009501 @Override
sqian8c685422019-02-22 15:55:18 -08009502 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009503 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009504 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009505 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9506 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009507 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9508 }
9509 final long identity = Binder.clearCallingIdentity();
9510 try {
sqian854d44b2018-12-12 16:48:18 -08009511 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9512 for (Phone phone: PhoneFactory.getPhones()) {
9513 if (phone.getEmergencyNumberTracker() != null
9514 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9515 emergencyNumberListInternal.put(
9516 phone.getSubId(),
9517 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9518 }
sqian11b7a0e2018-12-05 18:48:28 -08009519 }
sqian854d44b2018-12-12 16:48:18 -08009520 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009521 } finally {
9522 Binder.restoreCallingIdentity(identity);
9523 }
sqianc5eccab2018-10-19 18:46:41 -07009524 }
9525
9526 @Override
sqian8c685422019-02-22 15:55:18 -08009527 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009528 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009529 if (!exactMatch) {
9530 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009531 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009532 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009533 }
9534 final long identity = Binder.clearCallingIdentity();
9535 try {
sqian854d44b2018-12-12 16:48:18 -08009536 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009537 //Note: we ignore passed in param exactMatch. We can remove it once
9538 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009539 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009540 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009541 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009542 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009543 }
sqian11b7a0e2018-12-05 18:48:28 -08009544 }
9545 return false;
9546 } finally {
9547 Binder.restoreCallingIdentity(identity);
9548 }
9549 }
9550
sqianf4ca7ed2019-01-15 18:32:07 -08009551 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009552 * Start emergency callback mode for GsmCdmaPhone for testing.
9553 */
9554 @Override
9555 public void startEmergencyCallbackMode() {
9556 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9557 "startEmergencyCallbackMode");
9558 enforceModifyPermission();
9559 final long identity = Binder.clearCallingIdentity();
9560 try {
9561 for (Phone phone : PhoneFactory.getPhones()) {
9562 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9563 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9564 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9565 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9566 gsmCdmaPhone.obtainMessage(
9567 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9568 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9569 }
9570 }
9571 } finally {
9572 Binder.restoreCallingIdentity(identity);
9573 }
9574 }
9575
9576 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009577 * Update emergency number list for test mode.
9578 */
9579 @Override
9580 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9581 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9582 "updateEmergencyNumberListTestMode");
9583
9584 final long identity = Binder.clearCallingIdentity();
9585 try {
9586 for (Phone phone: PhoneFactory.getPhones()) {
9587 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9588 if (tracker != null) {
9589 tracker.executeEmergencyNumberTestModeCommand(action, num);
9590 }
9591 }
9592 } finally {
9593 Binder.restoreCallingIdentity(identity);
9594 }
9595 }
9596
9597 /**
9598 * Get the full emergency number list for test mode.
9599 */
9600 @Override
9601 public List<String> getEmergencyNumberListTestMode() {
9602 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9603 "getEmergencyNumberListTestMode");
9604
9605 final long identity = Binder.clearCallingIdentity();
9606 try {
9607 Set<String> emergencyNumbers = new HashSet<>();
9608 for (Phone phone: PhoneFactory.getPhones()) {
9609 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9610 if (tracker != null) {
9611 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9612 emergencyNumbers.add(num.getNumber());
9613 }
9614 }
9615 }
9616 return new ArrayList<>(emergencyNumbers);
9617 } finally {
9618 Binder.restoreCallingIdentity(identity);
9619 }
9620 }
9621
chen xud6b45bd2018-10-30 22:27:10 -07009622 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009623 public int getEmergencyNumberDbVersion(int subId) {
9624 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9625
9626 final long identity = Binder.clearCallingIdentity();
9627 try {
9628 final Phone phone = getPhone(subId);
9629 if (phone == null) {
9630 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9631 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9632 }
9633 return phone.getEmergencyNumberDbVersion();
9634 } finally {
9635 Binder.restoreCallingIdentity(identity);
9636 }
9637 }
9638
9639 @Override
9640 public void notifyOtaEmergencyNumberDbInstalled() {
9641 enforceModifyPermission();
9642
9643 final long identity = Binder.clearCallingIdentity();
9644 try {
9645 for (Phone phone: PhoneFactory.getPhones()) {
9646 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9647 if (tracker != null) {
9648 tracker.updateOtaEmergencyNumberDatabase();
9649 }
9650 }
9651 } finally {
9652 Binder.restoreCallingIdentity(identity);
9653 }
9654 }
9655
9656 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009657 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009658 enforceActiveEmergencySessionPermission();
9659
9660 final long identity = Binder.clearCallingIdentity();
9661 try {
9662 for (Phone phone: PhoneFactory.getPhones()) {
9663 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9664 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009665 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9666 }
9667 }
9668 } finally {
9669 Binder.restoreCallingIdentity(identity);
9670 }
9671 }
9672
9673 @Override
9674 public void resetOtaEmergencyNumberDbFilePath() {
9675 enforceActiveEmergencySessionPermission();
9676
9677 final long identity = Binder.clearCallingIdentity();
9678 try {
9679 for (Phone phone: PhoneFactory.getPhones()) {
9680 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9681 if (tracker != null) {
9682 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009683 }
9684 }
9685 } finally {
9686 Binder.restoreCallingIdentity(identity);
9687 }
9688 }
9689
9690 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009691 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9692 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9693 Phone phone = getPhone(subId);
9694 if (phone == null) {
9695 return null;
9696 }
9697 final long identity = Binder.clearCallingIdentity();
9698 try {
9699 UiccProfile profile = UiccController.getInstance()
9700 .getUiccProfileForPhone(phone.getPhoneId());
9701 if (profile != null) {
9702 return profile.getCertsFromCarrierPrivilegeAccessRules();
9703 }
9704 } finally {
9705 Binder.restoreCallingIdentity(identity);
9706 }
9707 return null;
9708 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009709
9710 /**
9711 * Enable or disable a modem stack.
9712 */
9713 @Override
9714 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9715 enforceModifyPermission();
9716
9717 final long identity = Binder.clearCallingIdentity();
9718 try {
9719 Phone phone = PhoneFactory.getPhone(slotIndex);
9720 if (phone == null) {
9721 return false;
9722 } else {
9723 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9724 }
9725 } finally {
9726 Binder.restoreCallingIdentity(identity);
9727 }
9728 }
Michelecea4cf22018-12-21 15:00:11 -08009729
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009730 /**
9731 * Whether a modem stack is enabled or not.
9732 */
9733 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009734 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9735 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009736 Phone phone = PhoneFactory.getPhone(slotIndex);
9737 if (phone == null) return false;
9738
9739 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009740 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9741 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009742 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9743 }
9744
9745 final long identity = Binder.clearCallingIdentity();
9746 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009747 try {
9748 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9749 } catch (NoSuchElementException ex) {
9750 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9751 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009752 } finally {
9753 Binder.restoreCallingIdentity(identity);
9754 }
9755 }
9756
Michelecea4cf22018-12-21 15:00:11 -08009757 @Override
Michele0ea7d782019-03-19 14:58:42 -07009758 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009759 enforceModifyPermission();
9760
9761 final long identity = Binder.clearCallingIdentity();
9762 try {
9763 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009764 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009765 .commit();
9766 } finally {
9767 Binder.restoreCallingIdentity(identity);
9768 }
9769 }
9770
9771 @Override
Michele0ea7d782019-03-19 14:58:42 -07009772 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009773 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009774 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009775 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9776 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009777 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009778 }
Michelecea4cf22018-12-21 15:00:11 -08009779
9780 final long identity = Binder.clearCallingIdentity();
9781 try {
Michele0ea7d782019-03-19 14:58:42 -07009782 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009783 } finally {
9784 Binder.restoreCallingIdentity(identity);
9785 }
9786 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009787
Michele0ea7d782019-03-19 14:58:42 -07009788 @TelephonyManager.IsMultiSimSupportedResult
9789 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009790 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9791 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9792 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009793 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9794 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009795 }
9796 // Check if the hardware supports multisim functionality. If usage of multisim is not
9797 // supported by the modem, indicate that it is restricted.
9798 PhoneCapability staticCapability =
9799 mPhoneConfigurationManager.getStaticPhoneCapability();
9800 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009801 loge("isMultiSimSupportedInternal: no static configuration available");
9802 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009803 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009804 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009805 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9806 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009807 }
9808 // Check if support of multiple SIMs is restricted by carrier
9809 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009810 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009811 }
9812
Michele0ea7d782019-03-19 14:58:42 -07009813 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009814 }
9815
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009816 /**
9817 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009818 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9819 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9820 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009821 * @param numOfSims number of active sims we want to switch to
9822 */
9823 @Override
9824 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009825 if (numOfSims == 1) {
9826 enforceModifyPermission();
9827 } else {
9828 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9829 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9830 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009831 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009832
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009833 try {
Michele30b57b22019-03-01 12:01:14 -08009834 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009835 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009836 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9837 return;
9838 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009839 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9840 } finally {
9841 Binder.restoreCallingIdentity(identity);
9842 }
9843 }
9844
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009845 @Override
9846 public boolean isApplicationOnUicc(int subId, int appType) {
9847 enforceReadPrivilegedPermission("isApplicationOnUicc");
9848 Phone phone = getPhone(subId);
9849 if (phone == null) {
9850 return false;
9851 }
9852 final long identity = Binder.clearCallingIdentity();
9853 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009854 UiccPort uiccPort = phone.getUiccPort();
9855 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009856 return false;
9857 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009858 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009859 if (uiccProfile == null) {
9860 return false;
9861 }
9862 if (TelephonyManager.APPTYPE_SIM <= appType
9863 && appType <= TelephonyManager.APPTYPE_ISIM) {
9864 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9865 }
9866 return false;
9867 } finally {
9868 Binder.restoreCallingIdentity(identity);
9869 }
9870 }
9871
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009872 /**
chen xub4baa772019-04-03 10:23:41 -07009873 * Get whether making changes to modem configurations will trigger reboot.
9874 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009875 */
9876 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009877 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9878 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009879 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009880 mApp, subId, callingPackage, callingFeatureId,
9881 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009882 return false;
9883 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009884 final long identity = Binder.clearCallingIdentity();
9885 try {
9886 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9887 } finally {
9888 Binder.restoreCallingIdentity(identity);
9889 }
9890 }
9891
Nathan Harold29f5f052019-02-15 13:41:57 -08009892 private void updateModemStateMetrics() {
9893 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9894 // TODO: check the state for each modem if the api is ready.
9895 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9896 }
9897
Pengquan Meng3889a572019-01-23 11:16:29 -08009898 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009899 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009900 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009901 // Verify that the callingPackage belongs to the calling UID
9902 mApp.getSystemService(AppOpsManager.class)
9903 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009904 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009905 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009906 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009907 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9908 if (slotInfos != null) {
9909 for (int i = 0; i < slotInfos.length; i++) {
9910 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9911 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9912 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9913 portInfo.getLogicalSlotIndex()));
9914 }
9915 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009916 }
9917 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009918 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009919 } finally {
9920 Binder.restoreCallingIdentity(identity);
9921 }
9922 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009923
9924 /**
9925 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009926 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009927 */
jimsunf9ec1622022-09-13 21:18:43 +08009928 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009929 @Override
9930 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009931 return getHalVersion(HAL_SERVICE_RADIO);
9932 }
9933
9934 /**
9935 * Get the HAL Version of a specific service
9936 */
9937 @Override
9938 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009939 Phone phone = getDefaultPhone();
9940 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009941 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009942 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9943 return hv.major * 100 + hv.minor;
9944 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009945
9946 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009947 * Get the current calling package name.
9948 * @return the current calling package name
9949 */
9950 @Override
9951 public String getCurrentPackageName() {
9952 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9953 }
9954
9955 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009956 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9957 * corresponding network requests on a subId.
9958 *
9959 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009960 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009961 * 2) APN is un-metered for this subscription, or
9962 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009963 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009964 *
9965 * @return whether data is allowed for a apn type.
9966 *
9967 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009968 */
9969 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009970 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009971 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9972 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009973
9974 // Now that all security checks passes, perform the operation as ourselves.
9975 final long identity = Binder.clearCallingIdentity();
9976 try {
9977 Phone phone = getPhone(subId);
9978 if (phone == null) return false;
9979
Jack Yu27422a52022-03-21 10:38:05 -07009980 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009981 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009982 isMetered = phone.getDataNetworkController().getDataConfigManager()
9983 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9984 phone.getServiceState().getDataRoaming());
9985 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009986 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -07009987 } finally {
9988 Binder.restoreCallingIdentity(identity);
9989 }
9990 }
9991
9992 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07009993 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07009994 enforceReadPrivilegedPermission("isApnMetered");
9995
9996 // Now that all security checks passes, perform the operation as ourselves.
9997 final long identity = Binder.clearCallingIdentity();
9998 try {
9999 Phone phone = getPhone(subId);
10000 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -070010001 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10002 DataUtils.apnTypeToNetworkCapability(apnType),
10003 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010004 } finally {
10005 Binder.restoreCallingIdentity(identity);
10006 }
10007 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010008
10009 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010010 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10011 int subscriptionId, IBooleanConsumer resultCallback) {
10012 enforceModifyPermission();
10013 long token = Binder.clearCallingIdentity();
10014 try {
10015 Phone phone = getPhone(subscriptionId);
10016 if (phone == null) {
10017 try {
10018 if (resultCallback != null) {
10019 resultCallback.accept(false);
10020 }
10021 } catch (RemoteException e) {
10022 // ignore
10023 }
10024 return;
10025 }
10026 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10027 Pair.create(specifiers, (x) -> {
10028 try {
10029 if (resultCallback != null) {
10030 resultCallback.accept(x);
10031 }
10032 } catch (RemoteException e) {
10033 // ignore
10034 }
10035 });
10036 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10037 } finally {
10038 Binder.restoreCallingIdentity(token);
10039 }
10040 }
10041
10042 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010043 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10044 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010045 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010046 mApp, subId, "getSystemSelectionChannels");
10047 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10048 final long identity = Binder.clearCallingIdentity();
10049 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010050 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10051 if (result instanceof IllegalStateException) {
10052 throw (IllegalStateException) result;
10053 }
10054 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010055 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10056 return specifiers;
10057 } finally {
10058 Binder.restoreCallingIdentity(identity);
10059 }
10060 }
10061
10062 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010063 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010064 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010065 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010066 }
10067
10068 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010069 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10070 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010071 if (callingPackage == null) {
10072 callingPackage = getCurrentPackageName();
10073 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010074 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10075 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010076 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10077 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010078 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10079 }
10080 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10081 Intent intent = new Intent();
10082 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10083 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10084 // Bring up choose default SMS subscription dialog right now
10085 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10086 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10087 mApp.startActivity(intent);
10088 }
chen xud5ca2d52019-05-28 15:20:57 -070010089
10090 @Override
10091 public String getMmsUAProfUrl(int subId) {
10092 //TODO investigate if this API should require proper permission check in R b/133791609
10093 final long identity = Binder.clearCallingIdentity();
10094 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010095 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10096 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10097 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10098 return carrierUAProfUrl;
10099 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010100 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10101 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010102 } finally {
10103 Binder.restoreCallingIdentity(identity);
10104 }
10105 }
10106
10107 @Override
10108 public String getMmsUserAgent(int subId) {
10109 //TODO investigate if this API should require proper permission check in R b/133791609
10110 final long identity = Binder.clearCallingIdentity();
10111 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010112 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10113 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10114 if (!TextUtils.isEmpty(carrierUserAgent)) {
10115 return carrierUserAgent;
10116 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010117 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10118 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010119 } finally {
10120 Binder.restoreCallingIdentity(identity);
10121 }
10122 }
Jack Yub07d4972019-05-28 16:12:25 -070010123
10124 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010125 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10126 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010127
Jack Yub07d4972019-05-28 16:12:25 -070010128 final long identity = Binder.clearCallingIdentity();
10129 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010130 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010131 if (phone == null) return false;
10132
Ling Maf188d502022-09-16 15:22:36 -070010133 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010134 } finally {
10135 Binder.restoreCallingIdentity(identity);
10136 }
10137 }
10138
10139 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010140 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010141 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010142 enforceModifyPermission();
10143
changbettyd5c246e2019-12-24 15:40:37 +080010144 final long identity = Binder.clearCallingIdentity();
10145 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010146 Phone phone = getPhone(subscriptionId);
10147 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010148
Ling Maf188d502022-09-16 15:22:36 -070010149 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010150 } finally {
10151 Binder.restoreCallingIdentity(identity);
10152 }
10153 }
10154
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010155 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010156 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010157 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10158 * otherwise.
10159 */
10160 @Override
10161 public void setCepEnabled(boolean isCepEnabled) {
10162 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10163
10164 final long identity = Binder.clearCallingIdentity();
10165 try {
10166 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10167 for (Phone phone : PhoneFactory.getPhones()) {
10168 Phone defaultPhone = phone.getImsPhone();
10169 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10170 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10171 ImsPhoneCallTracker imsPhoneCallTracker =
10172 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10173 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10174 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10175 + imsPhone.getMsisdn());
10176 }
10177 }
10178 } finally {
10179 Binder.restoreCallingIdentity(identity);
10180 }
10181 }
allenwtsu46dcc572020-01-08 18:24:03 +080010182
10183 /**
10184 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10185 *
10186 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10187 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10188 * before being read.
10189 */
10190 @Override
10191 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10192 isCompressed) {
10193 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10194 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010195 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10196 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10197 }
10198 if (!isImsAvailableOnDevice()) {
10199 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10200 "IMS not available on device.");
10201 }
10202
10203 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010204 try {
Hui Wang761a6682020-10-31 05:12:53 +000010205 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10206 } finally {
10207 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010208 }
10209 }
zoey chene02881a2019-12-30 16:11:23 +080010210
10211 @Override
10212 public boolean isIccLockEnabled(int subId) {
10213 enforceReadPrivilegedPermission("isIccLockEnabled");
10214
10215 // Now that all security checks passes, perform the operation as ourselves.
10216 final long identity = Binder.clearCallingIdentity();
10217 try {
10218 Phone phone = getPhone(subId);
10219 if (phone != null && phone.getIccCard() != null) {
10220 return phone.getIccCard().getIccLockEnabled();
10221 } else {
10222 return false;
10223 }
10224 } finally {
10225 Binder.restoreCallingIdentity(identity);
10226 }
10227 }
10228
10229 /**
10230 * Set the ICC pin lock enabled or disabled.
10231 *
10232 * @return an integer representing the status of IccLock enabled or disabled in the following
10233 * three cases:
10234 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10235 * successfully.
10236 * - Positive number and zero for remaining password attempts.
10237 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10238 *
10239 */
10240 @Override
10241 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10242 enforceModifyPermission();
10243
10244 Phone phone = getPhone(subId);
10245 if (phone == null) {
10246 return 0;
10247 }
10248 // Now that all security checks passes, perform the operation as ourselves.
10249 final long identity = Binder.clearCallingIdentity();
10250 try {
10251 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10252 new Pair<Boolean, String>(enabled, password), phone, null);
10253 return attemptsRemaining;
10254
10255 } catch (Exception e) {
10256 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10257 } finally {
10258 Binder.restoreCallingIdentity(identity);
10259 }
10260 return 0;
10261 }
10262
10263 /**
10264 * Change the ICC password used in ICC pin lock.
10265 *
10266 * @return an integer representing the status of IccLock changed in the following three cases:
10267 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10268 * - Positive number and zero for remaining password attempts.
10269 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10270 *
10271 */
10272 @Override
10273 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10274 enforceModifyPermission();
10275
10276 Phone phone = getPhone(subId);
10277 if (phone == null) {
10278 return 0;
10279 }
10280 // Now that all security checks passes, perform the operation as ourselves.
10281 final long identity = Binder.clearCallingIdentity();
10282 try {
10283 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10284 new Pair<String, String>(oldPassword, newPassword), phone, null);
10285 return attemptsRemaining;
10286
10287 } catch (Exception e) {
10288 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10289 } finally {
10290 Binder.restoreCallingIdentity(identity);
10291 }
10292 return 0;
10293 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010294
10295 /**
10296 * Request for receiving user activity notification
10297 */
10298 @Override
10299 public void requestUserActivityNotification() {
10300 if (!mNotifyUserActivity.get()
10301 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10302 mNotifyUserActivity.set(true);
10303 }
10304 }
10305
10306 /**
10307 * Called when userActivity is signalled in the power manager.
10308 * This is safe to call from any thread, with any window manager locks held or not.
10309 */
10310 @Override
10311 public void userActivity() {
10312 // ***************************************
10313 // * Inherited from PhoneWindowManager *
10314 // ***************************************
10315 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10316 // WITH ITS LOCKS HELD.
10317 //
10318 // This code must be VERY careful about the locks
10319 // it acquires.
10320 // In fact, the current code acquires way too many,
10321 // and probably has lurking deadlocks.
10322
10323 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10324 throw new SecurityException("Only the OS may call notifyUserActivity()");
10325 }
10326
10327 if (mNotifyUserActivity.getAndSet(false)) {
10328 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10329 USER_ACTIVITY_NOTIFICATION_DELAY);
10330 }
10331 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010332
10333 @Override
10334 public boolean canConnectTo5GInDsdsMode() {
10335 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10336 }
Jack Yud10cdd42020-09-28 20:28:01 -070010337
10338 @Override
10339 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10340 String callingFeatureId) {
10341 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10342 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10343 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10344 }
10345
10346 Phone phone = getPhone(subId);
10347 if (phone == null) {
10348 throw new RuntimeException("phone is not available");
10349 }
10350 // Now that all security checks passes, perform the operation as ourselves.
10351 final long identity = Binder.clearCallingIdentity();
10352 try {
10353 return phone.getEquivalentHomePlmns();
10354 } finally {
10355 Binder.restoreCallingIdentity(identity);
10356 }
10357 }
Daniel Bright59e67312020-11-13 11:49:37 -080010358
10359 @Override
10360 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010361 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10362 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010363 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010364 if (radioInterfaceCapabilities == null) {
10365 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010366 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010367 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010368 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010369
Hui Wang641e81c2020-10-12 12:14:23 -070010370 @Override
10371 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10372 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010373 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10374 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10375 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10376 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10377 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010378 if (DBG) {
10379 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10380 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10381 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10382 }
10383
10384 if (!SubscriptionManager.isValidSubscriptionId(subId)
10385 || appType < TelephonyManager.APPTYPE_UNKNOWN
10386 || appType > TelephonyManager.APPTYPE_ISIM
10387 || nafUrl == null || securityProtocol == null || callback == null) {
10388 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10389 if (callback != null) {
10390 try {
10391 callback.onAuthenticationFailure(
10392 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10393 } catch (RemoteException exception) {
10394 log("Fail to notify onAuthenticationFailure due to " + exception);
10395 }
10396 return;
10397 }
10398 }
10399
10400 final long token = Binder.clearCallingIdentity();
10401 try {
10402 getGbaManager(subId).bootstrapAuthenticationRequest(
10403 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10404 forceBootStrapping, callback));
10405 } finally {
10406 Binder.restoreCallingIdentity(token);
10407 }
10408 }
10409
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010410 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010411 * Attempts to set the radio power state for all phones for thermal reason.
10412 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010413 * requested radio power state will actually be set. See {@link
10414 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10415 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010416 * @param enable {@code true} if trying to turn radio on.
10417 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10418 * false}.
10419 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010420 private boolean setRadioPowerForThermal(boolean enable) {
10421 boolean isPhoneAvailable = false;
10422 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10423 Phone phone = PhoneFactory.getPhone(i);
10424 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010425 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010426 isPhoneAvailable = true;
10427 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010428 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010429
10430 // return true if successfully informed the phone object about the thermal radio power
10431 // request.
10432 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010433 }
10434
10435 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010436 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010437 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10438 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10439 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10440 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10441 throw new IllegalArgumentException("modem does not support data throttling");
10442 }
10443
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010444 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10445 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010446 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010447 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10448 }
10449
Sarah Chinecc78c42022-03-31 21:16:48 -070010450 setDataEnabledForReason(
10451 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010452
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010453 if (isDataThrottlingSupported) {
10454 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010455 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010456 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10457 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10458 } else if (thermalMitigationResult
10459 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010460 log("Modem likely does not support data throttling on secondary carrier. Data " +
10461 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10462 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010463 }
10464 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010465 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010466
10467 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010468 }
10469
Jack Nudelman644b91a2021-03-12 14:09:48 -080010470 private static List<String> getThermalMitigationAllowlist(Context context) {
10471 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10472 for (String pckg : context.getResources()
10473 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10474 sThermalMitigationAllowlistedPackages.add(pckg);
10475 }
10476 }
10477
10478 return sThermalMitigationAllowlistedPackages;
10479 }
10480
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010481 private boolean isAnyPhoneInEmergencyState() {
10482 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10483 if (tm.isInEmergencyCall()) {
10484 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10485 return true;
10486 }
10487 for (Phone phone : PhoneFactory.getPhones()) {
10488 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10489 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10490 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10491 + phone.isInEcm());
10492 return true;
10493 }
10494 }
10495
10496 return false;
10497 }
10498
Jack Nudelman644b91a2021-03-12 14:09:48 -080010499 /**
10500 * Used by shell commands to add an authorized package name for thermal mitigation.
10501 * @param packageName name of package to be allowlisted
10502 * @param context
10503 */
10504 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10505 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10506 sThermalMitigationAllowlistedPackages.add(packageName);
10507 }
10508
10509 /**
10510 * Used by shell commands to remove an authorized package name for thermal mitigation.
10511 * @param packageName name of package to remove from allowlist
10512 * @param context
10513 */
10514 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10515 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10516 sThermalMitigationAllowlistedPackages.remove(packageName);
10517 }
10518
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010519 /**
10520 * Thermal mitigation request to control functionalities at modem.
10521 *
10522 * @param subId the id of the subscription.
10523 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010524 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010525 *
10526 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10527 */
10528 @Override
10529 @ThermalMitigationResult
10530 public int sendThermalMitigationRequest(
10531 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010532 ThermalMitigationRequest thermalMitigationRequest,
10533 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010534 enforceModifyPermission();
10535
Jack Nudelman644b91a2021-03-12 14:09:48 -080010536 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10537 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10538 .contains(callingPackage)) {
10539 throw new SecurityException("Calling package must be configured in the device config. "
10540 + "calling package: " + callingPackage);
10541 }
10542
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010543 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10544 final long identity = Binder.clearCallingIdentity();
10545
10546 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10547 try {
10548 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10549 switch (thermalMitigationAction) {
10550 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10551 thermalMitigationResult =
10552 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010553 thermalMitigationRequest.getDataThrottlingRequest(),
10554 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010555 break;
10556 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10557 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10558 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10559 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10560 }
10561
10562 // Ensure that radio is on. If not able to power on due to phone being
10563 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010564 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010565 thermalMitigationResult =
10566 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10567 break;
10568 }
10569
10570 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010571 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010572 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10573 break;
10574 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10575 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10576 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10577 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10578 }
10579
10580 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10581 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010582 Phone phone = getPhone(subId);
10583 if (phone == null) {
10584 thermalMitigationResult =
10585 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10586 break;
10587 }
10588
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010589 TelephonyConnectionService service =
10590 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010591 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010592 Log.e(LOG_TAG, "An emergency call is pending");
10593 thermalMitigationResult =
10594 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10595 break;
10596 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010597 thermalMitigationResult =
10598 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10599 break;
10600 }
10601 } else {
10602 thermalMitigationResult =
10603 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10604 break;
10605 }
10606
10607 // Turn radio off. If not able to power off due to phone being unavailable,
10608 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010609 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010610 thermalMitigationResult =
10611 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10612 break;
10613 }
10614 thermalMitigationResult =
10615 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10616 break;
10617 default:
10618 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10619 + "not exist. Requested action: " + thermalMitigationAction);
10620 }
10621 } catch (IllegalArgumentException e) {
10622 throw e;
10623 } catch (Exception e) {
10624 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10625 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10626 } finally {
10627 Binder.restoreCallingIdentity(identity);
10628 }
10629
10630 if (DBG) {
10631 log("thermalMitigationRequest returning with thermalMitigationResult: "
10632 + thermalMitigationResult);
10633 }
10634
10635 return thermalMitigationResult;
10636 }
Hui Wang641e81c2020-10-12 12:14:23 -070010637
10638 /**
10639 * Set the GbaService Package Name that Telephony will bind to.
10640 *
10641 * @param subId The sim that the GbaService is associated with.
10642 * @param packageName The name of the package to be replaced with.
10643 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10644 */
10645 @Override
10646 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10647 enforceModifyPermission();
10648
10649 final long identity = Binder.clearCallingIdentity();
10650 try {
10651 return getGbaManager(subId).overrideServicePackage(packageName);
10652 } finally {
10653 Binder.restoreCallingIdentity(identity);
10654 }
10655 }
10656
10657 /**
10658 * Return the package name of the currently bound GbaService.
10659 *
10660 * @param subId The sim that the GbaService is associated with.
10661 * @return the package name of the GbaService configuration, null if GBA is not supported.
10662 */
10663 @Override
10664 public String getBoundGbaService(int subId) {
10665 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10666
10667 final long identity = Binder.clearCallingIdentity();
10668 try {
10669 return getGbaManager(subId).getServicePackage();
10670 } finally {
10671 Binder.restoreCallingIdentity(identity);
10672 }
10673 }
10674
10675 /**
10676 * Set the release time for telephony to unbind GbaService.
10677 *
10678 * @param subId The sim that the GbaService is associated with.
10679 * @param interval The release time to unbind GbaService by millisecond.
10680 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10681 */
10682 @Override
10683 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10684 enforceModifyPermission();
10685
10686 final long identity = Binder.clearCallingIdentity();
10687 try {
10688 return getGbaManager(subId).overrideReleaseTime(interval);
10689 } finally {
10690 Binder.restoreCallingIdentity(identity);
10691 }
10692 }
10693
10694 /**
10695 * Return the release time for telephony to unbind GbaService.
10696 *
10697 * @param subId The sim that the GbaService is associated with.
10698 * @return The release time to unbind GbaService by millisecond.
10699 */
10700 @Override
10701 public int getGbaReleaseTime(int subId) {
10702 enforceReadPrivilegedPermission("getGbaReleaseTime");
10703
10704 final long identity = Binder.clearCallingIdentity();
10705 try {
10706 return getGbaManager(subId).getReleaseTime();
10707 } finally {
10708 Binder.restoreCallingIdentity(identity);
10709 }
10710 }
10711
10712 private GbaManager getGbaManager(int subId) {
10713 GbaManager instance = GbaManager.getInstance(subId);
10714 if (instance == null) {
10715 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10716 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10717 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10718 }
10719 return instance;
10720 }
Hui Wang761a6682020-10-31 05:12:53 +000010721
10722 /**
10723 * indicate whether the device and the carrier can support
10724 * RCS VoLTE single registration.
10725 */
10726 @Override
10727 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010728 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10729 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10730 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10731 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010732
10733 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10734 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10735 }
10736
10737 final long identity = Binder.clearCallingIdentity();
10738 try {
10739 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10740 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010741 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10742 if (isCapable != null) {
10743 return isCapable;
10744 }
Hui Wang761a6682020-10-31 05:12:53 +000010745 }
Hui Wang67af90e2021-06-04 16:57:15 -070010746 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10747 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010748 } finally {
10749 Binder.restoreCallingIdentity(identity);
10750 }
10751 }
10752
10753 /**
10754 * Register RCS provisioning callback.
10755 */
10756 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010757 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010758 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010759 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010760 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010761 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10762 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010763
10764 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10765 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10766 }
10767 if (!isImsAvailableOnDevice()) {
10768 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10769 "IMS not available on device.");
10770 }
10771
10772 final long identity = Binder.clearCallingIdentity();
10773 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010774 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010775 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010776 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10777 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010778 }
Hui Wang761a6682020-10-31 05:12:53 +000010779 } finally {
10780 Binder.restoreCallingIdentity(identity);
10781 }
10782 }
10783
10784 /**
10785 * Unregister RCS provisioning callback.
10786 */
10787 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010788 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010789 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010790 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010791 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010792 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10793 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010794
10795 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10796 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10797 }
10798 if (!isImsAvailableOnDevice()) {
10799 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10800 "IMS not available on device.");
10801 }
10802
10803 final long identity = Binder.clearCallingIdentity();
10804 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010805 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010806 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010807 } finally {
10808 Binder.restoreCallingIdentity(identity);
10809 }
10810 }
10811
10812 /**
10813 * trigger RCS reconfiguration.
10814 */
10815 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010816 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10817 "triggerRcsReconfiguration",
10818 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010819
10820 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10821 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10822 }
10823 if (!isImsAvailableOnDevice()) {
10824 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10825 "IMS not available on device.");
10826 }
10827
10828 final long identity = Binder.clearCallingIdentity();
10829 try {
10830 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10831 } finally {
10832 Binder.restoreCallingIdentity(identity);
10833 }
10834 }
10835
10836 /**
10837 * Provide the client configuration parameters of the RCS application.
10838 */
10839 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010840 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10841 "setRcsClientConfiguration",
10842 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010843
10844 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10845 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10846 }
10847 if (!isImsAvailableOnDevice()) {
10848 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10849 "IMS not available on device.");
10850 }
10851
10852 final long identity = Binder.clearCallingIdentity();
10853
10854 try {
10855 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10856 if (configBinder == null) {
10857 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010858 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10859 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010860 } else {
10861 configBinder.setRcsClientConfiguration(rcc);
10862 }
joonhunshin3e154242021-09-17 06:33:39 +000010863
10864 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10865 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010866 } catch (RemoteException e) {
10867 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010868 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10869 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010870 } finally {
10871 Binder.restoreCallingIdentity(identity);
10872 }
10873 }
10874
10875 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010876 * Enables or disables the test mode for RCS VoLTE single registration.
10877 */
10878 @Override
10879 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10880 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10881 "setRcsSingleRegistrationTestModeEnabled");
10882
10883 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10884 }
10885
10886 /**
10887 * Gets the test mode for RCS VoLTE single registration.
10888 */
10889 @Override
10890 public boolean getRcsSingleRegistrationTestModeEnabled() {
10891 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10892 "getRcsSingleRegistrationTestModeEnabled");
10893
10894 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10895 }
10896
10897 /**
Hui Wang761a6682020-10-31 05:12:53 +000010898 * Overrides the config of RCS VoLTE single registration enabled for the device.
10899 */
10900 @Override
10901 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10902 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10903 "setDeviceSingleRegistrationEnabledOverride");
10904 enforceModifyPermission();
10905
10906 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10907 : Boolean.parseBoolean(enabledStr);
10908 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010909 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010910 }
10911
10912 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010913 * Sends a device to device communication message. Only usable via shell.
10914 * @param message message to send.
10915 * @param value message value.
10916 */
10917 @Override
10918 public void sendDeviceToDeviceMessage(int message, int value) {
10919 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010920 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010921 enforceModifyPermission();
10922
10923 final long identity = Binder.clearCallingIdentity();
10924 try {
10925 TelephonyConnectionService service =
10926 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10927 if (service == null) {
10928 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10929 return;
10930 }
10931 service.sendTestDeviceToDeviceMessage(message, value);
10932 } finally {
10933 Binder.restoreCallingIdentity(identity);
10934 }
10935 }
10936
Tyler Gunnbabbda02021-02-10 11:05:02 -080010937 /**
10938 * Sets the specified device to device transport active.
10939 * @param transport The transport to set active.
10940 */
10941 @Override
10942 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10943 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10944 "setActiveDeviceToDeviceTransport");
10945 enforceModifyPermission();
10946
10947 final long identity = Binder.clearCallingIdentity();
10948 try {
10949 TelephonyConnectionService service =
10950 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10951 if (service == null) {
10952 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10953 return;
10954 }
10955 service.setActiveDeviceToDeviceTransport(transport);
10956 } finally {
10957 Binder.restoreCallingIdentity(identity);
10958 }
10959 }
Tyler Gunn92479152021-01-20 16:30:10 -080010960
Tyler Gunnd4339262021-05-03 14:46:49 -070010961 @Override
10962 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10963 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10964 "setDeviceToDeviceForceEnabled");
10965
10966 final long identity = Binder.clearCallingIdentity();
10967 try {
10968 Arrays.stream(PhoneFactory.getPhones()).forEach(
10969 p -> {
10970 Phone thePhone = p.getImsPhone();
10971 if (thePhone != null && thePhone instanceof ImsPhone) {
10972 ImsPhone imsPhone = (ImsPhone) thePhone;
10973 CallTracker tracker = imsPhone.getCallTracker();
10974 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10975 ImsPhoneCallTracker imsPhoneCallTracker =
10976 (ImsPhoneCallTracker) tracker;
10977 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10978 }
10979 }
10980 }
10981 );
10982 } finally {
10983 Binder.restoreCallingIdentity(identity);
10984 }
10985 }
10986
Tyler Gunn92479152021-01-20 16:30:10 -080010987 /**
Hui Wang761a6682020-10-31 05:12:53 +000010988 * Gets the config of RCS VoLTE single registration enabled for the device.
10989 */
10990 @Override
10991 public boolean getDeviceSingleRegistrationEnabled() {
10992 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
10993 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
10994 }
10995
10996 /**
10997 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
10998 */
10999 @Override
11000 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11001 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11002 "setCarrierSingleRegistrationEnabledOverride");
11003 enforceModifyPermission();
11004
11005 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11006 : Boolean.parseBoolean(enabledStr);
11007 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11008 subId, enabled);
11009 }
11010
11011 /**
11012 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11013 */
11014 @Override
11015 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11016 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11017 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11018 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011019
11020 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011021 * Overrides the ims feature validation result
11022 */
11023 @Override
11024 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11025 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11026 "setImsFeatureValidationOverride");
11027
11028 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11029 : Boolean.parseBoolean(enabledStr);
11030 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11031 subId, enabled);
11032 }
11033
11034 /**
11035 * Gets the ims feature validation override value
11036 */
11037 @Override
11038 public boolean getImsFeatureValidationOverride(int subId) {
11039 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11040 "getImsFeatureValidationOverride");
11041 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11042 }
11043
11044 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011045 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11046 * their mobile plan.
11047 */
11048 @Override
11049 public String getMobileProvisioningUrl() {
11050 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11051 final long identity = Binder.clearCallingIdentity();
11052 try {
11053 return getDefaultPhone().getMobileProvisioningUrl();
11054 } finally {
11055 Binder.restoreCallingIdentity(identity);
11056 }
11057 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011058
James.cf Linbcdf8b32021-01-14 16:44:13 +080011059 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011060 * Get the EAB contact from the EAB database.
11061 */
11062 @Override
11063 public String getContactFromEab(String contact) {
11064 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11065 enforceModifyPermission();
11066 final long identity = Binder.clearCallingIdentity();
11067 try {
11068 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11069 } finally {
11070 Binder.restoreCallingIdentity(identity);
11071 }
11072 }
11073
11074 /**
Calvin Pana1434322021-07-01 19:27:01 +080011075 * Get the EAB capability from the EAB database.
11076 */
11077 @Override
11078 public String getCapabilityFromEab(String contact) {
11079 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11080 enforceModifyPermission();
11081 final long identity = Binder.clearCallingIdentity();
11082 try {
11083 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11084 } finally {
11085 Binder.restoreCallingIdentity(identity);
11086 }
11087 }
11088
11089 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011090 * Remove the EAB contacts from the EAB database.
11091 */
11092 @Override
11093 public int removeContactFromEab(int subId, String contacts) {
11094 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11095 enforceModifyPermission();
11096 final long identity = Binder.clearCallingIdentity();
11097 try {
11098 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11099 } finally {
11100 Binder.restoreCallingIdentity(identity);
11101 }
11102 }
11103
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011104 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011105 public boolean getDeviceUceEnabled() {
11106 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11107 final long identity = Binder.clearCallingIdentity();
11108 try {
11109 return mApp.getDeviceUceEnabled();
11110 } finally {
11111 Binder.restoreCallingIdentity(identity);
11112 }
11113 }
11114
11115 @Override
11116 public void setDeviceUceEnabled(boolean isEnabled) {
11117 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11118 final long identity = Binder.clearCallingIdentity();
11119 try {
11120 mApp.setDeviceUceEnabled(isEnabled);
11121 } finally {
11122 Binder.restoreCallingIdentity(identity);
11123 }
11124 }
11125
Brad Ebinger14d467f2021-02-12 06:18:28 +000011126 /**
11127 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11128 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11129 */
11130 // Used for SHELL command only right now.
11131 @Override
11132 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11133 List<String> featureTags) {
11134 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11135 "addUceRegistrationOverrideShell");
11136 final long identity = Binder.clearCallingIdentity();
11137 try {
11138 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11139 new ArraySet<>(featureTags));
11140 } catch (ImsException e) {
11141 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11142 } finally {
11143 Binder.restoreCallingIdentity(identity);
11144 }
11145 }
11146
11147 /**
11148 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11149 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11150 */
11151 // Used for SHELL command only right now.
11152 @Override
11153 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11154 List<String> featureTags) {
11155 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11156 "removeUceRegistrationOverrideShell");
11157 final long identity = Binder.clearCallingIdentity();
11158 try {
11159 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11160 new ArraySet<>(featureTags));
11161 } catch (ImsException e) {
11162 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11163 } finally {
11164 Binder.restoreCallingIdentity(identity);
11165 }
11166 }
11167
11168 /**
11169 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11170 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11171 */
11172 // Used for SHELL command only right now.
11173 @Override
11174 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11175 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11176 "clearUceRegistrationOverrideShell");
11177 final long identity = Binder.clearCallingIdentity();
11178 try {
11179 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11180 } catch (ImsException e) {
11181 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11182 } finally {
11183 Binder.restoreCallingIdentity(identity);
11184 }
11185 }
11186
11187 /**
11188 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11189 */
11190 // Used for SHELL command only right now.
11191 @Override
11192 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11193 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11194 "getLatestRcsContactUceCapabilityShell");
11195 final long identity = Binder.clearCallingIdentity();
11196 try {
11197 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11198 } catch (ImsException e) {
11199 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11200 } finally {
11201 Binder.restoreCallingIdentity(identity);
11202 }
11203 }
11204
11205 /**
11206 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11207 * device does not have an active PUBLISH.
11208 */
11209 // Used for SHELL command only right now.
11210 @Override
11211 public String getLastUcePidfXmlShell(int subId) {
11212 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11213 final long identity = Binder.clearCallingIdentity();
11214 try {
11215 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11216 } catch (ImsException e) {
11217 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11218 } finally {
11219 Binder.restoreCallingIdentity(identity);
11220 }
11221 }
11222
James.cf Line8713a42021-04-29 16:04:26 +080011223 /**
11224 * Remove UCE requests cannot be sent to the network status.
11225 */
11226 // Used for SHELL command only right now.
11227 @Override
11228 public boolean removeUceRequestDisallowedStatus(int subId) {
11229 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11230 final long identity = Binder.clearCallingIdentity();
11231 try {
11232 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11233 } catch (ImsException e) {
11234 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11235 } finally {
11236 Binder.restoreCallingIdentity(identity);
11237 }
11238 }
11239
James.cf Lin18bb9002021-05-25 01:37:38 +080011240 /**
11241 * Remove UCE requests cannot be sent to the network status.
11242 */
11243 // Used for SHELL command only.
11244 @Override
11245 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11246 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11247 final long identity = Binder.clearCallingIdentity();
11248 try {
11249 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11250 } catch (ImsException e) {
11251 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11252 } finally {
11253 Binder.restoreCallingIdentity(identity);
11254 }
11255 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011256
James.cf Lin4b784aa2021-01-31 03:25:15 +080011257 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011258 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11259 String callingPackage) {
11260 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11261 mApp, subId, "setSignalStrengthUpdateRequest");
11262
11263 final int callingUid = Binder.getCallingUid();
11264 // Verify that tha callingPackage belongs to the calling UID
11265 mApp.getSystemService(AppOpsManager.class)
11266 .checkPackage(callingUid, callingPackage);
11267
Rambo Wang3607f502021-02-01 21:51:40 -080011268 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011269
11270 final long identity = Binder.clearCallingIdentity();
11271 try {
11272 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11273 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11274
11275 if (result instanceof IllegalStateException) {
11276 throw (IllegalStateException) result;
11277 }
11278 } finally {
11279 Binder.restoreCallingIdentity(identity);
11280 }
11281 }
11282
11283 @Override
11284 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11285 String callingPackage) {
11286 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11287 mApp, subId, "clearSignalStrengthUpdateRequest");
11288
11289 final int callingUid = Binder.getCallingUid();
11290 // Verify that tha callingPackage belongs to the calling UID
11291 mApp.getSystemService(AppOpsManager.class)
11292 .checkPackage(callingUid, callingPackage);
11293
11294 final long identity = Binder.clearCallingIdentity();
11295 try {
11296 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11297 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11298
11299 if (result instanceof IllegalStateException) {
11300 throw (IllegalStateException) result;
11301 }
11302 } finally {
11303 Binder.restoreCallingIdentity(identity);
11304 }
11305 }
11306
Rambo Wang3607f502021-02-01 21:51:40 -080011307 private static void validateSignalStrengthUpdateRequest(Context context,
11308 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011309 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11310 // phone/system process do not have further restriction on request
11311 return;
11312 }
11313
11314 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011315 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011316 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011317 context.enforceCallingOrSelfPermission(
11318 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11319 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011320 }
11321
11322 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011323 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011324 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011325 || info.isEnabled()) {
11326 throw new IllegalArgumentException(
11327 "Only system can set hide fields in SignalThresholdInfo");
11328 }
11329
11330 // Thresholds length for each RAN need in range. This has been validated in
11331 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11332 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11333 final int[] thresholds = info.getThresholds();
11334 Objects.requireNonNull(thresholds);
11335 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11336 || thresholds.length
11337 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11338 throw new IllegalArgumentException(
11339 "thresholds length is out of range: " + thresholds.length);
11340 }
11341 }
11342 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011343
11344 /**
11345 * Gets the current phone capability.
11346 *
11347 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11348 * @return the PhoneCapability which describes the data connection capability of modem.
11349 * It's used to evaluate possible phone config change, for example from single
11350 * SIM device to multi-SIM device.
11351 */
11352 @Override
11353 public PhoneCapability getPhoneCapability() {
11354 enforceReadPrivilegedPermission("getPhoneCapability");
11355 final long identity = Binder.clearCallingIdentity();
11356 try {
11357 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11358 } finally {
11359 Binder.restoreCallingIdentity(identity);
11360 }
11361 }
Michele Berionne5e411512020-11-13 02:36:59 +000011362
11363 /**
11364 * Prepare TelephonyManager for an unattended reboot. The reboot is
11365 * required to be done shortly after the API is invoked.
11366 */
11367 @Override
11368 @TelephonyManager.PrepareUnattendedRebootResult
11369 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011370 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011371 enforceRebootPermission();
11372
11373 final long identity = Binder.clearCallingIdentity();
11374 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011375 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011376 } finally {
11377 Binder.restoreCallingIdentity(identity);
11378 }
11379 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011380
11381 /**
11382 * Request to get the current slicing configuration including URSP rules and
11383 * NSSAIs (configured, allowed and rejected).
11384 *
11385 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11386 */
11387 @Override
11388 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011389 TelephonyPermissions
11390 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11391 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011392
11393 final long identity = Binder.clearCallingIdentity();
11394 try {
11395 Phone phone = getDefaultPhone();
11396 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11397 } finally {
11398 Binder.restoreCallingIdentity(identity);
11399 }
11400 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011401
11402 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011403 * Check whether the given premium capability is available for purchase from the carrier.
11404 *
11405 * @param capability The premium capability to check.
11406 * @param subId The subId to check the premium capability for.
11407 *
11408 * @return Whether the given premium capability is available to purchase.
11409 */
11410 @Override
11411 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11412 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11413 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11414 log("Premium capability "
11415 + TelephonyManager.convertPremiumCapabilityToString(capability)
11416 + " is not available for purchase due to missing permissions.");
11417 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11418 + "permission READ_BASIC_PHONE_STATE.");
11419 }
11420
11421 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011422 if (phone == null) {
11423 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11424 return false;
11425 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011426 final long identity = Binder.clearCallingIdentity();
11427 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011428 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011429 .isPremiumCapabilityAvailableForPurchase(capability);
11430 } finally {
11431 Binder.restoreCallingIdentity(identity);
11432 }
11433 }
11434
11435 /**
11436 * Purchase the given premium capability from the carrier.
11437 *
11438 * @param capability The premium capability to purchase.
11439 * @param callback The result of the purchase request.
11440 * @param subId The subId to purchase the premium capability for.
11441 */
11442 @Override
11443 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11444 log("purchasePremiumCapability: capability="
11445 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11446 + getCurrentPackageName());
11447
11448 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11449 mApp, "purchasePremiumCapability")) {
11450 log("purchasePremiumCapability "
11451 + TelephonyManager.convertPremiumCapabilityToString(capability)
11452 + " failed due to missing permissions.");
11453 throw new SecurityException("purchasePremiumCapability requires permission "
11454 + "READ_BASIC_PHONE_STATE.");
11455 }
11456
11457 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011458 if (phone == null) {
11459 try {
11460 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11461 callback.accept(result);
11462 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11463 } catch (RemoteException e) {
11464 String logStr = "Purchase premium capability "
11465 + TelephonyManager.convertPremiumCapabilityToString(capability)
11466 + " failed due to RemoteException handling null phone: " + e;
11467 if (DBG) log(logStr);
11468 AnomalyReporter.reportAnomaly(
11469 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11470 }
11471 return;
11472 }
Sarah Chin71b3a852022-09-28 15:54:19 -070011473 String appName;
11474 try {
11475 appName = mApp.getPackageManager().getApplicationLabel(mApp.getPackageManager()
11476 .getApplicationInfo(getCurrentPackageName(), 0)).toString();
11477 } catch (PackageManager.NameNotFoundException e) {
11478 appName = "An application";
11479 }
11480 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
11481 new PurchasePremiumCapabilityArgument(capability, appName, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011482 }
11483
11484 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011485 * Register an IMS connection state callback
11486 */
11487 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011488 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11489 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011490 if (feature == ImsFeature.FEATURE_MMTEL) {
11491 // ImsMmTelManager
11492 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11493 TelephonyPermissions
11494 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11495 mApp, subId, "registerImsStateCallback");
11496 } else if (feature == ImsFeature.FEATURE_RCS) {
11497 // ImsRcsManager or SipDelegateManager
11498 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11499 Binder.getCallingUid(), "registerImsStateCallback",
11500 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11501 Manifest.permission.READ_PRECISE_PHONE_STATE,
11502 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11503 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11504 }
11505
11506 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11507 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11508 "IMS not available on device.");
11509 }
11510
11511 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11512 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11513 }
11514
11515 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11516 if (controller == null) {
11517 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11518 "IMS not available on device.");
11519 }
11520
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011521 if (callingPackage == null) {
11522 callingPackage = getCurrentPackageName();
11523 }
11524
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011525 final long token = Binder.clearCallingIdentity();
11526 try {
11527 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011528 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011529 } catch (ImsException e) {
11530 throw new ServiceSpecificException(e.getCode());
11531 } finally {
11532 Binder.restoreCallingIdentity(token);
11533 }
11534 }
11535
11536 /**
11537 * Unregister an IMS connection state callback
11538 */
11539 @Override
11540 public void unregisterImsStateCallback(IImsStateCallback cb) {
11541 final long token = Binder.clearCallingIdentity();
11542 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11543 if (controller == null) {
11544 return;
11545 }
11546 try {
11547 controller.unregisterImsStateCallback(cb);
11548 } finally {
11549 Binder.restoreCallingIdentity(token);
11550 }
11551 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011552
11553 /**
11554 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11555 *
11556 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11557 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11558 * SecurityException.
11559 * If there is current registered network this value will be same as the registered cell
11560 * identity. If the device goes out of service the previous cell identity is cached and
11561 * will be returned. If the cache age of the Cell identity is more than 24 hours
11562 * it will be cleared and null will be returned.
11563 *
11564 */
11565 @Override
11566 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11567 String callingFeatureId) {
11568 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11569 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11570 LocationAccessPolicy.checkLocationPermission(mApp,
11571 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11572 .setCallingPackage(callingPackage)
11573 .setCallingFeatureId(callingFeatureId)
11574 .setCallingPid(Binder.getCallingPid())
11575 .setCallingUid(Binder.getCallingUid())
11576 .setMethod("getLastKnownCellIdentity")
11577 .setLogAsInfo(true)
11578 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11579 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11580 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11581 .build());
11582
11583 boolean hasFinePermission =
11584 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11585 if (!hasFinePermission
11586 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11587 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011588 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011589 }
11590
11591 final long identity = Binder.clearCallingIdentity();
11592 try {
11593 Phone phone = getPhone(subId);
11594 if (phone == null) return null;
11595 ServiceStateTracker sst = phone.getServiceStateTracker();
11596 if (sst == null) return null;
11597 return sst.getLastKnownCellIdentity();
11598 } finally {
11599 Binder.restoreCallingIdentity(identity);
11600 }
11601 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011602
jimsun3b9ccac2021-10-26 15:01:23 +080011603 /**
11604 * Sets the modem service class Name that Telephony will bind to.
11605 *
11606 * @param serviceName The class name of the modem service.
11607 * @return true if the operation is succeed, otherwise false.
11608 */
11609 public boolean setModemService(String serviceName) {
11610 Log.d(LOG_TAG, "setModemService - " + serviceName);
11611 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11612 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11613 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11614 "setModemService");
11615 return mPhoneConfigurationManager.setModemService(serviceName);
11616 }
11617
11618 /**
11619 * Return the class name of the currently bounded modem service.
11620 *
11621 * @return the class name of the modem service.
11622 */
11623 public String getModemService() {
11624 String result;
11625 Log.d(LOG_TAG, "getModemService");
11626 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11627 TelephonyPermissions
11628 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11629 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11630 "getModemService");
11631 result = mPhoneConfigurationManager.getModemService();
11632 Log.d(LOG_TAG, "result = " + result);
11633 return result;
11634 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011635
11636 @Override
11637 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11638 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11639 mApp.enforceCallingOrSelfPermission(
11640 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11641 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11642
11643 final long identity = Binder.clearCallingIdentity();
11644 try {
11645 Phone phone = getPhone(subId);
11646 if (phone == null) return;
11647 phone.setVoiceServiceStateOverride(hasService);
11648 } finally {
11649 Binder.restoreCallingIdentity(identity);
11650 }
11651 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011652
11653 /**
11654 * set removable eSIM as default eUICC.
11655 *
11656 * @hide
11657 */
11658 @Override
11659 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11660 enforceModifyPermission();
11661 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11662
11663 final long identity = Binder.clearCallingIdentity();
11664 try {
11665 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11666 } finally {
11667 Binder.restoreCallingIdentity(identity);
11668 }
11669 }
11670
11671 /**
11672 * Returns whether the removable eSIM is default eUICC or not.
11673 *
11674 * @hide
11675 */
11676 @Override
11677 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11678 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11679 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11680
11681 final long identity = Binder.clearCallingIdentity();
11682 try {
11683 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11684 } finally {
11685 Binder.restoreCallingIdentity(identity);
11686 }
11687 }
11688
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011689 /**
11690 * Get the component name of the default app to direct respond-via-message intent for the
11691 * user associated with this subscription, update the cache if there is no respond-via-message
11692 * application currently configured for this user.
11693 * @return component name of the app and class to direct Respond Via Message intent to, or
11694 * {@code null} if the functionality is not supported.
11695 * @hide
11696 */
11697 @Override
11698 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11699 boolean updateIfNeeded) {
11700 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011701
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011702 Context context = getPhone(subId).getContext();
11703 UserHandle userHandle = null;
11704 final long identity = Binder.clearCallingIdentity();
11705 try {
11706 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11707 } finally {
11708 Binder.restoreCallingIdentity(identity);
11709 }
11710 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11711 updateIfNeeded, userHandle);
11712 }
Jack Yuf5badd92022-12-08 00:50:53 -080011713
11714 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011715 * Set whether the device is able to connect with null ciphering or integrity
11716 * algorithms. This is a global setting and will apply to all active subscriptions
11717 * and all new subscriptions after this.
11718 *
11719 * @param enabled when true, null cipher and integrity algorithms are allowed.
11720 * @hide
11721 */
11722 @Override
11723 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11724 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11725 enforceModifyPermission();
11726 checkForNullCipherAndIntegritySupport();
11727
11728 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11729 // for listening to these preference changes and applying them immediately.
11730 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11731 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11732 editor.apply();
11733
11734 for (Phone phone: PhoneFactory.getPhones()) {
11735 phone.handleNullCipherEnabledChange();
11736 }
11737 }
11738
11739
11740 /**
11741 * Get whether the device is able to connect with null ciphering or integrity
11742 * algorithms. Note that this retrieves the phone-global preference and not
11743 * the state of the radio.
11744 *
11745 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11746 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11747 * version for this feature.
11748 * @hide
11749 */
11750 @Override
11751 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11752 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11753 enforceReadPermission();
11754 checkForNullCipherAndIntegritySupport();
11755 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11756 }
11757
11758 private void checkForNullCipherAndIntegritySupport() {
11759 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11760 throw new UnsupportedOperationException(
11761 "Null cipher and integrity operations require HAL 2.1 or above");
11762 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011763 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11764 throw new UnsupportedOperationException(
11765 "Null cipher and integrity operations unsupported by modem");
11766 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011767 }
11768
11769 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011770 * Get the SIM state for the slot index.
11771 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11772 *
11773 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11774 */
11775 @Override
11776 @SimState
11777 public int getSimStateForSlotIndex(int slotIndex) {
11778 IccCardConstants.State simState;
11779 if (slotIndex < 0) {
11780 simState = IccCardConstants.State.UNKNOWN;
11781 } else {
11782 Phone phone = null;
11783 try {
11784 phone = PhoneFactory.getPhone(slotIndex);
11785 } catch (IllegalStateException e) {
11786 // ignore
11787 }
11788 if (phone == null) {
11789 simState = IccCardConstants.State.UNKNOWN;
11790 } else {
11791 IccCard icc = phone.getIccCard();
11792 if (icc == null) {
11793 simState = IccCardConstants.State.UNKNOWN;
11794 } else {
11795 simState = icc.getState();
11796 }
11797 }
11798 }
11799 return simState.ordinal();
11800 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011801
11802 /**
11803 * Get current cell broadcast ranges.
11804 */
11805 @Override
11806 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11807 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11808 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11809 "getCellBroadcastIdRanges");
11810 final long identity = Binder.clearCallingIdentity();
11811 try {
11812 return getPhone(subId).getCellBroadcastIdRanges();
11813 } finally {
11814 Binder.restoreCallingIdentity(identity);
11815 }
11816 }
11817
11818 /**
11819 * Set reception of cell broadcast messages with the list of the given ranges
11820 *
11821 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11822 */
11823 @Override
11824 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11825 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11826 @Nullable IIntegerConsumer callback) {
11827 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11828 "setCellBroadcastIdRanges");
11829 final long identity = Binder.clearCallingIdentity();
11830 try {
11831 Phone phone = getPhoneFromSubId(subId);
11832 if (DBG) {
11833 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11834 }
11835 phone.setCellBroadcastIdRanges(ranges, result -> {
11836 if (callback != null) {
11837 try {
11838 callback.accept(result);
11839 } catch (RemoteException e) {
11840 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11841 }
11842 }
11843 });
11844 } finally {
11845 Binder.restoreCallingIdentity(identity);
11846 }
11847 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011848
11849 /**
11850 * Returns whether the device supports the domain selection service.
11851 *
11852 * @return {@code true} if the device supports the domain selection service.
11853 */
11854 @Override
11855 public boolean isDomainSelectionSupported() {
11856 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11857 "isDomainSelectionSupported");
11858
11859 final long identity = Binder.clearCallingIdentity();
11860 try {
11861 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11862 } finally {
11863 Binder.restoreCallingIdentity(identity);
11864 }
11865 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011866
11867 /**
11868 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11869 *
11870 * <p>This method behaves in one of the following ways:
11871 * <ul>
11872 * <li>return true : if the calling package has the appop permission {@link
11873 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11874 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11875 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11876 * Owner device identifier access check, or the calling package has carrier privileges</>
11877 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11878 * </ul>
11879 */
11880 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11881 String callingPackage, @Nullable String callingFeatureId, String message) {
11882 for (Phone phone : PhoneFactory.getPhones()) {
11883 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11884 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11885 return true;
11886 }
11887 }
11888 return false;
11889 }
11890}