blob: fd5e97f9ac48535be453cc2b644afcb412ece58c [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Hall Liud892bec2018-11-30 14:51:45 -080019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
Gil Cukierman1c0eb932022-12-06 22:28:24 +000020import static android.telephony.TelephonyManager.HAL_SERVICE_NETWORK;
jimsunf9ec1622022-09-13 21:18:43 +080021import static android.telephony.TelephonyManager.HAL_SERVICE_RADIO;
Hall Liud892bec2018-11-30 14:51:45 -080022
Shuo Qianccbaf742021-02-22 18:32:21 -080023import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_CDMA;
24import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_GSM;
Tyler Gunn7bcdc742019-10-04 15:56:59 -070025import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070026import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
joonhunshin3e154242021-09-17 06:33:39 +000027import static com.android.internal.telephony.TelephonyStatsLog.RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT;
Ta-wei Yen87c49842016-05-13 21:19:52 -070028
Brad Ebinger34c09a52021-02-17 23:23:21 +000029import android.Manifest;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080030import android.Manifest.permission;
Hall Liua1548bd2019-12-24 14:14:12 -080031import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070032import android.annotation.Nullable;
sandeepjsb6c87872021-09-27 15:34:44 +000033import android.annotation.RequiresPermission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080035import android.app.PendingIntent;
Tyler Gunn64144d92022-03-17 14:16:41 -070036import android.app.PropertyInvalidatedCache;
Brad Ebinger4f6208e2021-03-23 21:04:45 +000037import android.app.compat.CompatChanges;
Hall Liu82694d52020-12-11 18:22:04 -080038import android.app.role.RoleManager;
sandeepjsb6c87872021-09-27 15:34:44 +000039import android.compat.annotation.ChangeId;
40import android.compat.annotation.EnabledSince;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070041import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070042import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.content.Context;
44import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070046import android.content.pm.ComponentInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070047import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.net.Uri;
49import android.os.AsyncResult;
50import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080051import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.os.Bundle;
53import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070054import android.os.IBinder;
tom hsu0b59d292022-09-29 23:49:21 +080055import android.os.LocaleList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.os.Looper;
57import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070058import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080059import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070060import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070061import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080062import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080063import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070064import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070065import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080066import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070068import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070069import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070070import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070071import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080072import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070073import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090074import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080075import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080076import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070077import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080078import android.telephony.AccessNetworkConstants;
79import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070080import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070081import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080082import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070083import android.telephony.AnomalyReporter;
Shuo Qian4a594052020-01-23 11:59:30 -080084import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070085import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080086import android.telephony.CarrierRestrictionRules;
Hui Wang9b5793a2022-12-05 14:38:06 -060087import android.telephony.CellBroadcastIdRange;
yincheng zhao2737e882019-09-06 17:06:54 -070088import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080089import android.telephony.CellIdentityCdma;
90import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070092import android.telephony.CellInfoGsm;
93import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070094import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080095import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -070096import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070097import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070098import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080099import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700100import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -0800101import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -0700102import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -0800103import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800104import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700105import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800106import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800108import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800109import android.telephony.SignalStrengthUpdateRequest;
110import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800111import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800112import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800113import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700114import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700115import android.telephony.TelephonyManager;
Jack Yuf5badd92022-12-08 00:50:53 -0800116import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800117import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800118import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800119import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000120import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000121import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000122import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700123import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700124import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800125import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800126import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700127import android.telephony.gba.GbaAuthRequest;
128import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700129import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800130import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000131import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000132import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700133import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000134import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700135import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800136import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700137import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800138import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700139import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000140import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700141import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800142import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800143import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700144import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800145import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700146import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800148import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800149
Andrew Lee312e8172014-10-23 17:01:36 -0700150import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800151import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800152import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800153import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800154import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700156import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700157import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800158import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800159import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700160import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700161import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800162import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700163import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800164import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800165import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800166import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700167import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000168import com.android.internal.telephony.IImsStateCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700169import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800170import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800172import com.android.internal.telephony.IccCard;
Jack Yuf5badd92022-12-08 00:50:53 -0800173import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800174import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700175import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700176import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700177import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700179import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800180import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700181import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700182import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700183import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700184import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800185import com.android.internal.telephony.RILConstants;
Daniel Bright94f43662021-03-01 14:43:40 -0800186import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700187import com.android.internal.telephony.ServiceStateTracker;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000188import com.android.internal.telephony.SmsApplication;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700189import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700190import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800191import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800192import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800193import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700194import com.android.internal.telephony.data.DataUtils;
Hunsuk Choi42cc62a2022-10-16 06:03:40 +0000195import com.android.internal.telephony.domainselection.DomainSelectionResolver;
sqianf4ca7ed2019-01-15 18:32:07 -0800196import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700197import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800198import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700199import com.android.internal.telephony.imsphone.ImsPhone;
200import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000201import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800202import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yu285100e2022-12-02 22:48:35 -0800203import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
204import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700205import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700206import com.android.internal.telephony.uicc.IccIoResult;
207import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800208import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700209import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800210import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700211import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000212import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800213import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000214import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800215import com.android.internal.telephony.util.LocaleUtils;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000216import com.android.internal.telephony.util.TelephonyUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700217import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700218import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800219import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800220import com.android.phone.callcomposer.CallComposerPictureManager;
221import com.android.phone.callcomposer.CallComposerPictureTransfer;
222import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700223import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin46355ba2022-11-01 23:51:16 -0700224import com.android.phone.slice.SlicePurchaseController;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700225import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800226import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700227import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700228import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800229import com.android.services.telephony.TelecomAccountRegistry;
230import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800231import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800232
Hall Liu82694d52020-12-11 18:22:04 -0800233import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700234import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800235import java.io.IOException;
236import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700237import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800239import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000240import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800241import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800242import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800243import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800244import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100245import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800246import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700247import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800248import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800249import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700250import java.util.UUID;
Hall Liu82694d52020-12-11 18:22:04 -0800251import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800252import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800253import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254
255/**
256 * Implementation of the ITelephony interface.
257 */
Santos Cordon117fee72014-05-16 17:56:12 -0700258public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 private static final String LOG_TAG = "PhoneInterfaceManager";
260 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
261 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800262 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263
264 // Message codes used with mMainThreadHandler
265 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700266 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
267 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700268 private static final int CMD_OPEN_CHANNEL = 9;
269 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
270 private static final int CMD_CLOSE_CHANNEL = 11;
271 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800272 private static final int CMD_NV_READ_ITEM = 13;
273 private static final int EVENT_NV_READ_ITEM_DONE = 14;
274 private static final int CMD_NV_WRITE_ITEM = 15;
275 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
276 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
277 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700278 private static final int CMD_RESET_MODEM_CONFIG = 19;
279 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800280 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
281 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800282 private static final int CMD_SEND_ENVELOPE = 25;
283 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000284 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
285 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700286 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
287 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
288 private static final int CMD_EXCHANGE_SIM_IO = 31;
289 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800290 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
291 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700292 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
293 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700294 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
295 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700296 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
297 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
298 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
299 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700300 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
301 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
302 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
303 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700304 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800305 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
306 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000307 private static final int CMD_SWITCH_SLOTS = 50;
308 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700309 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
310 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
311 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
312 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
313 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
314 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
315 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
316 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700317 private static final int CMD_GET_ALL_CELL_INFO = 60;
318 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
319 private static final int CMD_GET_CELL_LOCATION = 62;
320 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700321 private static final int CMD_MODEM_REBOOT = 64;
322 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700323 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
324 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800325 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
326 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700327 private static final int CMD_GET_MODEM_STATUS = 70;
328 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700329 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
330 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700331 private static final int CMD_ERASE_MODEM_CONFIG = 74;
332 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800333 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
334 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
335 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
336 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800337 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
338 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800339 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800340 private static final int CMD_GET_CALL_FORWARDING = 83;
341 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
342 private static final int CMD_SET_CALL_FORWARDING = 85;
343 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
344 private static final int CMD_GET_CALL_WAITING = 87;
345 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
346 private static final int CMD_SET_CALL_WAITING = 89;
347 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700348 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
349 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
350 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
351 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700352 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
353 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800354 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
355 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800356 private static final int CMD_SET_DATA_THROTTLING = 99;
357 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800358 private static final int CMD_SET_SIM_POWER = 101;
359 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800360 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
361 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
362 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
363 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800364 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
365 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000366 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800367 private static final int CMD_GET_SLICING_CONFIG = 110;
368 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800369 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700370 private static final int CMD_ENABLE_VONR = 113;
371 private static final int EVENT_ENABLE_VONR_DONE = 114;
372 private static final int CMD_IS_VONR_ENABLED = 115;
373 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700374 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
375 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700376
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800377 // Parameters of select command.
378 private static final int SELECT_COMMAND = 0xA4;
379 private static final int SELECT_P1 = 0x04;
380 private static final int SELECT_P2 = 0;
381 private static final int SELECT_P3 = 0x10;
382
Gil Cukierman1c0eb932022-12-06 22:28:24 +0000383 // Toggling null cipher and integrity support was added in IRadioNetwork 2.1
384 private static final int MIN_NULL_CIPHER_AND_INTEGRITY_VERSION = 201;
385
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700386 /** The singleton instance. */
387 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800388 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389
Wink Saville3ab207e2014-11-20 13:07:20 -0800390 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800391 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800392 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700393 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800394 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700395 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800396 private MainThreadHandler mMainThreadHandler;
Jack Yue37dd262022-12-16 11:53:37 -0800397 private final SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800398 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700399 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800400 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Gil Cukierman6dac5eb2022-09-19 16:09:04 +0000401 private final Telephony2gUpdater mTelephony2gUpdater;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402
Peter Wangdafb9ac2020-01-15 14:13:38 -0800403 /** User Activity */
404 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800405 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
406
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700407 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
408
Derek Tan97ebb422014-09-05 16:55:38 -0700409 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
410 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800411 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800412 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700413
Michelecea4cf22018-12-21 15:00:11 -0800414 // String to store multi SIM allowed
415 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
416
Derek Tan740e1672017-06-27 14:56:27 -0700417 // The AID of ISD-R.
418 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
419
yinxub1bed742017-04-17 11:45:04 -0700420 private NetworkScanRequestTracker mNetworkScanRequestTracker;
421
David Kelly5e06a7f2018-03-12 14:10:59 +0000422 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
423 private static final int MANUFACTURER_CODE_LENGTH = 8;
424
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800425 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800426 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800427
Sarah Chin2ec39f62022-08-31 17:03:26 -0700428 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
429 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
430
Derek Tan89e89d42014-07-08 17:00:10 -0700431 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700432 * Experiment flag to enable erase modem config on reset network, default value is false
433 */
434 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
435 "reset_network_erase_modem_config_enabled";
436
Rambo Wang0f050d82021-02-12 11:43:36 -0800437 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800438
Gary Jian76280a42022-12-07 16:18:33 +0800439 private static final int MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS = 50;
440
sandeepjsb6c87872021-09-27 15:34:44 +0000441 /**
442 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
443 * one ICCID active at the same time.
444 * Apps should use below API signatures if targeting SDK is T and beyond.
445 *
446 * @hide
447 */
448 @ChangeId
449 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
450 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800451
Naina Nallurid63128d2019-09-17 14:10:30 -0700452 /**
Chen Xu540470b2021-12-14 17:15:47 -0800453 * Apps targeting on Android T and beyond will get exception whenever icc close channel
454 * operation fails.
455 */
456 @ChangeId
457 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
458 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
459
460 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700461 * A request object to use for transmitting data to an ICC.
462 */
463 private static final class IccAPDUArgument {
464 public int channel, cla, command, p1, p2, p3;
465 public String data;
466
467 public IccAPDUArgument(int channel, int cla, int command,
468 int p1, int p2, int p3, String data) {
469 this.channel = channel;
470 this.cla = cla;
471 this.command = command;
472 this.p1 = p1;
473 this.p2 = p2;
474 this.p3 = p3;
475 this.data = data;
476 }
477 }
478
479 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700480 * A request object to use for transmitting data to an ICC.
481 */
482 private static final class ManualNetworkSelectionArgument {
483 public OperatorInfo operatorInfo;
484 public boolean persistSelection;
485
486 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
487 this.operatorInfo = operatorInfo;
488 this.persistSelection = persistSelection;
489 }
490 }
491
Sarah Chin71b3a852022-09-28 15:54:19 -0700492 private static final class PurchasePremiumCapabilityArgument {
493 public @TelephonyManager.PremiumCapability int capability;
494 public @NonNull String appName;
495 public @NonNull IIntegerConsumer callback;
496
497 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
498 @NonNull String appName, @NonNull IIntegerConsumer callback) {
499 this.capability = capability;
500 this.appName = appName;
501 this.callback = callback;
502 }
503 }
504
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700505 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700506 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
507 * request after sending. The main thread will notify the request when it is complete.
508 */
509 private static final class MainThreadRequest {
510 /** The argument to use for the request */
511 public Object argument;
512 /** The result of the request that is run on the main thread */
513 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800514 // The subscriber id that this request applies to. Defaults to
515 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
516 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700517
Nathan Harold92bed182018-10-12 18:16:49 -0700518 // In cases where subId is unavailable, the caller needs to specify the phone.
519 public Phone phone;
520
vagdeviaf9a5b92018-08-15 16:01:53 -0700521 public WorkSource workSource;
522
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 public MainThreadRequest(Object argument) {
524 this.argument = argument;
525 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800526
Nathan Harold92bed182018-10-12 18:16:49 -0700527 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
528 this.argument = argument;
529 if (phone != null) {
530 this.phone = phone;
531 }
532 this.workSource = workSource;
533 }
534
vagdeviaf9a5b92018-08-15 16:01:53 -0700535 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800536 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800537 if (subId != null) {
538 this.subId = subId;
539 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700540 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700542 }
543
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800544 private static final class IncomingThirdPartyCallArgs {
545 public final ComponentName component;
546 public final String callId;
547 public final String callerDisplayName;
548
549 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
550 String callerDisplayName) {
551 this.component = component;
552 this.callId = callId;
553 this.callerDisplayName = callerDisplayName;
554 }
555 }
556
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700557 /**
558 * A handler that processes messages on the main thread in the phone process. Since many
559 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
560 * inbound binder threads to the main thread in the phone process. The Binder thread
561 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
562 * on, which will be notified when the operation completes and will contain the result of the
563 * request.
564 *
565 * <p>If a MainThreadRequest object is provided in the msg.obj field,
566 * note that request.result must be set to something non-null for the calling thread to
567 * unblock.
568 */
569 private final class MainThreadHandler extends Handler {
570 @Override
571 public void handleMessage(Message msg) {
572 MainThreadRequest request;
573 Message onCompleted;
574 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000575 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700576 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800577 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578
579 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700580 case CMD_HANDLE_USSD_REQUEST: {
581 request = (MainThreadRequest) msg.obj;
582 final Phone phone = getPhoneFromRequest(request);
583 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800584 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700585 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700586
Pengquan Menga1bb6272018-09-06 09:59:22 -0700587 if (!isUssdApiAllowed(request.subId)) {
588 // Carrier does not support use of this API, return failure.
589 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
590 UssdResponse response = new UssdResponse(ussdRequest, null);
591 Bundle returnData = new Bundle();
592 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
593 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700594
Pengquan Menga1bb6272018-09-06 09:59:22 -0700595 request.result = true;
596 notifyRequester(request);
597 return;
598 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700599
Pengquan Menga1bb6272018-09-06 09:59:22 -0700600 try {
601 request.result = phone != null
602 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
603 } catch (CallStateException cse) {
604 request.result = false;
605 }
606 // Wake up the requesting thread
607 notifyRequester(request);
608 break;
pkanwar32d516d2016-10-14 19:37:38 -0700609 }
610
Yorke Lee716f67e2015-06-17 15:39:16 -0700611 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700613 final Phone phone = getPhoneFromRequest(request);
614 request.result = phone != null ?
615 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
616 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700617 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700618 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700620 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700621
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700622 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700623 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700624 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000625 uiccPort = getUiccPortFromRequest(request);
626 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700627 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800628 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700629 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700630 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700631 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800632 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000633 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800634 iccArgument.channel, iccArgument.cla, iccArgument.command,
635 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
636 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700637 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700638 break;
639
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700640 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700641 ar = (AsyncResult) msg.obj;
642 request = (MainThreadRequest) ar.userObj;
643 if (ar.exception == null && ar.result != null) {
644 request.result = ar.result;
645 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800646 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700647 if (ar.result == null) {
648 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800649 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700650 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800651 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700652 } else {
653 loge("iccTransmitApduLogicalChannel: Unknown exception");
654 }
655 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700656 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700657 break;
658
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700659 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
660 request = (MainThreadRequest) msg.obj;
661 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000662 uiccPort = getUiccPortFromRequest(request);
663 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700664 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800665 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700666 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700667 } else {
668 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800669 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000670 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800671 iccArgument.cla, iccArgument.command, iccArgument.p1,
672 iccArgument.p2,
673 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700674 }
675 break;
676
677 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
678 ar = (AsyncResult) msg.obj;
679 request = (MainThreadRequest) ar.userObj;
680 if (ar.exception == null && ar.result != null) {
681 request.result = ar.result;
682 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800683 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700684 if (ar.result == null) {
685 loge("iccTransmitApduBasicChannel: Empty response");
686 } else if (ar.exception instanceof CommandException) {
687 loge("iccTransmitApduBasicChannel: CommandException: " +
688 ar.exception);
689 } else {
690 loge("iccTransmitApduBasicChannel: Unknown exception");
691 }
692 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700693 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700694 break;
695
696 case CMD_EXCHANGE_SIM_IO:
697 request = (MainThreadRequest) msg.obj;
698 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000699 uiccPort = getUiccPortFromRequest(request);
700 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700701 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800702 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700703 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700704 } else {
705 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
706 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000707 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700708 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
709 iccArgument.data, onCompleted);
710 }
711 break;
712
713 case EVENT_EXCHANGE_SIM_IO_DONE:
714 ar = (AsyncResult) msg.obj;
715 request = (MainThreadRequest) ar.userObj;
716 if (ar.exception == null && ar.result != null) {
717 request.result = ar.result;
718 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800719 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700720 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700721 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700722 break;
723
Derek Tan4d5e5c12014-02-04 11:54:58 -0800724 case CMD_SEND_ENVELOPE:
725 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000726 uiccPort = getUiccPortFromRequest(request);
727 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700728 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800729 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700730 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700731 } else {
732 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800733 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700734 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800735 break;
736
737 case EVENT_SEND_ENVELOPE_DONE:
738 ar = (AsyncResult) msg.obj;
739 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700740 if (ar.exception == null && ar.result != null) {
741 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800742 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800743 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700744 if (ar.result == null) {
745 loge("sendEnvelopeWithStatus: Empty response");
746 } else if (ar.exception instanceof CommandException) {
747 loge("sendEnvelopeWithStatus: CommandException: " +
748 ar.exception);
749 } else {
750 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
751 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800752 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700753 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800754 break;
755
Shishir Agrawal566b7612013-10-28 14:41:00 -0700756 case CMD_OPEN_CHANNEL:
757 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000758 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800759 IccLogicalChannelRequest openChannelRequest =
760 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000761 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700762 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800763 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800764 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700765 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700766 } else {
767 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800768 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
769 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700770 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700771 break;
772
773 case EVENT_OPEN_CHANNEL_DONE:
774 ar = (AsyncResult) msg.obj;
775 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700776 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700777 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700778 int[] result = (int[]) ar.result;
779 int channelId = result[0];
780 byte[] selectResponse = null;
781 if (result.length > 1) {
782 selectResponse = new byte[result.length - 1];
783 for (int i = 1; i < result.length; ++i) {
784 selectResponse[i - 1] = (byte) result[i];
785 }
786 }
787 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800788 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800789
790 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700791 if (uiccPort == null) {
792 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
793 } else {
794 IccLogicalChannelRequest channelRequest =
795 (IccLogicalChannelRequest) request.argument;
796 channelRequest.channel = channelId;
797 uiccPort.onLogicalChannelOpened(channelRequest);
798 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700799 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700800 if (ar.result == null) {
801 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700802 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700803 if (ar.exception != null) {
804 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
805 }
806
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700807 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700808 if (ar.exception instanceof CommandException) {
809 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800810 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700811 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700812 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700813 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700814 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700815 }
816 }
817 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800818 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700819 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700820 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700821 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700822 break;
823
824 case CMD_CLOSE_CHANNEL:
825 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000826 uiccPort = getUiccPortFromRequest(request);
827 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700828 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800829 request.result = new IllegalArgumentException(
Chen Xue9d737e2022-01-01 23:41:31 -0800830 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800831 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700832 } else {
833 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000834 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700835 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700836 break;
837
838 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800839 ar = (AsyncResult) msg.obj;
840 request = (MainThreadRequest) ar.userObj;
841 if (ar.exception == null) {
842 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800843 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700844 if (uiccPort == null) {
845 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
846 } else {
847 final int channelId = (Integer) request.argument;
848 uiccPort.onLogicalChannelClosed(channelId);
849 }
Chen Xu540470b2021-12-14 17:15:47 -0800850 } else {
851 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -0800852 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -0800853 if (ar.exception instanceof CommandException) {
854 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
855 CommandException.Error error =
856 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -0800857 if (error == CommandException.Error.INVALID_ARGUMENTS) {
858 // should only throw exceptions from the binder threads.
859 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -0800860 "iccCloseLogicalChannel: invalid argument ");
861 }
862 } else {
863 loge("iccCloseLogicalChannel: Unknown exception");
864 }
Chen Xua8f0dff2022-02-12 00:34:15 -0800865 request.result = (exception != null) ? exception :
866 new IllegalStateException(
867 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -0800868 }
869 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -0800870 break;
871
872 case CMD_NV_READ_ITEM:
873 request = (MainThreadRequest) msg.obj;
874 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800875 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
876 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800877 break;
878
879 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700880 ar = (AsyncResult) msg.obj;
881 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800882 if (ar.exception == null && ar.result != null) {
883 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700884 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800885 request.result = "";
886 if (ar.result == null) {
887 loge("nvReadItem: Empty response");
888 } else if (ar.exception instanceof CommandException) {
889 loge("nvReadItem: CommandException: " +
890 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700891 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800892 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700893 }
894 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700895 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700896 break;
897
Jake Hambye994d462014-02-03 13:10:13 -0800898 case CMD_NV_WRITE_ITEM:
899 request = (MainThreadRequest) msg.obj;
900 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
901 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800902 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700903 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800904 break;
905
906 case EVENT_NV_WRITE_ITEM_DONE:
907 handleNullReturnEvent(msg, "nvWriteItem");
908 break;
909
910 case CMD_NV_WRITE_CDMA_PRL:
911 request = (MainThreadRequest) msg.obj;
912 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800913 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800914 break;
915
916 case EVENT_NV_WRITE_CDMA_PRL_DONE:
917 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
918 break;
919
chen xu6dac5ab2018-10-26 17:39:23 -0700920 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800921 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700922 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800923 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800924 break;
925
chen xu6dac5ab2018-10-26 17:39:23 -0700926 case EVENT_RESET_MODEM_CONFIG_DONE:
927 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800928 break;
929
Sooraj Sasindran37444802020-08-11 10:40:43 -0700930 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
931 request = (MainThreadRequest) msg.obj;
932 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
933 request);
934 Phone phone = getPhoneFromRequest(request);
935 if (phone != null) {
936 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
937 } else {
938 loge("isNRDualConnectivityEnabled: No phone object");
939 request.result = false;
940 notifyRequester(request);
941 }
942 break;
943 }
944
945 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
946 ar = (AsyncResult) msg.obj;
947 request = (MainThreadRequest) ar.userObj;
948 if (ar.exception == null && ar.result != null) {
949 request.result = ar.result;
950 } else {
951 // request.result must be set to something non-null
952 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700953 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -0700954 request.result = ar.result;
955 } else {
956 request.result = false;
957 }
958 if (ar.result == null) {
959 loge("isNRDualConnectivityEnabled: Empty response");
960 } else if (ar.exception instanceof CommandException) {
961 loge("isNRDualConnectivityEnabled: CommandException: "
962 + ar.exception);
963 } else {
964 loge("isNRDualConnectivityEnabled: Unknown exception");
965 }
966 }
967 notifyRequester(request);
968 break;
969
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700970 case CMD_IS_VONR_ENABLED: {
971 request = (MainThreadRequest) msg.obj;
972 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
973 request);
974 Phone phone = getPhoneFromRequest(request);
975 if (phone != null) {
976 phone.isVoNrEnabled(onCompleted, request.workSource);
977 } else {
978 loge("isVoNrEnabled: No phone object");
979 request.result = false;
980 notifyRequester(request);
981 }
982 break;
983 }
984
985 case EVENT_IS_VONR_ENABLED_DONE:
986 ar = (AsyncResult) msg.obj;
987 request = (MainThreadRequest) ar.userObj;
988 if (ar.exception == null && ar.result != null) {
989 request.result = ar.result;
990 } else {
991 // request.result must be set to something non-null
992 // for the calling thread to unblock
993 if (ar.result != null) {
994 request.result = ar.result;
995 } else {
996 request.result = false;
997 }
998 if (ar.result == null) {
999 loge("isVoNrEnabled: Empty response");
1000 } else if (ar.exception instanceof CommandException) {
1001 loge("isVoNrEnabled: CommandException: "
1002 + ar.exception);
1003 } else {
1004 loge("isVoNrEnabled: Unknown exception");
1005 }
1006 }
1007 notifyRequester(request);
1008 break;
1009
Sooraj Sasindran37444802020-08-11 10:40:43 -07001010 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1011 request = (MainThreadRequest) msg.obj;
1012 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1013 Phone phone = getPhoneFromRequest(request);
1014 if (phone != null) {
1015 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1016 request.workSource);
1017 } else {
1018 loge("enableNrDualConnectivity: No phone object");
1019 request.result =
1020 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1021 notifyRequester(request);
1022 }
1023 break;
1024 }
1025
1026 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1027 ar = (AsyncResult) msg.obj;
1028 request = (MainThreadRequest) ar.userObj;
1029 if (ar.exception == null) {
1030 request.result =
1031 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1032 } else {
1033 request.result =
1034 TelephonyManager
1035 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1036 if (ar.exception instanceof CommandException) {
1037 CommandException.Error error =
1038 ((CommandException) (ar.exception)).getCommandError();
1039 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1040 request.result =
1041 TelephonyManager
1042 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001043 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1044 request.result =
1045 TelephonyManager
1046 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001047 }
1048 loge("enableNrDualConnectivity" + ": CommandException: "
1049 + ar.exception);
1050 } else {
1051 loge("enableNrDualConnectivity" + ": Unknown exception");
1052 }
1053 }
1054 notifyRequester(request);
1055 break;
1056 }
1057
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001058 case CMD_ENABLE_VONR: {
1059 request = (MainThreadRequest) msg.obj;
1060 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1061 Phone phone = getPhoneFromRequest(request);
1062 if (phone != null) {
1063 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1064 request.workSource);
1065 } else {
1066 loge("setVoNrEnabled: No phone object");
1067 request.result =
1068 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1069 notifyRequester(request);
1070 }
1071 break;
1072 }
1073
1074 case EVENT_ENABLE_VONR_DONE: {
1075 ar = (AsyncResult) msg.obj;
1076 request = (MainThreadRequest) ar.userObj;
1077 if (ar.exception == null) {
1078 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1079 } else {
1080 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1081 if (ar.exception instanceof CommandException) {
1082 CommandException.Error error =
1083 ((CommandException) (ar.exception)).getCommandError();
1084 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1085 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1086 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1087 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1088 } else {
1089 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1090 }
1091 loge("setVoNrEnabled" + ": CommandException: "
1092 + ar.exception);
1093 } else {
1094 loge("setVoNrEnabled" + ": Unknown exception");
1095 }
1096 }
1097 notifyRequester(request);
1098 break;
1099 }
1100
SongFerngWang3ef3e072020-12-21 16:41:52 +08001101 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001102 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001103 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1104 request);
1105 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001106 break;
1107
SongFerngWang3ef3e072020-12-21 16:41:52 +08001108 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001109 ar = (AsyncResult) msg.obj;
1110 request = (MainThreadRequest) ar.userObj;
1111 if (ar.exception == null && ar.result != null) {
1112 request.result = ar.result; // Integer
1113 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301114 // request.result must be set to something non-null
1115 // for the calling thread to unblock
1116 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001117 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001118 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001119 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001120 loge("getAllowedNetworkTypesBitmask: CommandException: "
1121 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001122 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001123 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001124 }
1125 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001126 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001127 break;
1128
SongFerngWang3ef3e072020-12-21 16:41:52 +08001129 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001130 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001131 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1132 request);
1133 Pair<Integer, Long> reasonWithNetworkTypes =
1134 (Pair<Integer, Long>) request.argument;
1135 getPhoneFromRequest(request).setAllowedNetworkTypes(
1136 reasonWithNetworkTypes.first,
1137 reasonWithNetworkTypes.second,
1138 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001139 break;
1140
SongFerngWang3ef3e072020-12-21 16:41:52 +08001141 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1142 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001143 break;
1144
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001145 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1146 request = (MainThreadRequest)msg.obj;
1147 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001148 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001149 break;
1150
1151 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1152 ar = (AsyncResult)msg.obj;
1153 request = (MainThreadRequest)ar.userObj;
1154 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001155 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001156 break;
1157
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001158 case CMD_SET_VOICEMAIL_NUMBER:
1159 request = (MainThreadRequest) msg.obj;
1160 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1161 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001162 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1163 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001164 break;
1165
1166 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1167 handleNullReturnEvent(msg, "setVoicemailNumber");
1168 break;
1169
Stuart Scott54788802015-03-30 13:18:01 -07001170 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1171 request = (MainThreadRequest) msg.obj;
1172 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1173 request);
1174 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1175 break;
1176
1177 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1178 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1179 break;
1180
Shishir Agrawal302c8692015-06-19 13:49:39 -07001181 case CMD_PERFORM_NETWORK_SCAN:
1182 request = (MainThreadRequest) msg.obj;
1183 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1184 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1185 break;
1186
Hall Liu27d24262020-09-18 19:04:59 -07001187 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001188 request = (MainThreadRequest) msg.obj;
1189 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001190 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1191 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1192 request.argument;
1193 int callForwardingReason = args.first;
1194 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001195 break;
Hall Liu27d24262020-09-18 19:04:59 -07001196 }
1197 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001198 ar = (AsyncResult) msg.obj;
1199 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001200 TelephonyManager.CallForwardingInfoCallback callback =
1201 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1202 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001203 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001204 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001205 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1206 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1207 // Service Class is a bit mask per 3gpp 27.007. Search for
1208 // any service for voice call.
1209 if ((callForwardInfo.serviceClass
1210 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001211 callForwardingInfo = new CallForwardingInfo(
1212 callForwardInfo.status
1213 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001214 callForwardInfo.reason,
1215 callForwardInfo.number,
1216 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001217 break;
1218 }
1219 }
1220 // Didn't find a call forward info for voice call.
1221 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001222 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1223 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001224 }
Hall Liu27d24262020-09-18 19:04:59 -07001225 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001226 } else {
1227 if (ar.result == null) {
1228 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1229 }
1230 if (ar.exception != null) {
1231 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1232 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001233 int errorCode = TelephonyManager
1234 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001235 if (ar.exception instanceof CommandException) {
1236 CommandException.Error error =
1237 ((CommandException) (ar.exception)).getCommandError();
1238 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001239 errorCode = TelephonyManager
1240 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001241 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001242 errorCode = TelephonyManager
1243 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001244 }
1245 }
Hall Liu27d24262020-09-18 19:04:59 -07001246 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001247 }
Shuo Qian4a594052020-01-23 11:59:30 -08001248 break;
Hall Liu27d24262020-09-18 19:04:59 -07001249 }
Shuo Qian4a594052020-01-23 11:59:30 -08001250
Hall Liu27d24262020-09-18 19:04:59 -07001251 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001252 request = (MainThreadRequest) msg.obj;
1253 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001254 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001255 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001256 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1257 request.argument).first;
1258 request.phone.setCallForwardingOption(
1259 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001260 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001261 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001262 callForwardingInfoToSet.getReason(),
1263 callForwardingInfoToSet.getNumber(),
1264 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1265 break;
Hall Liu27d24262020-09-18 19:04:59 -07001266 }
Shuo Qian4a594052020-01-23 11:59:30 -08001267
Hall Liu27d24262020-09-18 19:04:59 -07001268 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001269 ar = (AsyncResult) msg.obj;
1270 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001271 Consumer<Integer> callback =
1272 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1273 request.argument).second;
1274 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001275 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001276 int errorCode = TelephonyManager.CallForwardingInfoCallback
1277 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001278 if (ar.exception instanceof CommandException) {
1279 CommandException.Error error =
1280 ((CommandException) (ar.exception)).getCommandError();
1281 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001282 errorCode = TelephonyManager.CallForwardingInfoCallback
1283 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001284 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001285 errorCode = TelephonyManager.CallForwardingInfoCallback
1286 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001287 }
1288 }
1289 callback.accept(errorCode);
1290 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001291 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001292 }
Shuo Qian4a594052020-01-23 11:59:30 -08001293 break;
Hall Liu27d24262020-09-18 19:04:59 -07001294 }
Shuo Qian4a594052020-01-23 11:59:30 -08001295
Hall Liu27d24262020-09-18 19:04:59 -07001296 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001297 request = (MainThreadRequest) msg.obj;
1298 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1299 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1300 break;
Hall Liu27d24262020-09-18 19:04:59 -07001301 }
Shuo Qian4a594052020-01-23 11:59:30 -08001302
Hall Liu27d24262020-09-18 19:04:59 -07001303 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001304 ar = (AsyncResult) msg.obj;
1305 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001306 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001307 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001308 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001309 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001310 // Service Class is a bit mask per 3gpp 27.007.
1311 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001312 if (callForwardResults.length > 1
1313 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001314 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001315 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001316 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1317 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001318 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001319 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001320 }
1321 } else {
1322 if (ar.result == null) {
1323 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1324 }
1325 if (ar.exception != null) {
1326 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1327 }
1328 if (ar.exception instanceof CommandException) {
1329 CommandException.Error error =
1330 ((CommandException) (ar.exception)).getCommandError();
1331 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001332 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001333 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001334 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1335 callWaitingStatus =
1336 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001337 }
1338 }
1339 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001340 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001341 break;
Hall Liu27d24262020-09-18 19:04:59 -07001342 }
Shuo Qian4a594052020-01-23 11:59:30 -08001343
Hall Liu27d24262020-09-18 19:04:59 -07001344 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001345 request = (MainThreadRequest) msg.obj;
1346 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001347 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1348 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001349 break;
Hall Liu27d24262020-09-18 19:04:59 -07001350 }
Shuo Qian4a594052020-01-23 11:59:30 -08001351
Hall Liu27d24262020-09-18 19:04:59 -07001352 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001353 ar = (AsyncResult) msg.obj;
1354 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001355 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1356 Consumer<Integer> callback =
1357 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1358 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001359 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001360 if (ar.exception instanceof CommandException) {
1361 CommandException.Error error =
1362 ((CommandException) (ar.exception)).getCommandError();
1363 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1364 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001365 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1366 callback.accept(
1367 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001368 } else {
1369 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1370 }
1371 } else {
1372 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1373 }
1374 } else {
1375 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1376 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001377 }
Shuo Qian4a594052020-01-23 11:59:30 -08001378 break;
Hall Liu27d24262020-09-18 19:04:59 -07001379 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001380 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1381 ar = (AsyncResult) msg.obj;
1382 request = (MainThreadRequest) ar.userObj;
1383 CellNetworkScanResult cellScanResult;
1384 if (ar.exception == null && ar.result != null) {
1385 cellScanResult = new CellNetworkScanResult(
1386 CellNetworkScanResult.STATUS_SUCCESS,
1387 (List<OperatorInfo>) ar.result);
1388 } else {
1389 if (ar.result == null) {
1390 loge("getCellNetworkScanResults: Empty response");
1391 }
1392 if (ar.exception != null) {
1393 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1394 }
1395 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1396 if (ar.exception instanceof CommandException) {
1397 CommandException.Error error =
1398 ((CommandException) (ar.exception)).getCommandError();
1399 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1400 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1401 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1402 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1403 }
1404 }
1405 cellScanResult = new CellNetworkScanResult(errorCode, null);
1406 }
1407 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001408 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001409 break;
1410
1411 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1412 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001413 ManualNetworkSelectionArgument selArg =
1414 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001415 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1416 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001417 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1418 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001419 break;
1420
1421 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001422 ar = (AsyncResult) msg.obj;
1423 request = (MainThreadRequest) ar.userObj;
1424 if (ar.exception == null) {
1425 request.result = true;
1426 } else {
1427 request.result = false;
1428 loge("setNetworkSelectionModeManual " + ar.exception);
1429 }
1430 notifyRequester(request);
1431 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001432 break;
1433
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001434 case CMD_GET_MODEM_ACTIVITY_INFO:
1435 request = (MainThreadRequest) msg.obj;
1436 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001437 if (defaultPhone != null) {
1438 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001439 } else {
1440 ResultReceiver result = (ResultReceiver) request.argument;
1441 Bundle bundle = new Bundle();
1442 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001443 new ModemActivityInfo(0, 0, 0,
1444 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001445 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001446 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001447 break;
1448
Hall Liud0f208c2020-10-14 16:54:44 -07001449 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001450 ar = (AsyncResult) msg.obj;
1451 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001452 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001453 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001454 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001455 if (mLastModemActivityInfo == null) {
1456 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1457 mLastModemActivitySpecificInfo[0] =
1458 new ActivityStatsTechSpecificInfo(
1459 0,
1460 0,
1461 new int[ModemActivityInfo.getNumTxPowerLevels()],
1462 0);
1463 mLastModemActivityInfo =
1464 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1465 }
1466
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001467 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001468 // Update the last modem activity info and the result of the request.
1469 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1470 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001471 mergeModemActivityInfo(info);
Gary Jian76280a42022-12-07 16:18:33 +08001472 } else {
1473 loge("queryModemActivityInfo: invalid response");
Shuo Qian8f4750a2020-02-20 17:12:10 -08001474 }
Kai Shi917fdc62022-11-28 14:01:02 -08001475 // This is needed to decouple ret from mLastModemActivityInfo
1476 // We don't want to return mLastModemActivityInfo which is updated
1477 // inside mergeModemActivityInfo()
1478 ret = new ModemActivityInfo(
1479 mLastModemActivityInfo.getTimestampMillis(),
1480 mLastModemActivityInfo.getSleepTimeMillis(),
1481 mLastModemActivityInfo.getIdleTimeMillis(),
1482 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001483
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001484 } else {
1485 if (ar.result == null) {
1486 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001487 error = TelephonyManager.ModemActivityInfoException
1488 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001489 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001490 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001491 error = TelephonyManager.ModemActivityInfoException
1492 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001493 } else {
1494 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001495 error = TelephonyManager.ModemActivityInfoException
1496 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001497 }
1498 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001499 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001500 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001501 bundle.putParcelable(
1502 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001503 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001504 } else {
1505 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1506 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001507 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001508 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001509 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001510 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001511
Meng Wang1a7c35a2016-05-05 20:56:15 -07001512 case CMD_SET_ALLOWED_CARRIERS:
1513 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001514 CarrierRestrictionRules argument =
1515 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001516 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001517 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001518 break;
1519
1520 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1521 ar = (AsyncResult) msg.obj;
1522 request = (MainThreadRequest) ar.userObj;
1523 if (ar.exception == null && ar.result != null) {
1524 request.result = ar.result;
1525 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001526 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1527 if (ar.exception instanceof CommandException) {
1528 loge("setAllowedCarriers: CommandException: " + ar.exception);
1529 CommandException.Error error =
1530 ((CommandException) (ar.exception)).getCommandError();
1531 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1532 request.result =
1533 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1534 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001535 } else {
1536 loge("setAllowedCarriers: Unknown exception");
1537 }
1538 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001539 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001540 break;
1541
1542 case CMD_GET_ALLOWED_CARRIERS:
1543 request = (MainThreadRequest) msg.obj;
1544 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001545 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001546 break;
1547
1548 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1549 ar = (AsyncResult) msg.obj;
1550 request = (MainThreadRequest) ar.userObj;
1551 if (ar.exception == null && ar.result != null) {
1552 request.result = ar.result;
1553 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001554 request.result = new IllegalStateException(
1555 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001556 if (ar.result == null) {
1557 loge("getAllowedCarriers: Empty response");
1558 } else if (ar.exception instanceof CommandException) {
1559 loge("getAllowedCarriers: CommandException: " +
1560 ar.exception);
1561 } else {
1562 loge("getAllowedCarriers: Unknown exception");
1563 }
1564 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001565 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001566 break;
1567
Nathan Haroldb3014052017-01-25 15:57:32 -08001568 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1569 ar = (AsyncResult) msg.obj;
1570 request = (MainThreadRequest) ar.userObj;
1571 if (ar.exception == null && ar.result != null) {
1572 request.result = ar.result;
1573 } else {
1574 request.result = new IllegalArgumentException(
1575 "Failed to retrieve Forbidden Plmns");
1576 if (ar.result == null) {
1577 loge("getForbiddenPlmns: Empty response");
1578 } else {
1579 loge("getForbiddenPlmns: Unknown exception");
1580 }
1581 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001582 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001583 break;
1584
1585 case CMD_GET_FORBIDDEN_PLMNS:
1586 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001587 uiccPort = getUiccPortFromRequest(request);
1588 if (uiccPort == null) {
1589 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001590 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001591 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001592 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001593 break;
1594 }
1595 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001596 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001597 if (uiccApp == null) {
1598 loge("getForbiddenPlmns() no app with specified type -- "
1599 + appType);
1600 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001601 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001602 break;
1603 } else {
1604 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1605 + " specified type -- " + appType);
1606 }
1607 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1608 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1609 onCompleted);
1610 break;
1611
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001612 case CMD_SWITCH_SLOTS:
1613 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001614 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001615 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001616 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001617 break;
1618
1619 case EVENT_SWITCH_SLOTS_DONE:
1620 ar = (AsyncResult) msg.obj;
1621 request = (MainThreadRequest) ar.userObj;
1622 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001623 notifyRequester(request);
1624 break;
1625 case CMD_GET_NETWORK_SELECTION_MODE:
1626 request = (MainThreadRequest) msg.obj;
1627 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1628 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1629 break;
1630
1631 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1632 ar = (AsyncResult) msg.obj;
1633 request = (MainThreadRequest) ar.userObj;
1634 if (ar.exception != null) {
1635 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1636 } else {
1637 int mode = ((int[]) ar.result)[0];
1638 if (mode == 0) {
1639 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1640 } else {
1641 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1642 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001643 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001644 notifyRequester(request);
1645 break;
1646 case CMD_GET_CDMA_ROAMING_MODE:
1647 request = (MainThreadRequest) msg.obj;
1648 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1649 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1650 break;
1651 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1652 ar = (AsyncResult) msg.obj;
1653 request = (MainThreadRequest) ar.userObj;
1654 if (ar.exception != null) {
1655 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1656 } else {
1657 request.result = ((int[]) ar.result)[0];
1658 }
1659 notifyRequester(request);
1660 break;
1661 case CMD_SET_CDMA_ROAMING_MODE:
1662 request = (MainThreadRequest) msg.obj;
1663 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1664 int mode = (int) request.argument;
1665 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1666 break;
1667 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1668 ar = (AsyncResult) msg.obj;
1669 request = (MainThreadRequest) ar.userObj;
1670 request.result = ar.exception == null;
1671 notifyRequester(request);
1672 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001673 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1674 request = (MainThreadRequest) msg.obj;
1675 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1676 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1677 break;
1678 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1679 ar = (AsyncResult) msg.obj;
1680 request = (MainThreadRequest) ar.userObj;
1681 if (ar.exception != null) {
1682 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1683 } else {
1684 request.result = ((int[]) ar.result)[0];
1685 }
1686 notifyRequester(request);
1687 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001688 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1689 request = (MainThreadRequest) msg.obj;
1690 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1691 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001692 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1693 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001694 break;
1695 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1696 ar = (AsyncResult) msg.obj;
1697 request = (MainThreadRequest) ar.userObj;
1698 request.result = ar.exception == null;
1699 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001700 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001701 case CMD_GET_ALL_CELL_INFO:
1702 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001703 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001704 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001705 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001706 case EVENT_GET_ALL_CELL_INFO_DONE:
1707 ar = (AsyncResult) msg.obj;
1708 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001709 // If a timeout occurs, the response will be null
1710 request.result = (ar.exception == null && ar.result != null)
1711 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001712 synchronized (request) {
1713 request.notifyAll();
1714 }
1715 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001716 case CMD_REQUEST_CELL_INFO_UPDATE:
1717 request = (MainThreadRequest) msg.obj;
1718 request.phone.requestCellInfoUpdate(request.workSource,
1719 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1720 break;
1721 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1722 ar = (AsyncResult) msg.obj;
1723 request = (MainThreadRequest) ar.userObj;
1724 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1725 try {
1726 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001727 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001728 cb.onError(
1729 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1730 ar.exception.getClass().getName(),
1731 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001732 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001733 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001734 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001735 } else {
1736 // use the result as returned
1737 cb.onCellInfo((List<CellInfo>) ar.result);
1738 }
1739 } catch (RemoteException re) {
1740 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1741 }
1742 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001743 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001744 request = (MainThreadRequest) msg.obj;
1745 WorkSource ws = (WorkSource) request.argument;
1746 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001747 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001748 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001749 }
1750 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001751 ar = (AsyncResult) msg.obj;
1752 request = (MainThreadRequest) ar.userObj;
1753 if (ar.exception == null) {
1754 request.result = ar.result;
1755 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001756 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001757 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001758 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001759 }
1760
1761 synchronized (request) {
1762 request.notifyAll();
1763 }
1764 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001765 }
chen xu6dac5ab2018-10-26 17:39:23 -07001766 case CMD_MODEM_REBOOT:
1767 request = (MainThreadRequest) msg.obj;
1768 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001769 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001770 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001771 case EVENT_CMD_MODEM_REBOOT_DONE:
1772 handleNullReturnEvent(msg, "rebootModem");
1773 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001774 case CMD_REQUEST_ENABLE_MODEM:
1775 request = (MainThreadRequest) msg.obj;
1776 boolean enable = (boolean) request.argument;
1777 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001778 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001779 PhoneConfigurationManager.getInstance()
1780 .enablePhone(request.phone, enable, onCompleted);
1781 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001782 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001783 ar = (AsyncResult) msg.obj;
1784 request = (MainThreadRequest) ar.userObj;
1785 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001786 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001787 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001788 if ((boolean) request.result) {
1789 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1790 updateModemStateMetrics();
1791 } else {
1792 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1793 + ar.exception);
1794 }
1795 notifyRequester(request);
1796 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001797 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001798 case CMD_GET_MODEM_STATUS:
1799 request = (MainThreadRequest) msg.obj;
1800 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1801 PhoneConfigurationManager.getInstance()
1802 .getPhoneStatusFromModem(request.phone, onCompleted);
1803 break;
1804 case EVENT_GET_MODEM_STATUS_DONE:
1805 ar = (AsyncResult) msg.obj;
1806 request = (MainThreadRequest) ar.userObj;
1807 int id = request.phone.getPhoneId();
1808 if (ar.exception == null && ar.result != null) {
1809 request.result = ar.result;
1810 //update the cache as modem status has changed
1811 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1812 (boolean) request.result);
1813 } else {
1814 // Return true if modem status cannot be retrieved. For most cases,
1815 // modem status is on. And for older version modems, GET_MODEM_STATUS
1816 // and disable modem are not supported. Modem is always on.
1817 // TODO: this should be fixed in R to support a third
1818 // status UNKNOWN b/131631629
1819 request.result = true;
1820 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1821 + ar.exception);
1822 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001823 notifyRequester(request);
1824 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001825 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1826 request = (MainThreadRequest) msg.obj;
1827 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1828 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1829 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1830 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1831 break;
1832 }
1833 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1834 ar = (AsyncResult) msg.obj;
1835 request = (MainThreadRequest) ar.userObj;
1836 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1837 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1838 args.second.accept(ar.exception == null);
1839 notifyRequester(request);
1840 break;
1841 }
Sarah Chin679c08a2020-11-18 13:39:35 -08001842 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
1843 request = (MainThreadRequest) msg.obj;
1844 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1845 Phone phone = getPhoneFromRequest(request);
1846 if (phone != null) {
1847 phone.getSystemSelectionChannels(onCompleted);
1848 } else {
1849 loge("getSystemSelectionChannels: No phone object");
1850 request.result = new ArrayList<RadioAccessSpecifier>();
1851 notifyRequester(request);
1852 }
1853 break;
1854 }
1855 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
1856 ar = (AsyncResult) msg.obj;
1857 request = (MainThreadRequest) ar.userObj;
1858 if (ar.exception == null && ar.result != null) {
1859 request.result = ar.result;
1860 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08001861 request.result = new IllegalStateException(
1862 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08001863 if (ar.result == null) {
1864 loge("getSystemSelectionChannels: Empty response");
1865 } else {
1866 loge("getSystemSelectionChannels: Unknown exception");
1867 }
1868 }
1869 notifyRequester(request);
1870 break;
yincheng zhao2737e882019-09-06 17:06:54 -07001871 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1872 ar = (AsyncResult) msg.obj;
1873 request = (MainThreadRequest) ar.userObj;
1874 if (ar.exception == null && ar.result != null) {
1875 request.result = ar.result;
1876 } else {
1877 request.result = -1;
1878 loge("Failed to set Forbidden Plmns");
1879 if (ar.result == null) {
1880 loge("setForbidenPlmns: Empty response");
1881 } else if (ar.exception != null) {
1882 loge("setForbiddenPlmns: Exception: " + ar.exception);
1883 request.result = -1;
1884 } else {
1885 loge("setForbiddenPlmns: Unknown exception");
1886 }
1887 }
1888 notifyRequester(request);
1889 break;
1890 case CMD_SET_FORBIDDEN_PLMNS:
1891 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001892 uiccPort = getUiccPortFromRequest(request);
1893 if (uiccPort == null) {
1894 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07001895 request.result = -1;
1896 notifyRequester(request);
1897 break;
1898 }
1899 Pair<Integer, List<String>> setFplmnsArgs =
1900 (Pair<Integer, List<String>>) request.argument;
1901 appType = setFplmnsArgs.first;
1902 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001903 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07001904 if (uiccApp == null) {
1905 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1906 request.result = -1;
1907 loge("Failed to get UICC App");
1908 notifyRequester(request);
1909 } else {
1910 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1911 ((SIMRecords) uiccApp.getIccRecords())
1912 .setForbiddenPlmns(onCompleted, fplmns);
1913 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001914 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001915 case CMD_ERASE_MODEM_CONFIG:
1916 request = (MainThreadRequest) msg.obj;
1917 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1918 defaultPhone.eraseModemConfig(onCompleted);
1919 break;
1920 case EVENT_ERASE_MODEM_CONFIG_DONE:
1921 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001922 break;
zoey chene02881a2019-12-30 16:11:23 +08001923
Kai Shif70f46f2021-03-03 13:59:46 -08001924 case CMD_ERASE_DATA_SHARED_PREFERENCES:
1925 request = (MainThreadRequest) msg.obj;
1926 request.result = defaultPhone.eraseDataInSharedPreferences();
1927 notifyRequester(request);
1928 break;
1929
zoey chene02881a2019-12-30 16:11:23 +08001930 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1931 request = (MainThreadRequest) msg.obj;
1932 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1933 Pair<String, String> changed = (Pair<String, String>) request.argument;
1934 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1935 changed.first, changed.second, onCompleted);
1936 break;
1937 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1938 ar = (AsyncResult) msg.obj;
1939 request = (MainThreadRequest) ar.userObj;
1940 if (ar.exception == null) {
1941 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001942 // If the operation is successful, update the PIN storage
1943 Pair<String, String> passwords = (Pair<String, String>) request.argument;
1944 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00001945 UiccController.getInstance().getPinStorage()
1946 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08001947 } else {
1948 request.result = msg.arg1;
1949 }
1950 notifyRequester(request);
1951 break;
1952
Michele Berionne5e411512020-11-13 02:36:59 +00001953 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08001954 request = (MainThreadRequest) msg.obj;
1955 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1956 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1957 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1958 enabled.first, enabled.second, onCompleted);
1959 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001960 }
zoey chene02881a2019-12-30 16:11:23 +08001961 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1962 ar = (AsyncResult) msg.obj;
1963 request = (MainThreadRequest) ar.userObj;
1964 if (ar.exception == null) {
1965 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00001966 // If the operation is successful, update the PIN storage
1967 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1968 int phoneId = getPhoneFromRequest(request).getPhoneId();
1969 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00001970 UiccController.getInstance().getPinStorage()
1971 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00001972 } else {
1973 UiccController.getInstance().getPinStorage().clearPin(phoneId);
1974 }
zoey chene02881a2019-12-30 16:11:23 +08001975 } else {
1976 request.result = msg.arg1;
1977 }
Michele Berionne5e411512020-11-13 02:36:59 +00001978
1979
zoey chene02881a2019-12-30 16:11:23 +08001980 notifyRequester(request);
1981 break;
1982
Peter Wangdafb9ac2020-01-15 14:13:38 -08001983 case MSG_NOTIFY_USER_ACTIVITY:
1984 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001985 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001986 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1987 getDefaultPhone().getContext().sendBroadcastAsUser(
1988 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1989 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08001990
1991 case CMD_SET_DATA_THROTTLING: {
1992 request = (MainThreadRequest) msg.obj;
1993 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
1994 DataThrottlingRequest dataThrottlingRequest =
1995 (DataThrottlingRequest) request.argument;
1996 Phone phone = getPhoneFromRequest(request);
1997 if (phone != null) {
1998 phone.setDataThrottling(onCompleted,
1999 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
2000 dataThrottlingRequest.getCompletionDurationMillis());
2001 } else {
2002 loge("setDataThrottling: No phone object");
2003 request.result =
2004 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2005 notifyRequester(request);
2006 }
2007
2008 break;
2009 }
2010 case EVENT_SET_DATA_THROTTLING_DONE:
2011 ar = (AsyncResult) msg.obj;
2012 request = (MainThreadRequest) ar.userObj;
2013
2014 if (ar.exception == null) {
2015 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2016 } else if (ar.exception instanceof CommandException) {
2017 loge("setDataThrottling: CommandException: " + ar.exception);
2018 CommandException.Error error =
2019 ((CommandException) (ar.exception)).getCommandError();
2020
2021 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2022 request.result = TelephonyManager
2023 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2024 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2025 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002026 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2027 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002028 } else {
2029 request.result =
2030 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2031 }
2032 } else {
2033 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2034 }
2035 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2036 notifyRequester(request);
2037 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002038
2039 case CMD_SET_SIM_POWER: {
2040 request = (MainThreadRequest) msg.obj;
2041 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2042 request = (MainThreadRequest) msg.obj;
2043 int stateToSet =
2044 ((Pair<Integer, IIntegerConsumer>)
2045 request.argument).first;
2046 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2047 break;
2048 }
2049 case EVENT_SET_SIM_POWER_DONE: {
2050 ar = (AsyncResult) msg.obj;
2051 request = (MainThreadRequest) ar.userObj;
2052 IIntegerConsumer callback =
2053 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2054 if (ar.exception != null) {
2055 loge("setSimPower exception: " + ar.exception);
2056 int errorCode = TelephonyManager.CallForwardingInfoCallback
2057 .RESULT_ERROR_UNKNOWN;
2058 if (ar.exception instanceof CommandException) {
2059 CommandException.Error error =
2060 ((CommandException) (ar.exception)).getCommandError();
2061 if (error == CommandException.Error.SIM_ERR) {
2062 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2063 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2064 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2065 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2066 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2067 } else {
2068 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2069 }
2070 }
2071 try {
2072 callback.accept(errorCode);
2073 } catch (RemoteException e) {
2074 // Ignore if the remote process is no longer available to call back.
2075 Log.w(LOG_TAG, "setSimPower: callback not available.");
2076 }
2077 } else {
2078 try {
2079 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2080 } catch (RemoteException e) {
2081 // Ignore if the remote process is no longer available to call back.
2082 Log.w(LOG_TAG, "setSimPower: callback not available.");
2083 }
2084 }
2085 break;
2086 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002087 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2088 request = (MainThreadRequest) msg.obj;
2089
2090 final Phone phone = getPhoneFromRequest(request);
2091 if (phone == null || phone.getServiceStateTracker() == null) {
2092 request.result = new IllegalStateException("Phone or SST is null");
2093 notifyRequester(request);
2094 break;
2095 }
2096
2097 Pair<Integer, SignalStrengthUpdateRequest> pair =
2098 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2099 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2100 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002101 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002102 request.subId, pair.first /*callingUid*/,
2103 pair.second /*request*/, onCompleted);
2104 break;
2105 }
2106 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2107 ar = (AsyncResult) msg.obj;
2108 request = (MainThreadRequest) ar.userObj;
2109 // request.result will be the exception of ar if present, true otherwise.
2110 // Be cautious not to leave result null which will wait() forever
2111 request.result = ar.exception != null ? ar.exception : true;
2112 notifyRequester(request);
2113 break;
2114 }
2115 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2116 request = (MainThreadRequest) msg.obj;
2117
2118 Phone phone = getPhoneFromRequest(request);
2119 if (phone == null || phone.getServiceStateTracker() == null) {
2120 request.result = new IllegalStateException("Phone or SST is null");
2121 notifyRequester(request);
2122 break;
2123 }
2124
2125 Pair<Integer, SignalStrengthUpdateRequest> pair =
2126 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2127 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2128 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002129 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002130 request.subId, pair.first /*callingUid*/,
2131 pair.second /*request*/, onCompleted);
2132 break;
2133 }
2134 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2135 ar = (AsyncResult) msg.obj;
2136 request = (MainThreadRequest) ar.userObj;
2137 request.result = ar.exception != null ? ar.exception : true;
2138 notifyRequester(request);
2139 break;
2140 }
Jordan Liu109698e2020-11-24 14:50:34 -08002141
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002142 case CMD_GET_SLICING_CONFIG: {
2143 request = (MainThreadRequest) msg.obj;
2144 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2145 request.phone.getSlicingConfig(onCompleted);
2146 break;
2147 }
2148 case EVENT_GET_SLICING_CONFIG_DONE: {
2149 ar = (AsyncResult) msg.obj;
2150 request = (MainThreadRequest) ar.userObj;
2151 ResultReceiver result = (ResultReceiver) request.argument;
2152
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002153 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002154 Bundle bundle = new Bundle();
2155 int resultCode = 0;
2156 if (ar.exception != null) {
2157 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2158 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002159 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002160 } else if (ar.result == null) {
2161 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002162 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002163 } else {
2164 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002165 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2166 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002167 }
2168
2169 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002170 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002171 }
2172 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2173 result.send(resultCode, bundle);
2174 notifyRequester(request);
2175 break;
2176 }
2177
Sarah Chin71b3a852022-09-28 15:54:19 -07002178 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002179 request = (MainThreadRequest) msg.obj;
2180 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002181 PurchasePremiumCapabilityArgument arg =
2182 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002183 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chin71b3a852022-09-28 15:54:19 -07002184 arg.capability, arg.appName, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002185 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002186 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002187
Sarah Chin71b3a852022-09-28 15:54:19 -07002188 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002189 ar = (AsyncResult) msg.obj;
2190 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002191 PurchasePremiumCapabilityArgument arg =
2192 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002193 try {
2194 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002195 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002196 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002197 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002198 + ", result= "
2199 + TelephonyManager.convertPurchaseResultToString(result));
2200 } catch (RemoteException e) {
2201 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002202 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002203 + " failed: " + e;
2204 if (DBG) log(logStr);
2205 AnomalyReporter.reportAnomaly(
2206 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2207 }
2208 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002209 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002210
Michele Berionne5e411512020-11-13 02:36:59 +00002211 case CMD_PREPARE_UNATTENDED_REBOOT:
2212 request = (MainThreadRequest) msg.obj;
2213 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002214 UiccController.getInstance().getPinStorage()
2215 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002216 notifyRequester(request);
2217 break;
2218
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002219 default:
2220 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2221 break;
2222 }
2223 }
Jake Hambye994d462014-02-03 13:10:13 -08002224
Pengquan Menga1bb6272018-09-06 09:59:22 -07002225 private void notifyRequester(MainThreadRequest request) {
2226 synchronized (request) {
2227 request.notifyAll();
2228 }
2229 }
2230
Jake Hambye994d462014-02-03 13:10:13 -08002231 private void handleNullReturnEvent(Message msg, String command) {
2232 AsyncResult ar = (AsyncResult) msg.obj;
2233 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2234 if (ar.exception == null) {
2235 request.result = true;
2236 } else {
2237 request.result = false;
2238 if (ar.exception instanceof CommandException) {
2239 loge(command + ": CommandException: " + ar.exception);
2240 } else {
2241 loge(command + ": Unknown exception");
2242 }
2243 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002244 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002245 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002246 }
2247
2248 /**
2249 * Posts the specified command to be executed on the main thread,
2250 * waits for the request to complete, and returns the result.
2251 * @see #sendRequestAsync
2252 */
2253 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002254 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2255 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002256 }
2257
2258 /**
2259 * Posts the specified command to be executed on the main thread,
2260 * waits for the request to complete, and returns the result.
2261 * @see #sendRequestAsync
2262 */
2263 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2264 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002265 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002266 }
2267
2268 /**
2269 * Posts the specified command to be executed on the main thread,
2270 * waits for the request to complete, and returns the result.
2271 * @see #sendRequestAsync
2272 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002273 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002274 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2275 }
2276
2277 /**
2278 * Posts the specified command to be executed on the main thread,
2279 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2280 * if not timeout or null otherwise.
2281 * @see #sendRequestAsync
2282 */
2283 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2284 long timeoutInMs) {
2285 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002286 }
2287
2288 /**
2289 * Posts the specified command to be executed on the main thread,
2290 * waits for the request to complete, and returns the result.
2291 * @see #sendRequestAsync
2292 */
Nathan Harold92bed182018-10-12 18:16:49 -07002293 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002294 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002295 }
2296
2297 /**
2298 * Posts the specified command to be executed on the main thread,
2299 * waits for the request to complete, and returns the result.
2300 * @see #sendRequestAsync
2301 */
2302 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002303 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2304 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002305 }
2306
2307 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002308 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2309 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2310 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002311 * @see #sendRequestAsync
2312 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002313 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2314 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002315 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2316 throw new RuntimeException("This method will deadlock if called from the main thread.");
2317 }
2318
Nathan Harold92bed182018-10-12 18:16:49 -07002319 MainThreadRequest request = null;
2320 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2321 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2322 } else if (phone != null) {
2323 request = new MainThreadRequest(argument, phone, workSource);
2324 } else {
2325 request = new MainThreadRequest(argument, subId, workSource);
2326 }
2327
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002328 Message msg = mMainThreadHandler.obtainMessage(command, request);
2329 msg.sendToTarget();
2330
Rambo Wang0f050d82021-02-12 11:43:36 -08002331
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002333 if (timeoutInMs >= 0) {
2334 // Wait for at least timeoutInMs before returning null request result
2335 long now = SystemClock.elapsedRealtime();
2336 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002337 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002338 try {
2339 request.wait(deadline - now);
2340 } catch (InterruptedException e) {
2341 // Do nothing, go back and check if request is completed or timeout
2342 } finally {
2343 now = SystemClock.elapsedRealtime();
2344 }
2345 }
2346 } else {
2347 // Wait for the request to complete
2348 while (request.result == null) {
2349 try {
2350 request.wait();
2351 } catch (InterruptedException e) {
2352 // Do nothing, go back and wait until the request is complete
2353 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002354 }
2355 }
2356 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002357 if (request.result == null) {
2358 Log.wtf(LOG_TAG,
2359 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2360 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002361 return request.result;
2362 }
2363
2364 /**
2365 * Asynchronous ("fire and forget") version of sendRequest():
2366 * Posts the specified command to be executed on the main thread, and
2367 * returns immediately.
2368 * @see #sendRequest
2369 */
2370 private void sendRequestAsync(int command) {
2371 mMainThreadHandler.sendEmptyMessage(command);
2372 }
2373
2374 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002375 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002376 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002377 */
2378 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002379 sendRequestAsync(command, argument, null, null);
2380 }
2381
2382 /**
2383 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2384 * @see {@link #sendRequest(int,Object)}
2385 */
2386 private void sendRequestAsync(
2387 int command, Object argument, Phone phone, WorkSource workSource) {
2388 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002389 Message msg = mMainThreadHandler.obtainMessage(command, request);
2390 msg.sendToTarget();
2391 }
2392
2393 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002394 * Initialize the singleton PhoneInterfaceManager instance.
2395 * This is only done once, at startup, from PhoneApp.onCreate().
2396 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002397 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002398 synchronized (PhoneInterfaceManager.class) {
2399 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002400 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002401 } else {
2402 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2403 }
2404 return sInstance;
2405 }
2406 }
2407
2408 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00002409 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002410 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002411 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00002412 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07002413 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002414 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07002415 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002416 mMainThreadHandler = new MainThreadHandler();
Jack Yue37dd262022-12-16 11:53:37 -08002417 if (!PhoneFactory.isSubscriptionManagerServiceEnabled()) {
2418 mSubscriptionController = SubscriptionController.getInstance();
2419 } else {
2420 mSubscriptionController = null;
2421 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002422 mTelephonySharedPreferences =
2423 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07002424 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07002425 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08002426 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08002427 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07002428 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Gil Cukierman6dac5eb2022-09-19 16:09:04 +00002429 mTelephony2gUpdater = new Telephony2gUpdater(
2430 Executors.newSingleThreadExecutor(), mApp);
2431 mTelephony2gUpdater.init();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002432 publish();
2433 }
2434
Gil Cukierman1c0eb932022-12-06 22:28:24 +00002435 @VisibleForTesting
2436 public SharedPreferences getSharedPreferences() {
2437 return mTelephonySharedPreferences;
2438 }
2439
Gil Cukierman92cc7db2023-01-06 19:25:53 +00002440 /**
2441 * Get the default phone for this device.
2442 */
2443 @VisibleForTesting
2444 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002445 Phone thePhone = getPhone(getDefaultSubscription());
2446 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
2447 }
2448
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002449 private void publish() {
2450 if (DBG) log("publish: " + this);
2451
Peter Wangc035ce42020-01-08 21:00:22 -08002452 TelephonyFrameworkInitializer
2453 .getTelephonyServiceManager()
2454 .getTelephonyServiceRegisterer()
2455 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002456 }
2457
Stuart Scott584921c2015-01-15 17:10:34 -08002458 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08002459 if (request.phone != null) {
2460 return request.phone;
2461 } else {
2462 return getPhoneFromSubId(request.subId);
2463 }
2464 }
2465
2466 private Phone getPhoneFromSubId(int subId) {
2467 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
2468 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08002469 }
2470
Aishwarya Mallampati5e581e12023-01-17 21:57:06 +00002471 /**
2472 * Get phone object associated with a subscription.
2473 * Return default phone if phone object associated with subscription is null
2474 * @param subId - subscriptionId
2475 * @return phone object associated with a subscription or default phone if null.
2476 */
2477 private Phone getPhoneFromSubIdOrDefault(int subId) {
2478 Phone phone = getPhoneFromSubId(subId);
2479 if (phone == null) {
2480 phone = getDefaultPhone();
2481 }
2482 return phone;
2483 }
2484
Rambo Wange53e07d2022-05-10 13:01:13 -07002485 @Nullable
2486 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002487 Phone phone = getPhoneFromRequest(request);
2488 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002489 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002490 }
2491
Wink Saville36469e72014-06-11 15:17:00 -07002492 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07002493 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08002494 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07002495 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002496
Kai Shif70f46f2021-03-03 13:59:46 -08002497 private void sendEraseModemConfig(@NonNull Phone phone) {
2498 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
2499 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
2500 }
2501
2502 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
2503 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
2504 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07002505 }
2506
Peter Wang44b186e2020-01-13 23:33:09 -08002507 private boolean isImsAvailableOnDevice() {
2508 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
2509 if (pm == null) {
2510 // For some reason package manger is not available.. This will fail internally anyway,
2511 // so do not throw error and allow.
2512 return true;
2513 }
2514 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
2515 }
2516
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002517 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002518 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07002519 }
2520
Wink Savilleb564aae2014-10-23 10:18:09 -07002521 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002522 if (DBG) log("dial: " + number);
2523 // No permission check needed here: This is just a wrapper around the
2524 // ACTION_DIAL intent, which is available to any app since it puts up
2525 // the UI before it does anything.
2526
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002527 final long identity = Binder.clearCallingIdentity();
2528 try {
2529 String url = createTelUrl(number);
2530 if (url == null) {
2531 return;
2532 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002533
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002534 // PENDING: should we just silently fail if phone is offhook or ringing?
2535 PhoneConstants.State state = mCM.getState(subId);
2536 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
2537 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
2538 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2539 mApp.startActivity(intent);
2540 }
2541 } finally {
2542 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002543 }
2544 }
2545
2546 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002547 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07002548 }
2549
Wink Savilleb564aae2014-10-23 10:18:09 -07002550 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002551 if (DBG) log("call: " + number);
2552
2553 // This is just a wrapper around the ACTION_CALL intent, but we still
2554 // need to do a permission check since we're calling startActivity()
2555 // from the context of the phone app.
2556 enforceCallPermission();
2557
Jordan Liu1617b712019-07-10 15:06:26 -07002558 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002559 != AppOpsManager.MODE_ALLOWED) {
2560 return;
2561 }
2562
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002563 final long identity = Binder.clearCallingIdentity();
2564 try {
2565 String url = createTelUrl(number);
2566 if (url == null) {
2567 return;
2568 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002569
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002570 boolean isValid = false;
2571 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
2572 if (slist != null) {
2573 for (SubscriptionInfo subInfoRecord : slist) {
2574 if (subInfoRecord.getSubscriptionId() == subId) {
2575 isValid = true;
2576 break;
2577 }
Wink Saville3ab207e2014-11-20 13:07:20 -08002578 }
Wink Saville08874612014-08-31 19:19:58 -07002579 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002580 if (!isValid) {
2581 return;
2582 }
Wink Saville08874612014-08-31 19:19:58 -07002583
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002584 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
2585 intent.putExtra(SUBSCRIPTION_KEY, subId);
2586 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2587 mApp.startActivity(intent);
2588 } finally {
2589 Binder.restoreCallingIdentity(identity);
2590 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002591 }
2592
Wink Savilleb564aae2014-10-23 10:18:09 -07002593 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002594 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002595 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2596 }
2597
Wink Savilleb564aae2014-10-23 10:18:09 -07002598 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002599 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07002600 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
2601 }
2602
Wink Savilleb564aae2014-10-23 10:18:09 -07002603 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002604 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002605
2606 final long identity = Binder.clearCallingIdentity();
2607 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002608 Phone phone = getPhone(subId);
2609 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002610 checkSimPin.start();
2611 return checkSimPin.unlockSim(null, pin);
2612 } finally {
2613 Binder.restoreCallingIdentity(identity);
2614 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002615 }
2616
Wink Savilleb564aae2014-10-23 10:18:09 -07002617 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002618 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002619
2620 final long identity = Binder.clearCallingIdentity();
2621 try {
Michele Berionne5e411512020-11-13 02:36:59 +00002622 Phone phone = getPhone(subId);
2623 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002624 checkSimPuk.start();
2625 return checkSimPuk.unlockSim(puk, pin);
2626 } finally {
2627 Binder.restoreCallingIdentity(identity);
2628 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002629 }
2630
2631 /**
Wink Saville9de0f752013-10-22 19:04:03 -07002632 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002633 * a synchronous one.
2634 */
2635 private static class UnlockSim extends Thread {
2636
2637 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00002638 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002639
2640 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07002641 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2642 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002643
2644 // For replies from SimCard interface
2645 private Handler mHandler;
2646
2647 // For async handler to identify request type
2648 private static final int SUPPLY_PIN_COMPLETE = 100;
2649
Michele Berionne5e411512020-11-13 02:36:59 +00002650 UnlockSim(int phoneId, IccCard simCard) {
2651 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002652 mSimCard = simCard;
2653 }
2654
2655 @Override
2656 public void run() {
2657 Looper.prepare();
2658 synchronized (UnlockSim.this) {
2659 mHandler = new Handler() {
2660 @Override
2661 public void handleMessage(Message msg) {
2662 AsyncResult ar = (AsyncResult) msg.obj;
2663 switch (msg.what) {
2664 case SUPPLY_PIN_COMPLETE:
2665 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
2666 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07002667 mRetryCount = msg.arg1;
2668 if (ar.exception != null) {
2669 if (ar.exception instanceof CommandException &&
2670 ((CommandException)(ar.exception)).getCommandError()
2671 == CommandException.Error.PASSWORD_INCORRECT) {
2672 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08002673 } //When UiccCardApp dispose,handle message and return exception
2674 else if (ar.exception instanceof CommandException &&
2675 ((CommandException) (ar.exception)).getCommandError()
2676 == CommandException.Error.ABORTED) {
2677 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07002678 } else {
2679 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
2680 }
2681 } else {
2682 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
2683 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002684 mDone = true;
2685 UnlockSim.this.notifyAll();
2686 }
2687 break;
2688 }
2689 }
2690 };
2691 UnlockSim.this.notifyAll();
2692 }
2693 Looper.loop();
2694 }
2695
2696 /*
2697 * Use PIN or PUK to unlock SIM card
2698 *
2699 * If PUK is null, unlock SIM card with PIN
2700 *
2701 * If PUK is not null, unlock SIM card with PUK and set PIN code
2702 */
Wink Saville9de0f752013-10-22 19:04:03 -07002703 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002704
2705 while (mHandler == null) {
2706 try {
2707 wait();
2708 } catch (InterruptedException e) {
2709 Thread.currentThread().interrupt();
2710 }
2711 }
2712 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
2713
2714 if (puk == null) {
2715 mSimCard.supplyPin(pin, callback);
2716 } else {
2717 mSimCard.supplyPuk(puk, pin, callback);
2718 }
2719
2720 while (!mDone) {
2721 try {
2722 Log.d(LOG_TAG, "wait for done");
2723 wait();
2724 } catch (InterruptedException e) {
2725 // Restore the interrupted status
2726 Thread.currentThread().interrupt();
2727 }
2728 }
2729 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07002730 int[] resultArray = new int[2];
2731 resultArray[0] = mResult;
2732 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00002733
2734 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002735 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002736 }
2737
Wink Saville9de0f752013-10-22 19:04:03 -07002738 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002739 }
2740 }
2741
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002742 /**
2743 * This method has been removed due to privacy and stability concerns.
2744 */
2745 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002746 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002747 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
2748 return;
Wink Saville36469e72014-06-11 15:17:00 -07002749 }
2750
Nathan Harold1f889d82020-06-04 17:05:26 -07002751 @Override
2752 public void updateServiceLocationWithPackageName(String callingPackage) {
2753 mApp.getSystemService(AppOpsManager.class)
2754 .checkPackage(Binder.getCallingUid(), callingPackage);
2755
Nathan Haroldf096d982020-11-18 17:18:06 -08002756 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07002757 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2758 // Callers targeting S have no business invoking this method.
2759 return;
2760 }
2761
2762 LocationAccessPolicy.LocationPermissionResult locationResult =
2763 LocationAccessPolicy.checkLocationPermission(mApp,
2764 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2765 .setCallingPackage(callingPackage)
2766 .setCallingFeatureId(null)
2767 .setCallingPid(Binder.getCallingPid())
2768 .setCallingUid(Binder.getCallingUid())
2769 .setMethod("updateServiceLocation")
2770 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2771 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2772 .build());
2773 // Apps that lack location permission have no business calling this method;
2774 // however, because no permission was declared in the public API, denials must
2775 // all be "soft".
2776 switch (locationResult) {
2777 case DENIED_HARD: /* fall through */
2778 case DENIED_SOFT:
2779 return;
2780 }
2781
2782 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002783 final long identity = Binder.clearCallingIdentity();
2784 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002785 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002786 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002787 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002788 }
2789 } finally {
2790 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002791 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002792 }
2793
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002794 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002795 @Override
2796 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002797 return isRadioOnWithFeature(callingPackage, null);
2798 }
2799
2800
2801 @Override
2802 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2803 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2804 callingFeatureId);
2805 }
2806
2807 @Deprecated
2808 @Override
2809 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2810 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002811 }
2812
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002813 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002814 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2815 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002816 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002817 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002818 return false;
2819 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002820
2821 final long identity = Binder.clearCallingIdentity();
2822 try {
2823 return isRadioOnForSubscriber(subId);
2824 } finally {
2825 Binder.restoreCallingIdentity(identity);
2826 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002827 }
2828
2829 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002830 final long identity = Binder.clearCallingIdentity();
2831 try {
2832 final Phone phone = getPhone(subId);
2833 if (phone != null) {
2834 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2835 } else {
2836 return false;
2837 }
2838 } finally {
2839 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002840 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002841 }
2842
2843 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002844 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002845 }
Wink Saville36469e72014-06-11 15:17:00 -07002846
Wink Savilleb564aae2014-10-23 10:18:09 -07002847 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -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 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2855 }
2856 } finally {
2857 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002858 }
Wink Saville36469e72014-06-11 15:17:00 -07002859 }
2860
2861 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002862 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002863 }
2864
Wink Savilleb564aae2014-10-23 10:18:09 -07002865 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002866 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002867
2868 final long identity = Binder.clearCallingIdentity();
2869 try {
2870 final Phone phone = getPhone(subId);
2871 if (phone == null) {
2872 return false;
2873 }
2874 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2875 toggleRadioOnOffForSubscriber(subId);
2876 }
2877 return true;
2878 } finally {
2879 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002880 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002881 }
Wink Saville36469e72014-06-11 15:17:00 -07002882
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002883 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002884 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002885 /*
2886 * If any of the Radios are available, it will need to be
2887 * shutdown. So return true if any Radio is available.
2888 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002889 final long identity = Binder.clearCallingIdentity();
2890 try {
2891 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2892 Phone phone = PhoneFactory.getPhone(i);
2893 if (phone != null && phone.isRadioAvailable()) return true;
2894 }
2895 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2896 return false;
2897 } finally {
2898 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002899 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002900 }
2901
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002902 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002903 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002904 enforceModifyPermission();
2905
2906 final long identity = Binder.clearCallingIdentity();
2907 try {
2908 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2909 logv("Shutting down Phone " + i);
2910 shutdownRadioUsingPhoneId(i);
2911 }
2912 } finally {
2913 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002914 }
2915 }
2916
2917 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002918 Phone phone = PhoneFactory.getPhone(phoneId);
2919 if (phone != null && phone.isRadioAvailable()) {
2920 phone.shutdownRadio();
2921 }
2922 }
2923
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002924 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002925 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002926
2927 final long identity = Binder.clearCallingIdentity();
2928 try {
2929 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2930 if (defaultPhone != null) {
2931 defaultPhone.setRadioPower(turnOn);
2932 return true;
2933 } else {
2934 loge("There's no default phone.");
2935 return false;
2936 }
2937 } finally {
2938 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002939 }
Wink Saville36469e72014-06-11 15:17:00 -07002940 }
2941
Wink Savilleb564aae2014-10-23 10:18:09 -07002942 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002943 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002944
2945 final long identity = Binder.clearCallingIdentity();
2946 try {
2947 final Phone phone = getPhone(subId);
2948 if (phone != null) {
2949 phone.setRadioPower(turnOn);
2950 return true;
2951 } else {
2952 return false;
2953 }
2954 } finally {
2955 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002956 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002957 }
2958
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00002959 /**
2960 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
2961 * no reason to power it off. When any of the voters want to power it off, it will be turned
2962 * off. In case of emergency, the radio will be turned on even if there are some reasons for
2963 * powering it off, and these radio off votes will be cleared.
2964 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
2965 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
2966 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
2967 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
2968 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
2969 * check its vote.
2970 *
2971 * @param subId The subscription ID.
2972 * @param reason The reason for powering off radio.
2973 * @return true on success and false on failure.
2974 */
2975 public boolean requestRadioPowerOffForReason(int subId,
2976 @TelephonyManager.RadioPowerReason int reason) {
2977 enforceModifyPermission();
2978
2979 final long identity = Binder.clearCallingIdentity();
2980 try {
2981 final Phone phone = getPhone(subId);
2982 if (phone != null) {
2983 phone.setRadioPowerForReason(false, reason);
2984 return true;
2985 } else {
2986 return false;
2987 }
2988 } finally {
2989 Binder.restoreCallingIdentity(identity);
2990 }
2991 }
2992
2993 /**
2994 * Remove the vote on powering off the radio for a reason, as requested by
2995 * {@link requestRadioPowerOffForReason}.
2996 *
2997 * @param subId The subscription ID.
2998 * @param reason The reason for powering off radio.
2999 * @return true on success and false on failure.
3000 */
3001 public boolean clearRadioPowerOffForReason(int subId,
3002 @TelephonyManager.RadioPowerReason int reason) {
3003 enforceModifyPermission();
3004
3005 final long identity = Binder.clearCallingIdentity();
3006 try {
3007 final Phone phone = getPhone(subId);
3008 if (phone != null) {
3009 phone.setRadioPowerForReason(true, reason);
3010 return true;
3011 } else {
3012 return false;
3013 }
3014 } finally {
3015 Binder.restoreCallingIdentity(identity);
3016 }
3017 }
3018
3019 /**
3020 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3021 *
3022 * @param subId The subscription ID.
3023 * @param callingPackage The package making the call.
3024 * @param callingFeatureId The feature in the package.
3025 * @return List of reasons for powering off radio.
3026 */
3027 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3028 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3029
3030 final long identity = Binder.clearCallingIdentity();
3031 List result = new ArrayList();
3032 try {
3033 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3034 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3035 return result;
3036 }
3037
3038 final Phone phone = getPhone(subId);
3039 if (phone != null) {
3040 result.addAll(phone.getRadioPowerOffReasons());
3041 }
3042 } finally {
3043 Binder.restoreCallingIdentity(identity);
3044 }
3045 return result;
3046 }
3047
Wink Saville36469e72014-06-11 15:17:00 -07003048 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003049 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003050 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003051 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003052
3053 final long identity = Binder.clearCallingIdentity();
3054 try {
Jack Yu285100e2022-12-02 22:48:35 -08003055 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003056 final Phone phone = getPhone(subId);
3057 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003058 phone.getDataSettingsManager().setDataEnabled(
3059 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003060 return true;
3061 } else {
3062 return false;
3063 }
3064 } finally {
3065 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003066 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003067 }
3068
Wink Saville36469e72014-06-11 15:17:00 -07003069 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003070 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003071 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003072 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003073
3074 final long identity = Binder.clearCallingIdentity();
3075 try {
Jack Yu285100e2022-12-02 22:48:35 -08003076 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003077 final Phone phone = getPhone(subId);
3078 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003079 phone.getDataSettingsManager().setDataEnabled(
3080 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003081 return true;
3082 } else {
3083 return false;
3084 }
3085 } finally {
3086 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003087 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003088 }
3089
Sanket Padawe356d7632015-06-22 14:03:32 -07003090 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003091 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003092 final long identity = Binder.clearCallingIdentity();
3093 try {
3094 final Phone phone = getPhone(subId);
3095 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003096 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003097 } else {
3098 return false;
3099 }
3100 } finally {
3101 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003102 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003103 }
3104
3105 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003106 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003107 }
3108
pkanwarae03a6b2016-11-06 20:37:09 -08003109 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003110 enforceCallPermission();
3111
3112 final long identity = Binder.clearCallingIdentity();
3113 try {
3114 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3115 return;
3116 }
3117 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3118 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3119 } finally {
3120 Binder.restoreCallingIdentity(identity);
3121 }
pkanwar32d516d2016-10-14 19:37:38 -07003122 };
3123
Wink Savilleb564aae2014-10-23 10:18:09 -07003124 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003125 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003126
3127 final long identity = Binder.clearCallingIdentity();
3128 try {
3129 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3130 return false;
3131 }
3132 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3133 } finally {
3134 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003135 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003136 }
3137
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003138 /**
3139 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3140 * tag on getCallState Binder call.
3141 */
3142 @Deprecated
3143 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003144 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003145 if (CompatChanges.isChangeEnabled(
3146 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3147 Binder.getCallingUid())) {
3148 // Do not allow this API to be called on API version 31+, it should only be
3149 // called on old apps using this Binder call directly.
3150 throw new SecurityException("This method can only be used for applications "
3151 + "targeting API version 30 or less.");
3152 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003153 final long identity = Binder.clearCallingIdentity();
3154 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003155 Phone phone = getPhone(getDefaultSubscription());
3156 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3157 PhoneConstantConversions.convertCallState(phone.getState());
3158 } finally {
3159 Binder.restoreCallingIdentity(identity);
3160 }
3161 }
3162
3163 @Override
3164 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3165 if (CompatChanges.isChangeEnabled(
3166 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3167 Binder.getCallingUid())) {
3168 // Check READ_PHONE_STATE for API version 31+
3169 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3170 featureId, "getCallStateForSubscription")) {
3171 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3172 + "targeting API level 31+.");
3173 }
3174 }
3175 final long identity = Binder.clearCallingIdentity();
3176 try {
3177 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003178 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3179 PhoneConstantConversions.convertCallState(phone.getState());
3180 } finally {
3181 Binder.restoreCallingIdentity(identity);
3182 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003183 }
3184
Sanket Padawe356d7632015-06-22 14:03:32 -07003185 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003186 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003187 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003188 }
3189
3190 @Override
3191 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003192 final long identity = Binder.clearCallingIdentity();
3193 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003194 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003195 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003196 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003197 } else {
3198 return PhoneConstantConversions.convertDataState(
3199 PhoneConstants.DataState.DISCONNECTED);
3200 }
3201 } finally {
3202 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003203 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003204 }
3205
Sanket Padawe356d7632015-06-22 14:03:32 -07003206 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003207 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003208 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003209 }
3210
3211 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003212 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003213 final long identity = Binder.clearCallingIdentity();
3214 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003215 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003216 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003217 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003218 } else {
3219 return TelephonyManager.DATA_ACTIVITY_NONE;
3220 }
3221 } finally {
3222 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003224 }
3225
3226 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003227 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003228 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003229 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003230
3231 LocationAccessPolicy.LocationPermissionResult locationResult =
3232 LocationAccessPolicy.checkLocationPermission(mApp,
3233 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3234 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003235 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003236 .setCallingPid(Binder.getCallingPid())
3237 .setCallingUid(Binder.getCallingUid())
3238 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003239 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003240 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3241 .build());
3242 switch (locationResult) {
3243 case DENIED_HARD:
3244 throw new SecurityException("Not allowed to access cell location");
3245 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003246 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3247 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003248 }
3249
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003250 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003251 final long identity = Binder.clearCallingIdentity();
3252 try {
3253 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003254 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003255 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003256 } finally {
3257 Binder.restoreCallingIdentity(identity);
3258 }
Svetoslav64fad262015-04-14 14:35:21 -07003259 }
3260
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003261 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003262 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003263 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3264 // registered cell info, so return a NULL country instead.
3265 final long identity = Binder.clearCallingIdentity();
3266 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003267 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3268 // Get default phone in this case.
3269 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3270 }
Jack Yu285100e2022-12-02 22:48:35 -08003271 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003272 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003273 if (phone == null) return "";
3274 ServiceStateTracker sst = phone.getServiceStateTracker();
3275 if (sst == null) return "";
3276 LocaleTracker lt = sst.getLocaleTracker();
3277 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003278 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003279 } finally {
3280 Binder.restoreCallingIdentity(identity);
3281 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003282 }
3283
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003284 /**
3285 * This method was removed due to potential issues caused by performing partial
3286 * updates of service state, and lack of a credible use case.
3287 *
3288 * This has the ability to break the telephony implementation by disabling notification of
3289 * changes in device connectivity. DO NOT USE THIS!
3290 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003291 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003292 public void enableLocationUpdates() {
3293 mApp.enforceCallingOrSelfPermission(
3294 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003295 }
3296
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003297 /**
3298 * This method was removed due to potential issues caused by performing partial
3299 * updates of service state, and lack of a credible use case.
3300 *
3301 * This has the ability to break the telephony implementation by disabling notification of
3302 * changes in device connectivity. DO NOT USE THIS!
3303 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003304 @Override
3305 public void disableLocationUpdates() {
3306 mApp.enforceCallingOrSelfPermission(
3307 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003308 }
3309
3310 @Override
3311 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003312 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3313 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003314 try {
3315 mApp.getSystemService(AppOpsManager.class)
3316 .checkPackage(Binder.getCallingUid(), callingPackage);
3317 } catch (SecurityException e) {
3318 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3319 throw e;
3320 }
3321
Nathan Haroldf096d982020-11-18 17:18:06 -08003322 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003323 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3324 throw new SecurityException(
3325 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3326 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003327
Jordan Liu1617b712019-07-10 15:06:26 -07003328 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003329 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3330 return null;
3331 }
Svetoslav64fad262015-04-14 14:35:21 -07003332
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003333 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003334
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003335 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003336 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003337
Nathan Haroldf180aac2018-06-01 18:43:55 -07003338 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3339 for (CellInfo ci : info) {
3340 if (ci instanceof CellInfoGsm) {
3341 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3342 } else if (ci instanceof CellInfoWcdma) {
3343 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003345 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003346 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003347 }
3348
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003349 private List<CellInfo> getCachedCellInfo() {
3350 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3351 for (Phone phone : PhoneFactory.getPhones()) {
3352 List<CellInfo> info = phone.getAllCellInfo();
3353 if (info != null) cellInfos.addAll(info);
3354 }
3355 return cellInfos;
3356 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003357
3358 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003359 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003360 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003361 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003362
3363 LocationAccessPolicy.LocationPermissionResult locationResult =
3364 LocationAccessPolicy.checkLocationPermission(mApp,
3365 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3366 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003367 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003368 .setCallingPid(Binder.getCallingPid())
3369 .setCallingUid(Binder.getCallingUid())
3370 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003371 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003372 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3373 .build());
3374 switch (locationResult) {
3375 case DENIED_HARD:
3376 throw new SecurityException("Not allowed to access cell info");
3377 case DENIED_SOFT:
3378 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003379 }
3380
Nathan Haroldf096d982020-11-18 17:18:06 -08003381 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003382 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3383 return getCachedCellInfo();
3384 }
3385
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003386 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003387 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003388 final long identity = Binder.clearCallingIdentity();
3389 try {
3390 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3391 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003392 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003393 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003394 if (info != null) cellInfos.addAll(info);
3395 }
3396 return cellInfos;
3397 } finally {
3398 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003399 }
3400 }
3401
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07003402 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003403 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
3404 String callingFeatureId) {
3405 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
3406 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003407 }
3408
3409 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003410 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
3411 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003412 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003413 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003414 }
3415
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003416 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
3417 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003418 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003419 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003420
3421 LocationAccessPolicy.LocationPermissionResult locationResult =
3422 LocationAccessPolicy.checkLocationPermission(mApp,
3423 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3424 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003425 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003426 .setCallingPid(Binder.getCallingPid())
3427 .setCallingUid(Binder.getCallingUid())
3428 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07003429 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3430 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003431 .build());
3432 switch (locationResult) {
3433 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08003434 if (TelephonyPermissions
3435 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003436 // Safetynet logging for b/154934934
3437 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3438 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003439 throw new SecurityException("Not allowed to access cell info");
3440 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08003441 if (TelephonyPermissions
3442 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07003443 // Safetynet logging for b/154934934
3444 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
3445 }
Nathan Harold5320c422019-05-09 10:26:08 -07003446 try {
3447 cb.onCellInfo(new ArrayList<CellInfo>());
3448 } catch (RemoteException re) {
3449 // Drop without consequences
3450 }
Hall Liuf19c44f2018-11-27 14:38:17 -08003451 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003452 }
3453
Nathan Harolda939a962019-05-09 10:13:47 -07003454
3455 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003456 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
3457
3458 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
3459 }
3460
3461 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003462 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08003463 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003464 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003465
3466 final long identity = Binder.clearCallingIdentity();
3467 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00003468 Phone phone = getPhone(subId);
3469 if (phone == null) {
3470 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
3471 } else {
3472 phone.setCellInfoListRate(rateInMillis, workSource);
3473 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003474 } finally {
3475 Binder.restoreCallingIdentity(identity);
3476 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003477 }
3478
Shishir Agrawala9f32182016-04-12 12:00:16 -07003479 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003480 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003481 Phone phone = PhoneFactory.getPhone(slotIndex);
3482 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003483 return null;
3484 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003485 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07003486 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07003487 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003488 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003489 return null;
3490 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003491
3492 final long identity = Binder.clearCallingIdentity();
3493 try {
3494 return phone.getImei();
3495 } finally {
3496 Binder.restoreCallingIdentity(identity);
3497 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003498 }
3499
3500 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00003501 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
3502 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
3503 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
3504 callingFeatureId, "getPrimaryImei")) {
3505 throw new SecurityException("Caller does not have permission");
3506 }
3507 final long identity = Binder.clearCallingIdentity();
3508 try {
3509 for (Phone phone : PhoneFactory.getPhones()) {
3510 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
3511 return phone.getImei();
3512 }
3513 }
3514 throw new UnsupportedOperationException("Operation not supported");
3515 } finally {
3516 Binder.restoreCallingIdentity(identity);
3517 }
3518 }
3519
3520 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003521 public String getTypeAllocationCodeForSlot(int slotIndex) {
3522 Phone phone = PhoneFactory.getPhone(slotIndex);
3523 String tac = null;
3524 if (phone != null) {
3525 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07003526 try {
3527 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
3528 } catch (IndexOutOfBoundsException e) {
3529 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
3530 return null;
3531 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003532 }
3533 return tac;
3534 }
3535
3536 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003537 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07003538 try {
3539 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3540 } catch (SecurityException se) {
3541 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
3542 throw new SecurityException("Package " + callingPackage + " does not belong to "
3543 + Binder.getCallingUid());
3544 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003545 Phone phone = PhoneFactory.getPhone(slotIndex);
3546 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07003547 return null;
3548 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003549
Jeff Davidson913390f2018-02-23 17:11:49 -08003550 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07003551 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003552 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003553 return null;
3554 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003555
3556 final long identity = Binder.clearCallingIdentity();
3557 try {
3558 return phone.getMeid();
3559 } finally {
3560 Binder.restoreCallingIdentity(identity);
3561 }
Jack Yu2af8d712017-03-15 17:14:14 -07003562 }
3563
3564 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00003565 public String getManufacturerCodeForSlot(int slotIndex) {
3566 Phone phone = PhoneFactory.getPhone(slotIndex);
3567 String manufacturerCode = null;
3568 if (phone != null) {
3569 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07003570 try {
3571 manufacturerCode =
3572 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
3573 } catch (IndexOutOfBoundsException e) {
3574 Log.e(LOG_TAG, "MEID length shorter than upper index.");
3575 return null;
3576 }
David Kelly5e06a7f2018-03-12 14:10:59 +00003577 }
3578 return manufacturerCode;
3579 }
3580
3581 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003582 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
3583 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003584 Phone phone = PhoneFactory.getPhone(slotIndex);
3585 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003586 return null;
3587 }
Jeff Davidson913390f2018-02-23 17:11:49 -08003588 int subId = phone.getSubId();
3589 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003590 mApp, subId, callingPackage, callingFeatureId,
3591 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003592 return null;
3593 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003594
3595 final long identity = Binder.clearCallingIdentity();
3596 try {
3597 return phone.getDeviceSvn();
3598 } finally {
3599 Binder.restoreCallingIdentity(identity);
3600 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07003601 }
3602
fionaxu43304da2017-11-27 22:51:16 -08003603 @Override
3604 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003605 final long identity = Binder.clearCallingIdentity();
3606 try {
3607 final Phone phone = getPhone(subId);
3608 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
3609 } finally {
3610 Binder.restoreCallingIdentity(identity);
3611 }
fionaxu43304da2017-11-27 22:51:16 -08003612 }
3613
3614 @Override
3615 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003616 final long identity = Binder.clearCallingIdentity();
3617 try {
3618 final Phone phone = getPhone(subId);
3619 return phone == null ? null : phone.getCarrierName();
3620 } finally {
3621 Binder.restoreCallingIdentity(identity);
3622 }
fionaxu43304da2017-11-27 22:51:16 -08003623 }
3624
calvinpanffe225e2018-11-01 19:43:06 +08003625 @Override
chen xu0026ca62019-03-06 15:28:50 -08003626 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08003627 final long identity = Binder.clearCallingIdentity();
3628 try {
3629 final Phone phone = getPhone(subId);
3630 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08003631 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08003632 } finally {
3633 Binder.restoreCallingIdentity(identity);
3634 }
3635 }
3636
3637 @Override
chen xu0026ca62019-03-06 15:28:50 -08003638 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08003639 final long identity = Binder.clearCallingIdentity();
3640 try {
3641 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08003642 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08003643 } finally {
3644 Binder.restoreCallingIdentity(identity);
3645 }
3646 }
3647
chen xu651eec72018-11-11 19:03:44 -08003648 @Override
chen xu864e11c2018-12-06 22:10:03 -08003649 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
3650 if (!isSubscriptionMccMnc) {
3651 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
3652 }
chen xu651eec72018-11-11 19:03:44 -08003653 final Phone phone = PhoneFactory.getPhone(slotIndex);
3654 if (phone == null) {
3655 return TelephonyManager.UNKNOWN_CARRIER_ID;
3656 }
3657 final long identity = Binder.clearCallingIdentity();
3658 try {
3659 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
3660 } finally {
3661 Binder.restoreCallingIdentity(identity);
3662 }
3663 }
3664
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003665 //
3666 // Internal helper methods.
3667 //
3668
Sanket Padaweee13a9b2016-03-08 17:30:28 -08003669 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07003670 * Make sure the caller is the calling package itself
3671 *
3672 * @throws SecurityException if the caller is not the calling package
3673 */
3674 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
3675 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07003676 PackageManager pm = mApp.getBaseContext().createContextAsUser(
3677 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07003678 try {
Grace Jiadbefca02021-04-26 15:13:31 -07003679 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07003680 } catch (PackageManager.NameNotFoundException e) {
3681 // packageUid is -1
3682 }
3683 if (packageUid != callingUid) {
3684 throw new SecurityException(message + ": Package " + callingPackage
3685 + " does not belong to " + callingUid);
3686 }
3687 }
3688
3689 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003690 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3691 *
3692 * @throws SecurityException if the caller does not have the required permission
3693 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003694 @VisibleForTesting
3695 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003696 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
3697 }
3698
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003699 /**
3700 * Make sure the caller has the MODIFY_PHONE_STATE permission.
3701 *
3702 * @throws SecurityException if the caller does not have the required permission
3703 */
3704 @VisibleForTesting
3705 public void enforceReadPermission() {
3706 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, null);
3707 }
3708
Shuo Qian3b6ee772019-11-13 17:43:31 -08003709 private void enforceActiveEmergencySessionPermission() {
3710 mApp.enforceCallingOrSelfPermission(
3711 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
3712 }
3713
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003714 /**
3715 * Make sure the caller has the CALL_PHONE permission.
3716 *
3717 * @throws SecurityException if the caller does not have the required permission
3718 */
3719 private void enforceCallPermission() {
3720 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
3721 }
3722
paulhu5a773602019-08-23 19:17:33 +08003723 private void enforceSettingsPermission() {
3724 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003725 }
3726
Michele Berionne5e411512020-11-13 02:36:59 +00003727 private void enforceRebootPermission() {
3728 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
3729 }
3730
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003731 private String createTelUrl(String number) {
3732 if (TextUtils.isEmpty(number)) {
3733 return null;
3734 }
3735
Jake Hambye994d462014-02-03 13:10:13 -08003736 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003737 }
3738
Ihab Awadf9e92732013-12-05 18:02:52 -08003739 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003740 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
3741 }
3742
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003743 private static void logv(String msg) {
3744 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
3745 }
3746
Ihab Awadf9e92732013-12-05 18:02:52 -08003747 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003748 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
3749 }
3750
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003751 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003752 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07003753 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07003754 }
3755
Sanket Padawe356d7632015-06-22 14:03:32 -07003756 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003757 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003758 final long identity = Binder.clearCallingIdentity();
3759 try {
3760 final Phone phone = PhoneFactory.getPhone(slotIndex);
3761 if (phone == null) {
3762 return PhoneConstants.PHONE_TYPE_NONE;
3763 } else {
3764 return phone.getPhoneType();
3765 }
3766 } finally {
3767 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003768 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003769 }
3770
3771 /**
3772 * Returns the CDMA ERI icon index to display
3773 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003774 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003775 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
3776 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
3777 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003778 }
3779
Sanket Padawe356d7632015-06-22 14:03:32 -07003780 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003781 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
3782 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003783 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003784 mApp, subId, callingPackage, callingFeatureId,
3785 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003786 return -1;
3787 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003788
3789 final long identity = Binder.clearCallingIdentity();
3790 try {
3791 final Phone phone = getPhone(subId);
3792 if (phone != null) {
3793 return phone.getCdmaEriIconIndex();
3794 } else {
3795 return -1;
3796 }
3797 } finally {
3798 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003799 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003800 }
3801
3802 /**
3803 * Returns the CDMA ERI icon mode,
3804 * 0 - ON
3805 * 1 - FLASHING
3806 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003807 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003808 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
3809 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
3810 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003811 }
3812
Sanket Padawe356d7632015-06-22 14:03:32 -07003813 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003814 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
3815 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003816 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003817 mApp, subId, callingPackage, callingFeatureId,
3818 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003819 return -1;
3820 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003821
3822 final long identity = Binder.clearCallingIdentity();
3823 try {
3824 final Phone phone = getPhone(subId);
3825 if (phone != null) {
3826 return phone.getCdmaEriIconMode();
3827 } else {
3828 return -1;
3829 }
3830 } finally {
3831 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003832 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003833 }
3834
3835 /**
3836 * Returns the CDMA ERI text,
3837 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003838 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003839 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
3840 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
3841 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07003842 }
3843
Sanket Padawe356d7632015-06-22 14:03:32 -07003844 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003845 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
3846 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003847 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003848 mApp, subId, callingPackage, callingFeatureId,
3849 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003850 return null;
3851 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003852
3853 final long identity = Binder.clearCallingIdentity();
3854 try {
3855 final Phone phone = getPhone(subId);
3856 if (phone != null) {
3857 return phone.getCdmaEriText();
3858 } else {
3859 return null;
3860 }
3861 } finally {
3862 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003863 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003864 }
3865
3866 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07003867 * Returns the CDMA MDN.
3868 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003869 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003870 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003871 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3872 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003873
3874 final long identity = Binder.clearCallingIdentity();
3875 try {
3876 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003877 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003878 return phone.getLine1Number();
3879 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003880 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003881 return null;
3882 }
3883 } finally {
3884 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003885 }
3886 }
3887
3888 /**
3889 * Returns the CDMA MIN.
3890 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003891 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07003892 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003893 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3894 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003895
3896 final long identity = Binder.clearCallingIdentity();
3897 try {
3898 final Phone phone = getPhone(subId);
3899 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
3900 return phone.getCdmaMin();
3901 } else {
3902 return null;
3903 }
3904 } finally {
3905 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07003906 }
3907 }
3908
Hall Liud892bec2018-11-30 14:51:45 -08003909 @Override
3910 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
3911 INumberVerificationCallback callback, String callingPackage) {
3912 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
3913 != PERMISSION_GRANTED) {
3914 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
3915 }
3916 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3917
3918 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
3919 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08003920 throw new SecurityException("Calling package must be configured in the device config: "
3921 + "calling package: " + callingPackage
3922 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08003923 }
3924
3925 if (range == null) {
3926 throw new NullPointerException("Range must be non-null");
3927 }
3928
3929 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08003930 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08003931
3932 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3933 }
3934
Junda Liuca05d5d2014-08-14 22:36:34 -07003935 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003936 * Returns true if CDMA provisioning needs to run.
3937 */
3938 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003939 final long identity = Binder.clearCallingIdentity();
3940 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003941 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003942 } finally {
3943 Binder.restoreCallingIdentity(identity);
3944 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003945 }
3946
3947 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003948 * Sets the voice mail number of a given subId.
3949 */
3950 @Override
3951 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003952 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3953 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003954
3955 final long identity = Binder.clearCallingIdentity();
3956 try {
3957 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3958 new Pair<String, String>(alphaTag, number), new Integer(subId));
3959 return success;
3960 } finally {
3961 Binder.restoreCallingIdentity(identity);
3962 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003963 }
3964
Ta-wei Yen87c49842016-05-13 21:19:52 -07003965 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003966 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3967 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003968 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3969 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003970 if (!TextUtils.equals(callingPackage, systemDialer)) {
3971 throw new SecurityException("caller must be system dialer");
3972 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003973
3974 final long identity = Binder.clearCallingIdentity();
3975 try {
3976 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3977 if (phoneAccountHandle == null) {
3978 return null;
3979 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003980 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003981 } finally {
3982 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003983 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003984 }
3985
3986 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003987 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3988 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003989 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003990 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003991 mApp, subId, callingPackage, callingFeatureId,
3992 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003993 return null;
3994 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003995
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003996 final long identity = Binder.clearCallingIdentity();
3997 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003998 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003999 } finally {
4000 Binder.restoreCallingIdentity(identity);
4001 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08004002 }
4003
4004 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004005 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
4006 VisualVoicemailSmsFilterSettings settings) {
4007 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004008
4009 final long identity = Binder.clearCallingIdentity();
4010 try {
4011 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004012 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004013 } finally {
4014 Binder.restoreCallingIdentity(identity);
4015 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004016 }
4017
4018 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004019 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4020 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004021
4022 final long identity = Binder.clearCallingIdentity();
4023 try {
4024 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004025 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004026 } finally {
4027 Binder.restoreCallingIdentity(identity);
4028 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004029 }
4030
4031 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004032 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4033 String callingPackage, int subId) {
4034 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004035
4036 final long identity = Binder.clearCallingIdentity();
4037 try {
4038 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004039 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004040 } finally {
4041 Binder.restoreCallingIdentity(identity);
4042 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004043 }
4044
4045 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004046 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004047 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004048
4049 final long identity = Binder.clearCallingIdentity();
4050 try {
4051 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004052 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004053 } finally {
4054 Binder.restoreCallingIdentity(identity);
4055 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004056 }
4057
4058 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004059 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4060 String callingAttributionTag, int subId, String number, int port, String text,
4061 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004062 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004063 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004064 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004065 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004066 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4067 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004068 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004069
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004070 /**
fionaxu0152e512016-11-14 13:36:14 -08004071 * Sets the voice activation state of a given subId.
4072 */
4073 @Override
4074 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004075 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4076 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004077
4078 final long identity = Binder.clearCallingIdentity();
4079 try {
4080 final Phone phone = getPhone(subId);
4081 if (phone != null) {
4082 phone.setVoiceActivationState(activationState);
4083 } else {
4084 loge("setVoiceActivationState fails with invalid subId: " + subId);
4085 }
4086 } finally {
4087 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004088 }
4089 }
4090
4091 /**
4092 * Sets the data activation state of a given subId.
4093 */
4094 @Override
4095 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004096 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4097 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004098
4099 final long identity = Binder.clearCallingIdentity();
4100 try {
4101 final Phone phone = getPhone(subId);
4102 if (phone != null) {
4103 phone.setDataActivationState(activationState);
4104 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004105 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004106 }
4107 } finally {
4108 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004109 }
4110 }
4111
4112 /**
4113 * Returns the voice activation state of a given subId.
4114 */
4115 @Override
4116 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004117 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004118
fionaxu0152e512016-11-14 13:36:14 -08004119 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004120 final long identity = Binder.clearCallingIdentity();
4121 try {
4122 if (phone != null) {
4123 return phone.getVoiceActivationState();
4124 } else {
4125 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4126 }
4127 } finally {
4128 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004129 }
4130 }
4131
4132 /**
4133 * Returns the data activation state of a given subId.
4134 */
4135 @Override
4136 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004137 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004138
fionaxu0152e512016-11-14 13:36:14 -08004139 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004140 final long identity = Binder.clearCallingIdentity();
4141 try {
4142 if (phone != null) {
4143 return phone.getDataActivationState();
4144 } else {
4145 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4146 }
4147 } finally {
4148 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004149 }
4150 }
4151
4152 /**
Wink Saville36469e72014-06-11 15:17:00 -07004153 * Returns the unread count of voicemails for a subId
4154 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004155 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004156 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4157 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004158 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004159 mApp, subId, callingPackage, callingFeatureId,
4160 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004161 return 0;
4162 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004163 final long identity = Binder.clearCallingIdentity();
4164 try {
4165 final Phone phone = getPhone(subId);
4166 if (phone != null) {
4167 return phone.getVoiceMessageCount();
4168 } else {
4169 return 0;
4170 }
4171 } finally {
4172 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004173 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004174 }
4175
4176 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08004177 * returns true, if the device is in a state where both voice and data
4178 * are supported simultaneously. This can change based on location or network condition.
4179 */
4180 @Override
4181 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004182 final long identity = Binder.clearCallingIdentity();
4183 try {
4184 final Phone phone = getPhone(subId);
4185 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4186 } finally {
4187 Binder.restoreCallingIdentity(identity);
4188 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004189 }
4190
4191 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004192 * Send the dialer code if called from the current default dialer or the caller has
4193 * carrier privilege.
4194 * @param inputCode The dialer code to send
4195 */
4196 @Override
4197 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004198 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004199 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004200 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4201 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004202 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004203 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004204 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004205 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004206
4207 final long identity = Binder.clearCallingIdentity();
4208 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004209 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004210 } finally {
4211 Binder.restoreCallingIdentity(identity);
4212 }
fionaxu235cc5e2017-03-06 22:25:57 -08004213 }
4214
Pengquan Menga1bb6272018-09-06 09:59:22 -07004215 @Override
4216 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004217 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07004218 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilufc958392020-01-20 11:36:01 -08004219 mApp, subId, "getNetworkSelectionMode");
4220 final long identity = Binder.clearCallingIdentity();
4221 try {
4222 if (!isActiveSubscription(subId)) {
4223 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4224 }
4225 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4226 } finally {
4227 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004228 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004229 }
4230
Brad Ebinger35c841c2018-10-01 10:40:55 -07004231 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004232 public boolean isInEmergencySmsMode() {
4233 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4234 final long identity = Binder.clearCallingIdentity();
4235 try {
4236 for (Phone phone : PhoneFactory.getPhones()) {
4237 if (phone.isInEmergencySmsMode()) {
4238 return true;
4239 }
4240 }
4241 } finally {
4242 Binder.restoreCallingIdentity(identity);
4243 }
4244 return false;
4245 }
4246
shilu366312e2019-12-17 09:28:10 -08004247 /**
4248 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4249 * @param subId The subscription to use to check the configuration.
4250 * @param c The callback that will be used to send the result.
4251 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004252 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004253 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4254 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004255 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004256 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004257
4258 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4259 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4260 "IMS not available on device.");
4261 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004262 final long token = Binder.clearCallingIdentity();
4263 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004264 int slotId = getSlotIndexOrException(subId);
4265 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004266
4267 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4268 if (controller != null) {
4269 ImsManager imsManager = controller.getImsManager(subId);
4270 if (imsManager != null) {
4271 imsManager.addRegistrationCallbackForSubscription(c, subId);
4272 } else {
4273 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4274 }
4275 } else {
4276 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4277 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004278 } catch (ImsException e) {
4279 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004280 } finally {
4281 Binder.restoreCallingIdentity(token);
4282 }
4283 }
4284
shilu366312e2019-12-17 09:28:10 -08004285 /**
4286 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4287 * @param subId The subscription to use to check the configuration.
4288 * @param c The callback that will be used to send the result.
4289 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004290 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004291 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004292 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004293 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004294 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4295 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4296 }
Meng Wangafbc5852019-09-19 17:37:13 -07004297 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004298
Meng Wangafbc5852019-09-19 17:37:13 -07004299 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004300 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4301 if (controller != null) {
4302 ImsManager imsManager = controller.getImsManager(subId);
4303 if (imsManager != null) {
4304 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4305 } else {
4306 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4307 + "is inactive, ignoring unregister.");
4308 // If the ImsManager is not valid, just return, since the callback
4309 // will already have been removed internally.
4310 }
4311 }
Meng Wangafbc5852019-09-19 17:37:13 -07004312 } finally {
4313 Binder.restoreCallingIdentity(token);
4314 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004315 }
4316
Brad Ebingera34a6c22019-10-22 17:36:18 -07004317 /**
4318 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4319 */
4320 @Override
4321 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4322 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4323 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4324 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4325 "IMS not available on device.");
4326 }
4327 final long token = Binder.clearCallingIdentity();
4328 try {
4329 Phone phone = getPhone(subId);
4330 if (phone == null) {
4331 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4332 + subId + "'");
4333 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4334 }
4335 phone.getImsRegistrationState(regState -> {
4336 try {
4337 consumer.accept((regState == null)
4338 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4339 } catch (RemoteException e) {
4340 // Ignore if the remote process is no longer available to call back.
4341 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4342 }
4343 });
4344 } finally {
4345 Binder.restoreCallingIdentity(token);
4346 }
4347 }
4348
4349 /**
4350 * Get the transport type for the IMS service registration state.
4351 */
4352 @Override
4353 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004354 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004355 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004356 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4357 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4358 "IMS not available on device.");
4359 }
4360 final long token = Binder.clearCallingIdentity();
4361 try {
4362 Phone phone = getPhone(subId);
4363 if (phone == null) {
4364 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4365 + subId + "'");
4366 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4367 }
4368 phone.getImsRegistrationTech(regTech -> {
4369 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4370 int regTechConverted = (regTech == null)
4371 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4372 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4373 regTechConverted);
4374 try {
4375 consumer.accept(regTechConverted);
4376 } catch (RemoteException e) {
4377 // Ignore if the remote process is no longer available to call back.
4378 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4379 }
4380 });
4381 } finally {
4382 Binder.restoreCallingIdentity(token);
4383 }
4384 }
4385
shilu366312e2019-12-17 09:28:10 -08004386 /**
4387 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4388 * @param subId The subscription to use to check the configuration.
4389 * @param c The callback that will be used to send the result.
4390 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004391 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004392 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
4393 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004394 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004395 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004396 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4397 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4398 "IMS not available on device.");
4399 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004400 final long token = Binder.clearCallingIdentity();
4401 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004402 int slotId = getSlotIndexOrException(subId);
4403 verifyImsMmTelConfiguredOrThrow(slotId);
4404 ImsManager.getInstance(mApp, slotId).addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004405 } catch (ImsException e) {
4406 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004407 } finally {
4408 Binder.restoreCallingIdentity(token);
4409 }
4410 }
4411
shilu366312e2019-12-17 09:28:10 -08004412 /**
4413 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4414 * @param subId The subscription to use to check the configuration.
4415 * @param c The callback that will be used to send the result.
4416 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004417 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004418 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004419 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004420 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004421 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4422 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4423 }
Meng Wangafbc5852019-09-19 17:37:13 -07004424
4425 final long token = Binder.clearCallingIdentity();
4426 try {
Meng Wangafbc5852019-09-19 17:37:13 -07004427 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004428 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07004429 } catch (ImsException e) {
4430 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
4431 + "is inactive, ignoring unregister.");
4432 // If the subscription is no longer active, just return, since the callback
4433 // will already have been removed internally.
4434 } finally {
4435 Binder.restoreCallingIdentity(token);
4436 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004437 }
4438
4439 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004440 public boolean isCapable(int subId, int capability, int regTech) {
4441 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004442 final long token = Binder.clearCallingIdentity();
4443 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004444 int slotId = getSlotIndexOrException(subId);
4445 verifyImsMmTelConfiguredOrThrow(slotId);
4446 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
4447 } catch (com.android.ims.ImsException e) {
4448 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
4449 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004450 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08004451 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
4452 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004453 } finally {
4454 Binder.restoreCallingIdentity(token);
4455 }
4456 }
4457
4458 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004459 public boolean isAvailable(int subId, int capability, int regTech) {
4460 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004461 final long token = Binder.clearCallingIdentity();
4462 try {
4463 Phone phone = getPhone(subId);
4464 if (phone == null) return false;
4465 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07004466 } catch (com.android.ims.ImsException e) {
4467 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
4468 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07004469 } finally {
4470 Binder.restoreCallingIdentity(token);
4471 }
4472 }
4473
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004474 /**
4475 * Determines if the MmTel feature capability is supported by the carrier configuration for this
4476 * subscription.
4477 * @param subId The subscription to use to check the configuration.
4478 * @param callback The callback that will be used to send the result.
4479 * @param capability The MmTelFeature capability that will be used to send the result.
4480 * @param transportType The transport type of the MmTelFeature capability.
4481 */
4482 @Override
4483 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
4484 int transportType) {
4485 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00004486 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4487 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4488 "IMS not available on device.");
4489 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004490 final long token = Binder.clearCallingIdentity();
4491 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004492 int slotId = getSlotIndex(subId);
4493 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4494 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
4495 + subId + "'");
4496 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4497 }
4498 verifyImsMmTelConfiguredOrThrow(slotId);
4499 ImsManager.getInstance(mApp, slotId).isSupported(capability,
4500 transportType, aBoolean -> {
4501 try {
4502 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
4503 } catch (RemoteException e) {
4504 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
4505 + "running. Ignore");
4506 }
4507 });
Brad Ebinger919631e2021-06-02 17:46:35 -07004508 } catch (ImsException e) {
4509 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07004510 } finally {
4511 Binder.restoreCallingIdentity(token);
4512 }
4513 }
4514
shilu366312e2019-12-17 09:28:10 -08004515 /**
4516 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4517 * @param subId The subscription to use to check the configuration.
4518 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004519 @Override
4520 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004521 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004522 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08004523
Brad Ebinger35c841c2018-10-01 10:40:55 -07004524 final long token = Binder.clearCallingIdentity();
4525 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004526 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004527 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004528 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004529 } catch (ImsException e) {
4530 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004531 } finally {
4532 Binder.restoreCallingIdentity(token);
4533 }
4534 }
4535
4536 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004537 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004538 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004539 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004540 final long identity = Binder.clearCallingIdentity();
4541 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004542 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004543 // This setting doesn't require an active ImsService connection, so do not verify. The
4544 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004545 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004546 } catch (ImsException e) {
4547 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004548 } finally {
4549 Binder.restoreCallingIdentity(identity);
4550 }
4551 }
4552
shilu366312e2019-12-17 09:28:10 -08004553 /**
4554 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4555 * @param subId The subscription to use to check the configuration.
4556 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004557 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004558 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004559 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004560 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004561 final long identity = Binder.clearCallingIdentity();
4562 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004563 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004564 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004565 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004566 } catch (ImsException e) {
4567 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004568 } finally {
4569 Binder.restoreCallingIdentity(identity);
4570 }
4571 }
4572
4573 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004574 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004575 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004576 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004577 final long identity = Binder.clearCallingIdentity();
4578 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004579 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004580 // This setting doesn't require an active ImsService connection, so do not verify. The
4581 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004582 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004583 } catch (ImsException e) {
4584 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004585 } finally {
4586 Binder.restoreCallingIdentity(identity);
4587 }
4588 }
4589
shilu366312e2019-12-17 09:28:10 -08004590 /**
4591 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4592 * @param subId The subscription to use to check the configuration.
4593 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004594 @Override
4595 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004596 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004597 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004598 final long identity = Binder.clearCallingIdentity();
4599 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004600 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004601 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004602 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004603 } catch (ImsException e) {
4604 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004605 } finally {
4606 Binder.restoreCallingIdentity(identity);
4607 }
4608 }
4609
4610 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004611 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004612 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004613 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004614 final long identity = Binder.clearCallingIdentity();
4615 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004616 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004617 // This setting doesn't require an active ImsService connection, so do not verify. The
4618 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004619 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004620 } catch (ImsException e) {
4621 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004622 } finally {
4623 Binder.restoreCallingIdentity(identity);
4624 }
4625 }
4626
shilu366312e2019-12-17 09:28:10 -08004627 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004628 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
4629 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4630 * @param subId The subscription to use to check the configuration.
4631 */
4632 @Override
4633 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004634 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004635 mApp, subId, "isCrossSimCallingEnabledByUser");
4636 final long identity = Binder.clearCallingIdentity();
4637 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004638 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004639 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004640 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004641 } catch (ImsException e) {
4642 throw new ServiceSpecificException(e.getCode());
4643 } finally {
4644 Binder.restoreCallingIdentity(identity);
4645 }
4646 }
4647
4648 /**
4649 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
4650 * Requires MODIFY_PHONE_STATE permission.
4651 * @param subId The subscription to use to check the configuration.
4652 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
4653 * false otherwise
4654 */
4655 @Override
4656 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
4657 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4658 "setCrossSimCallingEnabled");
4659 final long identity = Binder.clearCallingIdentity();
4660 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004661 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004662 // This setting doesn't require an active ImsService connection, so do not verify. The
4663 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004664 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08004665 } catch (ImsException e) {
4666 throw new ServiceSpecificException(e.getCode());
4667 } finally {
4668 Binder.restoreCallingIdentity(identity);
4669 }
4670 }
4671
4672 /**
shilu366312e2019-12-17 09:28:10 -08004673 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4674 * @param subId The subscription to use to check the configuration.
4675 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004676 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07004677
Brad Ebinger35c841c2018-10-01 10:40:55 -07004678 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004679 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004680 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004681 final long identity = Binder.clearCallingIdentity();
4682 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004683 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004684 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004685 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004686 } catch (ImsException e) {
4687 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004688 } finally {
4689 Binder.restoreCallingIdentity(identity);
4690 }
4691 }
4692
4693 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08004694 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004695 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08004696 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004697 final long identity = Binder.clearCallingIdentity();
4698 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004699 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004700 // This setting doesn't require an active ImsService connection, so do not verify. The
4701 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004702 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004703 } catch (ImsException e) {
4704 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004705 } finally {
4706 Binder.restoreCallingIdentity(identity);
4707 }
4708 }
4709
4710 @Override
4711 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
4712 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4713 "setVoWiFiNonPersistent");
4714 final long identity = Binder.clearCallingIdentity();
4715 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004716 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004717 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004718 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004719 } catch (ImsException e) {
4720 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004721 } finally {
4722 Binder.restoreCallingIdentity(identity);
4723 }
4724 }
4725
shilu366312e2019-12-17 09:28:10 -08004726 /**
4727 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4728 * @param subId The subscription to use to check the configuration.
4729 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004730 @Override
4731 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004732 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004733 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004734 final long identity = Binder.clearCallingIdentity();
4735 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004736 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004737 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004738 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004739 } catch (ImsException e) {
4740 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004741 } finally {
4742 Binder.restoreCallingIdentity(identity);
4743 }
4744 }
4745
4746 @Override
4747 public void setVoWiFiModeSetting(int subId, int mode) {
4748 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4749 "setVoWiFiModeSetting");
4750 final long identity = Binder.clearCallingIdentity();
4751 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004752 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004753 // This setting doesn't require an active ImsService connection, so do not verify. The
4754 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004755 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004756 } catch (ImsException e) {
4757 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004758 } finally {
4759 Binder.restoreCallingIdentity(identity);
4760 }
4761 }
4762
4763 @Override
4764 public int getVoWiFiRoamingModeSetting(int subId) {
4765 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
4766 final long identity = Binder.clearCallingIdentity();
4767 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004768 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004769 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004770 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004771 } catch (ImsException e) {
4772 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004773 } finally {
4774 Binder.restoreCallingIdentity(identity);
4775 }
4776 }
4777
4778 @Override
4779 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
4780 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4781 "setVoWiFiRoamingModeSetting");
4782 final long identity = Binder.clearCallingIdentity();
4783 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004784 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004785 // This setting doesn't require an active ImsService connection, so do not verify. The
4786 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004787 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004788 } catch (ImsException e) {
4789 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004790 } finally {
4791 Binder.restoreCallingIdentity(identity);
4792 }
4793 }
4794
4795 @Override
4796 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
4797 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4798 "setRttCapabilityEnabled");
4799 final long identity = Binder.clearCallingIdentity();
4800 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004801 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004802 // This setting doesn't require an active ImsService connection, so do not verify. The
4803 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07004804 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004805 } catch (ImsException e) {
4806 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004807 } finally {
4808 Binder.restoreCallingIdentity(identity);
4809 }
4810 }
4811
shilu366312e2019-12-17 09:28:10 -08004812 /**
4813 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4814 * @param subId The subscription to use to check the configuration.
4815 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004816 @Override
4817 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07004818 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004819 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07004820 final long identity = Binder.clearCallingIdentity();
4821 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07004822 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07004823 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07004824 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004825 } catch (ImsException e) {
4826 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004827 } finally {
4828 Binder.restoreCallingIdentity(identity);
4829 }
4830 }
4831
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004832 @Override
4833 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4834 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004835
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004836 final long identity = Binder.clearCallingIdentity();
4837 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004838 if (!isImsAvailableOnDevice()) {
4839 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4840 "IMS not available on device.");
4841 }
4842 int slotId = getSlotIndexOrException(subId);
4843 verifyImsMmTelConfiguredOrThrow(slotId);
4844 ImsManager.getInstance(mApp, slotId)
4845 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004846 } catch (ImsException e) {
4847 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004848 } finally {
4849 Binder.restoreCallingIdentity(identity);
4850 }
4851 }
4852
4853 @Override
4854 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
4855 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00004856
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004857 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004858 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4859 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4860 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004861 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004862 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004863 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004864 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004865 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
4866 + "is inactive, ignoring unregister.");
4867 // If the subscription is no longer active, just return, since the callback will already
4868 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004869 } finally {
4870 Binder.restoreCallingIdentity(identity);
4871 }
4872 }
4873
joonhunshincffb7fc2021-11-28 07:32:01 +00004874 @Override
4875 public void registerFeatureProvisioningChangedCallback(int subId,
4876 IFeatureProvisioningCallback callback) {
4877 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4878 mApp, subId, "registerFeatureProvisioningChangedCallback");
4879
4880 final long identity = Binder.clearCallingIdentity();
4881 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4882 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4883 }
4884
joonhunshin91bc1952022-04-29 08:47:15 +00004885 try {
4886 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4887 if (controller == null) {
4888 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4889 "Device does not support IMS");
4890 }
4891 controller.addFeatureProvisioningChangedCallback(subId, callback);
4892 } finally {
4893 Binder.restoreCallingIdentity(identity);
4894 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004895 }
4896
4897 @Override
4898 public void unregisterFeatureProvisioningChangedCallback(int subId,
4899 IFeatureProvisioningCallback callback) {
4900 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4901 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
4902
4903 final long identity = Binder.clearCallingIdentity();
4904 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4905 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4906 }
4907
joonhunshin91bc1952022-04-29 08:47:15 +00004908 try {
4909 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4910 if (controller == null) {
4911 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
4912 return;
4913 }
4914 controller.removeFeatureProvisioningChangedCallback(subId, callback);
4915 } finally {
4916 Binder.restoreCallingIdentity(identity);
4917 }
joonhunshincffb7fc2021-11-28 07:32:01 +00004918 }
allenwtsu99c623b2020-01-03 18:24:23 +08004919
4920 private void checkModifyPhoneStatePermission(int subId, String message) {
4921 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4922 message);
4923 }
4924
allenwtsu99c623b2020-01-03 18:24:23 +08004925 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004926 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08004927 boolean isProvisioned) {
4928 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
4929
4930 final long identity = Binder.clearCallingIdentity();
4931 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004932 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4933 if (controller == null) {
4934 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
4935 return;
4936 }
4937 controller.setRcsProvisioningStatusForCapability(
4938 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08004939 } finally {
4940 Binder.restoreCallingIdentity(identity);
4941 }
allenwtsu99c623b2020-01-03 18:24:23 +08004942 }
4943
4944
4945 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00004946 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
4947 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4948 mApp, subId, "getRcsProvisioningStatusForCapability");
4949
allenwtsu99c623b2020-01-03 18:24:23 +08004950 final long identity = Binder.clearCallingIdentity();
4951 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004952 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4953 if (controller == null) {
4954 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
4955
4956 // device does not support IMS, this method will return true always.
4957 return true;
4958 }
4959 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08004960 } finally {
4961 Binder.restoreCallingIdentity(identity);
4962 }
4963 }
4964
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004965 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004966 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
4967 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08004968 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00004969
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004970 final long identity = Binder.clearCallingIdentity();
4971 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004972 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4973 if (controller == null) {
4974 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
4975 return;
4976 }
4977 controller.setImsProvisioningStatusForCapability(
4978 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004979 } finally {
4980 Binder.restoreCallingIdentity(identity);
4981 }
4982 }
4983
4984 @Override
4985 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00004986 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4987 mApp, subId, "getProvisioningStatusForCapability");
4988
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004989 final long identity = Binder.clearCallingIdentity();
4990 try {
joonhunshin91bc1952022-04-29 08:47:15 +00004991 ImsProvisioningController controller = ImsProvisioningController.getInstance();
4992 if (controller == null) {
4993 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004994
joonhunshin91bc1952022-04-29 08:47:15 +00004995 // device does not support IMS, this method will return true always.
4996 return true;
4997 }
4998 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004999 } finally {
5000 Binder.restoreCallingIdentity(identity);
5001 }
5002 }
5003
5004 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005005 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
5006 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5007 mApp, subId, "isProvisioningRequiredForCapability");
5008
5009 final long identity = Binder.clearCallingIdentity();
5010 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005011 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5012 if (controller == null) {
5013 loge("isProvisioningRequiredForCapability: Device does not support IMS");
5014
5015 // device does not support IMS, this method will return false
5016 return false;
5017 }
5018 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005019 } finally {
5020 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005021 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005022 }
5023
5024 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005025 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5026 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5027 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005028
joonhunshincffb7fc2021-11-28 07:32:01 +00005029 final long identity = Binder.clearCallingIdentity();
5030 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005031 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5032 if (controller == null) {
5033 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5034
5035 // device does not support IMS, this method will return false
5036 return false;
5037 }
5038 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005039 } finally {
5040 Binder.restoreCallingIdentity(identity);
5041 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005042 }
5043
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005044 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005045 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005046 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5047 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5048 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005049 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5050 mApp, subId, "getImsProvisioningInt");
5051
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005052 final long identity = Binder.clearCallingIdentity();
5053 try {
5054 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005055 int slotId = getSlotIndex(subId);
5056 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5057 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5058 + subId + "' for key:" + key);
5059 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5060 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005061
joonhunshin91bc1952022-04-29 08:47:15 +00005062 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5063 if (controller == null) {
5064 loge("getImsProvisioningInt: Device does not support IMS");
5065
5066 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5067 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5068 }
5069 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005070 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5071 return retVal;
5072 }
5073
calvinpanb5a34062021-02-08 19:59:36 +08005074 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005075 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005076 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5077 + subId + "' for key:" + key);
5078 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005079 } finally {
5080 Binder.restoreCallingIdentity(identity);
5081 }
5082 }
5083
5084 @Override
5085 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005086 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5087 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5088 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005089 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5090 mApp, subId, "getImsProvisioningString");
5091
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005092 final long identity = Binder.clearCallingIdentity();
5093 try {
5094 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005095 int slotId = getSlotIndex(subId);
5096 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5097 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5098 + subId + "' for key:" + key);
5099 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5100 }
calvinpanb5a34062021-02-08 19:59:36 +08005101 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005102 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005103 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5104 + subId + "' for key:" + key);
5105 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005106 } finally {
5107 Binder.restoreCallingIdentity(identity);
5108 }
5109 }
5110
5111 @Override
5112 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005113 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5114 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5115 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005116 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5117 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005118
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005119 final long identity = Binder.clearCallingIdentity();
5120 try {
5121 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005122 int slotId = getSlotIndex(subId);
5123 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5124 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5125 + subId + "' for key:" + key);
5126 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5127 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005128
joonhunshin91bc1952022-04-29 08:47:15 +00005129 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5130 if (controller == null) {
5131 loge("setImsProvisioningInt: Device does not support IMS");
5132
5133 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5134 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5135 }
5136 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005137 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5138 return retVal;
5139 }
5140
calvinpanb5a34062021-02-08 19:59:36 +08005141 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5142 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005143 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005144 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005145 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005146 } finally {
5147 Binder.restoreCallingIdentity(identity);
5148 }
5149 }
5150
5151 @Override
5152 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005153 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5154 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5155 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005156 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5157 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005158 final long identity = Binder.clearCallingIdentity();
5159 try {
5160 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005161 int slotId = getSlotIndex(subId);
5162 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5163 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5164 + subId + "' for key:" + key);
5165 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5166 }
calvinpanb5a34062021-02-08 19:59:36 +08005167 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5168 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005169 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005170 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005171 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005172 } finally {
5173 Binder.restoreCallingIdentity(identity);
5174 }
5175 }
5176
Brad Ebinger919631e2021-06-02 17:46:35 -07005177 /**
5178 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5179 * for the given slot ID or no ImsResolver instance has been created.
5180 * @param slotId The slot ID that the IMS service is created for.
5181 * @throws ImsException If there is no ImsService configured for this slot.
5182 */
5183 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5184 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5185 ImsFeature.FEATURE_MMTEL)) {
5186 throw new ImsException("This subscription does not support MMTEL over IMS",
5187 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5188 }
5189 }
5190
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005191 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005192 int slotId = SubscriptionManager.getSlotIndex(subId);
5193 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005194 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5195 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005196 }
5197 return slotId;
5198 }
5199
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005200 private int getSlotIndex(int subId) {
5201 int slotId = SubscriptionManager.getSlotIndex(subId);
5202 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5203 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5204 }
5205 return slotId;
5206 }
5207
Wink Saville36469e72014-06-11 15:17:00 -07005208 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005209 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005210 */
5211 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005212 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5213 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005214 try {
5215 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5216 } catch (SecurityException se) {
5217 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5218 throw new SecurityException("Package " + callingPackage + " does not belong to "
5219 + Binder.getCallingUid());
5220 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005221 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005222 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005223 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005224 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005225 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005226 mApp, subId, callingPackage, callingFeatureId,
5227 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005228 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5229 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005230
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005231 final long identity = Binder.clearCallingIdentity();
5232 try {
5233 final Phone phone = getPhone(subId);
5234 if (phone != null) {
5235 return phone.getServiceState().getDataNetworkType();
5236 } else {
5237 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5238 }
5239 } finally {
5240 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005241 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005242 }
5243
5244 /**
5245 * Returns the data network type
5246 */
5247 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005248 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005249 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005250 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005251 }
5252
5253 /**
5254 * Returns the data network type for a subId
5255 */
5256 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005257 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5258 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005259 String functionName = "getDataNetworkTypeForSubscriber";
5260 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5261 mApp, functionName)) {
5262 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5263 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5264 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5265 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005266 }
5267
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005268 final long identity = Binder.clearCallingIdentity();
5269 try {
5270 final Phone phone = getPhone(subId);
5271 if (phone != null) {
5272 return phone.getServiceState().getDataNetworkType();
5273 } else {
5274 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5275 }
5276 } finally {
5277 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005279 }
5280
5281 /**
Wink Saville36469e72014-06-11 15:17:00 -07005282 * Returns the Voice network type for a subId
5283 */
5284 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005285 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5286 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005287 String functionName = "getVoiceNetworkTypeForSubscriber";
5288 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5289 mApp, functionName)) {
5290 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5291 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5292 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5293 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005294 }
5295
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005296 final long identity = Binder.clearCallingIdentity();
5297 try {
5298 final Phone phone = getPhone(subId);
5299 if (phone != null) {
5300 return phone.getServiceState().getVoiceNetworkType();
5301 } else {
5302 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5303 }
5304 } finally {
5305 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005306 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005307 }
5308
5309 /**
5310 * @return true if a ICC card is present
5311 */
5312 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005313 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005314 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005315 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005316 }
5317
5318 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005319 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005320 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005321 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005322 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005323 final long identity = Binder.clearCallingIdentity();
5324 try {
5325 final Phone phone = PhoneFactory.getPhone(slotIndex);
5326 if (phone != null) {
5327 return phone.getIccCard().hasIccCard();
5328 } else {
5329 return false;
5330 }
5331 } finally {
5332 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005334 }
5335
5336 /**
5337 * Return if the current radio is LTE on CDMA. This
5338 * is a tri-state return value as for a period of time
5339 * the mode may be unknown.
5340 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005341 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005342 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005343 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005344 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005345 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005346 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5347 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
5348 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005349 }
5350
Sanket Padawe356d7632015-06-22 14:03:32 -07005351 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005352 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
5353 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08005354 try {
5355 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
5356 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005357 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5358 }
5359
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005360 final long identity = Binder.clearCallingIdentity();
5361 try {
5362 final Phone phone = getPhone(subId);
5363 if (phone == null) {
5364 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
5365 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07005366 return TelephonyProperties.lte_on_cdma_device()
5367 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005368 }
5369 } finally {
5370 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005371 }
Wink Saville36469e72014-06-11 15:17:00 -07005372 }
5373
Wink Saville36469e72014-06-11 15:17:00 -07005374 /**
5375 * {@hide}
5376 * Returns Default subId, 0 in the case of single standby.
5377 */
Wink Savilleb564aae2014-10-23 10:18:09 -07005378 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005379 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07005380 }
5381
Shishir Agrawala9f32182016-04-12 12:00:16 -07005382 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005383 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07005384 }
5385
Wink Savilleb564aae2014-10-23 10:18:09 -07005386 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08005387 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005388 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005389
Pengquan Menge92a50d2018-09-21 15:54:48 -07005390 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08005391 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
5392 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
5393 mApp.getOpPackageName(), mApp.getFeatureId());
5394 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07005395 return mSubscriptionController.isActiveSubId(subId);
5396 }
5397
Ihab Awadf2177b72013-11-25 13:33:23 -08005398 /**
5399 * @see android.telephony.TelephonyManager.WifiCallingChoices
5400 */
5401 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005402 final long identity = Binder.clearCallingIdentity();
5403 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005404 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005405 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
5406 getWhenToMakeWifiCallsDefaultPreference());
5407 } finally {
5408 Binder.restoreCallingIdentity(identity);
5409 }
Ihab Awadf2177b72013-11-25 13:33:23 -08005410 }
5411
5412 /**
5413 * @see android.telephony.TelephonyManager.WifiCallingChoices
5414 */
5415 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005416 final long identity = Binder.clearCallingIdentity();
5417 try {
5418 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005419 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005420 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
5421 } finally {
5422 Binder.restoreCallingIdentity(identity);
5423 }
Ihab Awadf9e92732013-12-05 18:02:52 -08005424 }
5425
Sailesh Nepald1e68152013-12-12 19:08:02 -08005426 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07005427 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08005428 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08005429 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08005430
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005431 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
5432 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
5433 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08005434 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005435 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
5436 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08005437 }
5438 return PhoneFactory.getPhone(phoneId);
5439 }
5440
Shishir Agrawal566b7612013-10-28 14:41:00 -07005441 @Override
Derek Tan740e1672017-06-27 14:56:27 -07005442 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08005443 @NonNull IccLogicalChannelRequest request) {
5444 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5445 /*message=*/ "iccOpenLogicalChannel");
5446
5447 if (DBG) log("iccOpenLogicalChannel: request=" + request);
5448 // Verify that the callingPackage in the request belongs to the calling UID
5449 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
5450
5451 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005452 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005453
Rambo Wanga1782702021-11-10 20:15:19 -08005454 private Phone getPhoneFromValidIccLogicalChannelRequest(
5455 @NonNull IccLogicalChannelRequest request, String message) {
5456 Phone phone;
5457 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
5458 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5459 mApp, request.subId, message);
5460 phone = getPhoneFromSubId(request.subId);
5461 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5462 enforceModifyPermission();
5463 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
5464 } else {
5465 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08005466 }
Rambo Wanga1782702021-11-10 20:15:19 -08005467 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08005468 }
5469
5470 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08005471 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005472 final long identity = Binder.clearCallingIdentity();
5473 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005474 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005475 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005476 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5477 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08005478 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
5479 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005480 loge("The calling package is not allowed to access ISD-R.");
5481 throw new SecurityException(
5482 "The calling package is not allowed to access ISD-R.");
5483 }
Derek Tan740e1672017-06-27 14:56:27 -07005484 }
Derek Tan740e1672017-06-27 14:56:27 -07005485
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005486 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08005487 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
5488 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005489 return response;
5490 } finally {
5491 Binder.restoreCallingIdentity(identity);
5492 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005493 }
5494
5495 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08005496 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
5497 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
5498 /*message=*/"iccCloseLogicalChannel");
5499
5500 if (DBG) log("iccCloseLogicalChannel: request=" + request);
5501
5502 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08005503 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005504
Rambo Wanga1782702021-11-10 20:15:19 -08005505 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
5506 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08005507 // before this feature is enabled, this API should only return false if
5508 // the operation fails instead of throwing runtime exception for
5509 // backward-compatibility.
5510 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
5511 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005512 final long identity = Binder.clearCallingIdentity();
5513 try {
Rambo Wanga1782702021-11-10 20:15:19 -08005514 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08005515 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005516 }
Chen Xue9d737e2022-01-01 23:41:31 -08005517 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08005518 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08005519 Boolean success = false;
5520 if (result instanceof RuntimeException) {
5521 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08005522 if (shouldThrowExceptionOnFailure) {
5523 throw (RuntimeException) result;
5524 } else {
5525 return false;
5526 }
Chen Xue9d737e2022-01-01 23:41:31 -08005527 } else if (result instanceof Boolean) {
5528 success = (Boolean) result;
5529 } else {
5530 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
5531 }
Rambo Wanga1782702021-11-10 20:15:19 -08005532 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005533 return success;
5534 } finally {
5535 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005536 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005537 }
5538
5539 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005540 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07005541 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005542 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5543 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08005544 if (DBG) {
5545 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
5546 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
5547 + p3 + " data=" + data);
5548 }
5549 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
5550 command, p1, p2, p3, data);
5551 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005552
Jordan Liu4c733742019-02-28 12:03:40 -08005553 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005554 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
5555 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005556 enforceModifyPermission();
5557 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005558 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
5559 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
5560 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005561 }
5562 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005563 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
5564 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005565 }
5566
5567 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
5568 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005569 final long identity = Binder.clearCallingIdentity();
5570 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07005571 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005572 return "";
5573 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005574
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005575 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005576 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
5577 null /* workSource */);
5578 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07005579
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005580 // Append the returned status code to the end of the response payload.
5581 String s = Integer.toHexString(
5582 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5583 if (response.payload != null) {
5584 s = IccUtils.bytesToHexString(response.payload) + s;
5585 }
5586 return s;
5587 } finally {
5588 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005589 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07005590 }
Jake Hambye994d462014-02-03 13:10:13 -08005591
Evan Charltonc66da362014-05-16 14:06:40 -07005592 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005593 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
5594 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005595 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5596 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005597 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08005598 if (DBG) {
5599 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
5600 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
5601 }
5602 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
5603 cla, command, p1, p2, p3, data);
5604 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005605
Jordan Liu4c733742019-02-28 12:03:40 -08005606 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005607 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
5608 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08005609 enforceModifyPermission();
5610 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5611 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005612 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
5613 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
5614 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08005615 }
5616
5617 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00005618 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
5619 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08005620 }
5621
5622 // open APDU basic channel assuming the caller has sufficient permissions
5623 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
5624 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005625 final long identity = Binder.clearCallingIdentity();
5626 try {
5627 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
5628 && TextUtils.equals(ISDR_AID, data)) {
5629 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005630 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
5631 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005632 if (bestComponent == null
5633 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
5634 loge("The calling package is not allowed to select ISD-R.");
5635 throw new SecurityException(
5636 "The calling package is not allowed to select ISD-R.");
5637 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005638 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08005639
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005640 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08005641 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
5642 null /* workSource */);
5643 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005644
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005645 // Append the returned status code to the end of the response payload.
5646 String s = Integer.toHexString(
5647 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5648 if (response.payload != null) {
5649 s = IccUtils.bytesToHexString(response.payload) + s;
5650 }
5651 return s;
5652 } finally {
5653 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07005654 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005655 }
5656
5657 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005658 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005659 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005660 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5661 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005662
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005663 final long identity = Binder.clearCallingIdentity();
5664 try {
5665 if (DBG) {
5666 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
5667 + p1 + " " + p2 + " " + p3 + ":" + filePath);
5668 }
5669
5670 IccIoResult response =
5671 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
5672 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
5673 subId);
5674
5675 if (DBG) {
5676 log("Exchange SIM_IO [R]" + response);
5677 }
5678
5679 byte[] result = null;
5680 int length = 2;
5681 if (response.payload != null) {
5682 length = 2 + response.payload.length;
5683 result = new byte[length];
5684 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
5685 } else {
5686 result = new byte[length];
5687 }
5688
5689 result[length - 1] = (byte) response.sw2;
5690 result[length - 2] = (byte) response.sw1;
5691 return result;
5692 } finally {
5693 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005694 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005695 }
5696
Nathan Haroldb3014052017-01-25 15:57:32 -08005697 /**
5698 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
5699 * on a particular subscription
5700 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005701 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
5702 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07005703 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005704 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07005705 return null;
5706 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005707
5708 final long identity = Binder.clearCallingIdentity();
5709 try {
5710 if (appType != TelephonyManager.APPTYPE_USIM
5711 && appType != TelephonyManager.APPTYPE_SIM) {
5712 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
5713 return null;
5714 }
5715 Object response = sendRequest(
5716 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
5717 if (response instanceof String[]) {
5718 return (String[]) response;
5719 }
yincheng zhao2737e882019-09-06 17:06:54 -07005720 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005721 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08005722 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005723 } finally {
5724 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08005725 }
Nathan Haroldb3014052017-01-25 15:57:32 -08005726 }
5727
yincheng zhao2737e882019-09-06 17:06:54 -07005728 /**
5729 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
5730 * subscription.
5731 *
5732 * @param subId the id of the subscription.
5733 * @param appType the uicc app type, must be USIM or SIM.
5734 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
5735 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005736 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07005737 * @return number of fplmns that is successfully written to the SIM.
5738 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005739 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
5740 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07005741 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5742 mApp, subId, "setForbiddenPlmns");
5743
yincheng zhao2737e882019-09-06 17:06:54 -07005744 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
5745 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
5746 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
5747 }
5748 if (fplmns == null) {
5749 throw new IllegalArgumentException("Fplmn List provided is null");
5750 }
5751 for (String fplmn : fplmns) {
5752 if (!CellIdentity.isValidPlmn(fplmn)) {
5753 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
5754 }
5755 }
5756 final long identity = Binder.clearCallingIdentity();
5757 try {
5758 Object response = sendRequest(
5759 CMD_SET_FORBIDDEN_PLMNS,
5760 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
5761 subId);
5762 return (int) response;
5763 } finally {
5764 Binder.restoreCallingIdentity(identity);
5765 }
5766 }
5767
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07005768 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005769 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005770 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5771 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07005772
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005773 final long identity = Binder.clearCallingIdentity();
5774 try {
5775 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
5776 if (response.payload == null) {
5777 return "";
5778 }
Evan Charltonc66da362014-05-16 14:06:40 -07005779
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005780 // Append the returned status code to the end of the response payload.
5781 String s = Integer.toHexString(
5782 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
5783 s = IccUtils.bytesToHexString(response.payload) + s;
5784 return s;
5785 } finally {
5786 Binder.restoreCallingIdentity(identity);
5787 }
Evan Charltonc66da362014-05-16 14:06:40 -07005788 }
5789
Jake Hambye994d462014-02-03 13:10:13 -08005790 /**
5791 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5792 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5793 *
5794 * @param itemID the ID of the item to read
5795 * @return the NV item as a String, or null on error.
5796 */
5797 @Override
5798 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005799 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005800 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5801 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005802
5803 final long identity = Binder.clearCallingIdentity();
5804 try {
5805 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07005806 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005807 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
5808 return value;
5809 } finally {
5810 Binder.restoreCallingIdentity(identity);
5811 }
Jake Hambye994d462014-02-03 13:10:13 -08005812 }
5813
5814 /**
5815 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
5816 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
5817 *
5818 * @param itemID the ID of the item to read
5819 * @param itemValue the value to write, as a String
5820 * @return true on success; false on any failure
5821 */
5822 @Override
5823 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07005824 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08005825 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5826 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005827
5828 final long identity = Binder.clearCallingIdentity();
5829 try {
5830 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
5831 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07005832 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005833 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
5834 return success;
5835 } finally {
5836 Binder.restoreCallingIdentity(identity);
5837 }
Jake Hambye994d462014-02-03 13:10:13 -08005838 }
5839
5840 /**
5841 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
5842 * Used for device configuration by some CDMA operators.
5843 *
5844 * @param preferredRoamingList byte array containing the new PRL
5845 * @return true on success; false on any failure
5846 */
5847 @Override
5848 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005849 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5850 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005851
5852 final long identity = Binder.clearCallingIdentity();
5853 try {
5854 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
5855 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
5856 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
5857 return success;
5858 } finally {
5859 Binder.restoreCallingIdentity(identity);
5860 }
Jake Hambye994d462014-02-03 13:10:13 -08005861 }
5862
5863 /**
chen xu6dac5ab2018-10-26 17:39:23 -07005864 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08005865 * Used for device configuration by some CDMA operators.
5866 *
chen xu6dac5ab2018-10-26 17:39:23 -07005867 * @param slotIndex - device slot.
5868 *
Jake Hambye994d462014-02-03 13:10:13 -08005869 * @return true on success; false on any failure
5870 */
5871 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07005872 public boolean resetModemConfig(int slotIndex) {
5873 Phone phone = PhoneFactory.getPhone(slotIndex);
5874 if (phone != null) {
5875 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5876 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005877
chen xu6dac5ab2018-10-26 17:39:23 -07005878 final long identity = Binder.clearCallingIdentity();
5879 try {
5880 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
5881 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
5882 return success;
5883 } finally {
5884 Binder.restoreCallingIdentity(identity);
5885 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005886 }
chen xu6dac5ab2018-10-26 17:39:23 -07005887 return false;
5888 }
5889
5890 /**
5891 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
5892 *
5893 * @param slotIndex - device slot.
5894 *
5895 * @return true on success; false on any failure
5896 */
5897 @Override
5898 public boolean rebootModem(int slotIndex) {
5899 Phone phone = PhoneFactory.getPhone(slotIndex);
5900 if (phone != null) {
5901 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5902 mApp, phone.getSubId(), "rebootModem");
5903
5904 final long identity = Binder.clearCallingIdentity();
5905 try {
5906 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
5907 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
5908 return success;
5909 } finally {
5910 Binder.restoreCallingIdentity(identity);
5911 }
5912 }
5913 return false;
Jake Hambye994d462014-02-03 13:10:13 -08005914 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005915
Brad Ebinger51f743a2017-01-23 13:50:20 -08005916 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005917 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
5918 * {@link #disableIms(int)}.
5919 * @param slotIndex device slot.
5920 */
5921 public void resetIms(int slotIndex) {
5922 enforceModifyPermission();
5923
5924 final long identity = Binder.clearCallingIdentity();
5925 try {
5926 if (mImsResolver == null) {
5927 // may happen if the does not support IMS.
5928 return;
5929 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00005930 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08005931 } finally {
5932 Binder.restoreCallingIdentity(identity);
5933 }
5934 }
5935
5936 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005937 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5938 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005939 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005940 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005941 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005942
5943 final long identity = Binder.clearCallingIdentity();
5944 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005945 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005946 // may happen if the device does not support IMS.
5947 return;
5948 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005949 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005950 } finally {
5951 Binder.restoreCallingIdentity(identity);
5952 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005953 }
5954
5955 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005956 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5957 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005958 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005959 public void disableIms(int slotId) {
5960 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005961
5962 final long identity = Binder.clearCallingIdentity();
5963 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005964 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005965 // may happen if the device does not support IMS.
5966 return;
5967 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005968 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005969 } finally {
5970 Binder.restoreCallingIdentity(identity);
5971 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005972 }
5973
5974 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005975 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5976 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005977 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005978 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005979 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005980 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005981
5982 final long identity = Binder.clearCallingIdentity();
5983 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005984 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005985 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5986 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005987 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005988 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005989 } finally {
5990 Binder.restoreCallingIdentity(identity);
5991 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005992 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005993 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005994 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5995 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005996 @Override
5997 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005998 enforceModifyPermission();
5999
6000 final long identity = Binder.clearCallingIdentity();
6001 try {
6002 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07006003 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006004 } finally {
6005 Binder.restoreCallingIdentity(identity);
6006 }
6007 }
6008
6009 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08006010 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006011 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08006012 */
6013 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
6014 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006015
6016 final long identity = Binder.clearCallingIdentity();
6017 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006018 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006019 // may happen if the device does not support IMS.
6020 return null;
6021 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006022 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006023 } finally {
6024 Binder.restoreCallingIdentity(identity);
6025 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006026 }
6027
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006028 /**
6029 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006030 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006031 */
6032 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6033 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006034
6035 final long identity = Binder.clearCallingIdentity();
6036 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006037 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006038 // may happen if the device does not support IMS.
6039 return null;
6040 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006041 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006042 } finally {
6043 Binder.restoreCallingIdentity(identity);
6044 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006045 }
6046
Brad Ebinger884c07b2018-02-15 16:17:40 -08006047 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006048 * Sets the ImsService Package Name that Telephony will bind to.
6049 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006050 * @param slotIndex the slot ID that the ImsService should bind for.
6051 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006052 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006053 * @param featureTypes An integer array of feature types associated with a packageName.
6054 * @param packageName The name of the package that the current configuration will be replaced
6055 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006056 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006057 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006058 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6059 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006060 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006061 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006062 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006063
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006064 final long identity = Binder.clearCallingIdentity();
6065 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006066 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006067 // may happen if the device does not support IMS.
6068 return false;
6069 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006070 Map<Integer, String> featureConfig = new HashMap<>();
6071 for (int featureType : featureTypes) {
6072 featureConfig.put(featureType, packageName);
6073 }
6074 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6075 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006076 } finally {
6077 Binder.restoreCallingIdentity(identity);
6078 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006079 }
6080
6081 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006082 * Clears any carrier ImsService overrides for the slot index specified that were previously
6083 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6084 *
6085 * This should only be used for testing.
6086 *
6087 * @param slotIndex the slot ID that the ImsService should bind for.
6088 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6089 */
6090 @Override
6091 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006092 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6093 "clearCarrierImsServiceOverride");
6094 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006095 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006096
6097 final long identity = Binder.clearCallingIdentity();
6098 try {
6099 if (mImsResolver == null) {
6100 // may happen if the device does not support IMS.
6101 return false;
6102 }
6103 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6104 } finally {
6105 Binder.restoreCallingIdentity(identity);
6106 }
6107 }
6108
6109 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006110 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006111 *
6112 * @param slotId The slot that the ImsService is associated with.
6113 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6114 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006115 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006116 * @return the package name of the ImsService configuration.
6117 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006118 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6119 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006120 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006121 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6122 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006123
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006124 final long identity = Binder.clearCallingIdentity();
6125 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006126 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006127 // may happen if the device does not support IMS.
6128 return "";
6129 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006130 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006131 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6132 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006133 } finally {
6134 Binder.restoreCallingIdentity(identity);
6135 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006136 }
6137
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006138 /**
6139 * Get the MmTelFeature state associated with the requested subscription id.
6140 * @param subId The subscription that the MmTelFeature is associated with.
6141 * @param callback A callback with an integer containing the
6142 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6143 */
6144 @Override
6145 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6146 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006147 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6148 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6149 "IMS not available on device.");
6150 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006151 final long token = Binder.clearCallingIdentity();
6152 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006153 int slotId = getSlotIndex(subId);
6154 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6155 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6156 + subId + "'");
6157 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6158 }
6159 verifyImsMmTelConfiguredOrThrow(slotId);
6160 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6161 try {
6162 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6163 } catch (RemoteException e) {
6164 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6165 + "Ignore");
6166 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006167 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006168 } catch (ImsException e) {
6169 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006170 } finally {
6171 Binder.restoreCallingIdentity(token);
6172 }
6173 }
6174
Daniel Brightbb5840b2021-01-12 15:48:18 -08006175 /**
6176 * Sets the ims registration state on all valid {@link Phone}s.
6177 */
6178 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006179 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006180
6181 final long identity = Binder.clearCallingIdentity();
6182 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006183 // NOTE: Before S, this method only set the default phone.
6184 for (final Phone phone : PhoneFactory.getPhones()) {
6185 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6186 phone.setImsRegistrationState(registered);
6187 }
6188 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006189 } finally {
6190 Binder.restoreCallingIdentity(identity);
6191 }
Wink Saville36469e72014-06-11 15:17:00 -07006192 }
6193
6194 /**
Stuart Scott54788802015-03-30 13:18:01 -07006195 * Set the network selection mode to automatic.
6196 *
6197 */
6198 @Override
6199 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006200 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6201 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006202
6203 final long identity = Binder.clearCallingIdentity();
6204 try {
shilufc958392020-01-20 11:36:01 -08006205 if (!isActiveSubscription(subId)) {
6206 return;
6207 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006208 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006209 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6210 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006211 } finally {
6212 Binder.restoreCallingIdentity(identity);
6213 }
Stuart Scott54788802015-03-30 13:18:01 -07006214 }
6215
Jack Yud10cdd42020-09-28 20:28:01 -07006216 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006217 * Ask the radio to connect to the input network and change selection mode to manual.
6218 *
6219 * @param subId the id of the subscription.
6220 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6221 * the operator to attach to.
6222 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6223 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6224 * normal network selection next time.
6225 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006226 */
6227 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006228 public boolean setNetworkSelectionModeManual(
6229 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006230 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6231 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006232
Jack Yu285100e2022-12-02 22:48:35 -08006233 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006234 if (!isActiveSubscription(subId)) {
6235 return false;
6236 }
6237
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006238 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006239 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006240 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006241 if (DBG) {
6242 log("setNetworkSelectionModeManual: subId: " + subId
6243 + " operator: " + operatorInfo);
6244 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006245 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6246 } finally {
6247 Binder.restoreCallingIdentity(identity);
6248 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006249 }
shilu84f6e8b2019-12-19 13:58:01 -08006250 /**
6251 * Get the manual network selection
6252 *
6253 * @param subId the id of the subscription.
6254 *
6255 * @return the previously saved user selected PLMN
6256 */
6257 @Override
6258 public String getManualNetworkSelectionPlmn(int subId) {
6259 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006260 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
shilu84f6e8b2019-12-19 13:58:01 -08006261 mApp, subId, "getManualNetworkSelectionPlmn");
6262
6263 final long identity = Binder.clearCallingIdentity();
6264 try {
6265 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006266 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006267 }
6268
6269 final Phone phone = getPhone(subId);
6270 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006271 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006272 }
6273 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6274 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
6275 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
6276 } finally {
6277 Binder.restoreCallingIdentity(identity);
6278 }
6279 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006280
6281 /**
6282 * Scans for available networks.
6283 */
6284 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006285 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6286 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006287 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6288 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006289 LocationAccessPolicy.LocationPermissionResult locationResult =
6290 LocationAccessPolicy.checkLocationPermission(mApp,
6291 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6292 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006293 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006294 .setCallingPid(Binder.getCallingPid())
6295 .setCallingUid(Binder.getCallingUid())
6296 .setMethod("getCellNetworkScanResults")
6297 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006298 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6299 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006300 .build());
6301 switch (locationResult) {
6302 case DENIED_HARD:
6303 throw new SecurityException("Not allowed to access scan results -- location");
6304 case DENIED_SOFT:
6305 return null;
6306 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006307
Pengquan Menga1bb6272018-09-06 09:59:22 -07006308 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006309 try {
6310 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006311 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006312 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006313 } finally {
6314 Binder.restoreCallingIdentity(identity);
6315 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006316 }
6317
6318 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006319 * Get the call forwarding info, given the call forwarding reason.
6320 */
6321 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006322 public void getCallForwarding(int subId, int callForwardingReason,
6323 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006324 enforceReadPrivilegedPermission("getCallForwarding");
6325 long identity = Binder.clearCallingIdentity();
6326 try {
6327 if (DBG) {
6328 log("getCallForwarding: subId " + subId
6329 + " callForwardingReason" + callForwardingReason);
6330 }
Hall Liu27d24262020-09-18 19:04:59 -07006331
6332 Phone phone = getPhone(subId);
6333 if (phone == null) {
6334 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006335 callback.onError(
6336 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006337 } catch (RemoteException e) {
6338 // ignore
6339 }
6340 return;
6341 }
6342
6343 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6344 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6345 @Override
6346 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6347 try {
6348 callback.onCallForwardingInfoAvailable(info);
6349 } catch (RemoteException e) {
6350 // ignore
6351 }
6352 }
6353
6354 @Override
6355 public void onError(int error) {
6356 try {
6357 callback.onError(error);
6358 } catch (RemoteException e) {
6359 // ignore
6360 }
6361 }
6362 });
6363 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006364 } finally {
6365 Binder.restoreCallingIdentity(identity);
6366 }
6367 }
6368
6369 /**
6370 * Sets the voice call forwarding info including status (enable/disable), call forwarding
6371 * reason, the number to forward, and the timeout before the forwarding is attempted.
6372 */
6373 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006374 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
6375 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006376 enforceModifyPermission();
6377 long identity = Binder.clearCallingIdentity();
6378 try {
6379 if (DBG) {
6380 log("setCallForwarding: subId " + subId
6381 + " callForwardingInfo" + callForwardingInfo);
6382 }
Hall Liu27d24262020-09-18 19:04:59 -07006383
6384 Phone phone = getPhone(subId);
6385 if (phone == null) {
6386 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006387 callback.accept(
6388 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006389 } catch (RemoteException e) {
6390 // ignore
6391 }
6392 return;
6393 }
6394
6395 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
6396 FunctionalUtils.ignoreRemoteException(callback::accept));
6397
6398 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08006399 } finally {
6400 Binder.restoreCallingIdentity(identity);
6401 }
6402 }
6403
6404 /**
Hall Liu27d24262020-09-18 19:04:59 -07006405 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006406 */
6407 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006408 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08006409 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08006410 long identity = Binder.clearCallingIdentity();
6411 try {
Hall Liu27d24262020-09-18 19:04:59 -07006412 Phone phone = getPhone(subId);
6413 if (phone == null) {
6414 try {
6415 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6416 } catch (RemoteException e) {
6417 // ignore
6418 }
6419 return;
6420 }
SongFerngWang0e767992021-03-31 22:08:45 +08006421 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6422 PersistableBundle c = configManager.getConfigForSubId(subId);
6423 boolean requireUssd = c.getBoolean(
6424 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006425
Shuo Qian4a594052020-01-23 11:59:30 -08006426 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08006427 if (requireUssd) {
6428 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6429 getSubscriptionCarrierId(subId));
6430 String newUssdCommand = "";
6431 try {
6432 newUssdCommand = carrierXmlParser.getFeature(
6433 CarrierXmlParser.FEATURE_CALL_WAITING)
6434 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
6435 } catch (NullPointerException e) {
6436 loge("Failed to generate USSD number" + e);
6437 }
6438 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6439 mMainThreadHandler, callback, carrierXmlParser,
6440 CarrierXmlParser.SsEntry.SSAction.QUERY);
6441 final String ussdCommand = newUssdCommand;
6442 Executors.newSingleThreadExecutor().execute(() -> {
6443 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6444 });
6445 } else {
6446 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
6447 callback::accept);
6448 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
6449 }
Shuo Qian4a594052020-01-23 11:59:30 -08006450 } finally {
6451 Binder.restoreCallingIdentity(identity);
6452 }
6453 }
6454
6455 /**
Hall Liu27d24262020-09-18 19:04:59 -07006456 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08006457 */
6458 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006459 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006460 enforceModifyPermission();
6461 long identity = Binder.clearCallingIdentity();
6462 try {
Hall Liu27d24262020-09-18 19:04:59 -07006463 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
6464
6465 Phone phone = getPhone(subId);
6466 if (phone == null) {
6467 try {
6468 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
6469 } catch (RemoteException e) {
6470 // ignore
6471 }
6472 return;
6473 }
6474
SongFerngWang0e767992021-03-31 22:08:45 +08006475 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
6476 PersistableBundle c = configManager.getConfigForSubId(subId);
6477 boolean requireUssd = c.getBoolean(
6478 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07006479
SongFerngWang0e767992021-03-31 22:08:45 +08006480 if (DBG) log("getCallWaitingStatus: subId " + subId);
6481 if (requireUssd) {
6482 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
6483 getSubscriptionCarrierId(subId));
6484 CarrierXmlParser.SsEntry.SSAction ssAction =
6485 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
6486 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
6487 String newUssdCommand = "";
6488 try {
6489 newUssdCommand = carrierXmlParser.getFeature(
6490 CarrierXmlParser.FEATURE_CALL_WAITING)
6491 .makeCommand(ssAction, null);
6492 } catch (NullPointerException e) {
6493 loge("Failed to generate USSD number" + e);
6494 }
6495 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
6496 mMainThreadHandler, callback, carrierXmlParser, ssAction);
6497 final String ussdCommand = newUssdCommand;
6498 Executors.newSingleThreadExecutor().execute(() -> {
6499 handleUssdRequest(subId, ussdCommand, wrappedCallback);
6500 });
6501 } else {
6502 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
6503 FunctionalUtils.ignoreRemoteException(callback::accept));
6504
6505 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
6506 }
Shuo Qian4a594052020-01-23 11:59:30 -08006507 } finally {
6508 Binder.restoreCallingIdentity(identity);
6509 }
6510 }
6511
6512 /**
yinxub1bed742017-04-17 11:45:04 -07006513 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07006514 *
yinxub1bed742017-04-17 11:45:04 -07006515 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006516 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
6517 * location related information which will be sent if the caller already possess
6518 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07006519 * @param request contains the radio access networks with bands/channels to scan
6520 * @param messenger callback messenger for scan results or errors
6521 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07006522 * @return the id of the requested scan which can be used to stop the scan.
6523 */
6524 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006525 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
6526 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006527 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006528 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6529 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08006530 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006531 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
6532 if (!renounceFineLocationAccess) {
6533 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
6534 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6535 .setCallingPackage(callingPackage)
6536 .setCallingFeatureId(callingFeatureId)
6537 .setCallingPid(Binder.getCallingPid())
6538 .setCallingUid(Binder.getCallingUid())
6539 .setMethod("requestNetworkScan")
6540 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
6541 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6542 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
6543 .build());
6544 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08006545 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07006546 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
6547 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08006548 if (e != null) {
6549 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
6550 throw e;
6551 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07006552 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08006553 return TelephonyScanManager.INVALID_SCAN_ID;
6554 }
6555 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006556 }
Hall Liu912dfd32019-04-25 14:02:26 -07006557 int callingUid = Binder.getCallingUid();
6558 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07006559 final long identity = Binder.clearCallingIdentity();
6560 try {
6561 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08006562 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07006563 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07006564 } finally {
6565 Binder.restoreCallingIdentity(identity);
6566 }
yinxu504e1392017-04-12 16:03:22 -07006567 }
6568
Hall Liub2ac8ef2019-02-28 15:56:23 -08006569 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07006570 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00006571 boolean hasCarrierPriv;
6572 final long identity = Binder.clearCallingIdentity();
6573 try {
6574 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
6575 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6576 } finally {
6577 Binder.restoreCallingIdentity(identity);
6578 }
Hall Liu558027f2019-05-15 19:14:05 -07006579 boolean hasNetworkScanPermission =
6580 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
6581 == PERMISSION_GRANTED;
6582
6583 if (!hasCarrierPriv && !hasNetworkScanPermission) {
6584 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
6585 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08006586 }
6587
6588 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
6589 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08006590 if (ras.getChannels() != null && ras.getChannels().length > 0) {
6591 return new SecurityException("Specific channels must not be"
6592 + " scanned without location access.");
6593 }
6594 }
6595 }
6596
Hall Liub2ac8ef2019-02-28 15:56:23 -08006597 return null;
6598 }
6599
yinxu504e1392017-04-12 16:03:22 -07006600 /**
6601 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07006602 *
6603 * @param subId id of the subscription
6604 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07006605 */
6606 @Override
6607 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006608 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6609 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006610
Hall Liu912dfd32019-04-25 14:02:26 -07006611 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006612 final long identity = Binder.clearCallingIdentity();
6613 try {
Hall Liu912dfd32019-04-25 14:02:26 -07006614 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006615 } finally {
6616 Binder.restoreCallingIdentity(identity);
6617 }
yinxu504e1392017-04-12 16:03:22 -07006618 }
6619
6620 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08006621 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07006622 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08006623 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07006624 */
6625 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08006626 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08006627 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006628 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08006629 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006630
6631 final long identity = Binder.clearCallingIdentity();
6632 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006633 if (DBG) log("getAllowedNetworkTypesBitmask");
6634 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
6635 int networkTypesBitmask = (result != null ? result[0] : -1);
6636 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
6637 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006638 } finally {
6639 Binder.restoreCallingIdentity(identity);
6640 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006641 }
6642
6643 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006644 * Get the allowed network types for certain reason.
6645 *
6646 * @param subId the id of the subscription.
6647 * @param reason the reason the allowed network type change is taking place
6648 * @return the allowed network types.
6649 */
6650 @Override
6651 public long getAllowedNetworkTypesForReason(int subId,
6652 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07006653 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006654 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006655 final long identity = Binder.clearCallingIdentity();
6656 try {
6657 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
6658 } finally {
6659 Binder.restoreCallingIdentity(identity);
6660 }
6661 }
6662
6663 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07006664 * Enable/Disable E-UTRA-NR Dual Connectivity
6665 * @param subId subscription id of the sim card
6666 * @param nrDualConnectivityState expected NR dual connectivity state
6667 * This can be passed following states
6668 * <ol>
6669 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
6670 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
6671 * <li>Disable NR dual connectivity and force secondary cell to be released
6672 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
6673 * </ol>
6674 * @return operation result.
6675 */
6676 @Override
6677 public int setNrDualConnectivityState(int subId,
6678 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
6679 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6680 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006681 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006682 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6683 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
6684 }
6685
Sooraj Sasindran37444802020-08-11 10:40:43 -07006686 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6687 final long identity = Binder.clearCallingIdentity();
6688 try {
6689 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
6690 nrDualConnectivityState, subId,
6691 workSource);
6692 if (DBG) log("enableNRDualConnectivity result: " + result);
6693 return result;
6694 } finally {
6695 Binder.restoreCallingIdentity(identity);
6696 }
6697 }
6698
6699 /**
6700 * Is E-UTRA-NR Dual Connectivity enabled
6701 * @return true if dual connectivity is enabled else false
6702 */
6703 @Override
6704 public boolean isNrDualConnectivityEnabled(int subId) {
6705 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07006706 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07006707 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07006708 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08006709 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
6710 return false;
6711 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07006712 WorkSource workSource = getWorkSource(Binder.getCallingUid());
6713 final long identity = Binder.clearCallingIdentity();
6714 try {
6715 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
6716 null, subId, workSource);
6717 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
6718 return isEnabled;
6719 } finally {
6720 Binder.restoreCallingIdentity(identity);
6721 }
6722 }
6723
6724 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006725 * Set the allowed network types of the device and
6726 * provide the reason triggering the allowed network change.
6727 *
6728 * @param subId the id of the subscription.
6729 * @param reason the reason the allowed network type change is taking place
6730 * @param allowedNetworkTypes the allowed network types.
6731 * @return true on success; false on any failure.
6732 */
6733 @Override
6734 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08006735 @TelephonyManager.AllowedNetworkTypesReason int reason,
6736 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006737 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6738 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00006739 // If the caller only has carrier privileges, then they should not be able to override
6740 // any network types which were set for security reasons.
6741 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
6742 != PERMISSION_GRANTED
6743 && (reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G
6744 || reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER_RESTRICTIONS)) {
6745 throw new SecurityException(
6746 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
6747 + " reason "
6748 + reason);
6749 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08006750 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
SongFerngWang7ffc2732021-04-15 19:46:33 +08006751 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
6752 return false;
6753 }
6754 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
6755 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08006756 return false;
6757 }
6758
Jack Yue37dd262022-12-16 11:53:37 -08006759 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason=" + reason + " value: "
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006760 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
6761
Jack Yue37dd262022-12-16 11:53:37 -08006762 Phone phone = getPhone(subId);
6763 if (phone == null) {
6764 return false;
6765 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006766
Jack Yue37dd262022-12-16 11:53:37 -08006767 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006768 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
6769 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08006770 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08006771
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006772 final long identity = Binder.clearCallingIdentity();
6773 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08006774 Boolean success = (Boolean) sendRequest(
6775 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
6776 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
6777
6778 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
6779 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07006780 } finally {
6781 Binder.restoreCallingIdentity(identity);
6782 }
6783 }
6784
6785 /**
Miaoa84611c2019-03-15 09:21:10 +08006786 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08006787 *
Miaoa84611c2019-03-15 09:21:10 +08006788 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07006789 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08006790 * @hide
6791 */
6792 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08006793 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006794 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006795 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08006796 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006797 try {
Miaoa84611c2019-03-15 09:21:10 +08006798 if (phone != null) {
6799 return phone.hasMatchedTetherApnSetting();
6800 } else {
6801 return false;
6802 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006803 } finally {
6804 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08006805 }
Junda Liu475951f2014-11-07 16:45:03 -08006806 }
6807
6808 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08006809 * Get the user enabled state of Mobile Data.
6810 *
6811 * TODO: remove and use isUserDataEnabled.
6812 * This can't be removed now because some vendor codes
6813 * calls through ITelephony directly while they should
6814 * use TelephonyManager.
6815 *
6816 * @return true on enabled
6817 */
6818 @Override
6819 public boolean getDataEnabled(int subId) {
6820 return isUserDataEnabled(subId);
6821 }
6822
6823 /**
6824 * Get whether mobile data is enabled per user setting.
6825 *
6826 * There are other factors deciding whether mobile data is actually enabled, but they are
6827 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07006828 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006829 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
6830 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07006831 *
6832 * @return {@code true} if data is enabled else {@code false}
6833 */
6834 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08006835 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006836 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07006837 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006838 try {
6839 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6840 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006841 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006842 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
6843 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006844 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006845 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006846 mApp, subId, functionName);
6847
Robert Greenwalt646120a2014-05-23 11:54:03 -07006848 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006849
6850 final long identity = Binder.clearCallingIdentity();
6851 try {
Jack Yu285100e2022-12-02 22:48:35 -08006852 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006853 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
6854 Phone phone = PhoneFactory.getPhone(phoneId);
6855 if (phone != null) {
6856 boolean retVal = phone.isUserDataEnabled();
6857 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
6858 return retVal;
6859 } else {
6860 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
6861 return false;
6862 }
6863 } finally {
6864 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08006865 }
6866 }
6867
6868 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08006869 * Checks if the device is capable of mobile data by considering whether whether the
6870 * user has enabled mobile data, whether the carrier has enabled mobile data, and
6871 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08006872 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08006873 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08006874 */
6875 @Override
6876 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006877 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006878 try {
6879 try {
6880 mApp.enforceCallingOrSelfPermission(
6881 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006882 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006883 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006884 try {
6885 mApp.enforceCallingOrSelfPermission(
6886 android.Manifest.permission.READ_PHONE_STATE,
6887 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006888 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006889 mApp.enforceCallingOrSelfPermission(
6890 permission.READ_BASIC_PHONE_STATE, functionName);
6891 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006892 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006893 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006894 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006895 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006896
6897 final long identity = Binder.clearCallingIdentity();
6898 try {
Jack Yu285100e2022-12-02 22:48:35 -08006899 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006900 Phone phone = PhoneFactory.getPhone(phoneId);
6901 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07006902 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08006903 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006904 return retVal;
6905 } else {
6906 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
6907 return false;
6908 }
6909 } finally {
6910 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08006911 }
Robert Greenwalted86e582014-05-21 20:03:20 -07006912 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006913
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006914 /**
6915 * Check if data is enabled for a specific reason
6916 * @param subId Subscription index
6917 * @param reason the reason the data enable change is taking place
6918 * @return {@code true} if the overall data is enabled; {@code false} if not.
6919 */
6920 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006921 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006922 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006923 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006924 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006925 try {
6926 mApp.enforceCallingOrSelfPermission(
6927 android.Manifest.permission.ACCESS_NETWORK_STATE,
6928 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006929 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006930 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
6931 functionName);
6932 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006933 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006934 try {
6935 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006936 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07006937 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006938 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07006939 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08006940 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006941 }
6942
6943
6944 final long identity = Binder.clearCallingIdentity();
6945 try {
Jack Yu285100e2022-12-02 22:48:35 -08006946 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006947 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006948 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006949 + " reason=" + reason);
6950 }
6951 Phone phone = PhoneFactory.getPhone(phoneId);
6952 if (phone != null) {
6953 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07006954 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006955 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006956 return retVal;
6957 } else {
6958 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006959 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006960 + subId + " retVal=false");
6961 }
6962 return false;
6963 }
6964 } finally {
6965 Binder.restoreCallingIdentity(identity);
6966 }
6967 }
6968
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006969 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006970 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006971 // No permission needed; this only lets the caller inspect their own status.
6972 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006973 }
Junda Liu29340342014-07-10 15:23:27 -07006974
6975 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006976 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006977 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006978 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
6979 }
6980
6981 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
6982 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006983 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006984 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006985 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6986 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006987 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
6988 if (cpt == null) {
6989 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006990 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6991 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08006992 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006993 }
6994
6995 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006996 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07006997 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07006998 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006999 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07007000 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007001 Phone phone = getPhone(subId);
7002 if (phone == null) {
7003 loge("checkCarrierPrivilegesForPackage: Invalid subId");
7004 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7005 }
7006 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7007 if (cpt == null) {
7008 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07007009 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7010 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007011 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007012 }
7013
7014 @Override
7015 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007016 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007017 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7018 }
7019
7020 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007021 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007022 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007023 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007024 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007025 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7026 Phone phone = PhoneFactory.getPhone(phoneId);
7027 if (phone == null) {
7028 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007029 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007030 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7031 if (cpt == null) {
7032 continue;
7033 }
7034 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007035 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7036 break;
7037 }
7038 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007039 return result;
Junda Liu29340342014-07-10 15:23:27 -07007040 }
Derek Tan89e89d42014-07-08 17:00:10 -07007041
7042 @Override
Junda Liue64de782015-04-16 17:19:16 -07007043 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007044 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007045 Phone phone = PhoneFactory.getPhone(phoneId);
7046 if (phone == null) {
7047 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007048 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007049 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7050 if (cpt == null) {
7051 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007052 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007053 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007054 }
7055
Amith Yamasani6e118872016-02-19 12:53:51 -08007056 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007057 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007058 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007059 Phone phone = PhoneFactory.getPhone(phoneId);
7060 if (phone == null) {
7061 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007062 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007063 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7064 if (cpt == null) {
7065 return Collections.emptyList();
7066 }
7067 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007068 }
7069
chen xuf7e9fe82019-05-09 19:31:02 -07007070 @Override
7071 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007072 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007073 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007074 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007075 try {
7076 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7077 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7078 }
7079 } finally {
7080 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007081 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007082 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007083 }
7084
Rambo Wang6812ffb2022-03-15 16:54:17 -07007085 @Override
7086 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7087 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7088
7089 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7090 if (phone == null) {
7091 return null;
7092 }
7093 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7094 if (cpt == null) {
7095 return null;
7096 }
7097 return cpt.getCarrierServicePackageName();
7098 }
7099
Wink Savilleb564aae2014-10-23 10:18:09 -07007100 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007101 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007102 UiccPort port = phone == null ? null : phone.getUiccPort();
7103 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007104 return null;
7105 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007106 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007107 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007108 return null;
7109 }
7110 return iccId;
7111 }
7112
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007113 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007114 public void setCallComposerStatus(int subId, int status) {
7115 enforceModifyPermission();
7116
7117 final long identity = Binder.clearCallingIdentity();
7118 try {
7119 Phone phone = getPhone(subId);
7120 if (phone != null) {
7121 Phone defaultPhone = phone.getImsPhone();
7122 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7123 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7124 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007125 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7126 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007127 }
7128 }
Shuo Qian284ae752020-12-22 19:10:14 -08007129 } catch (ImsException e) {
7130 throw new ServiceSpecificException(e.getCode());
7131 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007132 Binder.restoreCallingIdentity(identity);
7133 }
7134 }
7135
7136 @Override
7137 public int getCallComposerStatus(int subId) {
7138 enforceReadPrivilegedPermission("getCallComposerStatus");
7139
7140 final long identity = Binder.clearCallingIdentity();
7141 try {
7142 Phone phone = getPhone(subId);
7143 if (phone != null) {
7144 Phone defaultPhone = phone.getImsPhone();
7145 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7146 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7147 return imsPhone.getCallComposerStatus();
7148 }
7149 }
7150 } finally {
7151 Binder.restoreCallingIdentity(identity);
7152 }
7153 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7154 }
7155
7156 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007157 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7158 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007159 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007160 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007161
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007162 final long identity = Binder.clearCallingIdentity();
7163 try {
7164 final String iccId = getIccId(subId);
7165 final Phone phone = getPhone(subId);
7166 if (phone == null) {
7167 return false;
7168 }
7169 final String subscriberId = phone.getSubscriberId();
7170
7171 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007172 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007173 + subscriberId + " to " + number);
7174 }
7175
7176 if (TextUtils.isEmpty(iccId)) {
7177 return false;
7178 }
7179
7180 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7181
7182 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7183 if (alphaTag == null) {
7184 editor.remove(alphaTagPrefKey);
7185 } else {
7186 editor.putString(alphaTagPrefKey, alphaTag);
7187 }
7188
7189 // Record both the line number and IMSI for this ICCID, since we need to
7190 // track all merged IMSIs based on line number
7191 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7192 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7193 if (number == null) {
7194 editor.remove(numberPrefKey);
7195 editor.remove(subscriberPrefKey);
7196 } else {
7197 editor.putString(numberPrefKey, number);
7198 editor.putString(subscriberPrefKey, subscriberId);
7199 }
7200
7201 editor.commit();
7202 return true;
7203 } finally {
7204 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007205 }
Derek Tan7226c842014-07-02 17:42:23 -07007206 }
7207
7208 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007209 public String getLine1NumberForDisplay(int subId, String callingPackage,
7210 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007211 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007212 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007213 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007214 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007215 return null;
7216 }
Derek Tan97ebb422014-09-05 16:55:38 -07007217
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007218 final long identity = Binder.clearCallingIdentity();
7219 try {
7220 String iccId = getIccId(subId);
7221 if (iccId != null) {
7222 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7223 if (DBG_MERGE) {
7224 log("getLine1NumberForDisplay returning "
7225 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7226 }
7227 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007228 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007229 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7230 return null;
7231 } finally {
7232 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007233 }
Derek Tan7226c842014-07-02 17:42:23 -07007234 }
7235
7236 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007237 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7238 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007239 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007240 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007241 return null;
7242 }
Derek Tan97ebb422014-09-05 16:55:38 -07007243
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007244 final long identity = Binder.clearCallingIdentity();
7245 try {
7246 String iccId = getIccId(subId);
7247 if (iccId != null) {
7248 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7249 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7250 }
7251 return null;
7252 } finally {
7253 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007254 }
Derek Tan7226c842014-07-02 17:42:23 -07007255 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007256
7257 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007258 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7259 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007260 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7261 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007262 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007263 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007264 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007265 return null;
7266 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007267
Jordan Liub49b04b2019-05-06 14:45:15 -07007268 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7269 // the process, where TelephonyManager was instantiated.
7270 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007271 final long identity = Binder.clearCallingIdentity();
7272 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007273 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007274 final TelephonyManager tele = TelephonyManager.from(context);
7275 final SubscriptionManager sub = SubscriptionManager.from(context);
7276
7277 // Figure out what subscribers are currently active
7278 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007279
Jordan Liub49b04b2019-05-06 14:45:15 -07007280 // Only consider subs which match the current subId
7281 // This logic can be simplified. See b/131189269 for progress.
7282 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007283 activeSubscriberIds.add(tele.getSubscriberId(subId));
7284 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007285
7286 // First pass, find a number override for an active subscriber
7287 String mergeNumber = null;
7288 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7289 for (String key : prefs.keySet()) {
7290 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7291 final String subscriberId = (String) prefs.get(key);
7292 if (activeSubscriberIds.contains(subscriberId)) {
7293 final String iccId = key.substring(
7294 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7295 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7296 mergeNumber = (String) prefs.get(numberKey);
7297 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007298 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007299 + " for active subscriber " + subscriberId);
7300 }
7301 if (!TextUtils.isEmpty(mergeNumber)) {
7302 break;
7303 }
7304 }
7305 }
7306 }
7307
7308 // Shortcut when no active merged subscribers
7309 if (TextUtils.isEmpty(mergeNumber)) {
7310 return null;
7311 }
7312
7313 // Second pass, find all subscribers under that line override
7314 final ArraySet<String> result = new ArraySet<>();
7315 for (String key : prefs.keySet()) {
7316 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7317 final String number = (String) prefs.get(key);
7318 if (mergeNumber.equals(number)) {
7319 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7320 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7321 final String subscriberId = (String) prefs.get(subscriberKey);
7322 if (!TextUtils.isEmpty(subscriberId)) {
7323 result.add(subscriberId);
7324 }
7325 }
7326 }
7327 }
7328
7329 final String[] resultArray = result.toArray(new String[result.size()]);
7330 Arrays.sort(resultArray);
7331 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007332 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007333 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7334 }
7335 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007336 } finally {
7337 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007338 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007339 }
7340
7341 @Override
zoey chen38003472019-12-13 17:16:31 +08007342 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7343 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007344
7345 final long identity = Binder.clearCallingIdentity();
7346 try {
7347 final TelephonyManager telephonyManager = mApp.getSystemService(
7348 TelephonyManager.class);
7349 String subscriberId = telephonyManager.getSubscriberId(subId);
7350 if (subscriberId == null) {
7351 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08007352 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07007353 + subId);
7354 }
7355 return null;
7356 }
7357
Jack Yu285100e2022-12-02 22:48:35 -08007358 ParcelUuid groupUuid;
7359 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7360 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
7361 .getSubscriptionInfo(subId);
7362 groupUuid = info.getGroupUuid();
7363 } else {
7364 final SubscriptionInfo info = mSubscriptionController
7365 .getSubscriptionInfo(subId);
7366 groupUuid = info.getGroupUuid();
7367 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007368 // If it doesn't belong to any group, return just subscriberId of itself.
7369 if (groupUuid == null) {
7370 return new String[]{subscriberId};
7371 }
7372
7373 // Get all subscriberIds from the group.
7374 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08007375 List<SubscriptionInfo> groupInfos;
7376 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7377 groupInfos = SubscriptionManagerService.getInstance()
7378 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7379 mApp.getAttributionTag());
7380 } else {
7381 groupInfos = mSubscriptionController
7382 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
7383 mApp.getAttributionTag());
7384 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07007385 for (SubscriptionInfo subInfo : groupInfos) {
7386 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
7387 if (subscriberId != null) {
7388 mergedSubscriberIds.add(subscriberId);
7389 }
7390 }
7391
7392 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
7393 } finally {
7394 Binder.restoreCallingIdentity(identity);
7395
7396 }
7397 }
7398
7399 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007400 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007401 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007402 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007403
7404 final long identity = Binder.clearCallingIdentity();
7405 try {
7406 final Phone phone = getPhone(subId);
7407 return phone == null ? false : phone.setOperatorBrandOverride(brand);
7408 } finally {
7409 Binder.restoreCallingIdentity(identity);
7410 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007411 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05007412
7413 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007414 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007415 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
7416 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007417 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
7418 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007419
7420 final long identity = Binder.clearCallingIdentity();
7421 try {
7422 final Phone phone = getPhone(subId);
7423 if (phone == null) {
7424 return false;
7425 }
7426 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
7427 cdmaNonRoamingList);
7428 } finally {
7429 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007430 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08007431 }
7432
7433 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007434 @Deprecated
7435 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
7436 enforceModifyPermission();
7437
7438 int returnValue = 0;
7439 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07007440 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00007441 if(result.exception == null) {
7442 if (result.result != null) {
7443 byte[] responseData = (byte[])(result.result);
7444 if(responseData.length > oemResp.length) {
7445 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
7446 responseData.length + "bytes. Buffer Size is " +
7447 oemResp.length + "bytes.");
7448 }
7449 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
7450 returnValue = responseData.length;
7451 }
7452 } else {
7453 CommandException ex = (CommandException) result.exception;
7454 returnValue = ex.getCommandError().ordinal();
7455 if(returnValue > 0) returnValue *= -1;
7456 }
7457 } catch (RuntimeException e) {
7458 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
7459 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
7460 if(returnValue > 0) returnValue *= -1;
7461 }
7462
7463 return returnValue;
7464 }
7465
7466 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07007467 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007468 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07007469 try {
7470 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007471 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07007472 mApp, phone.getSubId(), "getRadioAccessFamily");
7473 } catch (SecurityException e) {
7474 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
7475 throw e;
7476 }
chen xub97461a2018-10-26 14:17:57 -07007477 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08007478 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07007479 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08007480 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007481 final long identity = Binder.clearCallingIdentity();
7482 try {
chen xub97461a2018-10-26 14:17:57 -07007483 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007484 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07007485 mApp, phone.getSubId(), "getRadioAccessFamily");
7486 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007487 } finally {
7488 Binder.restoreCallingIdentity(identity);
7489 }
chen xub97461a2018-10-26 14:17:57 -07007490 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07007491 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007492
7493 @Override
Hall Liu82694d52020-12-11 18:22:04 -08007494 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08007495 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08007496 try {
7497 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
7498 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007499 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007500 }
7501 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07007502 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08007503 }
7504 RoleManager rm = mApp.getSystemService(RoleManager.class);
7505 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
7506 if (!dialerRoleHolders.contains(callingPackage)) {
7507 throw new SecurityException("App must be the dialer role holder to"
7508 + " upload a call composer pic");
7509 }
7510
7511 Executors.newSingleThreadExecutor().execute(() -> {
7512 ByteArrayOutputStream output = new ByteArrayOutputStream(
7513 (int) TelephonyManager.getMaximumCallComposerPictureSize());
7514 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
7515 boolean readUntilEnd = false;
7516 int totalBytesRead = 0;
7517 byte[] buffer = new byte[16 * 1024];
7518 while (true) {
7519 int numRead;
7520 try {
7521 numRead = input.read(buffer);
7522 } catch (IOException e) {
7523 try {
7524 fd.checkError();
7525 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
7526 null);
7527 } catch (IOException e1) {
7528 // This means that the other side closed explicitly with an error. If this
7529 // happens, log and ignore.
7530 loge("Remote end of call composer picture pipe closed: " + e1);
7531 }
7532 break;
7533 }
7534 if (numRead == -1) {
7535 readUntilEnd = true;
7536 break;
7537 }
7538 totalBytesRead += numRead;
7539 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
7540 loge("Too many bytes read for call composer picture: " + totalBytesRead);
7541 try {
7542 input.close();
7543 } catch (IOException e) {
7544 // ignore
7545 }
7546 break;
7547 }
7548 output.write(buffer, 0, numRead);
7549 }
7550 // Generally, the remote end will close the file descriptors. The only case where we
7551 // close is above, where the picture size is too big.
7552
7553 try {
7554 fd.checkError();
7555 } catch (IOException e) {
7556 loge("Remote end for call composer closed with an error: " + e);
7557 return;
7558 }
7559
Hall Liuaa4211e2021-01-20 15:43:39 -08007560 if (!readUntilEnd) {
7561 loge("Did not finish reading entire image; aborting");
7562 return;
7563 }
Hall Liu82694d52020-12-11 18:22:04 -08007564
Hall Liuaa4211e2021-01-20 15:43:39 -08007565 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
7566 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
7567 new CallComposerPictureTransfer.Factory() {},
7568 imageData,
7569 (result) -> {
7570 if (result.first != null) {
7571 ParcelUuid parcelUuid = new ParcelUuid(result.first);
7572 Bundle outputResult = new Bundle();
7573 outputResult.putParcelable(
7574 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
7575 callback.send(TelephonyManager.CallComposerException.SUCCESS,
7576 outputResult);
7577 } else {
7578 callback.send(result.second, null);
7579 }
7580 }
7581 );
Hall Liu82694d52020-12-11 18:22:04 -08007582 });
7583 }
7584
7585 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07007586 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007587 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07007588 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007589
7590 final long identity = Binder.clearCallingIdentity();
7591 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007592 ImsManager.getInstance(defaultPhone.getContext(),
7593 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007594 } finally {
7595 Binder.restoreCallingIdentity(identity);
7596 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007597 }
7598
7599 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007600 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007601 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007602 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
7603 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00007604 return false;
7605 }
Svet Ganovb320e182015-04-16 12:30:10 -07007606
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007607 final long identity = Binder.clearCallingIdentity();
7608 try {
7609 // Check the user preference and the system-level IMS setting. Even if the user has
7610 // enabled video calling, if IMS is disabled we aren't able to support video calling.
7611 // In the long run, we may instead need to check if there exists a connection service
7612 // which can support video calling.
7613 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007614 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007615 return imsManager.isVtEnabledByPlatform()
7616 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
7617 && imsManager.isVtEnabledByUser();
7618 } finally {
7619 Binder.restoreCallingIdentity(identity);
7620 }
Andrew Leedf14ead2014-10-17 14:22:52 -07007621 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06007622
Andrew Leea1239f22015-03-02 17:44:07 -08007623 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007624 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
7625 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007626 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007627 mApp, subId, callingPackage, callingFeatureId,
7628 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007629 return false;
7630 }
7631
7632 final long identity = Binder.clearCallingIdentity();
7633 try {
7634 CarrierConfigManager configManager =
7635 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007636 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007637 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
7638 } finally {
7639 Binder.restoreCallingIdentity(identity);
7640 }
Andrew Leea1239f22015-03-02 17:44:07 -08007641 }
7642
7643 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007644 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007645 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007646 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007647 return false;
7648 }
7649
7650 final long identity = Binder.clearCallingIdentity();
7651 try {
7652 CarrierConfigManager configManager =
7653 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007654 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007655 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
7656 } finally {
7657 Binder.restoreCallingIdentity(identity);
7658 }
Andrew Leea1239f22015-03-02 17:44:07 -08007659 }
7660
Andrew Lee9431b832015-03-09 18:46:45 -07007661 @Override
7662 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007663 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08007664 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07007665 }
7666
7667 @Override
7668 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007669 final long identity = Binder.clearCallingIdentity();
7670 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007671 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007672 } finally {
7673 Binder.restoreCallingIdentity(identity);
7674 }
Andrew Lee9431b832015-03-09 18:46:45 -07007675 }
7676
Hall Liuf6668912018-10-31 17:05:23 -07007677 /**
7678 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
7679 * support for the feature and device firmware support.
7680 *
7681 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
7682 */
7683 @Override
7684 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007685 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007686 final Phone phone = getPhone(subscriptionId);
7687 if (phone == null) {
7688 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
7689 return false;
7690 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007691 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007692 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007693 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
7694 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007695 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007696 return isCarrierSupported && isDeviceSupported;
7697 } finally {
7698 Binder.restoreCallingIdentity(identity);
7699 }
Hall Liu98187582018-01-22 19:15:32 -08007700 }
7701
Hall Liuf6668912018-10-31 17:05:23 -07007702 /**
Hall Liuf2daa022019-07-23 18:39:00 -07007703 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
7704 * RTT setting, will return true if the device and carrier both support RTT.
7705 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07007706 */
7707 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007708 final long identity = Binder.clearCallingIdentity();
7709 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00007710 boolean isRttSupported = isRttSupported(subscriptionId);
7711 boolean isUserRttSettingOn = Settings.Secure.getInt(
7712 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
7713 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
7714 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
7715 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007716 } finally {
7717 Binder.restoreCallingIdentity(identity);
7718 }
Hall Liu3ad5f012018-04-06 16:23:39 -07007719 }
7720
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007721 @Deprecated
7722 @Override
7723 public String getDeviceId(String callingPackage) {
7724 return getDeviceIdWithFeature(callingPackage, null);
7725 }
7726
Sanket Padawe7310cc72015-01-14 09:53:20 -08007727 /**
7728 * Returns the unique device ID of phone, for example, the IMEI for
7729 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
7730 *
7731 * <p>Requires Permission:
7732 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
7733 */
7734 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007735 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07007736 try {
7737 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
7738 } catch (SecurityException se) {
7739 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
7740 throw new SecurityException("Package " + callingPackage + " does not belong to "
7741 + Binder.getCallingUid());
7742 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007743 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08007744 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08007745 return null;
7746 }
Jeff Davidson913390f2018-02-23 17:11:49 -08007747 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07007748 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007749 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007750 return null;
7751 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007752
7753 final long identity = Binder.clearCallingIdentity();
7754 try {
7755 return phone.getDeviceId();
7756 } finally {
7757 Binder.restoreCallingIdentity(identity);
7758 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08007759 }
7760
Ping Sunc67b7c22016-03-02 19:16:45 +08007761 /**
7762 * {@hide}
7763 * Returns the IMS Registration Status on a particular subid
7764 *
7765 * @param subId
7766 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007767 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08007768 Phone phone = getPhone(subId);
7769 if (phone != null) {
7770 return phone.isImsRegistered();
7771 } else {
7772 return false;
7773 }
7774 }
7775
Santos Cordon7a1885b2015-02-03 11:15:19 -08007776 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07007777 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007778 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007779 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007780 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07007781 throw new SecurityException("Requires READ_PHONE_STATE permission.");
7782 }
7783 final long identity = Binder.clearCallingIdentity();
7784 try {
7785 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
7786 } finally {
7787 Binder.restoreCallingIdentity(identity);
7788 }
7789 }
7790
7791 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07007792 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007793 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007794 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07007795 mApp,
7796 subscriptionId,
7797 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07007798 final long identity = Binder.clearCallingIdentity();
7799 try {
7800 Phone phone = getPhone(subscriptionId);
7801 if (phone == null) {
7802 return null;
7803 }
7804 return PhoneUtils.makePstnPhoneAccountHandle(phone);
7805 } finally {
7806 Binder.restoreCallingIdentity(identity);
7807 }
7808 }
7809
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007810 /**
7811 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07007812 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007813 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007814 final long identity = Binder.clearCallingIdentity();
7815 try {
7816 Phone phone = getPhone(subId);
7817 if (phone != null) {
7818 return phone.isWifiCallingEnabled();
7819 } else {
7820 return false;
7821 }
7822 } finally {
7823 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007824 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07007825 }
7826
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007827 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007828 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007829 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007830 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007831 final long identity = Binder.clearCallingIdentity();
7832 try {
7833 Phone phone = getPhone(subId);
7834 if (phone != null) {
7835 return phone.isVideoEnabled();
7836 } else {
7837 return false;
7838 }
7839 } finally {
7840 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007841 }
7842 }
7843
7844 /**
7845 * @return the IMS registration technology for the MMTEL feature. Valid return values are
7846 * defined in {@link ImsRegistrationImplBase}.
7847 */
7848 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007849 final long identity = Binder.clearCallingIdentity();
7850 try {
7851 Phone phone = getPhone(subId);
7852 if (phone != null) {
7853 return phone.getImsRegistrationTech();
7854 } else {
7855 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
7856 }
7857 } finally {
7858 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08007859 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07007860 }
7861
Stuart Scott8eef64f2015-04-08 15:13:54 -07007862 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07007863 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08007864 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07007865 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
7866 return;
7867 }
Kai Shif70f46f2021-03-03 13:59:46 -08007868 Phone defaultPhone = getDefaultPhone();
7869 if (defaultPhone != null) {
7870 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7871 mApp, getDefaultPhone().getSubId(), "factoryReset");
7872 }
Svet Ganovcc087f82015-05-12 20:35:54 -07007873 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007874
Svet Ganovcc087f82015-05-12 20:35:54 -07007875 try {
Stuart Scott981d8582015-04-21 14:09:50 -07007876 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
7877 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007878 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07007879 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07007880 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007881 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08007882 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007883 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08007884 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07007885 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007886 // There has been issues when Sms raw table somehow stores orphan
7887 // fragments. They lead to garbled message when new fragments come
7888 // in and combined with those stale ones. In case this happens again,
7889 // user can reset all network settings which will clean up this table.
7890 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07007891 // Clean up IMS settings as well here.
7892 int slotId = getSlotIndex(subId);
7893 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
7894 ImsManager.getInstance(mApp, slotId).factoryReset();
7895 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007896
Kai Shif70f46f2021-03-03 13:59:46 -08007897 if (defaultPhone == null) {
7898 return;
7899 }
Naina Nallurid63128d2019-09-17 14:10:30 -07007900 // Erase modem config if erase modem on network setting is enabled.
7901 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
7902 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
7903 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08007904 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07007905 }
Kai Shif70f46f2021-03-03 13:59:46 -08007906
7907 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07007908 } finally {
7909 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07007910 }
7911 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007912
SongFerngWangfd89b102021-05-27 22:44:54 +08007913 @VisibleForTesting
7914 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
7915 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
7916 return;
7917 }
7918 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
7919 RILConstants.PREFERRED_NETWORK_MODE);
7920 SubscriptionManager.setSubscriptionProperty(subId,
7921 SubscriptionManager.ALLOWED_NETWORK_TYPES,
7922 "user=" + defaultNetworkType);
7923 phone.loadAllowedNetworksFromSubscriptionDatabase();
7924 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
7925 defaultNetworkType, null);
7926 }
7927
Amit Mahajan7dbbd822019-03-13 17:33:47 -07007928 private void cleanUpSmsRawTable(Context context) {
7929 ContentResolver resolver = context.getContentResolver();
7930 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
7931 resolver.delete(uri, null, null);
7932 }
7933
Narayan Kamath1c496c22015-04-16 14:40:19 +01007934 @Override
chen xu5d3637b2019-01-21 23:31:38 -08007935 public String getSimLocaleForSubscriber(int subId) {
7936 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
7937 final Phone phone = getPhone(subId);
7938 if (phone == null) {
7939 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08007940 return null;
chen xu5d3637b2019-01-21 23:31:38 -08007941 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007942 final long identity = Binder.clearCallingIdentity();
7943 try {
Jack Yu285100e2022-12-02 22:48:35 -08007944 SubscriptionInfo info;
7945 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
7946 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
7947 phone.getContext().getOpPackageName(),
7948 phone.getContext().getAttributionTag());
7949 if (info == null) {
7950 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7951 return null;
7952 }
7953 } else {
7954 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
7955 phone.getContext().getOpPackageName(),
7956 phone.getContext().getAttributionTag());
7957 if (info == null) {
7958 log("getSimLocaleForSubscriber, inactive subId: " + subId);
7959 return null;
7960 }
chen xu6291c472019-02-04 12:55:53 -08007961 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007962 // Try and fetch the locale from the carrier properties or from the SIM language
7963 // preferences (EF-PL and EF-LI)...
7964 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007965 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08007966 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
7967 if (localeFromDefaultSim != null) {
7968 if (!localeFromDefaultSim.getCountry().isEmpty()) {
7969 if (DBG) log("Using locale from subId: " + subId + " locale: "
7970 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08007971 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08007972 } else {
7973 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007974 }
7975 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007976
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007977 // The SIM language preferences only store a language (e.g. fr = French), not an
7978 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
7979 // the SIM and carrier preferences does not include a country we add the country
7980 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08007981 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007982 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08007983 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08007984 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007985 }
7986
7987 if (DBG) log("No locale found - returning null");
7988 return null;
7989 } finally {
7990 Binder.restoreCallingIdentity(identity);
7991 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01007992 }
7993
tom hsu0b59d292022-09-29 23:49:21 +08007994 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08007995 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08007996 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08007997 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08007998 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08007999 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
8000 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08008001 Locale.forLanguageTag(localeTag).getCountry())) {
8002 return localeTag;
8003 }
8004 }
8005 return inputLocale.toLanguageTag();
8006 }
8007
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008008 /**
8009 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
8010 */
8011 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08008012 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8013 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
8014 mApp.getOpPackageName(), mApp.getAttributionTag());
8015 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008016 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008017 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008018 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008019
Gary Jian3aa9a762022-01-24 16:41:19 +08008020 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8021 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008022
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008023 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008024 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8025 * representing the state of the modem.
8026 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008027 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8028 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008029 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008030 */
8031 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008032 public void requestModemActivityInfo(ResultReceiver result) {
8033 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008034 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008035
8036 final long identity = Binder.clearCallingIdentity();
8037 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008038 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008039 } finally {
8040 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008041 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008042 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008043
Gary Jian76280a42022-12-07 16:18:33 +08008044 // Checks that ModemActivityInfo is valid. Sleep time and Idle time should be
Siddharth Rayb8114062018-06-17 15:02:38 -07008045 // less than total activity duration.
8046 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8047 if (info == null) {
8048 return false;
8049 }
8050 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008051 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
Gary Jian76280a42022-12-07 16:18:33 +08008052 activityDurationMs += MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS;
8053
Hall Liu49656c02020-10-09 19:00:11 -07008054 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8055
Siddharth Rayb8114062018-06-17 15:02:38 -07008056 return (info.isValid()
8057 && (info.getSleepTimeMillis() <= activityDurationMs)
Gary Jian76280a42022-12-07 16:18:33 +08008058 && (info.getIdleTimeMillis() <= activityDurationMs));
Siddharth Rayb8114062018-06-17 15:02:38 -07008059 }
8060
Gary Jian3aa9a762022-01-24 16:41:19 +08008061 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8062 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8063 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8064 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8065
8066 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8067 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8068 }
8069
8070 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8071 mLastModemActivityInfo.setReceiveTimeMillis(
8072 rat,
8073 freq,
8074 info.getReceiveTimeMillis(rat, freq)
8075 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8076 }
8077
8078 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8079 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8080 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8081 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8082
8083 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8084 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8085 }
8086 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8087 mLastModemActivityInfo.setReceiveTimeMillis(
8088 rat,
8089 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8090 }
8091
8092 /**
8093 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8094 * @param info recent ModemActivityInfo
8095 */
8096 private void mergeModemActivityInfo(ModemActivityInfo info) {
8097 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008098 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008099 boolean matched;
8100 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8101 matched = false;
8102 int rat = info.getSpecificInfoRat(i);
8103 int freq = info.getSpecificInfoFrequencyRange(i);
8104 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8105 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8106 //if it already exists
8107 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8108 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8109 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8110 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8111 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8112 updateLastModemActivityInfo(info, rat, freq);
8113 matched = true;
8114 }
8115 } else {
8116 updateLastModemActivityInfo(info, rat);
8117 matched = true;
8118 }
8119 }
8120 }
8121
8122 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008123 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008124 new ActivityStatsTechSpecificInfo(
8125 rat,
8126 freq,
8127 info.getTransmitTimeMillis(rat, freq),
8128 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008129 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008130 }
8131 }
8132 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8133 mLastModemActivitySpecificInfo =
8134 new ActivityStatsTechSpecificInfo[merged.size()];
8135 merged.toArray(mLastModemActivitySpecificInfo);
8136
8137 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8138 mLastModemActivityInfo.setSleepTimeMillis(
8139 info.getSleepTimeMillis()
8140 + mLastModemActivityInfo.getSleepTimeMillis());
8141 mLastModemActivityInfo.setIdleTimeMillis(
8142 info.getIdleTimeMillis()
8143 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008144
8145 mLastModemActivityInfo =
8146 new ModemActivityInfo(
8147 mLastModemActivityInfo.getTimestampMillis(),
8148 mLastModemActivityInfo.getSleepTimeMillis(),
8149 mLastModemActivityInfo.getIdleTimeMillis(),
8150 mLastModemActivitySpecificInfo);
8151 }
8152
8153 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8154 ActivityStatsTechSpecificInfo[] info) {
8155 int infoSize = info.length;
8156 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8157 for (int i = 0; i < infoSize; i++) {
8158 ret[i] = new ActivityStatsTechSpecificInfo(
8159 info[i].getRat(), info[i].getFrequencyRange(),
8160 info[i].getTransmitTimeMillis(),
8161 (int) info[i].getReceiveTimeMillis());
8162 }
8163 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008164 }
8165
Jack Yu85bd38a2015-11-09 11:34:32 -08008166 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008167 * Returns the service state information on specified subscription.
8168 */
8169 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008170 public ServiceState getServiceStateForSubscriber(int subId,
8171 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8172 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008173 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008174 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008175 return null;
8176 }
8177
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008178 boolean hasFinePermission = false;
8179 boolean hasCoarsePermission = false;
8180 if (!renounceFineLocationAccess) {
8181 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8182 LocationAccessPolicy.checkLocationPermission(mApp,
8183 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8184 .setCallingPackage(callingPackage)
8185 .setCallingFeatureId(callingFeatureId)
8186 .setCallingPid(Binder.getCallingPid())
8187 .setCallingUid(Binder.getCallingUid())
8188 .setMethod("getServiceStateForSubscriber")
8189 .setLogAsInfo(true)
8190 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8191 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8192 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8193 .build());
8194 hasFinePermission =
8195 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8196 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008197
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008198 if (!renounceCoarseLocationAccess) {
8199 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8200 LocationAccessPolicy.checkLocationPermission(mApp,
8201 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8202 .setCallingPackage(callingPackage)
8203 .setCallingFeatureId(callingFeatureId)
8204 .setCallingPid(Binder.getCallingPid())
8205 .setCallingUid(Binder.getCallingUid())
8206 .setMethod("getServiceStateForSubscriber")
8207 .setLogAsInfo(true)
8208 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8209 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8210 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8211 .build());
8212 hasCoarsePermission =
8213 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8214 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008215
Jack Yu479f40e2020-10-27 21:29:25 -07008216 final Phone phone = getPhone(subId);
8217 if (phone == null) {
8218 return null;
8219 }
8220
Jordan Liu0f2bc442020-11-18 16:47:37 -08008221 final long identity = Binder.clearCallingIdentity();
8222
Jack Yu479f40e2020-10-27 21:29:25 -07008223 boolean isCallingPackageDataService = phone.getDataServicePackages()
8224 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008225 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008226 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008227 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8228 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8229 .getSubscriptionInfoInternal(subId);
8230 if (subInfo == null || !subInfo.isActive()) {
8231 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8232 + "subId=" + subId);
8233 return null;
8234 }
8235 } else {
8236 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8237 callingFeatureId)) {
8238 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8239 + "subId=" + subId);
8240 return null;
8241 }
Jordan Liuc437b192020-08-17 10:59:12 -07008242 }
8243
Hall Liuf19c44f2018-11-27 14:38:17 -08008244 ServiceState ss = phone.getServiceState();
8245
8246 // Scrub out the location info in ServiceState depending on what level of access
8247 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008248 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008249 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8250 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008251 } finally {
8252 Binder.restoreCallingIdentity(identity);
8253 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008254 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008255
8256 /**
8257 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8258 *
8259 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8260 * voicemail ringtone.
8261 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8262 * PhoneAccount.
8263 */
8264 @Override
8265 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008266 final long identity = Binder.clearCallingIdentity();
8267 try {
8268 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8269 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008270 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008271 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008272
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008273 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8274 } finally {
8275 Binder.restoreCallingIdentity(identity);
8276 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008277 }
8278
8279 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008280 * Sets the per-account voicemail ringtone.
8281 *
8282 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8283 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8284 *
8285 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8286 * voicemail ringtone.
8287 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8288 * PhoneAccount.
8289 */
8290 @Override
8291 public void setVoicemailRingtoneUri(String callingPackage,
8292 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008293 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008294 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008295 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8296 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008297 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8298 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8299 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008300 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008301
8302 final long identity = Binder.clearCallingIdentity();
8303 try {
8304 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8305 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008306 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008307 }
8308 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8309 } finally {
8310 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008311 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008312 }
8313
8314 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008315 * Returns whether vibration is set for voicemail notification in Phone settings.
8316 *
8317 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8318 * voicemail vibration setting.
8319 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8320 */
8321 @Override
8322 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008323 final long identity = Binder.clearCallingIdentity();
8324 try {
8325 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8326 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008327 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008328 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008329
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008330 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8331 } finally {
8332 Binder.restoreCallingIdentity(identity);
8333 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008334 }
8335
Youhan Wange64578a2016-05-02 15:32:42 -07008336 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008337 * Sets the per-account voicemail vibration.
8338 *
8339 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8340 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8341 *
8342 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8343 * voicemail vibration setting.
8344 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8345 * specific PhoneAccount.
8346 */
8347 @Override
8348 public void setVoicemailVibrationEnabled(String callingPackage,
8349 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008350 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008351 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008352 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8353 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008354 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8355 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8356 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008357 }
8358
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008359 final long identity = Binder.clearCallingIdentity();
8360 try {
8361 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8362 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008363 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008364 }
8365 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
8366 } finally {
8367 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008368 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008369 }
8370
8371 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008372 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
8373 *
8374 * @throws SecurityException if the caller does not have the required permission
8375 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07008376 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07008377 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07008378 message);
Youhan Wange64578a2016-05-02 15:32:42 -07008379 }
8380
8381 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008382 * Make sure either called from same process as self (phone) or IPC caller has send SMS
8383 * permission.
8384 *
8385 * @throws SecurityException if the caller does not have the required permission
8386 */
8387 private void enforceSendSmsPermission() {
8388 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
8389 }
8390
8391 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00008392 * Make sure either called from same process as self (phone) or IPC caller has interact across
8393 * users permission.
8394 *
8395 * @throws SecurityException if the caller does not have the required permission
8396 */
8397 private void enforceInteractAcrossUsersPermission(String message) {
8398 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
8399 }
8400
8401 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008402 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008403 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008404 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008405 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08008406 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008407 final long identity = Binder.clearCallingIdentity();
8408 try {
8409 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008410 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008411 if (componentName == null) {
8412 throw new SecurityException(
8413 "Caller not current active visual voicemail package[null]");
8414 }
8415 String vvmPackage = componentName.getPackageName();
8416 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00008417 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008418 }
8419 } finally {
8420 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08008421 }
8422 }
8423
8424 /**
Youhan Wange64578a2016-05-02 15:32:42 -07008425 * Return the application ID for the app type.
8426 *
8427 * @param subId the subscription ID that this request applies to.
8428 * @param appType the uicc app type.
8429 * @return Application ID for specificied app type, or null if no uicc.
8430 */
8431 @Override
8432 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008433 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07008434 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008435
8436 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07008437 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008438 if (phone == null) {
8439 return null;
8440 }
8441 String aid = null;
8442 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00008443 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008444 .getApplicationByType(appType).getAid();
8445 } catch (Exception e) {
8446 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
8447 }
8448 return aid;
8449 } finally {
8450 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07008451 }
Youhan Wange64578a2016-05-02 15:32:42 -07008452 }
8453
Youhan Wang4001d252016-05-11 10:29:41 -07008454 /**
8455 * Return the Electronic Serial Number.
8456 *
8457 * @param subId the subscription ID that this request applies to.
8458 * @return ESN or null if error.
8459 */
8460 @Override
8461 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008462 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07008463 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008464
8465 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07008466 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008467 if (phone == null) {
8468 return null;
8469 }
8470 String esn = null;
8471 try {
8472 esn = phone.getEsn();
8473 } catch (Exception e) {
8474 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
8475 }
8476 return esn;
8477 } finally {
8478 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07008479 }
Youhan Wang4001d252016-05-11 10:29:41 -07008480 }
8481
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008482 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07008483 * Return the Preferred Roaming List Version.
8484 *
8485 * @param subId the subscription ID that this request applies to.
8486 * @return PRLVersion or null if error.
8487 */
8488 @Override
8489 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008490 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07008491 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008492
8493 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07008494 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008495 if (phone == null) {
8496 return null;
8497 }
8498 String cdmaPrlVersion = null;
8499 try {
8500 cdmaPrlVersion = phone.getCdmaPrlVersion();
8501 } catch (Exception e) {
8502 Log.e(LOG_TAG, "Not getting PRLVersion", e);
8503 }
8504 return cdmaPrlVersion;
8505 } finally {
8506 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07008507 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07008508 }
8509
8510 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008511 * Get snapshot of Telephony histograms
8512 * @return List of Telephony histograms
8513 * @hide
8514 */
8515 @Override
8516 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008517 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8518 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008519
8520 final long identity = Binder.clearCallingIdentity();
8521 try {
8522 return RIL.getTelephonyRILTimingHistograms();
8523 } finally {
8524 Binder.restoreCallingIdentity(identity);
8525 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07008526 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008527
8528 /**
8529 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008530 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
8531 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008532 * Require system privileges. In the future we may add this to carrier APIs.
8533 *
Michele Berionne482f8202018-11-27 18:57:59 -08008534 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008535 */
8536 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008537 @TelephonyManager.SetCarrierRestrictionResult
8538 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07008539 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008540 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008541
Michele Berionne482f8202018-11-27 18:57:59 -08008542 if (carrierRestrictionRules == null) {
8543 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08008544 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008545
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008546 final long identity = Binder.clearCallingIdentity();
8547 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008548 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07008549 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008550 } finally {
8551 Binder.restoreCallingIdentity(identity);
8552 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008553 }
8554
8555 /**
8556 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08008557 * Get the allowed carrier list and the excluded carrier list, including the priority between
8558 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07008559 * Require system privileges. In the future we may add this to carrier APIs.
8560 *
Michele Berionne482f8202018-11-27 18:57:59 -08008561 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07008562 */
8563 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08008564 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008565 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07008566 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008567
8568 final long identity = Binder.clearCallingIdentity();
8569 try {
Michele Berionne482f8202018-11-27 18:57:59 -08008570 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
8571 if (response instanceof CarrierRestrictionRules) {
8572 return (CarrierRestrictionRules) response;
8573 }
8574 // Response is an Exception of some kind,
8575 // which is signalled to the user as a NULL retval
8576 return null;
8577 } catch (Exception e) {
8578 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
8579 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008580 } finally {
8581 Binder.restoreCallingIdentity(identity);
8582 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07008583 }
8584
fionaxu59545b42016-05-25 15:53:37 -07008585 /**
fionaxu59545b42016-05-25 15:53:37 -07008586 * Action set from carrier signalling broadcast receivers to enable/disable radio
8587 * @param subId the subscription ID that this action applies to.
8588 * @param enabled control enable or disable radio.
8589 * {@hide}
8590 */
8591 @Override
8592 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
8593 enforceModifyPermission();
8594 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008595
8596 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07008597 if (phone == null) {
8598 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
8599 return;
8600 }
8601 try {
8602 phone.carrierActionSetRadioEnabled(enabled);
8603 } catch (Exception e) {
8604 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008605 } finally {
8606 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07008607 }
8608 }
8609
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008610 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008611 * Enable or disable Voice over NR (VoNR)
8612 * @param subId the subscription ID that this action applies to.
8613 * @param enabled enable or disable VoNR.
8614 * @return operation result.
8615 */
8616 @Override
8617 public int setVoNrEnabled(int subId, boolean enabled) {
8618 enforceModifyPermission();
8619 final Phone phone = getPhone(subId);
8620
8621 final long identity = Binder.clearCallingIdentity();
8622 if (phone == null) {
8623 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
8624 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
8625 }
8626
8627 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8628 try {
8629 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
8630 workSource);
8631 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08008632
8633 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
8634 if (DBG) {
8635 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
8636 }
8637 SubscriptionManager.setSubscriptionProperty(
8638 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
8639 (enabled ? "1" : "0"));
8640 }
8641
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07008642 return result;
8643 } finally {
8644 Binder.restoreCallingIdentity(identity);
8645 }
8646 }
8647
8648 /**
8649 * Is voice over NR enabled
8650 * @return true if VoNR is enabled else false
8651 */
8652 @Override
8653 public boolean isVoNrEnabled(int subId) {
8654 enforceReadPrivilegedPermission("isVoNrEnabled");
8655 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8656 final long identity = Binder.clearCallingIdentity();
8657 try {
8658 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
8659 null, subId, workSource);
8660 if (DBG) log("isVoNrEnabled: " + isEnabled);
8661 return isEnabled;
8662 } finally {
8663 Binder.restoreCallingIdentity(identity);
8664 }
8665 }
8666
8667 /**
fionaxu8da9cb12017-05-23 15:02:46 -07008668 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
8669 * network status based on which carrier apps could apply actions accordingly,
8670 * enable/disable default url handler for example.
8671 *
8672 * @param subId the subscription ID that this action applies to.
8673 * @param report control start/stop reporting the default network status.
8674 * {@hide}
8675 */
8676 @Override
8677 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
8678 enforceModifyPermission();
8679 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008680
8681 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07008682 if (phone == null) {
8683 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
8684 return;
8685 }
8686 try {
8687 phone.carrierActionReportDefaultNetworkStatus(report);
8688 } catch (Exception e) {
8689 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008690 } finally {
8691 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07008692 }
8693 }
8694
8695 /**
fionaxud9622282017-07-17 17:51:30 -07008696 * Action set from carrier signalling broadcast receivers to reset all carrier actions
8697 * @param subId the subscription ID that this action applies to.
8698 * {@hide}
8699 */
8700 @Override
8701 public void carrierActionResetAll(int subId) {
8702 enforceModifyPermission();
8703 final Phone phone = getPhone(subId);
8704 if (phone == null) {
8705 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
8706 return;
8707 }
8708 try {
8709 phone.carrierActionResetAll();
8710 } catch (Exception e) {
8711 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
8712 }
8713 }
8714
8715 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008716 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
8717 * bug report is being generated.
8718 */
8719 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07008720 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008721 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
8722 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07008723 writer.println("Permission Denial: can't dump Phone from pid="
8724 + Binder.getCallingPid()
8725 + ", uid=" + Binder.getCallingUid()
8726 + "without permission "
8727 + android.Manifest.permission.DUMP);
8728 return;
8729 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008730 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07008731 }
Jack Yueb89b242016-06-22 13:27:47 -07008732
Brad Ebingerdac2f002018-04-03 15:17:52 -07008733 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08008734 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
8735 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
8736 @NonNull String[] args) {
8737 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
8738 this, in.getFileDescriptor(), out.getFileDescriptor(),
8739 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07008740 }
8741
Jack Yueb89b242016-06-22 13:27:47 -07008742 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008743 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00008744 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07008745 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008746 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07008747 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008748 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07008749 */
8750 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008751 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07008752 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008753 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
8754 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8755 try {
8756 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008757 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008758 } catch (SecurityException se) {
8759 enforceModifyPermission();
8760 }
8761 } else {
8762 enforceModifyPermission();
8763 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008764
8765 final long identity = Binder.clearCallingIdentity();
8766 try {
8767 Phone phone = getPhone(subId);
8768 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008769 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
8770 phone.carrierActionSetMeteredApnsEnabled(enabled);
8771 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07008772 phone.getDataSettingsManager().setDataEnabled(
8773 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00008774 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008775 }
8776 } finally {
8777 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07008778 }
8779 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008780
8781 /**
8782 * Get Client request stats
8783 * @return List of Client Request Stats
8784 * @hide
8785 */
8786 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008787 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
8788 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008789 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008790 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008791 return null;
8792 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008793 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008794
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008795 final long identity = Binder.clearCallingIdentity();
8796 try {
8797 if (phone != null) {
8798 return phone.getClientRequestStats();
8799 }
8800
8801 return null;
8802 } finally {
8803 Binder.restoreCallingIdentity(identity);
8804 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008805 }
8806
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008807 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008808 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00008809 if (uid == Process.ROOT_UID && packageName == null) {
8810 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
8811 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
8812 // exception. ROOT_UID seems not to have a valid package name returned by
8813 // PackageManager, so just fake it here to avoid issues when running telephony shell
8814 // commands that plumb through the RIL as root, like so:
8815 // $ adb root
8816 // $ adb shell cmd phone ...
8817 packageName = "root";
8818 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00008819 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07008820 }
Jack Yueb4124c2017-02-16 15:32:43 -08008821
8822 /**
Grace Chen70990072017-03-24 17:21:30 -07008823 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08008824 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008825 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07008826 * @param state State of SIM (power down, power up, pass through)
8827 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8828 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8829 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08008830 *
8831 **/
8832 @Override
Grace Chen70990072017-03-24 17:21:30 -07008833 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08008834 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07008835 Phone phone = PhoneFactory.getPhone(slotIndex);
8836
vagdeviaf9a5b92018-08-15 16:01:53 -07008837 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8838
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008839 final long identity = Binder.clearCallingIdentity();
8840 try {
8841 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08008842 phone.setSimPowerState(state, null, workSource);
8843 }
8844 } finally {
8845 Binder.restoreCallingIdentity(identity);
8846 }
8847 }
8848
8849 /**
8850 * Set SIM card power state.
8851 *
8852 * @param slotIndex SIM slot id.
8853 * @param state State of SIM (power down, power up, pass through)
8854 * @param callback callback to trigger after success or failure
8855 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
8856 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
8857 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
8858 *
8859 **/
8860 @Override
8861 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
8862 IIntegerConsumer callback) {
8863 enforceModifyPermission();
8864 Phone phone = PhoneFactory.getPhone(slotIndex);
8865
8866 WorkSource workSource = getWorkSource(Binder.getCallingUid());
8867
8868 final long identity = Binder.clearCallingIdentity();
8869 try {
8870 if (phone != null) {
8871 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
8872 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008873 }
8874 } finally {
8875 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08008876 }
8877 }
Shuo Qiandd210312017-04-12 22:11:33 +00008878
Tyler Gunn65d45c22017-06-05 11:22:26 -07008879 private boolean isUssdApiAllowed(int subId) {
8880 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008881 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07008882 if (configManager == null) {
8883 return false;
8884 }
8885 PersistableBundle pb = configManager.getConfigForSubId(subId);
8886 if (pb == null) {
8887 return false;
8888 }
8889 return pb.getBoolean(
8890 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
8891 }
8892
Shuo Qiandd210312017-04-12 22:11:33 +00008893 /**
8894 * Check if phone is in emergency callback mode
8895 * @return true if phone is in emergency callback mode
8896 * @param subId sub id
8897 */
goneil9c5f4872017-12-05 14:07:56 -08008898 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00008899 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07008900 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00008901 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008902
8903 final long identity = Binder.clearCallingIdentity();
8904 try {
8905 if (phone != null) {
8906 return phone.isInEcm();
8907 } else {
8908 return false;
8909 }
8910 } finally {
8911 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00008912 }
8913 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008914
8915 /**
8916 * Get the current signal strength information for the given subscription.
8917 * Because this information is not updated when the device is in a low power state
8918 * it should not be relied-upon to be current.
8919 * @param subId Subscription index
8920 * @return the most recent cached signal strength info from the modem
8921 */
8922 @Override
8923 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008924 final long identity = Binder.clearCallingIdentity();
8925 try {
8926 Phone p = getPhone(subId);
8927 if (p == null) {
8928 return null;
8929 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008930
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008931 return p.getSignalStrength();
8932 } finally {
8933 Binder.restoreCallingIdentity(identity);
8934 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08008935 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00008936
Pengquan Meng77b7f132018-08-22 14:49:57 -07008937 /**
Chen Xuf792fd62018-10-17 17:54:36 +00008938 * Get the current modem radio state for the given slot.
8939 * @param slotIndex slot index.
8940 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008941 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00008942 * @return the current radio power state from the modem
8943 */
8944 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008945 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00008946 Phone phone = PhoneFactory.getPhone(slotIndex);
8947 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008948 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
8949 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00008950 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8951 }
8952
8953 final long identity = Binder.clearCallingIdentity();
8954 try {
8955 return phone.getRadioPowerState();
8956 } finally {
8957 Binder.restoreCallingIdentity(identity);
8958 }
8959 }
8960 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
8961 }
8962
8963 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07008964 * Checks if data roaming is enabled on the subscription with id {@code subId}.
8965 *
8966 * <p>Requires one of the following permissions:
8967 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008968 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07008969 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
8970 * privileges.
8971 *
8972 * @param subId subscription id
8973 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
8974 * {@code false}.
8975 */
8976 @Override
8977 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008978 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07008979 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008980 try {
8981 mApp.enforceCallingOrSelfPermission(
8982 android.Manifest.permission.ACCESS_NETWORK_STATE,
8983 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008984 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008985 mApp.enforceCallingOrSelfPermission(
8986 permission.READ_BASIC_PHONE_STATE, functionName);
8987 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07008988 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07008989 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07008990 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07008991 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07008992
Pengquan Menga1bb6272018-09-06 09:59:22 -07008993 boolean isEnabled = false;
8994 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07008995 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07008996 Phone phone = getPhone(subId);
8997 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07008998 } finally {
8999 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009000 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009001 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07009002 }
9003
9004
9005 /**
9006 * Enables/Disables the data roaming on the subscription with id {@code subId}.
9007 *
9008 * <p> Requires permission:
9009 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
9010 * privileges.
9011 *
9012 * @param subId subscription id
9013 * @param isEnabled {@code true} means enable, {@code false} means disable.
9014 */
9015 @Override
9016 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009017 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9018 mApp, subId, "setDataRoamingEnabled");
9019
Pengquan Menga1bb6272018-09-06 09:59:22 -07009020 final long identity = Binder.clearCallingIdentity();
9021 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009022 Phone phone = getPhone(subId);
9023 if (phone != null) {
9024 phone.setDataRoamingEnabled(isEnabled);
9025 }
9026 } finally {
9027 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009028 }
9029 }
9030
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009031 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009032 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009033 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009034 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07009035 mApp, subId, "isManualNetworkSelectionAllowed");
9036
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009037 boolean isAllowed = true;
9038 final long identity = Binder.clearCallingIdentity();
9039 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009040 Phone phone = getPhone(subId);
9041 if (phone != null) {
9042 isAllowed = phone.isCspPlmnEnabled();
9043 }
9044 } finally {
9045 Binder.restoreCallingIdentity(identity);
9046 }
9047 return isAllowed;
9048 }
9049
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009050 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9051 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009052 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009053 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009054 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009055 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9056 if (phone == null) {
9057 return false;
9058 }
9059 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9060 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9061 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009062 }
9063
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009064 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009065 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009066 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009067 mApp.getSystemService(AppOpsManager.class)
9068 .checkPackage(Binder.getCallingUid(), callingPackage);
9069
Jordan Liu1e142fc2019-04-22 15:10:43 -07009070 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009071 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009072 try {
9073 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009074 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009075 } catch (SecurityException e) {
9076 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9077 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009078 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Jordan Liuc65bc952019-02-12 17:54:02 -08009079 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009080 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009081 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009082 }
sandeepjsb6c87872021-09-27 15:34:44 +00009083 // checking compatibility, if calling app's target SDK is T and beyond.
9084 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9085 Binder.getCallingUid())) {
9086 isIccIdAccessRestricted = true;
9087 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009088 final long identity = Binder.clearCallingIdentity();
9089 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009090 UiccController uiccController = UiccController.getInstance();
9091 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009092 if (hasReadPermission) {
9093 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009094 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009095
9096 // Remove private info if the caller doesn't have access
9097 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9098 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009099 //setting the value after compatibility check
9100 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009101 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9102 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009103 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009104 if (card == null) {
9105 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009106 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009107 continue;
9108 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009109 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9110 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009111 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009112 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009113 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009114 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9115 for (UiccPortInfo portInfo : portInfos) {
9116 UiccPort port = uiccController.getUiccPortForSlot(
9117 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9118 if (port == null) {
9119 // assume no access if port is null
9120 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9121 continue;
9122 }
9123 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9124 uiccPortInfos.add(portInfo);
9125 } else {
9126 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9127 }
9128 }
9129 filteredInfos.add(new UiccCardInfo(
9130 cardInfo.isEuicc(),
9131 cardInfo.getCardId(),
9132 null,
9133 cardInfo.getPhysicalSlotIndex(),
9134 cardInfo.isRemovable(),
9135 cardInfo.isMultipleEnabledProfilesSupported(),
9136 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009137 }
9138 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009139 } finally {
9140 Binder.restoreCallingIdentity(identity);
9141 }
9142 }
9143
sandeepjsb6c87872021-09-27 15:34:44 +00009144 /**
9145 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9146 * generally private and require carrier privileges to view.
9147 *
9148 * @hide
9149 */
9150 @NonNull
9151 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9152 List<UiccPortInfo> portinfo = new ArrayList<>();
9153 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9154 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9155 }
9156 return new UiccCardInfo(
9157 cardInfo.isEuicc(),
9158 cardInfo.getCardId(),
9159 null,
9160 cardInfo.getPhysicalSlotIndex(),
9161 cardInfo.isRemovable(),
9162 cardInfo.isMultipleEnabledProfilesSupported(),
9163 portinfo
9164 );
9165 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009166
sandeepjsb6c87872021-09-27 15:34:44 +00009167 /**
9168 * @hide
9169 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9170 * These values are generally private and require carrier privileges to view.
9171 */
9172 @NonNull
9173 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9174 return new UiccPortInfo(
9175 UiccPortInfo.ICCID_REDACTED,
9176 portInfo.getPortIndex(),
9177 portInfo.getLogicalSlotIndex(),
9178 portInfo.isActive()
9179 );
9180 }
9181 @Override
9182 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009183 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009184 mApp.getSystemService(AppOpsManager.class)
9185 .checkPackage(Binder.getCallingUid(), callingPackage);
9186
sandeepjsb6c87872021-09-27 15:34:44 +00009187 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009188
Aman Guptaf3c90b32022-03-17 04:54:16 +00009189 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9190 // we are reading iccId which is PII data.
9191 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009192
9193 // checking compatibility, if calling app's target SDK is T and beyond.
9194 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9195 Binder.getCallingUid())) {
9196 isLogicalSlotAccessRestricted = true;
9197 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009198 final long identity = Binder.clearCallingIdentity();
9199 try {
9200 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009201 if (slots == null || slots.length == 0) {
9202 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009203 return null;
9204 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009205 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9206 for (int i = 0; i < slots.length; i++) {
9207 UiccSlot slot = slots[i];
9208 if (slot == null) {
9209 continue;
9210 }
9211
Jordan Liu7be7e652019-05-06 18:55:02 +00009212 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009213 UiccCard card = slot.getUiccCard();
9214 if (card != null) {
9215 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009216 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009217 cardId = slot.getEid();
9218 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009219 // If cardId is null, use iccId of default port as cardId.
9220 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009221 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009222 }
9223
Jordan Liu857451f2019-05-09 16:35:35 -07009224 if (cardId != null) {
9225 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9226 // if cardId is an EID, it's all digits so this is fine
9227 cardId = IccUtils.stripTrailingFs(cardId);
9228 }
9229
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009230 int cardState = 0;
9231 switch (slot.getCardState()) {
9232 case CARDSTATE_ABSENT:
9233 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9234 break;
9235 case CARDSTATE_PRESENT:
9236 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9237 break;
9238 case CARDSTATE_ERROR:
9239 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9240 break;
9241 case CARDSTATE_RESTRICTED:
9242 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9243 break;
9244 default:
9245 break;
9246
9247 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009248 List<UiccPortInfo> portInfos = new ArrayList<>();
9249 int[] portIndexes = slot.getPortList();
9250 for (int portIdx : portIndexes) {
9251 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009252 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009253 portInfos.add(new UiccPortInfo(iccId, portIdx,
9254 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009255 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009256 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009257 slot.isEuicc(),
9258 cardId,
9259 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009260 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009261 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009262 //setting the value after compatibility check
9263 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009264 }
9265 return infos;
9266 } finally {
9267 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009268 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009269 }
9270
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009271 /* Returns null if doesn't have read permission or carrier privilege access. */
9272 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9273 boolean hasReadPermission) {
9274 String iccId = slot.getIccId(portIndex);
9275 if (hasReadPermission) { // if has read permission
9276 return iccId;
9277 } else {
9278 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9279 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9280 // if no read permission, checking carrier privilege access
9281 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9282 return iccId;
9283 }
9284 }
9285 }
9286 // No read permission or carrier privilege access.
9287 return UiccPortInfo.ICCID_REDACTED;
9288 }
9289
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009290 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009291 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009292 public boolean switchSlots(int[] physicalSlots) {
9293 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009294
9295 final long identity = Binder.clearCallingIdentity();
9296 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009297 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9298 for (int i = 0; i < physicalSlots.length; i++) {
9299 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9300 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9301 physicalSlots[i], i));
9302 }
9303 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009304 } finally {
9305 Binder.restoreCallingIdentity(identity);
9306 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009307 }
Jack Yu4c988042018-02-27 15:30:01 -08009308
9309 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009310 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9311 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9312 enforceModifyPermission();
9313
9314 final long identity = Binder.clearCallingIdentity();
9315 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009316 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +00009317 } finally {
9318 Binder.restoreCallingIdentity(identity);
9319 }
9320 }
9321
9322 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08009323 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08009324 final long identity = Binder.clearCallingIdentity();
9325 try {
9326 return UiccController.getInstance().getCardIdForDefaultEuicc();
9327 } finally {
9328 Binder.restoreCallingIdentity(identity);
9329 }
9330 }
9331
Pengquan Meng85728fb2018-03-12 16:31:21 -07009332 /**
goneil47ffb6e2018-04-06 15:40:58 -07009333 * A test API to reload the UICC profile.
9334 *
9335 * <p>Requires that the calling app has permission
9336 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
9337 * @hide
9338 */
9339 @Override
9340 public void refreshUiccProfile(int subId) {
9341 enforceModifyPermission();
9342
9343 final long identity = Binder.clearCallingIdentity();
9344 try {
9345 Phone phone = getPhone(subId);
9346 if (phone == null) {
9347 return;
9348 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009349 UiccPort uiccPort = phone.getUiccPort();
9350 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -07009351 return;
9352 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009353 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -07009354 if (uiccProfile == null) {
9355 return;
9356 }
9357 uiccProfile.refresh();
9358 } finally {
9359 Binder.restoreCallingIdentity(identity);
9360 }
9361 }
9362
9363 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07009364 * Returns false if the mobile data is disabled by default, otherwise return true.
9365 */
9366 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009367 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009368 }
9369
9370 /**
9371 * Returns true if the data roaming is enabled by default, i.e the system property
9372 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
9373 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
9374 */
9375 private boolean getDefaultDataRoamingEnabled(int subId) {
9376 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009377 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07009378 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07009379 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
9380 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
9381 return isDataRoamingEnabled;
9382 }
9383
9384 /**
9385 * Returns the default network type for the given {@code subId}, if the default network type is
9386 * not set, return {@link Phone#PREFERRED_NT_MODE}.
9387 */
9388 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09009389 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -08009390 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +09009391 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
9392 return list.get(phoneId);
9393 }
9394 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07009395 }
fionaxua13278b2018-03-21 00:08:13 -07009396
9397 @Override
9398 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07009399 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07009400 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009401
9402 final long identity = Binder.clearCallingIdentity();
9403 try {
9404 final Phone phone = getPhone(subId);
9405 if (phone == null) {
9406 loge("setCarrierTestOverride fails with invalid subId: " + subId);
9407 return;
9408 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -08009409 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9410 if (cpt != null) {
9411 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
9412 }
9413 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -07009414 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
9415 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07009416 if (carrierPrivilegeRules == null) {
9417 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
9418 } else {
9419 mCarrierPrivilegeTestOverrideSubIds.add(subId);
9420 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009421 } finally {
9422 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009423 }
fionaxua13278b2018-03-21 00:08:13 -07009424 }
9425
9426 @Override
9427 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009428 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009429
9430 final long identity = Binder.clearCallingIdentity();
9431 try {
9432 final Phone phone = getPhone(subId);
9433 if (phone == null) {
9434 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
9435 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
9436 }
9437 return phone.getCarrierIdListVersion();
9438 } finally {
9439 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07009440 }
fionaxua13278b2018-03-21 00:08:13 -07009441 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07009442
9443 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009444 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
9445 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009446 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009447 mApp, subId, callingPackage, callingFeatureId,
9448 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07009449 return -1;
9450 }
9451
9452 final long identity = Binder.clearCallingIdentity();
9453 try {
9454 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
9455 } finally {
9456 Binder.restoreCallingIdentity(identity);
9457 }
9458 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009459
9460 @Override
9461 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08009462 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009463 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07009464 mApp, subId, "getCdmaRoamingMode");
9465
9466 final long identity = Binder.clearCallingIdentity();
9467 try {
9468 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
9469 } finally {
9470 Binder.restoreCallingIdentity(identity);
9471 }
9472 }
9473
9474 @Override
9475 public boolean setCdmaRoamingMode(int subId, int mode) {
9476 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9477 mApp, subId, "setCdmaRoamingMode");
9478
9479 final long identity = Binder.clearCallingIdentity();
9480 try {
9481 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
9482 } finally {
9483 Binder.restoreCallingIdentity(identity);
9484 }
9485 }
9486
9487 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -07009488 public int getCdmaSubscriptionMode(int subId) {
9489 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009490 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -07009491 mApp, subId, "getCdmaSubscriptionMode");
9492
9493 final long identity = Binder.clearCallingIdentity();
9494 try {
9495 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
9496 } finally {
9497 Binder.restoreCallingIdentity(identity);
9498 }
9499 }
9500
9501 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -07009502 public boolean setCdmaSubscriptionMode(int subId, int mode) {
9503 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9504 mApp, subId, "setCdmaSubscriptionMode");
9505
9506 final long identity = Binder.clearCallingIdentity();
9507 try {
9508 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
9509 } finally {
9510 Binder.restoreCallingIdentity(identity);
9511 }
9512 }
Makoto Onukida3bf792018-09-18 16:06:29 -07009513
sqianc5eccab2018-10-19 18:46:41 -07009514 @Override
sqian8c685422019-02-22 15:55:18 -08009515 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009516 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08009517 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009518 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
9519 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08009520 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9521 }
9522 final long identity = Binder.clearCallingIdentity();
9523 try {
sqian854d44b2018-12-12 16:48:18 -08009524 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
9525 for (Phone phone: PhoneFactory.getPhones()) {
9526 if (phone.getEmergencyNumberTracker() != null
9527 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
9528 emergencyNumberListInternal.put(
9529 phone.getSubId(),
9530 phone.getEmergencyNumberTracker().getEmergencyNumberList());
9531 }
sqian11b7a0e2018-12-05 18:48:28 -08009532 }
sqian854d44b2018-12-12 16:48:18 -08009533 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08009534 } finally {
9535 Binder.restoreCallingIdentity(identity);
9536 }
sqianc5eccab2018-10-19 18:46:41 -07009537 }
9538
9539 @Override
sqian8c685422019-02-22 15:55:18 -08009540 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009541 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08009542 if (!exactMatch) {
9543 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009544 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08009545 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08009546 }
9547 final long identity = Binder.clearCallingIdentity();
9548 try {
sqian854d44b2018-12-12 16:48:18 -08009549 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009550 //Note: we ignore passed in param exactMatch. We can remove it once
9551 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -08009552 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +09009553 && phone.getEmergencyNumberTracker()
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -07009554 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +09009555 return true;
sqian11b7a0e2018-12-05 18:48:28 -08009556 }
sqian11b7a0e2018-12-05 18:48:28 -08009557 }
9558 return false;
9559 } finally {
9560 Binder.restoreCallingIdentity(identity);
9561 }
9562 }
9563
sqianf4ca7ed2019-01-15 18:32:07 -08009564 /**
Shuo Qianccbaf742021-02-22 18:32:21 -08009565 * Start emergency callback mode for GsmCdmaPhone for testing.
9566 */
9567 @Override
9568 public void startEmergencyCallbackMode() {
9569 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9570 "startEmergencyCallbackMode");
9571 enforceModifyPermission();
9572 final long identity = Binder.clearCallingIdentity();
9573 try {
9574 for (Phone phone : PhoneFactory.getPhones()) {
9575 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
9576 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
9577 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
9578 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
9579 gsmCdmaPhone.obtainMessage(
9580 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
9581 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
9582 }
9583 }
9584 } finally {
9585 Binder.restoreCallingIdentity(identity);
9586 }
9587 }
9588
9589 /**
sqianf4ca7ed2019-01-15 18:32:07 -08009590 * Update emergency number list for test mode.
9591 */
9592 @Override
9593 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
9594 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9595 "updateEmergencyNumberListTestMode");
9596
9597 final long identity = Binder.clearCallingIdentity();
9598 try {
9599 for (Phone phone: PhoneFactory.getPhones()) {
9600 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9601 if (tracker != null) {
9602 tracker.executeEmergencyNumberTestModeCommand(action, num);
9603 }
9604 }
9605 } finally {
9606 Binder.restoreCallingIdentity(identity);
9607 }
9608 }
9609
9610 /**
9611 * Get the full emergency number list for test mode.
9612 */
9613 @Override
9614 public List<String> getEmergencyNumberListTestMode() {
9615 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
9616 "getEmergencyNumberListTestMode");
9617
9618 final long identity = Binder.clearCallingIdentity();
9619 try {
9620 Set<String> emergencyNumbers = new HashSet<>();
9621 for (Phone phone: PhoneFactory.getPhones()) {
9622 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9623 if (tracker != null) {
9624 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
9625 emergencyNumbers.add(num.getNumber());
9626 }
9627 }
9628 }
9629 return new ArrayList<>(emergencyNumbers);
9630 } finally {
9631 Binder.restoreCallingIdentity(identity);
9632 }
9633 }
9634
chen xud6b45bd2018-10-30 22:27:10 -07009635 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08009636 public int getEmergencyNumberDbVersion(int subId) {
9637 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
9638
9639 final long identity = Binder.clearCallingIdentity();
9640 try {
9641 final Phone phone = getPhone(subId);
9642 if (phone == null) {
9643 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
9644 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
9645 }
9646 return phone.getEmergencyNumberDbVersion();
9647 } finally {
9648 Binder.restoreCallingIdentity(identity);
9649 }
9650 }
9651
9652 @Override
9653 public void notifyOtaEmergencyNumberDbInstalled() {
9654 enforceModifyPermission();
9655
9656 final long identity = Binder.clearCallingIdentity();
9657 try {
9658 for (Phone phone: PhoneFactory.getPhones()) {
9659 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9660 if (tracker != null) {
9661 tracker.updateOtaEmergencyNumberDatabase();
9662 }
9663 }
9664 } finally {
9665 Binder.restoreCallingIdentity(identity);
9666 }
9667 }
9668
9669 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08009670 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08009671 enforceActiveEmergencySessionPermission();
9672
9673 final long identity = Binder.clearCallingIdentity();
9674 try {
9675 for (Phone phone: PhoneFactory.getPhones()) {
9676 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9677 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08009678 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
9679 }
9680 }
9681 } finally {
9682 Binder.restoreCallingIdentity(identity);
9683 }
9684 }
9685
9686 @Override
9687 public void resetOtaEmergencyNumberDbFilePath() {
9688 enforceActiveEmergencySessionPermission();
9689
9690 final long identity = Binder.clearCallingIdentity();
9691 try {
9692 for (Phone phone: PhoneFactory.getPhones()) {
9693 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
9694 if (tracker != null) {
9695 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08009696 }
9697 }
9698 } finally {
9699 Binder.restoreCallingIdentity(identity);
9700 }
9701 }
9702
9703 @Override
chen xud6b45bd2018-10-30 22:27:10 -07009704 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
9705 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
9706 Phone phone = getPhone(subId);
9707 if (phone == null) {
9708 return null;
9709 }
9710 final long identity = Binder.clearCallingIdentity();
9711 try {
9712 UiccProfile profile = UiccController.getInstance()
9713 .getUiccProfileForPhone(phone.getPhoneId());
9714 if (profile != null) {
9715 return profile.getCertsFromCarrierPrivilegeAccessRules();
9716 }
9717 } finally {
9718 Binder.restoreCallingIdentity(identity);
9719 }
9720 return null;
9721 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08009722
9723 /**
9724 * Enable or disable a modem stack.
9725 */
9726 @Override
9727 public boolean enableModemForSlot(int slotIndex, boolean enable) {
9728 enforceModifyPermission();
9729
9730 final long identity = Binder.clearCallingIdentity();
9731 try {
9732 Phone phone = PhoneFactory.getPhone(slotIndex);
9733 if (phone == null) {
9734 return false;
9735 } else {
9736 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
9737 }
9738 } finally {
9739 Binder.restoreCallingIdentity(identity);
9740 }
9741 }
Michelecea4cf22018-12-21 15:00:11 -08009742
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009743 /**
9744 * Whether a modem stack is enabled or not.
9745 */
9746 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009747 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
9748 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009749 Phone phone = PhoneFactory.getPhone(slotIndex);
9750 if (phone == null) return false;
9751
9752 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009753 mApp, phone.getSubId(), callingPackage, callingFeatureId,
9754 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009755 throw new SecurityException("Requires READ_PHONE_STATE permission.");
9756 }
9757
9758 final long identity = Binder.clearCallingIdentity();
9759 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07009760 try {
9761 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
9762 } catch (NoSuchElementException ex) {
9763 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
9764 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07009765 } finally {
9766 Binder.restoreCallingIdentity(identity);
9767 }
9768 }
9769
Michelecea4cf22018-12-21 15:00:11 -08009770 @Override
Michele0ea7d782019-03-19 14:58:42 -07009771 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08009772 enforceModifyPermission();
9773
9774 final long identity = Binder.clearCallingIdentity();
9775 try {
9776 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07009777 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08009778 .commit();
9779 } finally {
9780 Binder.restoreCallingIdentity(identity);
9781 }
9782 }
9783
9784 @Override
Michele0ea7d782019-03-19 14:58:42 -07009785 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009786 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08009787 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009788 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
9789 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07009790 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08009791 }
Michelecea4cf22018-12-21 15:00:11 -08009792
9793 final long identity = Binder.clearCallingIdentity();
9794 try {
Michele0ea7d782019-03-19 14:58:42 -07009795 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08009796 } finally {
9797 Binder.restoreCallingIdentity(identity);
9798 }
9799 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009800
Michele0ea7d782019-03-19 14:58:42 -07009801 @TelephonyManager.IsMultiSimSupportedResult
9802 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08009803 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
9804 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
9805 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009806 loge("isMultiSimSupportedInternal: requires at least 2 cards");
9807 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009808 }
9809 // Check if the hardware supports multisim functionality. If usage of multisim is not
9810 // supported by the modem, indicate that it is restricted.
9811 PhoneCapability staticCapability =
9812 mPhoneConfigurationManager.getStaticPhoneCapability();
9813 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07009814 loge("isMultiSimSupportedInternal: no static configuration available");
9815 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009816 }
SongFerngWang8236caa2021-01-17 21:51:44 +08009817 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07009818 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
9819 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08009820 }
9821 // Check if support of multiple SIMs is restricted by carrier
9822 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07009823 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08009824 }
9825
Michele0ea7d782019-03-19 14:58:42 -07009826 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08009827 }
9828
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009829 /**
9830 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009831 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
9832 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
9833 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009834 * @param numOfSims number of active sims we want to switch to
9835 */
9836 @Override
9837 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08009838 if (numOfSims == 1) {
9839 enforceModifyPermission();
9840 } else {
9841 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9842 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
9843 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009844 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08009845
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009846 try {
Michele30b57b22019-03-01 12:01:14 -08009847 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07009848 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08009849 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
9850 return;
9851 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009852 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
9853 } finally {
9854 Binder.restoreCallingIdentity(identity);
9855 }
9856 }
9857
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009858 @Override
9859 public boolean isApplicationOnUicc(int subId, int appType) {
9860 enforceReadPrivilegedPermission("isApplicationOnUicc");
9861 Phone phone = getPhone(subId);
9862 if (phone == null) {
9863 return false;
9864 }
9865 final long identity = Binder.clearCallingIdentity();
9866 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009867 UiccPort uiccPort = phone.getUiccPort();
9868 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009869 return false;
9870 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009871 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09009872 if (uiccProfile == null) {
9873 return false;
9874 }
9875 if (TelephonyManager.APPTYPE_SIM <= appType
9876 && appType <= TelephonyManager.APPTYPE_ISIM) {
9877 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
9878 }
9879 return false;
9880 } finally {
9881 Binder.restoreCallingIdentity(identity);
9882 }
9883 }
9884
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08009885 /**
chen xub4baa772019-04-03 10:23:41 -07009886 * Get whether making changes to modem configurations will trigger reboot.
9887 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009888 */
9889 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009890 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
9891 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07009892 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009893 mApp, subId, callingPackage, callingFeatureId,
9894 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07009895 return false;
9896 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08009897 final long identity = Binder.clearCallingIdentity();
9898 try {
9899 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
9900 } finally {
9901 Binder.restoreCallingIdentity(identity);
9902 }
9903 }
9904
Nathan Harold29f5f052019-02-15 13:41:57 -08009905 private void updateModemStateMetrics() {
9906 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
9907 // TODO: check the state for each modem if the api is ready.
9908 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
9909 }
9910
Pengquan Meng3889a572019-01-23 11:16:29 -08009911 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +00009912 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -08009913 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +00009914 // Verify that the callingPackage belongs to the calling UID
9915 mApp.getSystemService(AppOpsManager.class)
9916 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -08009917 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +00009918 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -08009919 try {
sandeepjsa208e3b2021-11-17 04:05:58 +00009920 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
9921 if (slotInfos != null) {
9922 for (int i = 0; i < slotInfos.length; i++) {
9923 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
9924 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
9925 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
9926 portInfo.getLogicalSlotIndex()));
9927 }
9928 }
Pengquan Meng3889a572019-01-23 11:16:29 -08009929 }
9930 }
sandeepjsa208e3b2021-11-17 04:05:58 +00009931 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -08009932 } finally {
9933 Binder.restoreCallingIdentity(identity);
9934 }
9935 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08009936
9937 /**
9938 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +08009939 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -08009940 */
jimsunf9ec1622022-09-13 21:18:43 +08009941 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -08009942 @Override
9943 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +08009944 return getHalVersion(HAL_SERVICE_RADIO);
9945 }
9946
9947 /**
9948 * Get the HAL Version of a specific service
9949 */
9950 @Override
9951 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -08009952 Phone phone = getDefaultPhone();
9953 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +08009954 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -08009955 if (hv.equals(HalVersion.UNKNOWN)) return -1;
9956 return hv.major * 100 + hv.minor;
9957 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009958
9959 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08009960 * Get the current calling package name.
9961 * @return the current calling package name
9962 */
9963 @Override
9964 public String getCurrentPackageName() {
9965 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
9966 }
9967
9968 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07009969 * Return whether data is enabled for certain APN type. This will tell if framework will accept
9970 * corresponding network requests on a subId.
9971 *
9972 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009973 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07009974 * 2) APN is un-metered for this subscription, or
9975 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -07009976 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -07009977 *
9978 * @return whether data is allowed for a apn type.
9979 *
9980 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009981 */
9982 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07009983 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07009984 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
9985 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07009986
9987 // Now that all security checks passes, perform the operation as ourselves.
9988 final long identity = Binder.clearCallingIdentity();
9989 try {
9990 Phone phone = getPhone(subId);
9991 if (phone == null) return false;
9992
Jack Yu27422a52022-03-21 10:38:05 -07009993 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -08009994 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -07009995 isMetered = phone.getDataNetworkController().getDataConfigManager()
9996 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
9997 phone.getServiceState().getDataRoaming());
9998 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -08009999 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -070010000 } finally {
10001 Binder.restoreCallingIdentity(identity);
10002 }
10003 }
10004
10005 @Override
Jack Yu41407ee2019-05-13 16:54:09 -070010006 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -070010007 enforceReadPrivilegedPermission("isApnMetered");
10008
10009 // Now that all security checks passes, perform the operation as ourselves.
10010 final long identity = Binder.clearCallingIdentity();
10011 try {
10012 Phone phone = getPhone(subId);
10013 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -070010014 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10015 DataUtils.apnTypeToNetworkCapability(apnType),
10016 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010017 } finally {
10018 Binder.restoreCallingIdentity(identity);
10019 }
10020 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010021
10022 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010023 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10024 int subscriptionId, IBooleanConsumer resultCallback) {
10025 enforceModifyPermission();
10026 long token = Binder.clearCallingIdentity();
10027 try {
10028 Phone phone = getPhone(subscriptionId);
10029 if (phone == null) {
10030 try {
10031 if (resultCallback != null) {
10032 resultCallback.accept(false);
10033 }
10034 } catch (RemoteException e) {
10035 // ignore
10036 }
10037 return;
10038 }
10039 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10040 Pair.create(specifiers, (x) -> {
10041 try {
10042 if (resultCallback != null) {
10043 resultCallback.accept(x);
10044 }
10045 } catch (RemoteException e) {
10046 // ignore
10047 }
10048 });
10049 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10050 } finally {
10051 Binder.restoreCallingIdentity(token);
10052 }
10053 }
10054
10055 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010056 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10057 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010058 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010059 mApp, subId, "getSystemSelectionChannels");
10060 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10061 final long identity = Binder.clearCallingIdentity();
10062 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010063 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10064 if (result instanceof IllegalStateException) {
10065 throw (IllegalStateException) result;
10066 }
10067 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010068 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10069 return specifiers;
10070 } finally {
10071 Binder.restoreCallingIdentity(identity);
10072 }
10073 }
10074
10075 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010076 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010077 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010078 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010079 }
10080
10081 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010082 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10083 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010084 if (callingPackage == null) {
10085 callingPackage = getCurrentPackageName();
10086 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010087 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10088 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010089 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10090 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010091 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10092 }
10093 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10094 Intent intent = new Intent();
10095 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10096 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10097 // Bring up choose default SMS subscription dialog right now
10098 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10099 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10100 mApp.startActivity(intent);
10101 }
chen xud5ca2d52019-05-28 15:20:57 -070010102
10103 @Override
10104 public String getMmsUAProfUrl(int subId) {
10105 //TODO investigate if this API should require proper permission check in R b/133791609
10106 final long identity = Binder.clearCallingIdentity();
10107 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010108 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10109 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10110 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10111 return carrierUAProfUrl;
10112 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010113 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10114 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010115 } finally {
10116 Binder.restoreCallingIdentity(identity);
10117 }
10118 }
10119
10120 @Override
10121 public String getMmsUserAgent(int subId) {
10122 //TODO investigate if this API should require proper permission check in R b/133791609
10123 final long identity = Binder.clearCallingIdentity();
10124 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010125 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10126 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10127 if (!TextUtils.isEmpty(carrierUserAgent)) {
10128 return carrierUserAgent;
10129 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010130 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10131 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010132 } finally {
10133 Binder.restoreCallingIdentity(identity);
10134 }
10135 }
Jack Yub07d4972019-05-28 16:12:25 -070010136
10137 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010138 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10139 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010140
Jack Yub07d4972019-05-28 16:12:25 -070010141 final long identity = Binder.clearCallingIdentity();
10142 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010143 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010144 if (phone == null) return false;
10145
Ling Maf188d502022-09-16 15:22:36 -070010146 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010147 } finally {
10148 Binder.restoreCallingIdentity(identity);
10149 }
10150 }
10151
10152 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010153 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010154 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010155 enforceModifyPermission();
10156
changbettyd5c246e2019-12-24 15:40:37 +080010157 final long identity = Binder.clearCallingIdentity();
10158 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010159 Phone phone = getPhone(subscriptionId);
10160 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010161
Ling Maf188d502022-09-16 15:22:36 -070010162 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010163 } finally {
10164 Binder.restoreCallingIdentity(identity);
10165 }
10166 }
10167
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010168 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010169 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010170 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10171 * otherwise.
10172 */
10173 @Override
10174 public void setCepEnabled(boolean isCepEnabled) {
10175 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10176
10177 final long identity = Binder.clearCallingIdentity();
10178 try {
10179 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10180 for (Phone phone : PhoneFactory.getPhones()) {
10181 Phone defaultPhone = phone.getImsPhone();
10182 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10183 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10184 ImsPhoneCallTracker imsPhoneCallTracker =
10185 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10186 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10187 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10188 + imsPhone.getMsisdn());
10189 }
10190 }
10191 } finally {
10192 Binder.restoreCallingIdentity(identity);
10193 }
10194 }
allenwtsu46dcc572020-01-08 18:24:03 +080010195
10196 /**
10197 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10198 *
10199 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10200 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10201 * before being read.
10202 */
10203 @Override
10204 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10205 isCompressed) {
10206 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10207 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010208 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10209 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10210 }
10211 if (!isImsAvailableOnDevice()) {
10212 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10213 "IMS not available on device.");
10214 }
10215
10216 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010217 try {
Hui Wang761a6682020-10-31 05:12:53 +000010218 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10219 } finally {
10220 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010221 }
10222 }
zoey chene02881a2019-12-30 16:11:23 +080010223
10224 @Override
10225 public boolean isIccLockEnabled(int subId) {
10226 enforceReadPrivilegedPermission("isIccLockEnabled");
10227
10228 // Now that all security checks passes, perform the operation as ourselves.
10229 final long identity = Binder.clearCallingIdentity();
10230 try {
10231 Phone phone = getPhone(subId);
10232 if (phone != null && phone.getIccCard() != null) {
10233 return phone.getIccCard().getIccLockEnabled();
10234 } else {
10235 return false;
10236 }
10237 } finally {
10238 Binder.restoreCallingIdentity(identity);
10239 }
10240 }
10241
10242 /**
10243 * Set the ICC pin lock enabled or disabled.
10244 *
10245 * @return an integer representing the status of IccLock enabled or disabled in the following
10246 * three cases:
10247 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10248 * successfully.
10249 * - Positive number and zero for remaining password attempts.
10250 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10251 *
10252 */
10253 @Override
10254 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10255 enforceModifyPermission();
10256
10257 Phone phone = getPhone(subId);
10258 if (phone == null) {
10259 return 0;
10260 }
10261 // Now that all security checks passes, perform the operation as ourselves.
10262 final long identity = Binder.clearCallingIdentity();
10263 try {
10264 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10265 new Pair<Boolean, String>(enabled, password), phone, null);
10266 return attemptsRemaining;
10267
10268 } catch (Exception e) {
10269 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10270 } finally {
10271 Binder.restoreCallingIdentity(identity);
10272 }
10273 return 0;
10274 }
10275
10276 /**
10277 * Change the ICC password used in ICC pin lock.
10278 *
10279 * @return an integer representing the status of IccLock changed in the following three cases:
10280 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10281 * - Positive number and zero for remaining password attempts.
10282 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10283 *
10284 */
10285 @Override
10286 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10287 enforceModifyPermission();
10288
10289 Phone phone = getPhone(subId);
10290 if (phone == null) {
10291 return 0;
10292 }
10293 // Now that all security checks passes, perform the operation as ourselves.
10294 final long identity = Binder.clearCallingIdentity();
10295 try {
10296 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10297 new Pair<String, String>(oldPassword, newPassword), phone, null);
10298 return attemptsRemaining;
10299
10300 } catch (Exception e) {
10301 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10302 } finally {
10303 Binder.restoreCallingIdentity(identity);
10304 }
10305 return 0;
10306 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080010307
10308 /**
10309 * Request for receiving user activity notification
10310 */
10311 @Override
10312 public void requestUserActivityNotification() {
10313 if (!mNotifyUserActivity.get()
10314 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
10315 mNotifyUserActivity.set(true);
10316 }
10317 }
10318
10319 /**
10320 * Called when userActivity is signalled in the power manager.
10321 * This is safe to call from any thread, with any window manager locks held or not.
10322 */
10323 @Override
10324 public void userActivity() {
10325 // ***************************************
10326 // * Inherited from PhoneWindowManager *
10327 // ***************************************
10328 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
10329 // WITH ITS LOCKS HELD.
10330 //
10331 // This code must be VERY careful about the locks
10332 // it acquires.
10333 // In fact, the current code acquires way too many,
10334 // and probably has lurking deadlocks.
10335
10336 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
10337 throw new SecurityException("Only the OS may call notifyUserActivity()");
10338 }
10339
10340 if (mNotifyUserActivity.getAndSet(false)) {
10341 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
10342 USER_ACTIVITY_NOTIFICATION_DELAY);
10343 }
10344 }
Malcolm Chen4639c562020-04-13 11:59:40 -070010345
10346 @Override
10347 public boolean canConnectTo5GInDsdsMode() {
10348 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
10349 }
Jack Yud10cdd42020-09-28 20:28:01 -070010350
10351 @Override
10352 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
10353 String callingFeatureId) {
10354 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
10355 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
10356 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10357 }
10358
10359 Phone phone = getPhone(subId);
10360 if (phone == null) {
10361 throw new RuntimeException("phone is not available");
10362 }
10363 // Now that all security checks passes, perform the operation as ourselves.
10364 final long identity = Binder.clearCallingIdentity();
10365 try {
10366 return phone.getEquivalentHomePlmns();
10367 } finally {
10368 Binder.restoreCallingIdentity(identity);
10369 }
10370 }
Daniel Bright59e67312020-11-13 11:49:37 -080010371
10372 @Override
10373 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010374 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
10375 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080010376 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080010377 if (radioInterfaceCapabilities == null) {
10378 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080010379 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080010380 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080010381 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010382
Hui Wang641e81c2020-10-12 12:14:23 -070010383 @Override
10384 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
10385 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000010386 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
10387 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10388 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
10389 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10390 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070010391 if (DBG) {
10392 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
10393 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
10394 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
10395 }
10396
10397 if (!SubscriptionManager.isValidSubscriptionId(subId)
10398 || appType < TelephonyManager.APPTYPE_UNKNOWN
10399 || appType > TelephonyManager.APPTYPE_ISIM
10400 || nafUrl == null || securityProtocol == null || callback == null) {
10401 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
10402 if (callback != null) {
10403 try {
10404 callback.onAuthenticationFailure(
10405 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
10406 } catch (RemoteException exception) {
10407 log("Fail to notify onAuthenticationFailure due to " + exception);
10408 }
10409 return;
10410 }
10411 }
10412
10413 final long token = Binder.clearCallingIdentity();
10414 try {
10415 getGbaManager(subId).bootstrapAuthenticationRequest(
10416 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
10417 forceBootStrapping, callback));
10418 } finally {
10419 Binder.restoreCallingIdentity(token);
10420 }
10421 }
10422
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010423 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010424 * Attempts to set the radio power state for all phones for thermal reason.
10425 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010426 * requested radio power state will actually be set. See {@link
10427 * PhoneInternalInterface#setRadioPowerForReason} for more details.
10428 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010429 * @param enable {@code true} if trying to turn radio on.
10430 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
10431 * false}.
10432 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010433 private boolean setRadioPowerForThermal(boolean enable) {
10434 boolean isPhoneAvailable = false;
10435 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
10436 Phone phone = PhoneFactory.getPhone(i);
10437 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000010438 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010439 isPhoneAvailable = true;
10440 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010441 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010442
10443 // return true if successfully informed the phone object about the thermal radio power
10444 // request.
10445 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010446 }
10447
10448 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010449 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010450 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
10451 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
10452 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
10453 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
10454 throw new IllegalArgumentException("modem does not support data throttling");
10455 }
10456
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010457 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
10458 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010459 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010460 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10461 }
10462
Sarah Chinecc78c42022-03-31 21:16:48 -070010463 setDataEnabledForReason(
10464 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010465
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010466 if (isDataThrottlingSupported) {
10467 int thermalMitigationResult =
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010468 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010469 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
10470 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
10471 } else if (thermalMitigationResult
10472 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070010473 log("Modem likely does not support data throttling on secondary carrier. Data " +
10474 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
10475 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010476 }
10477 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010478 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080010479
10480 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010481 }
10482
Jack Nudelman644b91a2021-03-12 14:09:48 -080010483 private static List<String> getThermalMitigationAllowlist(Context context) {
10484 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
10485 for (String pckg : context.getResources()
10486 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
10487 sThermalMitigationAllowlistedPackages.add(pckg);
10488 }
10489 }
10490
10491 return sThermalMitigationAllowlistedPackages;
10492 }
10493
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010494 private boolean isAnyPhoneInEmergencyState() {
10495 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
10496 if (tm.isInEmergencyCall()) {
10497 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
10498 return true;
10499 }
10500 for (Phone phone : PhoneFactory.getPhones()) {
10501 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
10502 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
10503 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
10504 + phone.isInEcm());
10505 return true;
10506 }
10507 }
10508
10509 return false;
10510 }
10511
Jack Nudelman644b91a2021-03-12 14:09:48 -080010512 /**
10513 * Used by shell commands to add an authorized package name for thermal mitigation.
10514 * @param packageName name of package to be allowlisted
10515 * @param context
10516 */
10517 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
10518 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10519 sThermalMitigationAllowlistedPackages.add(packageName);
10520 }
10521
10522 /**
10523 * Used by shell commands to remove an authorized package name for thermal mitigation.
10524 * @param packageName name of package to remove from allowlist
10525 * @param context
10526 */
10527 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
10528 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
10529 sThermalMitigationAllowlistedPackages.remove(packageName);
10530 }
10531
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010532 /**
10533 * Thermal mitigation request to control functionalities at modem.
10534 *
10535 * @param subId the id of the subscription.
10536 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080010537 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010538 *
10539 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
10540 */
10541 @Override
10542 @ThermalMitigationResult
10543 public int sendThermalMitigationRequest(
10544 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080010545 ThermalMitigationRequest thermalMitigationRequest,
10546 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010547 enforceModifyPermission();
10548
Jack Nudelman644b91a2021-03-12 14:09:48 -080010549 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
10550 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
10551 .contains(callingPackage)) {
10552 throw new SecurityException("Calling package must be configured in the device config. "
10553 + "calling package: " + callingPackage);
10554 }
10555
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010556 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10557 final long identity = Binder.clearCallingIdentity();
10558
10559 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
10560 try {
10561 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
10562 switch (thermalMitigationAction) {
10563 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
10564 thermalMitigationResult =
10565 handleDataThrottlingRequest(subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070010566 thermalMitigationRequest.getDataThrottlingRequest(),
10567 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010568 break;
10569 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
10570 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10571 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
10572 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
10573 }
10574
10575 // Ensure that radio is on. If not able to power on due to phone being
10576 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010577 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010578 thermalMitigationResult =
10579 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10580 break;
10581 }
10582
10583 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070010584 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010585 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10586 break;
10587 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
10588 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
10589 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
10590 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
10591 }
10592
10593 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
10594 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010595 Phone phone = getPhone(subId);
10596 if (phone == null) {
10597 thermalMitigationResult =
10598 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10599 break;
10600 }
10601
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010602 TelephonyConnectionService service =
10603 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070010604 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070010605 Log.e(LOG_TAG, "An emergency call is pending");
10606 thermalMitigationResult =
10607 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10608 break;
10609 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010610 thermalMitigationResult =
10611 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
10612 break;
10613 }
10614 } else {
10615 thermalMitigationResult =
10616 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10617 break;
10618 }
10619
10620 // Turn radio off. If not able to power off due to phone being unavailable,
10621 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070010622 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080010623 thermalMitigationResult =
10624 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
10625 break;
10626 }
10627 thermalMitigationResult =
10628 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
10629 break;
10630 default:
10631 throw new IllegalArgumentException("the requested thermalMitigationAction does "
10632 + "not exist. Requested action: " + thermalMitigationAction);
10633 }
10634 } catch (IllegalArgumentException e) {
10635 throw e;
10636 } catch (Exception e) {
10637 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
10638 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
10639 } finally {
10640 Binder.restoreCallingIdentity(identity);
10641 }
10642
10643 if (DBG) {
10644 log("thermalMitigationRequest returning with thermalMitigationResult: "
10645 + thermalMitigationResult);
10646 }
10647
10648 return thermalMitigationResult;
10649 }
Hui Wang641e81c2020-10-12 12:14:23 -070010650
10651 /**
10652 * Set the GbaService Package Name that Telephony will bind to.
10653 *
10654 * @param subId The sim that the GbaService is associated with.
10655 * @param packageName The name of the package to be replaced with.
10656 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10657 */
10658 @Override
10659 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
10660 enforceModifyPermission();
10661
10662 final long identity = Binder.clearCallingIdentity();
10663 try {
10664 return getGbaManager(subId).overrideServicePackage(packageName);
10665 } finally {
10666 Binder.restoreCallingIdentity(identity);
10667 }
10668 }
10669
10670 /**
10671 * Return the package name of the currently bound GbaService.
10672 *
10673 * @param subId The sim that the GbaService is associated with.
10674 * @return the package name of the GbaService configuration, null if GBA is not supported.
10675 */
10676 @Override
10677 public String getBoundGbaService(int subId) {
10678 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
10679
10680 final long identity = Binder.clearCallingIdentity();
10681 try {
10682 return getGbaManager(subId).getServicePackage();
10683 } finally {
10684 Binder.restoreCallingIdentity(identity);
10685 }
10686 }
10687
10688 /**
10689 * Set the release time for telephony to unbind GbaService.
10690 *
10691 * @param subId The sim that the GbaService is associated with.
10692 * @param interval The release time to unbind GbaService by millisecond.
10693 * @return true if setting the GbaService to bind to succeeded, false if it did not.
10694 */
10695 @Override
10696 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
10697 enforceModifyPermission();
10698
10699 final long identity = Binder.clearCallingIdentity();
10700 try {
10701 return getGbaManager(subId).overrideReleaseTime(interval);
10702 } finally {
10703 Binder.restoreCallingIdentity(identity);
10704 }
10705 }
10706
10707 /**
10708 * Return the release time for telephony to unbind GbaService.
10709 *
10710 * @param subId The sim that the GbaService is associated with.
10711 * @return The release time to unbind GbaService by millisecond.
10712 */
10713 @Override
10714 public int getGbaReleaseTime(int subId) {
10715 enforceReadPrivilegedPermission("getGbaReleaseTime");
10716
10717 final long identity = Binder.clearCallingIdentity();
10718 try {
10719 return getGbaManager(subId).getReleaseTime();
10720 } finally {
10721 Binder.restoreCallingIdentity(identity);
10722 }
10723 }
10724
10725 private GbaManager getGbaManager(int subId) {
10726 GbaManager instance = GbaManager.getInstance(subId);
10727 if (instance == null) {
10728 String packageName = mApp.getResources().getString(R.string.config_gba_package);
10729 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
10730 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
10731 }
10732 return instance;
10733 }
Hui Wang761a6682020-10-31 05:12:53 +000010734
10735 /**
10736 * indicate whether the device and the carrier can support
10737 * RCS VoLTE single registration.
10738 */
10739 @Override
10740 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010741 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
10742 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
10743 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10744 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010745
10746 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10747 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10748 }
10749
10750 final long identity = Binder.clearCallingIdentity();
10751 try {
10752 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
10753 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070010754 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
10755 if (isCapable != null) {
10756 return isCapable;
10757 }
Hui Wang761a6682020-10-31 05:12:53 +000010758 }
Hui Wang67af90e2021-06-04 16:57:15 -070010759 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10760 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010761 } finally {
10762 Binder.restoreCallingIdentity(identity);
10763 }
10764 }
10765
10766 /**
10767 * Register RCS provisioning callback.
10768 */
10769 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010770 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010771 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010772 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010773 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010774 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10775 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010776
10777 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10778 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10779 }
10780 if (!isImsAvailableOnDevice()) {
10781 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10782 "IMS not available on device.");
10783 }
10784
10785 final long identity = Binder.clearCallingIdentity();
10786 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010787 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010788 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070010789 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10790 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080010791 }
Hui Wang761a6682020-10-31 05:12:53 +000010792 } finally {
10793 Binder.restoreCallingIdentity(identity);
10794 }
10795 }
10796
10797 /**
10798 * Unregister RCS provisioning callback.
10799 */
10800 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080010801 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000010802 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010803 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080010804 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000010805 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
10806 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000010807
10808 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10809 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10810 }
10811 if (!isImsAvailableOnDevice()) {
10812 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10813 "IMS not available on device.");
10814 }
10815
10816 final long identity = Binder.clearCallingIdentity();
10817 try {
Hui Wang68cd3722021-01-11 20:04:53 -080010818 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080010819 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000010820 } finally {
10821 Binder.restoreCallingIdentity(identity);
10822 }
10823 }
10824
10825 /**
10826 * trigger RCS reconfiguration.
10827 */
10828 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010829 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10830 "triggerRcsReconfiguration",
10831 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010832
10833 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10834 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10835 }
10836 if (!isImsAvailableOnDevice()) {
10837 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10838 "IMS not available on device.");
10839 }
10840
10841 final long identity = Binder.clearCallingIdentity();
10842 try {
10843 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
10844 } finally {
10845 Binder.restoreCallingIdentity(identity);
10846 }
10847 }
10848
10849 /**
10850 * Provide the client configuration parameters of the RCS application.
10851 */
10852 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000010853 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
10854 "setRcsClientConfiguration",
10855 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000010856
10857 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10858 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10859 }
10860 if (!isImsAvailableOnDevice()) {
10861 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
10862 "IMS not available on device.");
10863 }
10864
10865 final long identity = Binder.clearCallingIdentity();
10866
10867 try {
10868 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
10869 if (configBinder == null) {
10870 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070010871 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
10872 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000010873 } else {
10874 configBinder.setRcsClientConfiguration(rcc);
10875 }
joonhunshin3e154242021-09-17 06:33:39 +000010876
10877 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
10878 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000010879 } catch (RemoteException e) {
10880 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070010881 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
10882 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000010883 } finally {
10884 Binder.restoreCallingIdentity(identity);
10885 }
10886 }
10887
10888 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080010889 * Enables or disables the test mode for RCS VoLTE single registration.
10890 */
10891 @Override
10892 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
10893 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10894 "setRcsSingleRegistrationTestModeEnabled");
10895
10896 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
10897 }
10898
10899 /**
10900 * Gets the test mode for RCS VoLTE single registration.
10901 */
10902 @Override
10903 public boolean getRcsSingleRegistrationTestModeEnabled() {
10904 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10905 "getRcsSingleRegistrationTestModeEnabled");
10906
10907 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
10908 }
10909
10910 /**
Hui Wang761a6682020-10-31 05:12:53 +000010911 * Overrides the config of RCS VoLTE single registration enabled for the device.
10912 */
10913 @Override
10914 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
10915 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10916 "setDeviceSingleRegistrationEnabledOverride");
10917 enforceModifyPermission();
10918
10919 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
10920 : Boolean.parseBoolean(enabledStr);
10921 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000010922 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000010923 }
10924
10925 /**
Tyler Gunn92479152021-01-20 16:30:10 -080010926 * Sends a device to device communication message. Only usable via shell.
10927 * @param message message to send.
10928 * @param value message value.
10929 */
10930 @Override
10931 public void sendDeviceToDeviceMessage(int message, int value) {
10932 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080010933 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080010934 enforceModifyPermission();
10935
10936 final long identity = Binder.clearCallingIdentity();
10937 try {
10938 TelephonyConnectionService service =
10939 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10940 if (service == null) {
10941 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
10942 return;
10943 }
10944 service.sendTestDeviceToDeviceMessage(message, value);
10945 } finally {
10946 Binder.restoreCallingIdentity(identity);
10947 }
10948 }
10949
Tyler Gunnbabbda02021-02-10 11:05:02 -080010950 /**
10951 * Sets the specified device to device transport active.
10952 * @param transport The transport to set active.
10953 */
10954 @Override
10955 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
10956 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10957 "setActiveDeviceToDeviceTransport");
10958 enforceModifyPermission();
10959
10960 final long identity = Binder.clearCallingIdentity();
10961 try {
10962 TelephonyConnectionService service =
10963 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
10964 if (service == null) {
10965 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
10966 return;
10967 }
10968 service.setActiveDeviceToDeviceTransport(transport);
10969 } finally {
10970 Binder.restoreCallingIdentity(identity);
10971 }
10972 }
Tyler Gunn92479152021-01-20 16:30:10 -080010973
Tyler Gunnd4339262021-05-03 14:46:49 -070010974 @Override
10975 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
10976 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10977 "setDeviceToDeviceForceEnabled");
10978
10979 final long identity = Binder.clearCallingIdentity();
10980 try {
10981 Arrays.stream(PhoneFactory.getPhones()).forEach(
10982 p -> {
10983 Phone thePhone = p.getImsPhone();
10984 if (thePhone != null && thePhone instanceof ImsPhone) {
10985 ImsPhone imsPhone = (ImsPhone) thePhone;
10986 CallTracker tracker = imsPhone.getCallTracker();
10987 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
10988 ImsPhoneCallTracker imsPhoneCallTracker =
10989 (ImsPhoneCallTracker) tracker;
10990 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
10991 }
10992 }
10993 }
10994 );
10995 } finally {
10996 Binder.restoreCallingIdentity(identity);
10997 }
10998 }
10999
Tyler Gunn92479152021-01-20 16:30:10 -080011000 /**
Hui Wang761a6682020-10-31 05:12:53 +000011001 * Gets the config of RCS VoLTE single registration enabled for the device.
11002 */
11003 @Override
11004 public boolean getDeviceSingleRegistrationEnabled() {
11005 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
11006 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
11007 }
11008
11009 /**
11010 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
11011 */
11012 @Override
11013 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11014 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11015 "setCarrierSingleRegistrationEnabledOverride");
11016 enforceModifyPermission();
11017
11018 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11019 : Boolean.parseBoolean(enabledStr);
11020 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11021 subId, enabled);
11022 }
11023
11024 /**
11025 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11026 */
11027 @Override
11028 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11029 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11030 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11031 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011032
11033 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011034 * Overrides the ims feature validation result
11035 */
11036 @Override
11037 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11038 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11039 "setImsFeatureValidationOverride");
11040
11041 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11042 : Boolean.parseBoolean(enabledStr);
11043 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11044 subId, enabled);
11045 }
11046
11047 /**
11048 * Gets the ims feature validation override value
11049 */
11050 @Override
11051 public boolean getImsFeatureValidationOverride(int subId) {
11052 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11053 "getImsFeatureValidationOverride");
11054 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11055 }
11056
11057 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011058 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11059 * their mobile plan.
11060 */
11061 @Override
11062 public String getMobileProvisioningUrl() {
11063 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11064 final long identity = Binder.clearCallingIdentity();
11065 try {
11066 return getDefaultPhone().getMobileProvisioningUrl();
11067 } finally {
11068 Binder.restoreCallingIdentity(identity);
11069 }
11070 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011071
James.cf Linbcdf8b32021-01-14 16:44:13 +080011072 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011073 * Get the EAB contact from the EAB database.
11074 */
11075 @Override
11076 public String getContactFromEab(String contact) {
11077 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11078 enforceModifyPermission();
11079 final long identity = Binder.clearCallingIdentity();
11080 try {
11081 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11082 } finally {
11083 Binder.restoreCallingIdentity(identity);
11084 }
11085 }
11086
11087 /**
Calvin Pana1434322021-07-01 19:27:01 +080011088 * Get the EAB capability from the EAB database.
11089 */
11090 @Override
11091 public String getCapabilityFromEab(String contact) {
11092 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11093 enforceModifyPermission();
11094 final long identity = Binder.clearCallingIdentity();
11095 try {
11096 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11097 } finally {
11098 Binder.restoreCallingIdentity(identity);
11099 }
11100 }
11101
11102 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011103 * Remove the EAB contacts from the EAB database.
11104 */
11105 @Override
11106 public int removeContactFromEab(int subId, String contacts) {
11107 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11108 enforceModifyPermission();
11109 final long identity = Binder.clearCallingIdentity();
11110 try {
11111 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11112 } finally {
11113 Binder.restoreCallingIdentity(identity);
11114 }
11115 }
11116
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011117 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011118 public boolean getDeviceUceEnabled() {
11119 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11120 final long identity = Binder.clearCallingIdentity();
11121 try {
11122 return mApp.getDeviceUceEnabled();
11123 } finally {
11124 Binder.restoreCallingIdentity(identity);
11125 }
11126 }
11127
11128 @Override
11129 public void setDeviceUceEnabled(boolean isEnabled) {
11130 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11131 final long identity = Binder.clearCallingIdentity();
11132 try {
11133 mApp.setDeviceUceEnabled(isEnabled);
11134 } finally {
11135 Binder.restoreCallingIdentity(identity);
11136 }
11137 }
11138
Brad Ebinger14d467f2021-02-12 06:18:28 +000011139 /**
11140 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11141 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11142 */
11143 // Used for SHELL command only right now.
11144 @Override
11145 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11146 List<String> featureTags) {
11147 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11148 "addUceRegistrationOverrideShell");
11149 final long identity = Binder.clearCallingIdentity();
11150 try {
11151 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11152 new ArraySet<>(featureTags));
11153 } catch (ImsException e) {
11154 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11155 } finally {
11156 Binder.restoreCallingIdentity(identity);
11157 }
11158 }
11159
11160 /**
11161 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11162 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11163 */
11164 // Used for SHELL command only right now.
11165 @Override
11166 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11167 List<String> featureTags) {
11168 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11169 "removeUceRegistrationOverrideShell");
11170 final long identity = Binder.clearCallingIdentity();
11171 try {
11172 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11173 new ArraySet<>(featureTags));
11174 } catch (ImsException e) {
11175 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11176 } finally {
11177 Binder.restoreCallingIdentity(identity);
11178 }
11179 }
11180
11181 /**
11182 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11183 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11184 */
11185 // Used for SHELL command only right now.
11186 @Override
11187 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11188 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11189 "clearUceRegistrationOverrideShell");
11190 final long identity = Binder.clearCallingIdentity();
11191 try {
11192 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11193 } catch (ImsException e) {
11194 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11195 } finally {
11196 Binder.restoreCallingIdentity(identity);
11197 }
11198 }
11199
11200 /**
11201 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11202 */
11203 // Used for SHELL command only right now.
11204 @Override
11205 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11206 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11207 "getLatestRcsContactUceCapabilityShell");
11208 final long identity = Binder.clearCallingIdentity();
11209 try {
11210 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11211 } catch (ImsException e) {
11212 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11213 } finally {
11214 Binder.restoreCallingIdentity(identity);
11215 }
11216 }
11217
11218 /**
11219 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11220 * device does not have an active PUBLISH.
11221 */
11222 // Used for SHELL command only right now.
11223 @Override
11224 public String getLastUcePidfXmlShell(int subId) {
11225 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11226 final long identity = Binder.clearCallingIdentity();
11227 try {
11228 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11229 } catch (ImsException e) {
11230 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11231 } finally {
11232 Binder.restoreCallingIdentity(identity);
11233 }
11234 }
11235
James.cf Line8713a42021-04-29 16:04:26 +080011236 /**
11237 * Remove UCE requests cannot be sent to the network status.
11238 */
11239 // Used for SHELL command only right now.
11240 @Override
11241 public boolean removeUceRequestDisallowedStatus(int subId) {
11242 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11243 final long identity = Binder.clearCallingIdentity();
11244 try {
11245 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11246 } catch (ImsException e) {
11247 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11248 } finally {
11249 Binder.restoreCallingIdentity(identity);
11250 }
11251 }
11252
James.cf Lin18bb9002021-05-25 01:37:38 +080011253 /**
11254 * Remove UCE requests cannot be sent to the network status.
11255 */
11256 // Used for SHELL command only.
11257 @Override
11258 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11259 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11260 final long identity = Binder.clearCallingIdentity();
11261 try {
11262 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11263 } catch (ImsException e) {
11264 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11265 } finally {
11266 Binder.restoreCallingIdentity(identity);
11267 }
11268 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011269
James.cf Lin4b784aa2021-01-31 03:25:15 +080011270 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011271 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11272 String callingPackage) {
11273 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11274 mApp, subId, "setSignalStrengthUpdateRequest");
11275
11276 final int callingUid = Binder.getCallingUid();
11277 // Verify that tha callingPackage belongs to the calling UID
11278 mApp.getSystemService(AppOpsManager.class)
11279 .checkPackage(callingUid, callingPackage);
11280
Rambo Wang3607f502021-02-01 21:51:40 -080011281 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011282
11283 final long identity = Binder.clearCallingIdentity();
11284 try {
11285 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11286 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11287
11288 if (result instanceof IllegalStateException) {
11289 throw (IllegalStateException) result;
11290 }
11291 } finally {
11292 Binder.restoreCallingIdentity(identity);
11293 }
11294 }
11295
11296 @Override
11297 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11298 String callingPackage) {
11299 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11300 mApp, subId, "clearSignalStrengthUpdateRequest");
11301
11302 final int callingUid = Binder.getCallingUid();
11303 // Verify that tha callingPackage belongs to the calling UID
11304 mApp.getSystemService(AppOpsManager.class)
11305 .checkPackage(callingUid, callingPackage);
11306
11307 final long identity = Binder.clearCallingIdentity();
11308 try {
11309 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
11310 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11311
11312 if (result instanceof IllegalStateException) {
11313 throw (IllegalStateException) result;
11314 }
11315 } finally {
11316 Binder.restoreCallingIdentity(identity);
11317 }
11318 }
11319
Rambo Wang3607f502021-02-01 21:51:40 -080011320 private static void validateSignalStrengthUpdateRequest(Context context,
11321 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011322 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
11323 // phone/system process do not have further restriction on request
11324 return;
11325 }
11326
11327 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080011328 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011329 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080011330 context.enforceCallingOrSelfPermission(
11331 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
11332 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011333 }
11334
11335 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000011336 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011337 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011338 || info.isEnabled()) {
11339 throw new IllegalArgumentException(
11340 "Only system can set hide fields in SignalThresholdInfo");
11341 }
11342
11343 // Thresholds length for each RAN need in range. This has been validated in
11344 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
11345 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
11346 final int[] thresholds = info.getThresholds();
11347 Objects.requireNonNull(thresholds);
11348 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
11349 || thresholds.length
11350 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
11351 throw new IllegalArgumentException(
11352 "thresholds length is out of range: " + thresholds.length);
11353 }
11354 }
11355 }
SongFerngWang8236caa2021-01-17 21:51:44 +080011356
11357 /**
11358 * Gets the current phone capability.
11359 *
11360 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
11361 * @return the PhoneCapability which describes the data connection capability of modem.
11362 * It's used to evaluate possible phone config change, for example from single
11363 * SIM device to multi-SIM device.
11364 */
11365 @Override
11366 public PhoneCapability getPhoneCapability() {
11367 enforceReadPrivilegedPermission("getPhoneCapability");
11368 final long identity = Binder.clearCallingIdentity();
11369 try {
11370 return mPhoneConfigurationManager.getCurrentPhoneCapability();
11371 } finally {
11372 Binder.restoreCallingIdentity(identity);
11373 }
11374 }
Michele Berionne5e411512020-11-13 02:36:59 +000011375
11376 /**
11377 * Prepare TelephonyManager for an unattended reboot. The reboot is
11378 * required to be done shortly after the API is invoked.
11379 */
11380 @Override
11381 @TelephonyManager.PrepareUnattendedRebootResult
11382 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011383 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000011384 enforceRebootPermission();
11385
11386 final long identity = Binder.clearCallingIdentity();
11387 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000011388 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000011389 } finally {
11390 Binder.restoreCallingIdentity(identity);
11391 }
11392 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011393
11394 /**
11395 * Request to get the current slicing configuration including URSP rules and
11396 * NSSAIs (configured, allowed and rejected).
11397 *
11398 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
11399 */
11400 @Override
11401 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000011402 TelephonyPermissions
11403 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11404 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080011405
11406 final long identity = Binder.clearCallingIdentity();
11407 try {
11408 Phone phone = getDefaultPhone();
11409 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
11410 } finally {
11411 Binder.restoreCallingIdentity(identity);
11412 }
11413 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011414
11415 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070011416 * Check whether the given premium capability is available for purchase from the carrier.
11417 *
11418 * @param capability The premium capability to check.
11419 * @param subId The subId to check the premium capability for.
11420 *
11421 * @return Whether the given premium capability is available to purchase.
11422 */
11423 @Override
11424 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
11425 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11426 mApp, "isPremiumCapabilityAvailableForPurchase")) {
11427 log("Premium capability "
11428 + TelephonyManager.convertPremiumCapabilityToString(capability)
11429 + " is not available for purchase due to missing permissions.");
11430 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
11431 + "permission READ_BASIC_PHONE_STATE.");
11432 }
11433
11434 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080011435 if (phone == null) {
11436 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
11437 return false;
11438 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070011439 final long identity = Binder.clearCallingIdentity();
11440 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070011441 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070011442 .isPremiumCapabilityAvailableForPurchase(capability);
11443 } finally {
11444 Binder.restoreCallingIdentity(identity);
11445 }
11446 }
11447
11448 /**
11449 * Purchase the given premium capability from the carrier.
11450 *
11451 * @param capability The premium capability to purchase.
11452 * @param callback The result of the purchase request.
11453 * @param subId The subId to purchase the premium capability for.
11454 */
11455 @Override
11456 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
11457 log("purchasePremiumCapability: capability="
11458 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
11459 + getCurrentPackageName());
11460
11461 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
11462 mApp, "purchasePremiumCapability")) {
11463 log("purchasePremiumCapability "
11464 + TelephonyManager.convertPremiumCapabilityToString(capability)
11465 + " failed due to missing permissions.");
11466 throw new SecurityException("purchasePremiumCapability requires permission "
11467 + "READ_BASIC_PHONE_STATE.");
11468 }
11469
11470 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080011471 if (phone == null) {
11472 try {
11473 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
11474 callback.accept(result);
11475 loge("purchasePremiumCapability: phone is null, subId=" + subId);
11476 } catch (RemoteException e) {
11477 String logStr = "Purchase premium capability "
11478 + TelephonyManager.convertPremiumCapabilityToString(capability)
11479 + " failed due to RemoteException handling null phone: " + e;
11480 if (DBG) log(logStr);
11481 AnomalyReporter.reportAnomaly(
11482 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
11483 }
11484 return;
11485 }
Sarah Chin71b3a852022-09-28 15:54:19 -070011486 String appName;
11487 try {
11488 appName = mApp.getPackageManager().getApplicationLabel(mApp.getPackageManager()
11489 .getApplicationInfo(getCurrentPackageName(), 0)).toString();
11490 } catch (PackageManager.NameNotFoundException e) {
11491 appName = "An application";
11492 }
11493 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
11494 new PurchasePremiumCapabilityArgument(capability, appName, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070011495 }
11496
11497 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011498 * Register an IMS connection state callback
11499 */
11500 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011501 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
11502 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011503 if (feature == ImsFeature.FEATURE_MMTEL) {
11504 // ImsMmTelManager
11505 // The following also checks READ_PRIVILEGED_PHONE_STATE.
11506 TelephonyPermissions
11507 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
11508 mApp, subId, "registerImsStateCallback");
11509 } else if (feature == ImsFeature.FEATURE_RCS) {
11510 // ImsRcsManager or SipDelegateManager
11511 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11512 Binder.getCallingUid(), "registerImsStateCallback",
11513 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11514 Manifest.permission.READ_PRECISE_PHONE_STATE,
11515 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
11516 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
11517 }
11518
11519 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
11520 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11521 "IMS not available on device.");
11522 }
11523
11524 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
11525 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
11526 }
11527
11528 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11529 if (controller == null) {
11530 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11531 "IMS not available on device.");
11532 }
11533
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011534 if (callingPackage == null) {
11535 callingPackage = getCurrentPackageName();
11536 }
11537
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011538 final long token = Binder.clearCallingIdentity();
11539 try {
11540 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000011541 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000011542 } catch (ImsException e) {
11543 throw new ServiceSpecificException(e.getCode());
11544 } finally {
11545 Binder.restoreCallingIdentity(token);
11546 }
11547 }
11548
11549 /**
11550 * Unregister an IMS connection state callback
11551 */
11552 @Override
11553 public void unregisterImsStateCallback(IImsStateCallback cb) {
11554 final long token = Binder.clearCallingIdentity();
11555 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
11556 if (controller == null) {
11557 return;
11558 }
11559 try {
11560 controller.unregisterImsStateCallback(cb);
11561 } finally {
11562 Binder.restoreCallingIdentity(token);
11563 }
11564 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011565
11566 /**
11567 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
11568 *
11569 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
11570 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
11571 * SecurityException.
11572 * If there is current registered network this value will be same as the registered cell
11573 * identity. If the device goes out of service the previous cell identity is cached and
11574 * will be returned. If the cache age of the Cell identity is more than 24 hours
11575 * it will be cleared and null will be returned.
11576 *
11577 */
11578 @Override
11579 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
11580 String callingFeatureId) {
11581 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11582 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
11583 LocationAccessPolicy.checkLocationPermission(mApp,
11584 new LocationAccessPolicy.LocationPermissionQuery.Builder()
11585 .setCallingPackage(callingPackage)
11586 .setCallingFeatureId(callingFeatureId)
11587 .setCallingPid(Binder.getCallingPid())
11588 .setCallingUid(Binder.getCallingUid())
11589 .setMethod("getLastKnownCellIdentity")
11590 .setLogAsInfo(true)
11591 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
11592 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
11593 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
11594 .build());
11595
11596 boolean hasFinePermission =
11597 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
11598 if (!hasFinePermission
11599 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
11600 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070011601 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070011602 }
11603
11604 final long identity = Binder.clearCallingIdentity();
11605 try {
11606 Phone phone = getPhone(subId);
11607 if (phone == null) return null;
11608 ServiceStateTracker sst = phone.getServiceStateTracker();
11609 if (sst == null) return null;
11610 return sst.getLastKnownCellIdentity();
11611 } finally {
11612 Binder.restoreCallingIdentity(identity);
11613 }
11614 }
Jack Yu4c0a5502021-12-03 23:58:26 -080011615
jimsun3b9ccac2021-10-26 15:01:23 +080011616 /**
11617 * Sets the modem service class Name that Telephony will bind to.
11618 *
11619 * @param serviceName The class name of the modem service.
11620 * @return true if the operation is succeed, otherwise false.
11621 */
11622 public boolean setModemService(String serviceName) {
11623 Log.d(LOG_TAG, "setModemService - " + serviceName);
11624 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
11625 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
11626 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11627 "setModemService");
11628 return mPhoneConfigurationManager.setModemService(serviceName);
11629 }
11630
11631 /**
11632 * Return the class name of the currently bounded modem service.
11633 *
11634 * @return the class name of the modem service.
11635 */
11636 public String getModemService() {
11637 String result;
11638 Log.d(LOG_TAG, "getModemService");
11639 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
11640 TelephonyPermissions
11641 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
11642 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
11643 "getModemService");
11644 result = mPhoneConfigurationManager.getModemService();
11645 Log.d(LOG_TAG, "result = " + result);
11646 return result;
11647 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080011648
11649 @Override
11650 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
11651 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
11652 mApp.enforceCallingOrSelfPermission(
11653 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
11654 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11655
11656 final long identity = Binder.clearCallingIdentity();
11657 try {
11658 Phone phone = getPhone(subId);
11659 if (phone == null) return;
11660 phone.setVoiceServiceStateOverride(hasService);
11661 } finally {
11662 Binder.restoreCallingIdentity(identity);
11663 }
11664 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011665
11666 /**
11667 * set removable eSIM as default eUICC.
11668 *
11669 * @hide
11670 */
11671 @Override
11672 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
11673 enforceModifyPermission();
11674 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11675
11676 final long identity = Binder.clearCallingIdentity();
11677 try {
11678 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
11679 } finally {
11680 Binder.restoreCallingIdentity(identity);
11681 }
11682 }
11683
11684 /**
11685 * Returns whether the removable eSIM is default eUICC or not.
11686 *
11687 * @hide
11688 */
11689 @Override
11690 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
11691 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
11692 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11693
11694 final long identity = Binder.clearCallingIdentity();
11695 try {
11696 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
11697 } finally {
11698 Binder.restoreCallingIdentity(identity);
11699 }
11700 }
11701
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011702 /**
11703 * Get the component name of the default app to direct respond-via-message intent for the
11704 * user associated with this subscription, update the cache if there is no respond-via-message
11705 * application currently configured for this user.
11706 * @return component name of the app and class to direct Respond Via Message intent to, or
11707 * {@code null} if the functionality is not supported.
11708 * @hide
11709 */
11710 @Override
11711 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
11712 boolean updateIfNeeded) {
11713 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000011714
Aishwarya Mallampati5e581e12023-01-17 21:57:06 +000011715 Context context = getPhoneFromSubIdOrDefault(subId).getContext();
11716
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000011717 UserHandle userHandle = null;
11718 final long identity = Binder.clearCallingIdentity();
11719 try {
11720 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
11721 } finally {
11722 Binder.restoreCallingIdentity(identity);
11723 }
11724 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
11725 updateIfNeeded, userHandle);
11726 }
Jack Yuf5badd92022-12-08 00:50:53 -080011727
11728 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011729 * Set whether the device is able to connect with null ciphering or integrity
11730 * algorithms. This is a global setting and will apply to all active subscriptions
11731 * and all new subscriptions after this.
11732 *
11733 * @param enabled when true, null cipher and integrity algorithms are allowed.
11734 * @hide
11735 */
11736 @Override
11737 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
11738 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
11739 enforceModifyPermission();
11740 checkForNullCipherAndIntegritySupport();
11741
11742 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
11743 // for listening to these preference changes and applying them immediately.
11744 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
11745 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
11746 editor.apply();
11747
11748 for (Phone phone: PhoneFactory.getPhones()) {
11749 phone.handleNullCipherEnabledChange();
11750 }
11751 }
11752
11753
11754 /**
11755 * Get whether the device is able to connect with null ciphering or integrity
11756 * algorithms. Note that this retrieves the phone-global preference and not
11757 * the state of the radio.
11758 *
11759 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
11760 * @throws UnsupportedOperationException if the device does not support the minimum HAL
11761 * version for this feature.
11762 * @hide
11763 */
11764 @Override
11765 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
11766 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
11767 enforceReadPermission();
11768 checkForNullCipherAndIntegritySupport();
11769 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
11770 }
11771
11772 private void checkForNullCipherAndIntegritySupport() {
11773 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
11774 throw new UnsupportedOperationException(
11775 "Null cipher and integrity operations require HAL 2.1 or above");
11776 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000011777 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
11778 throw new UnsupportedOperationException(
11779 "Null cipher and integrity operations unsupported by modem");
11780 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000011781 }
11782
11783 /**
Jack Yuf5badd92022-12-08 00:50:53 -080011784 * Get the SIM state for the slot index.
11785 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
11786 *
11787 * @return SIM state as the ordinal of {@link IccCardConstants.State}
11788 */
11789 @Override
11790 @SimState
11791 public int getSimStateForSlotIndex(int slotIndex) {
11792 IccCardConstants.State simState;
11793 if (slotIndex < 0) {
11794 simState = IccCardConstants.State.UNKNOWN;
11795 } else {
11796 Phone phone = null;
11797 try {
11798 phone = PhoneFactory.getPhone(slotIndex);
11799 } catch (IllegalStateException e) {
11800 // ignore
11801 }
11802 if (phone == null) {
11803 simState = IccCardConstants.State.UNKNOWN;
11804 } else {
11805 IccCard icc = phone.getIccCard();
11806 if (icc == null) {
11807 simState = IccCardConstants.State.UNKNOWN;
11808 } else {
11809 simState = icc.getState();
11810 }
11811 }
11812 }
11813 return simState.ordinal();
11814 }
Hui Wang9b5793a2022-12-05 14:38:06 -060011815
11816 /**
11817 * Get current cell broadcast ranges.
11818 */
11819 @Override
11820 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11821 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
11822 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11823 "getCellBroadcastIdRanges");
11824 final long identity = Binder.clearCallingIdentity();
11825 try {
11826 return getPhone(subId).getCellBroadcastIdRanges();
11827 } finally {
11828 Binder.restoreCallingIdentity(identity);
11829 }
11830 }
11831
11832 /**
11833 * Set reception of cell broadcast messages with the list of the given ranges
11834 *
11835 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
11836 */
11837 @Override
11838 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
11839 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
11840 @Nullable IIntegerConsumer callback) {
11841 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
11842 "setCellBroadcastIdRanges");
11843 final long identity = Binder.clearCallingIdentity();
11844 try {
11845 Phone phone = getPhoneFromSubId(subId);
11846 if (DBG) {
11847 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
11848 }
11849 phone.setCellBroadcastIdRanges(ranges, result -> {
11850 if (callback != null) {
11851 try {
11852 callback.accept(result);
11853 } catch (RemoteException e) {
11854 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
11855 }
11856 }
11857 });
11858 } finally {
11859 Binder.restoreCallingIdentity(identity);
11860 }
11861 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000011862
11863 /**
11864 * Returns whether the device supports the domain selection service.
11865 *
11866 * @return {@code true} if the device supports the domain selection service.
11867 */
11868 @Override
11869 public boolean isDomainSelectionSupported() {
11870 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
11871 "isDomainSelectionSupported");
11872
11873 final long identity = Binder.clearCallingIdentity();
11874 try {
11875 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
11876 } finally {
11877 Binder.restoreCallingIdentity(identity);
11878 }
11879 }
arunvoddud5c6ce02022-12-11 06:03:12 +000011880
11881 /**
11882 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
11883 *
11884 * <p>This method behaves in one of the following ways:
11885 * <ul>
11886 * <li>return true : if the calling package has the appop permission {@link
11887 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
11888 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
11889 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
11890 * Owner device identifier access check, or the calling package has carrier privileges</>
11891 * <li>throw SecurityException: if the caller does not meet any of the requirements.
11892 * </ul>
11893 */
11894 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
11895 String callingPackage, @Nullable String callingFeatureId, String message) {
11896 for (Phone phone : PhoneFactory.getPhones()) {
11897 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
11898 phone.getSubId(), callingPackage, callingFeatureId, message)) {
11899 return true;
11900 }
11901 }
11902 return false;
11903 }
11904}