blob: 5ec4f6500612c5c221a241fb5c06cdd0fda459aa [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;
Sarah Chin532d6bb2022-12-28 22:50:43 -080034import android.app.ActivityManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080036import android.app.PendingIntent;
Tyler Gunn64144d92022-03-17 14:16:41 -070037import android.app.PropertyInvalidatedCache;
Brad Ebinger4f6208e2021-03-23 21:04:45 +000038import android.app.compat.CompatChanges;
Hall Liu82694d52020-12-11 18:22:04 -080039import android.app.role.RoleManager;
sandeepjsb6c87872021-09-27 15:34:44 +000040import android.compat.annotation.ChangeId;
41import android.compat.annotation.EnabledSince;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070042import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070043import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.content.Context;
45import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070046import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070047import android.content.pm.ComponentInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070048import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.net.Uri;
50import android.os.AsyncResult;
51import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080052import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.os.Bundle;
Thomas Nguyen8ee49682023-02-01 11:46:09 -080054import android.os.CancellationSignal;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070056import android.os.IBinder;
Thomas Nguyen8ee49682023-02-01 11:46:09 -080057import android.os.ICancellationSignal;
tom hsu0b59d292022-09-29 23:49:21 +080058import android.os.LocaleList;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.os.Looper;
60import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070061import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080062import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070063import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070064import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080065import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080066import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070067import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070068import android.os.ServiceSpecificException;
Rambo Wang0f050d82021-02-12 11:43:36 -080069import android.os.SystemClock;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070071import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070072import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070073import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070074import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080075import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070076import android.provider.Telephony;
arunvoddud7401012022-12-15 16:08:12 +000077import android.service.carrier.CarrierIdentifier;
Inseob Kim14bb3d02018-12-13 17:11:34 +090078import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080079import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080080import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070081import android.telecom.TelecomManager;
Gary Jian3aa9a762022-01-24 16:41:19 +080082import android.telephony.AccessNetworkConstants;
83import android.telephony.ActivityStatsTechSpecificInfo;
Chen Xu227e06f2019-09-26 22:48:11 -070084import android.telephony.Annotation.ApnType;
Jack Yu0eda6842022-04-18 00:34:46 -070085import android.telephony.Annotation.DataActivityType;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080086import android.telephony.Annotation.ThermalMitigationResult;
Sarah Chin2ec39f62022-08-31 17:03:26 -070087import android.telephony.AnomalyReporter;
Shuo Qian4a594052020-01-23 11:59:30 -080088import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070089import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080090import android.telephony.CarrierRestrictionRules;
Hui Wang9b5793a2022-12-05 14:38:06 -060091import android.telephony.CellBroadcastIdRange;
yincheng zhao2737e882019-09-06 17:06:54 -070092import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080093import android.telephony.CellIdentityCdma;
94import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070096import android.telephony.CellInfoGsm;
97import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070098import android.telephony.ClientRequestStats;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080099import android.telephony.DataThrottlingRequest;
Hui Wang641e81c2020-10-12 12:14:23 -0700100import android.telephony.IBootstrapAuthenticationCallback;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700101import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700102import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -0800103import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700104import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -0800105import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -0700106import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -0800107import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -0800108import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -0700109import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800110import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -0800112import android.telephony.SignalStrength;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800113import android.telephony.SignalStrengthUpdateRequest;
114import android.telephony.SignalThresholdInfo;
Wink Saville0f3b5fc2014-11-11 08:40:49 -0800115import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800116import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -0800117import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700118import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700119import android.telephony.TelephonyManager;
Jack Yuf5badd92022-12-08 00:50:53 -0800120import android.telephony.TelephonyManager.SimState;
Hall Liub2ac8ef2019-02-28 15:56:23 -0800121import android.telephony.TelephonyScanManager;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800122import android.telephony.ThermalMitigationRequest;
Jordan Liu5aa07002018-12-18 15:44:48 -0800123import android.telephony.UiccCardInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000124import android.telephony.UiccPortInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000125import android.telephony.UiccSlotInfo;
sandeepjsb6c87872021-09-27 15:34:44 +0000126import android.telephony.UiccSlotMapping;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700127import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -0700128import android.telephony.VisualVoicemailSmsFilterSettings;
Hongbo Zeng0e18b162021-04-07 16:52:18 +0800129import android.telephony.data.NetworkSlicingConfig;
Jack Yub5d8f642018-11-26 11:20:48 -0800130import android.telephony.emergency.EmergencyNumber;
Hui Wang641e81c2020-10-12 12:14:23 -0700131import android.telephony.gba.GbaAuthRequest;
132import android.telephony.gba.UaSecurityProtocolIdentifier;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700133import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800134import android.telephony.ims.ProvisioningManager;
Hui Wang761a6682020-10-31 05:12:53 +0000135import android.telephony.ims.RcsClientConfiguration;
Brad Ebinger14d467f2021-02-12 06:18:28 +0000136import android.telephony.ims.RcsContactUceCapability;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700137import android.telephony.ims.RegistrationManager;
joonhunshincffb7fc2021-11-28 07:32:01 +0000138import android.telephony.ims.aidl.IFeatureProvisioningCallback;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700139import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800140import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700141import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800142import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700143import android.telephony.ims.aidl.IImsRegistrationCallback;
Hui Wang761a6682020-10-31 05:12:53 +0000144import android.telephony.ims.aidl.IRcsConfigCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700145import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800146import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800147import android.telephony.ims.stub.ImsRegistrationImplBase;
Sarah Chin503828c2023-02-01 23:54:20 -0800148import android.telephony.satellite.ISatelliteCapabilitiesConsumer;
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800149import android.telephony.satellite.ISatelliteStateListener;
Sarah Chineccfbd12023-01-20 19:00:35 -0800150import android.telephony.satellite.PointingInfo;
Sarah Chin503828c2023-02-01 23:54:20 -0800151import android.telephony.satellite.SatelliteCapabilities;
Sarah Chineccfbd12023-01-20 19:00:35 -0800152import android.telephony.satellite.SatelliteManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800154import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700155import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700156import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800157import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800158
Andrew Lee312e8172014-10-23 17:01:36 -0700159import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800160import com.android.ims.internal.IImsServiceFeatureCallback;
James.cf Linbcdf8b32021-01-14 16:44:13 +0800161import com.android.ims.rcs.uce.eab.EabUtil;
SongFerngWangfd89b102021-05-27 22:44:54 +0800162import com.android.internal.annotations.VisibleForTesting;
Shuo Qian4a594052020-01-23 11:59:30 -0800163import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700164import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700165import com.android.internal.telephony.CallStateException;
Tyler Gunnd4339262021-05-03 14:46:49 -0700166import com.android.internal.telephony.CallTracker;
Rambo Wang9c9ffdd2022-01-13 21:51:44 -0800167import com.android.internal.telephony.CarrierPrivilegesTracker;
chen xu651eec72018-11-11 19:03:44 -0800168import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700169import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700170import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800171import com.android.internal.telephony.CommandsInterface;
Hui Wang641e81c2020-10-12 12:14:23 -0700172import com.android.internal.telephony.GbaManager;
Shuo Qianccbaf742021-02-22 18:32:21 -0800173import com.android.internal.telephony.GsmCdmaPhone;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800174import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800175import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700176import com.android.internal.telephony.ICallForwardingInfoCallback;
Hunsuk Choi3b742d62021-10-25 19:48:34 +0000177import com.android.internal.telephony.IImsStateCallback;
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800178import com.android.internal.telephony.IIntArrayConsumer;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700179import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800180import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700181import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800182import com.android.internal.telephony.IccCard;
Jack Yuf5badd92022-12-08 00:50:53 -0800183import com.android.internal.telephony.IccCardConstants;
Rambo Wanga1782702021-11-10 20:15:19 -0800184import com.android.internal.telephony.IccLogicalChannelRequest;
Jack Yu5f7092c2018-04-13 14:05:37 -0700185import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700186import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700187import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700189import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800190import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700191import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700192import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700193import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700194import com.android.internal.telephony.RIL;
SongFerngWang8c6e82e2021-03-02 22:09:29 +0800195import com.android.internal.telephony.RILConstants;
Sarah Chineccfbd12023-01-20 19:00:35 -0800196import com.android.internal.telephony.RILUtils;
Daniel Bright94f43662021-03-01 14:43:40 -0800197import com.android.internal.telephony.RadioInterfaceCapabilityController;
Jack Yu5f7092c2018-04-13 14:05:37 -0700198import com.android.internal.telephony.ServiceStateTracker;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000199import com.android.internal.telephony.SmsApplication;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700200import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700201import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800202import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800203import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800204import com.android.internal.telephony.TelephonyPermissions;
Jack Yu27422a52022-03-21 10:38:05 -0700205import com.android.internal.telephony.data.DataUtils;
Hunsuk Choi42cc62a2022-10-16 06:03:40 +0000206import com.android.internal.telephony.domainselection.DomainSelectionResolver;
sqianf4ca7ed2019-01-15 18:32:07 -0800207import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700208import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800209import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700210import com.android.internal.telephony.imsphone.ImsPhone;
211import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
joonhunshin3e154242021-09-17 06:33:39 +0000212import com.android.internal.telephony.metrics.RcsStats;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800213import com.android.internal.telephony.metrics.TelephonyMetrics;
Jack Yu285100e2022-12-02 22:48:35 -0800214import com.android.internal.telephony.subscription.SubscriptionInfoInternal;
215import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wangafbc5852019-09-19 17:37:13 -0700216import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700217import com.android.internal.telephony.uicc.IccIoResult;
218import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800219import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700220import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800221import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700222import com.android.internal.telephony.uicc.UiccController;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000223import com.android.internal.telephony.uicc.UiccPort;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800224import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000225import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800226import com.android.internal.telephony.util.LocaleUtils;
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +0000227import com.android.internal.telephony.util.TelephonyUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700228import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700229import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800230import com.android.internal.util.HexDump;
Hall Liuaa4211e2021-01-20 15:43:39 -0800231import com.android.phone.callcomposer.CallComposerPictureManager;
232import com.android.phone.callcomposer.CallComposerPictureTransfer;
233import com.android.phone.callcomposer.ImageData;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700234import com.android.phone.settings.PickSmsSubscriptionActivity;
Sarah Chin46355ba2022-11-01 23:51:16 -0700235import com.android.phone.slice.SlicePurchaseController;
arunvoddud7401012022-12-15 16:08:12 +0000236import com.android.phone.utils.CarrierAllowListInfo;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700237import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800238import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700239import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700240import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800241import com.android.services.telephony.TelecomAccountRegistry;
242import com.android.services.telephony.TelephonyConnectionService;
Peter Wang44b186e2020-01-13 23:33:09 -0800243import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800244
Hall Liu82694d52020-12-11 18:22:04 -0800245import java.io.ByteArrayOutputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700246import java.io.FileDescriptor;
Hall Liu82694d52020-12-11 18:22:04 -0800247import java.io.IOException;
248import java.io.InputStream;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700249import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800251import java.util.Arrays;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +0000252import java.util.Collection;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -0800253import java.util.Collections;
sqian11b7a0e2018-12-05 18:48:28 -0800254import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800255import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800256import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100257import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800258import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700259import java.util.NoSuchElementException;
Hall Liu82694d52020-12-11 18:22:04 -0800260import java.util.Objects;
sqianf4ca7ed2019-01-15 18:32:07 -0800261import java.util.Set;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700262import java.util.UUID;
Sarah Chineccfbd12023-01-20 19:00:35 -0800263import java.util.concurrent.ConcurrentHashMap;
Hall Liu82694d52020-12-11 18:22:04 -0800264import java.util.concurrent.Executors;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800265import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800266import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267
268/**
269 * Implementation of the ITelephony interface.
270 */
Santos Cordon117fee72014-05-16 17:56:12 -0700271public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272 private static final String LOG_TAG = "PhoneInterfaceManager";
273 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
274 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800275 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276
277 // Message codes used with mMainThreadHandler
278 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700279 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
280 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700281 private static final int CMD_OPEN_CHANNEL = 9;
282 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
283 private static final int CMD_CLOSE_CHANNEL = 11;
284 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800285 private static final int CMD_NV_READ_ITEM = 13;
286 private static final int EVENT_NV_READ_ITEM_DONE = 14;
287 private static final int CMD_NV_WRITE_ITEM = 15;
288 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
289 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
290 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700291 private static final int CMD_RESET_MODEM_CONFIG = 19;
292 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800293 private static final int CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK = 21;
294 private static final int EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE = 22;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800295 private static final int CMD_SEND_ENVELOPE = 25;
296 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000297 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
298 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700299 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
300 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
301 private static final int CMD_EXCHANGE_SIM_IO = 31;
302 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800303 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
304 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700305 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
306 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700307 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
308 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700309 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
310 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
311 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
312 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700313 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
314 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
315 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
316 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700317 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800318 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
319 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000320 private static final int CMD_SWITCH_SLOTS = 50;
321 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700322 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
323 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
324 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
325 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
326 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
327 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
328 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
329 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700330 private static final int CMD_GET_ALL_CELL_INFO = 60;
331 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
332 private static final int CMD_GET_CELL_LOCATION = 62;
333 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700334 private static final int CMD_MODEM_REBOOT = 64;
335 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700336 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
337 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800338 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
339 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700340 private static final int CMD_GET_MODEM_STATUS = 70;
341 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700342 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
343 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700344 private static final int CMD_ERASE_MODEM_CONFIG = 74;
345 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800346 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
347 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
348 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
349 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800350 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
351 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800352 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800353 private static final int CMD_GET_CALL_FORWARDING = 83;
354 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
355 private static final int CMD_SET_CALL_FORWARDING = 85;
356 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
357 private static final int CMD_GET_CALL_WAITING = 87;
358 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
359 private static final int CMD_SET_CALL_WAITING = 89;
360 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Sooraj Sasindran37444802020-08-11 10:40:43 -0700361 private static final int CMD_ENABLE_NR_DUAL_CONNECTIVITY = 91;
362 private static final int EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE = 92;
363 private static final int CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED = 93;
364 private static final int EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE = 94;
Sarah Chinbaab1432020-10-28 13:46:24 -0700365 private static final int CMD_GET_CDMA_SUBSCRIPTION_MODE = 95;
366 private static final int EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE = 96;
Sarah Chin679c08a2020-11-18 13:39:35 -0800367 private static final int CMD_GET_SYSTEM_SELECTION_CHANNELS = 97;
368 private static final int EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE = 98;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800369 private static final int CMD_SET_DATA_THROTTLING = 99;
370 private static final int EVENT_SET_DATA_THROTTLING_DONE = 100;
Jordan Liu109698e2020-11-24 14:50:34 -0800371 private static final int CMD_SET_SIM_POWER = 101;
372 private static final int EVENT_SET_SIM_POWER_DONE = 102;
Rambo Wanga5cc9b72021-01-07 10:51:54 -0800373 private static final int CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST = 103;
374 private static final int EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 104;
375 private static final int CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST = 105;
376 private static final int EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE = 106;
SongFerngWang3ef3e072020-12-21 16:41:52 +0800377 private static final int CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON = 107;
378 private static final int EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE = 108;
Michele Berionne5e411512020-11-13 02:36:59 +0000379 private static final int CMD_PREPARE_UNATTENDED_REBOOT = 109;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +0800380 private static final int CMD_GET_SLICING_CONFIG = 110;
381 private static final int EVENT_GET_SLICING_CONFIG_DONE = 111;
Kai Shif70f46f2021-03-03 13:59:46 -0800382 private static final int CMD_ERASE_DATA_SHARED_PREFERENCES = 112;
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -0700383 private static final int CMD_ENABLE_VONR = 113;
384 private static final int EVENT_ENABLE_VONR_DONE = 114;
385 private static final int CMD_IS_VONR_ENABLED = 115;
386 private static final int EVENT_IS_VONR_ENABLED_DONE = 116;
Sarah Chin2ec39f62022-08-31 17:03:26 -0700387 private static final int CMD_PURCHASE_PREMIUM_CAPABILITY = 117;
388 private static final int EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE = 118;
Sarah Chineccfbd12023-01-20 19:00:35 -0800389 private static final int CMD_START_SATELLITE_POSITION_UPDATES = 119;
390 private static final int EVENT_START_SATELLITE_POSITION_UPDATES_DONE = 120;
391 private static final int CMD_STOP_SATELLITE_POSITION_UPDATES = 121;
392 private static final int EVENT_STOP_SATELLITE_POSITION_UPDATES_DONE = 122;
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +0000393 private static final int CMD_GET_MAX_CHAR_PER_SATELLITE_TEXT_MSG = 123;
394 private static final int EVENT_GET_MAX_CHAR_PER_SATELLITE_TEXT_MSG_DONE = 124;
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800395 private static final int CMD_PROVISION_SATELLITE_SERVICE = 125;
396 private static final int EVENT_PROVISION_SATELLITE_SERVICE_DONE = 126;
397 private static final int CMD_CANCEL_PROVISION_SATELLITE_SERVICE = 127;
398 private static final int EVENT_CANCEL_PROVISION_SATELLITE_SERVICE_DONE = 128;
399 private static final int CMD_GET_PROVISIONED_SATELLITE_FEATURES = 129;
400 private static final int EVENT_GET_PROVISIONED_SATELLITE_FEATURES_DONE = 130;
Sarah Chin503828c2023-02-01 23:54:20 -0800401 private static final int CMD_SET_SATELLITE_POWER = 131;
402 private static final int EVENT_SET_SATELLITE_POWER_DONE = 132;
403 private static final int CMD_IS_SATELLITE_POWER_ON = 133;
404 private static final int EVENT_IS_SATELLITE_POWER_ON_DONE = 134;
405 private static final int CMD_IS_SATELLITE_SUPPORTED = 135;
406 private static final int EVENT_IS_SATELLITE_SUPPORTED_DONE = 136;
407 private static final int CMD_GET_SATELLITE_CAPABILITIES = 137;
408 private static final int EVENT_GET_SATELLITE_CAPABILITIES_DONE = 138;
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800409 // Parameters of select command.
410 private static final int SELECT_COMMAND = 0xA4;
411 private static final int SELECT_P1 = 0x04;
412 private static final int SELECT_P2 = 0;
413 private static final int SELECT_P3 = 0x10;
414
Gil Cukierman1c0eb932022-12-06 22:28:24 +0000415 // Toggling null cipher and integrity support was added in IRadioNetwork 2.1
416 private static final int MIN_NULL_CIPHER_AND_INTEGRITY_VERSION = 201;
417
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 /** The singleton instance. */
419 private static PhoneInterfaceManager sInstance;
Jack Nudelman644b91a2021-03-12 14:09:48 -0800420 private static List<String> sThermalMitigationAllowlistedPackages = new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700421
Wink Saville3ab207e2014-11-20 13:07:20 -0800422 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800423 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800424 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700425 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800426 private AppOpsManager mAppOps;
Grace Jia0ddb3612021-04-22 13:35:26 -0700427 private PackageManager mPm;
Wink Saville3ab207e2014-11-20 13:07:20 -0800428 private MainThreadHandler mMainThreadHandler;
Jack Yue37dd262022-12-16 11:53:37 -0800429 private final SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800430 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700431 private PhoneConfigurationManager mPhoneConfigurationManager;
Daniel Bright94f43662021-03-01 14:43:40 -0800432 private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433
Peter Wangdafb9ac2020-01-15 14:13:38 -0800434 /** User Activity */
435 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800436 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
437
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700438 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800439 private ConcurrentHashMap<IBinder, SatellitePositionUpdateHandler>
440 mSatellitePositionUpdateHandlers = new ConcurrentHashMap<>();
441 /**
442 * Map key: subId, value: callback to get error code of the provision request.
443 */
444 private ConcurrentHashMap<Integer, IIntegerConsumer> mSatelliteProvisionCallbacks =
Sarah Chineccfbd12023-01-20 19:00:35 -0800445 new ConcurrentHashMap<>();
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700446
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800447 /**
448 * Map key: subId, value: SatelliteProvisionStateChangedHandler to notify registrants.
449 */
450 private ConcurrentHashMap<Integer, SatelliteProvisionStateChangedHandler>
451 mSatelliteProvisionStateChangedHandlers = new ConcurrentHashMap<>();
452
Derek Tan97ebb422014-09-05 16:55:38 -0700453 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
454 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800455 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800456 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700457
Michelecea4cf22018-12-21 15:00:11 -0800458 // String to store multi SIM allowed
459 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
460
Derek Tan740e1672017-06-27 14:56:27 -0700461 // The AID of ISD-R.
462 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
463
yinxub1bed742017-04-17 11:45:04 -0700464 private NetworkScanRequestTracker mNetworkScanRequestTracker;
465
David Kelly5e06a7f2018-03-12 14:10:59 +0000466 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
467 private static final int MANUFACTURER_CODE_LENGTH = 8;
468
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800469 private static final int SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS = -1;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -0800470 private static final int MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE = -2;
Jack Nudelmanb0b87642020-11-12 15:04:39 -0800471
Sarah Chin2ec39f62022-08-31 17:03:26 -0700472 private static final String PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID =
473 "24bf97a6-e8a6-44d8-a6a4-255d7548733c";
474
Derek Tan89e89d42014-07-08 17:00:10 -0700475 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700476 * Experiment flag to enable erase modem config on reset network, default value is false
477 */
478 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
479 "reset_network_erase_modem_config_enabled";
480
Rambo Wang0f050d82021-02-12 11:43:36 -0800481 private static final int SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS = 2000; // 2 seconds
Chen Xu540470b2021-12-14 17:15:47 -0800482
Gary Jian76280a42022-12-07 16:18:33 +0800483 private static final int MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS = 50;
484
sandeepjsb6c87872021-09-27 15:34:44 +0000485 /**
486 * With support for MEP(multiple enabled profile) in Android T, a SIM card can have more than
487 * one ICCID active at the same time.
488 * Apps should use below API signatures if targeting SDK is T and beyond.
489 *
490 * @hide
491 */
492 @ChangeId
493 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
494 public static final long GET_API_SIGNATURES_FROM_UICC_PORT_INFO = 202110963L;
Rambo Wang0f050d82021-02-12 11:43:36 -0800495
Naina Nallurid63128d2019-09-17 14:10:30 -0700496 /**
Chen Xu540470b2021-12-14 17:15:47 -0800497 * Apps targeting on Android T and beyond will get exception whenever icc close channel
498 * operation fails.
499 */
500 @ChangeId
501 @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU)
502 public static final long ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE = 208739934L;
503
504 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700505 * A request object to use for transmitting data to an ICC.
506 */
507 private static final class IccAPDUArgument {
508 public int channel, cla, command, p1, p2, p3;
509 public String data;
510
511 public IccAPDUArgument(int channel, int cla, int command,
512 int p1, int p2, int p3, String data) {
513 this.channel = channel;
514 this.cla = cla;
515 this.command = command;
516 this.p1 = p1;
517 this.p2 = p2;
518 this.p3 = p3;
519 this.data = data;
520 }
521 }
522
523 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700524 * A request object to use for transmitting data to an ICC.
525 */
526 private static final class ManualNetworkSelectionArgument {
527 public OperatorInfo operatorInfo;
528 public boolean persistSelection;
529
530 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
531 this.operatorInfo = operatorInfo;
532 this.persistSelection = persistSelection;
533 }
534 }
535
Sarah Chin71b3a852022-09-28 15:54:19 -0700536 private static final class PurchasePremiumCapabilityArgument {
537 public @TelephonyManager.PremiumCapability int capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700538 public @NonNull IIntegerConsumer callback;
539
540 PurchasePremiumCapabilityArgument(@TelephonyManager.PremiumCapability int capability,
Sarah Chinb8218c22023-01-04 13:35:29 -0800541 @NonNull IIntegerConsumer callback) {
Sarah Chin71b3a852022-09-28 15:54:19 -0700542 this.capability = capability;
Sarah Chin71b3a852022-09-28 15:54:19 -0700543 this.callback = callback;
544 }
545 }
546
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800547 private static final class ProvisionSatelliteServiceArgument {
548 public @NonNull int[] features;
549 public @NonNull IIntegerConsumer callback;
550 public int subId;
551
552 ProvisionSatelliteServiceArgument(int[] features, IIntegerConsumer callback, int subId) {
553 this.features = features;
554 this.callback = callback;
555 this.subId = subId;
556 }
557 }
558
Sarah Chineccfbd12023-01-20 19:00:35 -0800559 private static final class SatellitePositionUpdateHandler extends Handler {
560 public static final int EVENT_POSITION_UPDATE = 1;
561 public static final int EVENT_MESSAGE_TRANSFER_STATE_UPDATE = 2;
562
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800563 private final ISatelliteStateListener mCallback;
Sarah Chineccfbd12023-01-20 19:00:35 -0800564
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800565 SatellitePositionUpdateHandler(ISatelliteStateListener callback, Looper looper) {
Sarah Chineccfbd12023-01-20 19:00:35 -0800566 super(looper);
567 mCallback = callback;
568 }
569
570 @Override
571 public void handleMessage(@NonNull Message msg) {
572 switch (msg.what) {
573 case EVENT_POSITION_UPDATE: {
574 AsyncResult ar = (AsyncResult) msg.obj;
575 PointingInfo pointingInfo = (PointingInfo) ar.result;
576 try {
577 mCallback.onSatellitePositionUpdate(pointingInfo);
578 } catch (RemoteException e) {
579 loge("EVENT_POSITION_UPDATE RemoteException: " + e);
580 }
581 break;
582 }
583 case EVENT_MESSAGE_TRANSFER_STATE_UPDATE: {
584 AsyncResult ar = (AsyncResult) msg.obj;
585 int state = (int) ar.result;
586 try {
587 mCallback.onMessageTransferStateUpdate(state);
588 } catch (RemoteException e) {
589 loge("EVENT_MESSAGE_TRANSFER_STATE_UPDATE RemoteException: " + e);
590 }
591 break;
592 }
593 default:
594 loge("SatellitePositionUpdateHandler unknown event: " + msg.what);
595 }
596 }
597 }
598
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800599 private static final class SatelliteProvisionStateChangedHandler extends Handler {
600 public static final int EVENT_PROVISION_STATE_CHANGED = 1;
601
602 private ConcurrentHashMap<IBinder, ISatelliteStateListener> mListeners;
603 private final int mSubId;
604
605 SatelliteProvisionStateChangedHandler(Looper looper, int subId) {
606 super(looper);
607 mListeners = new ConcurrentHashMap<>();
608 mSubId = subId;
609 }
610
611 public void addListener(ISatelliteStateListener listener) {
612 mListeners.put(listener.asBinder(), listener);
613 }
614
615 public boolean removeListener(ISatelliteStateListener listener) {
616 return (mListeners.remove(listener.asBinder()) != null);
617 }
618
619 @Override
620 public void handleMessage(@NonNull Message msg) {
621 switch (msg.what) {
622 case EVENT_PROVISION_STATE_CHANGED: {
623 AsyncResult ar = (AsyncResult) msg.obj;
624 boolean provisioned = (boolean) ar.userObj;
625 int[] features = (int[]) ar.result;
626 log("Received EVENT_PROVISION_STATE_CHANGED for subId=" + mSubId
627 + ", features=" + Arrays.toString(features)
628 + ", provisioned=" + provisioned);
629 mListeners.values().forEach(listener -> {
630 try {
631 listener.onSatelliteProvisionStateChanged(features, provisioned);
632 } catch (RemoteException e) {
633 log("EVENT_PROVISION_STATE_CHANGED RemoteException: " + e);
634 }
635 });
636
637 setSatelliteEnabled(provisioned);
638 /**
639 * TODO: Take bugreport if provisioned is true and user did not initiate the
640 * provision procedure for the corresponding subscription.
641 */
642 break;
643 }
644 default:
645 loge("SatelliteProvisionStateChangedHandler unknown event: " + msg.what);
646 }
647 }
648
649 private void setSatelliteEnabled(boolean isEnabled) {
650 if (mSubId != SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
651 SubscriptionManager.setSubscriptionProperty(
652 mSubId, SubscriptionManager.SATELLITE_ENABLED, isEnabled ? "1" : "0");
653 } else {
654 //TODO (b/267826133): set enabled via SatelliteController.
655 }
656 }
657 }
658
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700659 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700660 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
661 * request after sending. The main thread will notify the request when it is complete.
662 */
663 private static final class MainThreadRequest {
664 /** The argument to use for the request */
665 public Object argument;
666 /** The result of the request that is run on the main thread */
667 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800668 // The subscriber id that this request applies to. Defaults to
669 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
670 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700671
Nathan Harold92bed182018-10-12 18:16:49 -0700672 // In cases where subId is unavailable, the caller needs to specify the phone.
673 public Phone phone;
674
vagdeviaf9a5b92018-08-15 16:01:53 -0700675 public WorkSource workSource;
676
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700677 public MainThreadRequest(Object argument) {
678 this.argument = argument;
679 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800680
Nathan Harold92bed182018-10-12 18:16:49 -0700681 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
682 this.argument = argument;
683 if (phone != null) {
684 this.phone = phone;
685 }
686 this.workSource = workSource;
687 }
688
vagdeviaf9a5b92018-08-15 16:01:53 -0700689 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800690 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800691 if (subId != null) {
692 this.subId = subId;
693 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700694 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800695 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700696 }
697
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800698 private static final class IncomingThirdPartyCallArgs {
699 public final ComponentName component;
700 public final String callId;
701 public final String callerDisplayName;
702
703 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
704 String callerDisplayName) {
705 this.component = component;
706 this.callId = callId;
707 this.callerDisplayName = callerDisplayName;
708 }
709 }
710
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700711 /**
712 * A handler that processes messages on the main thread in the phone process. Since many
713 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
714 * inbound binder threads to the main thread in the phone process. The Binder thread
715 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
716 * on, which will be notified when the operation completes and will contain the result of the
717 * request.
718 *
719 * <p>If a MainThreadRequest object is provided in the msg.obj field,
720 * note that request.result must be set to something non-null for the calling thread to
721 * unblock.
722 */
723 private final class MainThreadHandler extends Handler {
724 @Override
725 public void handleMessage(Message msg) {
726 MainThreadRequest request;
727 Message onCompleted;
728 AsyncResult ar;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000729 UiccPort uiccPort;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700730 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800731 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700732
733 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700734 case CMD_HANDLE_USSD_REQUEST: {
735 request = (MainThreadRequest) msg.obj;
736 final Phone phone = getPhoneFromRequest(request);
737 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
Chen Xue9d737e2022-01-01 23:41:31 -0800738 String ussdRequest = ussdObject.first;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700739 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700740
Pengquan Menga1bb6272018-09-06 09:59:22 -0700741 if (!isUssdApiAllowed(request.subId)) {
742 // Carrier does not support use of this API, return failure.
743 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
744 UssdResponse response = new UssdResponse(ussdRequest, null);
745 Bundle returnData = new Bundle();
746 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
747 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700748
Pengquan Menga1bb6272018-09-06 09:59:22 -0700749 request.result = true;
750 notifyRequester(request);
751 return;
752 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700753
Pengquan Menga1bb6272018-09-06 09:59:22 -0700754 try {
755 request.result = phone != null
756 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
757 } catch (CallStateException cse) {
758 request.result = false;
759 }
760 // Wake up the requesting thread
761 notifyRequester(request);
762 break;
pkanwar32d516d2016-10-14 19:37:38 -0700763 }
764
Yorke Lee716f67e2015-06-17 15:39:16 -0700765 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700767 final Phone phone = getPhoneFromRequest(request);
768 request.result = phone != null ?
769 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
770 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700771 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700772 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700774 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700775
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700776 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700777 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700778 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000779 uiccPort = getUiccPortFromRequest(request);
780 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700781 loge("iccTransmitApduLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800782 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700783 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700784 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700785 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800786 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000787 uiccPort.iccTransmitApduLogicalChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800788 iccArgument.channel, iccArgument.cla, iccArgument.command,
789 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
790 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700791 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700792 break;
793
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700794 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700795 ar = (AsyncResult) msg.obj;
796 request = (MainThreadRequest) ar.userObj;
797 if (ar.exception == null && ar.result != null) {
798 request.result = ar.result;
799 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800800 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700801 if (ar.result == null) {
802 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800803 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700804 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800805 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700806 } else {
807 loge("iccTransmitApduLogicalChannel: Unknown exception");
808 }
809 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700810 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700811 break;
812
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700813 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
814 request = (MainThreadRequest) msg.obj;
815 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000816 uiccPort = getUiccPortFromRequest(request);
817 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700818 loge("iccTransmitApduBasicChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800819 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700820 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700821 } else {
822 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
Chen Xue9d737e2022-01-01 23:41:31 -0800823 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000824 uiccPort.iccTransmitApduBasicChannel(
Chen Xue9d737e2022-01-01 23:41:31 -0800825 iccArgument.cla, iccArgument.command, iccArgument.p1,
826 iccArgument.p2,
827 iccArgument.p3, iccArgument.data, onCompleted);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700828 }
829 break;
830
831 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
832 ar = (AsyncResult) msg.obj;
833 request = (MainThreadRequest) ar.userObj;
834 if (ar.exception == null && ar.result != null) {
835 request.result = ar.result;
836 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800837 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700838 if (ar.result == null) {
839 loge("iccTransmitApduBasicChannel: Empty response");
840 } else if (ar.exception instanceof CommandException) {
841 loge("iccTransmitApduBasicChannel: CommandException: " +
842 ar.exception);
843 } else {
844 loge("iccTransmitApduBasicChannel: Unknown exception");
845 }
846 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700847 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700848 break;
849
850 case CMD_EXCHANGE_SIM_IO:
851 request = (MainThreadRequest) msg.obj;
852 iccArgument = (IccAPDUArgument) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000853 uiccPort = getUiccPortFromRequest(request);
854 if (uiccPort == null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700855 loge("iccExchangeSimIO: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800856 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700857 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700858 } else {
859 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
860 request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000861 uiccPort.iccExchangeSimIO(iccArgument.cla, /* fileID */
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700862 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
863 iccArgument.data, onCompleted);
864 }
865 break;
866
867 case EVENT_EXCHANGE_SIM_IO_DONE:
868 ar = (AsyncResult) msg.obj;
869 request = (MainThreadRequest) ar.userObj;
870 if (ar.exception == null && ar.result != null) {
871 request.result = ar.result;
872 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800873 request.result = new IccIoResult(0x6f, 0, (byte[]) null);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700874 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700875 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700876 break;
877
Derek Tan4d5e5c12014-02-04 11:54:58 -0800878 case CMD_SEND_ENVELOPE:
879 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000880 uiccPort = getUiccPortFromRequest(request);
881 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700882 loge("sendEnvelopeWithStatus: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800883 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700884 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700885 } else {
886 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
Chen Xue9d737e2022-01-01 23:41:31 -0800887 uiccPort.sendEnvelopeWithStatus((String) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700888 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800889 break;
890
891 case EVENT_SEND_ENVELOPE_DONE:
892 ar = (AsyncResult) msg.obj;
893 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700894 if (ar.exception == null && ar.result != null) {
895 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800896 } else {
Chen Xue9d737e2022-01-01 23:41:31 -0800897 request.result = new IccIoResult(0x6F, 0, (byte[]) null);
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700898 if (ar.result == null) {
899 loge("sendEnvelopeWithStatus: Empty response");
900 } else if (ar.exception instanceof CommandException) {
901 loge("sendEnvelopeWithStatus: CommandException: " +
902 ar.exception);
903 } else {
904 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
905 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800906 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700907 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800908 break;
909
Shishir Agrawal566b7612013-10-28 14:41:00 -0700910 case CMD_OPEN_CHANNEL:
911 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000912 uiccPort = getUiccPortFromRequest(request);
Rambo Wanga1782702021-11-10 20:15:19 -0800913 IccLogicalChannelRequest openChannelRequest =
914 (IccLogicalChannelRequest) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000915 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700916 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800917 request.result = new IccOpenLogicalChannelResponse(-1,
Chen Xue9d737e2022-01-01 23:41:31 -0800918 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700919 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700920 } else {
921 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Rambo Wanga1782702021-11-10 20:15:19 -0800922 uiccPort.iccOpenLogicalChannel(openChannelRequest.aid,
923 openChannelRequest.p2, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700924 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700925 break;
926
927 case EVENT_OPEN_CHANNEL_DONE:
928 ar = (AsyncResult) msg.obj;
929 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700930 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700931 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700932 int[] result = (int[]) ar.result;
933 int channelId = result[0];
934 byte[] selectResponse = null;
935 if (result.length > 1) {
936 selectResponse = new byte[result.length - 1];
937 for (int i = 1; i < result.length; ++i) {
938 selectResponse[i - 1] = (byte) result[i];
939 }
940 }
941 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Chen Xue9d737e2022-01-01 23:41:31 -0800942 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800943
944 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700945 if (uiccPort == null) {
946 loge("EVENT_OPEN_CHANNEL_DONE: UiccPort is null");
947 } else {
948 IccLogicalChannelRequest channelRequest =
949 (IccLogicalChannelRequest) request.argument;
950 channelRequest.channel = channelId;
951 uiccPort.onLogicalChannelOpened(channelRequest);
952 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700953 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700954 if (ar.result == null) {
955 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700956 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700957 if (ar.exception != null) {
958 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
959 }
960
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700961 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700962 if (ar.exception instanceof CommandException) {
963 CommandException.Error error =
Chen Xue9d737e2022-01-01 23:41:31 -0800964 ((CommandException) (ar.exception)).getCommandError();
Junda Liua754ba12015-05-20 01:17:52 -0700965 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700966 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700967 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700968 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700969 }
970 }
971 openChannelResp = new IccOpenLogicalChannelResponse(
Chen Xue9d737e2022-01-01 23:41:31 -0800972 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700973 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700974 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700975 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700976 break;
977
978 case CMD_CLOSE_CHANNEL:
979 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000980 uiccPort = getUiccPortFromRequest(request);
981 if (uiccPort == null) {
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700982 loge("iccCloseLogicalChannel: No UICC");
Chen Xua8f0dff2022-02-12 00:34:15 -0800983 request.result = new IllegalArgumentException(
Thomas Nguyen8ee49682023-02-01 11:46:09 -0800984 "iccCloseLogicalChannel: No UICC");
Chen Xue9d737e2022-01-01 23:41:31 -0800985 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700986 } else {
987 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +0000988 uiccPort.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700989 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700990 break;
991
992 case EVENT_CLOSE_CHANNEL_DONE:
Chen Xu540470b2021-12-14 17:15:47 -0800993 ar = (AsyncResult) msg.obj;
994 request = (MainThreadRequest) ar.userObj;
995 if (ar.exception == null) {
996 request.result = true;
Rambo Wang3b77c4c2021-11-10 20:15:19 -0800997 uiccPort = getUiccPortFromRequest(request);
Rambo Wange53e07d2022-05-10 13:01:13 -0700998 if (uiccPort == null) {
999 loge("EVENT_CLOSE_CHANNEL_DONE: UiccPort is null");
1000 } else {
1001 final int channelId = (Integer) request.argument;
1002 uiccPort.onLogicalChannelClosed(channelId);
1003 }
Chen Xu540470b2021-12-14 17:15:47 -08001004 } else {
1005 request.result = false;
Chen Xue9d737e2022-01-01 23:41:31 -08001006 Exception exception = null;
Chen Xu540470b2021-12-14 17:15:47 -08001007 if (ar.exception instanceof CommandException) {
1008 loge("iccCloseLogicalChannel: CommandException: " + ar.exception);
1009 CommandException.Error error =
1010 ((CommandException) (ar.exception)).getCommandError();
Chen Xue9d737e2022-01-01 23:41:31 -08001011 if (error == CommandException.Error.INVALID_ARGUMENTS) {
1012 // should only throw exceptions from the binder threads.
1013 exception = new IllegalArgumentException(
Chen Xu540470b2021-12-14 17:15:47 -08001014 "iccCloseLogicalChannel: invalid argument ");
1015 }
1016 } else {
1017 loge("iccCloseLogicalChannel: Unknown exception");
1018 }
Chen Xua8f0dff2022-02-12 00:34:15 -08001019 request.result = (exception != null) ? exception :
1020 new IllegalStateException(
1021 "exception from modem to close iccLogical Channel");
Chen Xu540470b2021-12-14 17:15:47 -08001022 }
1023 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001024 break;
1025
1026 case CMD_NV_READ_ITEM:
1027 request = (MainThreadRequest) msg.obj;
1028 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001029 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
1030 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -08001031 break;
1032
1033 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -07001034 ar = (AsyncResult) msg.obj;
1035 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -08001036 if (ar.exception == null && ar.result != null) {
1037 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -07001038 } else {
Jake Hambye994d462014-02-03 13:10:13 -08001039 request.result = "";
1040 if (ar.result == null) {
1041 loge("nvReadItem: Empty response");
1042 } else if (ar.exception instanceof CommandException) {
1043 loge("nvReadItem: CommandException: " +
1044 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001045 } else {
Jake Hambye994d462014-02-03 13:10:13 -08001046 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -07001047 }
1048 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001049 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001050 break;
1051
Jake Hambye994d462014-02-03 13:10:13 -08001052 case CMD_NV_WRITE_ITEM:
1053 request = (MainThreadRequest) msg.obj;
1054 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
1055 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001056 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -07001057 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -08001058 break;
1059
1060 case EVENT_NV_WRITE_ITEM_DONE:
1061 handleNullReturnEvent(msg, "nvWriteItem");
1062 break;
1063
1064 case CMD_NV_WRITE_CDMA_PRL:
1065 request = (MainThreadRequest) msg.obj;
1066 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001067 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -08001068 break;
1069
1070 case EVENT_NV_WRITE_CDMA_PRL_DONE:
1071 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
1072 break;
1073
chen xu6dac5ab2018-10-26 17:39:23 -07001074 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -08001075 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -07001076 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001077 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -08001078 break;
1079
chen xu6dac5ab2018-10-26 17:39:23 -07001080 case EVENT_RESET_MODEM_CONFIG_DONE:
1081 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -08001082 break;
1083
Sooraj Sasindran37444802020-08-11 10:40:43 -07001084 case CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED: {
1085 request = (MainThreadRequest) msg.obj;
1086 onCompleted = obtainMessage(EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE,
1087 request);
1088 Phone phone = getPhoneFromRequest(request);
1089 if (phone != null) {
1090 phone.isNrDualConnectivityEnabled(onCompleted, request.workSource);
1091 } else {
1092 loge("isNRDualConnectivityEnabled: No phone object");
1093 request.result = false;
1094 notifyRequester(request);
1095 }
1096 break;
1097 }
1098
1099 case EVENT_IS_NR_DUAL_CONNECTIVITY_ENABLED_DONE:
1100 ar = (AsyncResult) msg.obj;
1101 request = (MainThreadRequest) ar.userObj;
1102 if (ar.exception == null && ar.result != null) {
1103 request.result = ar.result;
1104 } else {
1105 // request.result must be set to something non-null
1106 // for the calling thread to unblock
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001107 if (ar.result != null) {
Sooraj Sasindran37444802020-08-11 10:40:43 -07001108 request.result = ar.result;
1109 } else {
1110 request.result = false;
1111 }
1112 if (ar.result == null) {
1113 loge("isNRDualConnectivityEnabled: Empty response");
1114 } else if (ar.exception instanceof CommandException) {
1115 loge("isNRDualConnectivityEnabled: CommandException: "
1116 + ar.exception);
1117 } else {
1118 loge("isNRDualConnectivityEnabled: Unknown exception");
1119 }
1120 }
1121 notifyRequester(request);
1122 break;
1123
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001124 case CMD_IS_VONR_ENABLED: {
1125 request = (MainThreadRequest) msg.obj;
1126 onCompleted = obtainMessage(EVENT_IS_VONR_ENABLED_DONE,
1127 request);
1128 Phone phone = getPhoneFromRequest(request);
1129 if (phone != null) {
1130 phone.isVoNrEnabled(onCompleted, request.workSource);
1131 } else {
1132 loge("isVoNrEnabled: No phone object");
1133 request.result = false;
1134 notifyRequester(request);
1135 }
1136 break;
1137 }
1138
1139 case EVENT_IS_VONR_ENABLED_DONE:
1140 ar = (AsyncResult) msg.obj;
1141 request = (MainThreadRequest) ar.userObj;
1142 if (ar.exception == null && ar.result != null) {
1143 request.result = ar.result;
1144 } else {
1145 // request.result must be set to something non-null
1146 // for the calling thread to unblock
1147 if (ar.result != null) {
1148 request.result = ar.result;
1149 } else {
1150 request.result = false;
1151 }
1152 if (ar.result == null) {
1153 loge("isVoNrEnabled: Empty response");
1154 } else if (ar.exception instanceof CommandException) {
1155 loge("isVoNrEnabled: CommandException: "
1156 + ar.exception);
1157 } else {
1158 loge("isVoNrEnabled: Unknown exception");
1159 }
1160 }
1161 notifyRequester(request);
1162 break;
1163
Sooraj Sasindran37444802020-08-11 10:40:43 -07001164 case CMD_ENABLE_NR_DUAL_CONNECTIVITY: {
1165 request = (MainThreadRequest) msg.obj;
1166 onCompleted = obtainMessage(EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE, request);
1167 Phone phone = getPhoneFromRequest(request);
1168 if (phone != null) {
1169 phone.setNrDualConnectivityState((int) request.argument, onCompleted,
1170 request.workSource);
1171 } else {
1172 loge("enableNrDualConnectivity: No phone object");
1173 request.result =
1174 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
1175 notifyRequester(request);
1176 }
1177 break;
1178 }
1179
1180 case EVENT_ENABLE_NR_DUAL_CONNECTIVITY_DONE: {
1181 ar = (AsyncResult) msg.obj;
1182 request = (MainThreadRequest) ar.userObj;
1183 if (ar.exception == null) {
1184 request.result =
1185 TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_SUCCESS;
1186 } else {
1187 request.result =
1188 TelephonyManager
1189 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_ERROR;
1190 if (ar.exception instanceof CommandException) {
1191 CommandException.Error error =
1192 ((CommandException) (ar.exception)).getCommandError();
1193 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1194 request.result =
1195 TelephonyManager
1196 .ENABLE_NR_DUAL_CONNECTIVITY_RADIO_NOT_AVAILABLE;
Sooraj Sasindran29654162021-03-03 23:00:01 +00001197 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1198 request.result =
1199 TelephonyManager
1200 .ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
Sooraj Sasindran37444802020-08-11 10:40:43 -07001201 }
1202 loge("enableNrDualConnectivity" + ": CommandException: "
1203 + ar.exception);
1204 } else {
1205 loge("enableNrDualConnectivity" + ": Unknown exception");
1206 }
1207 }
1208 notifyRequester(request);
1209 break;
1210 }
1211
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07001212 case CMD_ENABLE_VONR: {
1213 request = (MainThreadRequest) msg.obj;
1214 onCompleted = obtainMessage(EVENT_ENABLE_VONR_DONE, request);
1215 Phone phone = getPhoneFromRequest(request);
1216 if (phone != null) {
1217 phone.setVoNrEnabled((boolean) request.argument, onCompleted,
1218 request.workSource);
1219 } else {
1220 loge("setVoNrEnabled: No phone object");
1221 request.result =
1222 TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1223 notifyRequester(request);
1224 }
1225 break;
1226 }
1227
1228 case EVENT_ENABLE_VONR_DONE: {
1229 ar = (AsyncResult) msg.obj;
1230 request = (MainThreadRequest) ar.userObj;
1231 if (ar.exception == null) {
1232 request.result = TelephonyManager.ENABLE_VONR_SUCCESS;
1233 } else {
1234 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1235 if (ar.exception instanceof CommandException) {
1236 CommandException.Error error =
1237 ((CommandException) (ar.exception)).getCommandError();
1238 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1239 request.result = TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
1240 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1241 request.result = TelephonyManager.ENABLE_VONR_REQUEST_NOT_SUPPORTED;
1242 } else {
1243 request.result = TelephonyManager.ENABLE_VONR_RADIO_ERROR;
1244 }
1245 loge("setVoNrEnabled" + ": CommandException: "
1246 + ar.exception);
1247 } else {
1248 loge("setVoNrEnabled" + ": Unknown exception");
1249 }
1250 }
1251 notifyRequester(request);
1252 break;
1253 }
1254
SongFerngWang3ef3e072020-12-21 16:41:52 +08001255 case CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK:
Jake Hamby7c27be32014-03-03 13:25:59 -08001256 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001257 onCompleted = obtainMessage(EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE,
1258 request);
1259 getPhoneFromRequest(request).getAllowedNetworkTypesBitmask(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001260 break;
1261
SongFerngWang3ef3e072020-12-21 16:41:52 +08001262 case EVENT_GET_ALLOWED_NETWORK_TYPES_BITMASK_DONE:
Jake Hamby7c27be32014-03-03 13:25:59 -08001263 ar = (AsyncResult) msg.obj;
1264 request = (MainThreadRequest) ar.userObj;
1265 if (ar.exception == null && ar.result != null) {
1266 request.result = ar.result; // Integer
1267 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +05301268 // request.result must be set to something non-null
1269 // for the calling thread to unblock
1270 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -08001271 if (ar.result == null) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001272 loge("getAllowedNetworkTypesBitmask: Empty response");
Jake Hamby7c27be32014-03-03 13:25:59 -08001273 } else if (ar.exception instanceof CommandException) {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001274 loge("getAllowedNetworkTypesBitmask: CommandException: "
1275 + ar.exception);
Jake Hamby7c27be32014-03-03 13:25:59 -08001276 } else {
SongFerngWang3ef3e072020-12-21 16:41:52 +08001277 loge("getAllowedNetworkTypesBitmask: Unknown exception");
Jake Hamby7c27be32014-03-03 13:25:59 -08001278 }
1279 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001280 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -08001281 break;
1282
SongFerngWang3ef3e072020-12-21 16:41:52 +08001283 case CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON:
Jake Hamby7c27be32014-03-03 13:25:59 -08001284 request = (MainThreadRequest) msg.obj;
SongFerngWang3ef3e072020-12-21 16:41:52 +08001285 onCompleted = obtainMessage(EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE,
1286 request);
1287 Pair<Integer, Long> reasonWithNetworkTypes =
1288 (Pair<Integer, Long>) request.argument;
1289 getPhoneFromRequest(request).setAllowedNetworkTypes(
1290 reasonWithNetworkTypes.first,
1291 reasonWithNetworkTypes.second,
1292 onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -08001293 break;
1294
SongFerngWang3ef3e072020-12-21 16:41:52 +08001295 case EVENT_SET_ALLOWED_NETWORK_TYPES_FOR_REASON_DONE:
1296 handleNullReturnEvent(msg, "setAllowedNetworkTypesForReason");
Jake Hamby7c27be32014-03-03 13:25:59 -08001297 break;
1298
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001299 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
1300 request = (MainThreadRequest)msg.obj;
1301 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001302 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001303 break;
1304
1305 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
1306 ar = (AsyncResult)msg.obj;
1307 request = (MainThreadRequest)ar.userObj;
1308 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001309 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00001310 break;
1311
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001312 case CMD_SET_VOICEMAIL_NUMBER:
1313 request = (MainThreadRequest) msg.obj;
1314 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
1315 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -08001316 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
1317 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001318 break;
1319
1320 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
1321 handleNullReturnEvent(msg, "setVoicemailNumber");
1322 break;
1323
Stuart Scott54788802015-03-30 13:18:01 -07001324 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
1325 request = (MainThreadRequest) msg.obj;
1326 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
1327 request);
1328 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
1329 break;
1330
1331 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
1332 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
1333 break;
1334
Shishir Agrawal302c8692015-06-19 13:49:39 -07001335 case CMD_PERFORM_NETWORK_SCAN:
1336 request = (MainThreadRequest) msg.obj;
1337 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
1338 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
1339 break;
1340
Hall Liu27d24262020-09-18 19:04:59 -07001341 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001342 request = (MainThreadRequest) msg.obj;
1343 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001344 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
1345 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1346 request.argument;
1347 int callForwardingReason = args.first;
1348 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001349 break;
Hall Liu27d24262020-09-18 19:04:59 -07001350 }
1351 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001352 ar = (AsyncResult) msg.obj;
1353 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001354 TelephonyManager.CallForwardingInfoCallback callback =
1355 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
1356 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -08001357 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -07001358 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -08001359 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
1360 for (CallForwardInfo callForwardInfo : callForwardInfos) {
1361 // Service Class is a bit mask per 3gpp 27.007. Search for
1362 // any service for voice call.
1363 if ((callForwardInfo.serviceClass
1364 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Yuchen Dong69cc1412021-09-27 20:27:01 +08001365 callForwardingInfo = new CallForwardingInfo(
1366 callForwardInfo.status
1367 == CommandsInterface.CF_ACTION_ENABLE,
Hall Liu27d24262020-09-18 19:04:59 -07001368 callForwardInfo.reason,
1369 callForwardInfo.number,
1370 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -08001371 break;
1372 }
1373 }
1374 // Didn't find a call forward info for voice call.
1375 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -07001376 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
1377 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -08001378 }
Hall Liu27d24262020-09-18 19:04:59 -07001379 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -08001380 } else {
1381 if (ar.result == null) {
1382 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
1383 }
1384 if (ar.exception != null) {
1385 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
1386 }
Hall Liu940c4ca2020-09-29 17:10:18 -07001387 int errorCode = TelephonyManager
1388 .CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -08001389 if (ar.exception instanceof CommandException) {
1390 CommandException.Error error =
1391 ((CommandException) (ar.exception)).getCommandError();
1392 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001393 errorCode = TelephonyManager
1394 .CallForwardingInfoCallback.RESULT_ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001395 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001396 errorCode = TelephonyManager
1397 .CallForwardingInfoCallback.RESULT_ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -08001398 }
1399 }
Hall Liu27d24262020-09-18 19:04:59 -07001400 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -08001401 }
Shuo Qian4a594052020-01-23 11:59:30 -08001402 break;
Hall Liu27d24262020-09-18 19:04:59 -07001403 }
Shuo Qian4a594052020-01-23 11:59:30 -08001404
Hall Liu27d24262020-09-18 19:04:59 -07001405 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001406 request = (MainThreadRequest) msg.obj;
1407 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001408 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -08001409 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -07001410 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1411 request.argument).first;
1412 request.phone.setCallForwardingOption(
1413 callForwardingInfoToSet.isEnabled()
Calvin Pan258f1f72021-07-28 21:46:56 +08001414 ? CommandsInterface.CF_ACTION_REGISTRATION
Hall Liu27d24262020-09-18 19:04:59 -07001415 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -08001416 callForwardingInfoToSet.getReason(),
1417 callForwardingInfoToSet.getNumber(),
1418 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
1419 break;
Hall Liu27d24262020-09-18 19:04:59 -07001420 }
Shuo Qian4a594052020-01-23 11:59:30 -08001421
Hall Liu27d24262020-09-18 19:04:59 -07001422 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001423 ar = (AsyncResult) msg.obj;
1424 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001425 Consumer<Integer> callback =
1426 ((Pair<CallForwardingInfo, Consumer<Integer>>)
1427 request.argument).second;
1428 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001429 loge("setCallForwarding exception: " + ar.exception);
Hall Liu940c4ca2020-09-29 17:10:18 -07001430 int errorCode = TelephonyManager.CallForwardingInfoCallback
1431 .RESULT_ERROR_UNKNOWN;
Hall Liu27d24262020-09-18 19:04:59 -07001432 if (ar.exception instanceof CommandException) {
1433 CommandException.Error error =
1434 ((CommandException) (ar.exception)).getCommandError();
1435 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001436 errorCode = TelephonyManager.CallForwardingInfoCallback
1437 .RESULT_ERROR_FDN_CHECK_FAILURE;
Hall Liu27d24262020-09-18 19:04:59 -07001438 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu940c4ca2020-09-29 17:10:18 -07001439 errorCode = TelephonyManager.CallForwardingInfoCallback
1440 .RESULT_ERROR_NOT_SUPPORTED;
Hall Liu27d24262020-09-18 19:04:59 -07001441 }
1442 }
1443 callback.accept(errorCode);
1444 } else {
Hall Liu940c4ca2020-09-29 17:10:18 -07001445 callback.accept(TelephonyManager.CallForwardingInfoCallback.RESULT_SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -08001446 }
Shuo Qian4a594052020-01-23 11:59:30 -08001447 break;
Hall Liu27d24262020-09-18 19:04:59 -07001448 }
Shuo Qian4a594052020-01-23 11:59:30 -08001449
Hall Liu27d24262020-09-18 19:04:59 -07001450 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001451 request = (MainThreadRequest) msg.obj;
1452 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
1453 getPhoneFromRequest(request).getCallWaiting(onCompleted);
1454 break;
Hall Liu27d24262020-09-18 19:04:59 -07001455 }
Shuo Qian4a594052020-01-23 11:59:30 -08001456
Hall Liu27d24262020-09-18 19:04:59 -07001457 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001458 ar = (AsyncResult) msg.obj;
1459 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001460 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
SongFerngWangebda2c52022-01-11 15:28:38 +08001461 int callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
Shuo Qian4a594052020-01-23 11:59:30 -08001462 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001463 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -08001464 // Service Class is a bit mask per 3gpp 27.007.
1465 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -08001466 if (callForwardResults.length > 1
1467 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -07001468 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001469 callWaitingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -07001470 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
1471 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001472 } else {
SongFerngWangebda2c52022-01-11 15:28:38 +08001473 callWaitingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -08001474 }
1475 } else {
1476 if (ar.result == null) {
1477 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
1478 }
1479 if (ar.exception != null) {
1480 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
1481 }
1482 if (ar.exception instanceof CommandException) {
1483 CommandException.Error error =
1484 ((CommandException) (ar.exception)).getCommandError();
1485 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
SongFerngWangebda2c52022-01-11 15:28:38 +08001486 callWaitingStatus =
Shuo Qian4a594052020-01-23 11:59:30 -08001487 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
SongFerngWangebda2c52022-01-11 15:28:38 +08001488 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1489 callWaitingStatus =
1490 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -08001491 }
1492 }
1493 }
SongFerngWangebda2c52022-01-11 15:28:38 +08001494 callback.accept(callWaitingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -08001495 break;
Hall Liu27d24262020-09-18 19:04:59 -07001496 }
Shuo Qian4a594052020-01-23 11:59:30 -08001497
Hall Liu27d24262020-09-18 19:04:59 -07001498 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -08001499 request = (MainThreadRequest) msg.obj;
1500 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -07001501 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1502 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -08001503 break;
Hall Liu27d24262020-09-18 19:04:59 -07001504 }
Shuo Qian4a594052020-01-23 11:59:30 -08001505
Hall Liu27d24262020-09-18 19:04:59 -07001506 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -08001507 ar = (AsyncResult) msg.obj;
1508 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -07001509 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
1510 Consumer<Integer> callback =
1511 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
1512 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -08001513 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -07001514 if (ar.exception instanceof CommandException) {
1515 CommandException.Error error =
1516 ((CommandException) (ar.exception)).getCommandError();
1517 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1518 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
SongFerngWangebda2c52022-01-11 15:28:38 +08001519 } else if (error == CommandException.Error.FDN_CHECK_FAILURE) {
1520 callback.accept(
1521 TelephonyManager.CALL_WAITING_STATUS_FDN_CHECK_FAILURE);
Hall Liu27d24262020-09-18 19:04:59 -07001522 } else {
1523 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1524 }
1525 } else {
1526 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1527 }
1528 } else {
1529 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1530 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001531 }
Shuo Qian4a594052020-01-23 11:59:30 -08001532 break;
Hall Liu27d24262020-09-18 19:04:59 -07001533 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07001534 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1535 ar = (AsyncResult) msg.obj;
1536 request = (MainThreadRequest) ar.userObj;
1537 CellNetworkScanResult cellScanResult;
1538 if (ar.exception == null && ar.result != null) {
1539 cellScanResult = new CellNetworkScanResult(
1540 CellNetworkScanResult.STATUS_SUCCESS,
1541 (List<OperatorInfo>) ar.result);
1542 } else {
1543 if (ar.result == null) {
1544 loge("getCellNetworkScanResults: Empty response");
1545 }
1546 if (ar.exception != null) {
1547 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1548 }
1549 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1550 if (ar.exception instanceof CommandException) {
1551 CommandException.Error error =
Thomas Nguyen8ee49682023-02-01 11:46:09 -08001552 ((CommandException) (ar.exception)).getCommandError();
Shishir Agrawal302c8692015-06-19 13:49:39 -07001553 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1554 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1555 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1556 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1557 }
1558 }
1559 cellScanResult = new CellNetworkScanResult(errorCode, null);
1560 }
1561 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001562 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001563 break;
1564
1565 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1566 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001567 ManualNetworkSelectionArgument selArg =
1568 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001569 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1570 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001571 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1572 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001573 break;
1574
1575 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001576 ar = (AsyncResult) msg.obj;
1577 request = (MainThreadRequest) ar.userObj;
1578 if (ar.exception == null) {
1579 request.result = true;
1580 } else {
1581 request.result = false;
1582 loge("setNetworkSelectionModeManual " + ar.exception);
1583 }
1584 notifyRequester(request);
1585 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001586 break;
1587
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001588 case CMD_GET_MODEM_ACTIVITY_INFO:
1589 request = (MainThreadRequest) msg.obj;
1590 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001591 if (defaultPhone != null) {
1592 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001593 } else {
1594 ResultReceiver result = (ResultReceiver) request.argument;
1595 Bundle bundle = new Bundle();
1596 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Hall Liu49656c02020-10-09 19:00:11 -07001597 new ModemActivityInfo(0, 0, 0,
1598 new int[ModemActivityInfo.getNumTxPowerLevels()], 0));
Shuo Qian8f4750a2020-02-20 17:12:10 -08001599 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001600 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001601 break;
1602
Hall Liud0f208c2020-10-14 16:54:44 -07001603 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE: {
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001604 ar = (AsyncResult) msg.obj;
1605 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001606 ResultReceiver result = (ResultReceiver) request.argument;
Hall Liud0f208c2020-10-14 16:54:44 -07001607 int error = 0;
Kai Shi917fdc62022-11-28 14:01:02 -08001608 ModemActivityInfo ret = null;
Gary Jian3aa9a762022-01-24 16:41:19 +08001609 if (mLastModemActivityInfo == null) {
1610 mLastModemActivitySpecificInfo = new ActivityStatsTechSpecificInfo[1];
1611 mLastModemActivitySpecificInfo[0] =
1612 new ActivityStatsTechSpecificInfo(
1613 0,
1614 0,
1615 new int[ModemActivityInfo.getNumTxPowerLevels()],
1616 0);
1617 mLastModemActivityInfo =
1618 new ModemActivityInfo(0, 0, 0, mLastModemActivitySpecificInfo);
1619 }
1620
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001621 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001622 // Update the last modem activity info and the result of the request.
1623 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1624 if (isModemActivityInfoValid(info)) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001625 mergeModemActivityInfo(info);
Gary Jian76280a42022-12-07 16:18:33 +08001626 } else {
1627 loge("queryModemActivityInfo: invalid response");
Shuo Qian8f4750a2020-02-20 17:12:10 -08001628 }
Kai Shi917fdc62022-11-28 14:01:02 -08001629 // This is needed to decouple ret from mLastModemActivityInfo
1630 // We don't want to return mLastModemActivityInfo which is updated
1631 // inside mergeModemActivityInfo()
1632 ret = new ModemActivityInfo(
1633 mLastModemActivityInfo.getTimestampMillis(),
1634 mLastModemActivityInfo.getSleepTimeMillis(),
1635 mLastModemActivityInfo.getIdleTimeMillis(),
1636 deepCopyModemActivitySpecificInfo(mLastModemActivitySpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08001637
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001638 } else {
1639 if (ar.result == null) {
1640 loge("queryModemActivityInfo: Empty response");
Hall Liud0f208c2020-10-14 16:54:44 -07001641 error = TelephonyManager.ModemActivityInfoException
1642 .ERROR_INVALID_INFO_RECEIVED;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001643 } else if (ar.exception instanceof CommandException) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001644 loge("queryModemActivityInfo: CommandException: " + ar.exception);
Hall Liud0f208c2020-10-14 16:54:44 -07001645 error = TelephonyManager.ModemActivityInfoException
1646 .ERROR_MODEM_RESPONSE_ERROR;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001647 } else {
1648 loge("queryModemActivityInfo: Unknown exception");
Hall Liud0f208c2020-10-14 16:54:44 -07001649 error = TelephonyManager.ModemActivityInfoException
1650 .ERROR_UNKNOWN;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001651 }
1652 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001653 Bundle bundle = new Bundle();
Kai Shi917fdc62022-11-28 14:01:02 -08001654 if (ret != null) {
Gary Jian3aa9a762022-01-24 16:41:19 +08001655 bundle.putParcelable(
1656 TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
Kai Shi917fdc62022-11-28 14:01:02 -08001657 ret);
Hall Liud0f208c2020-10-14 16:54:44 -07001658 } else {
1659 bundle.putInt(TelephonyManager.EXCEPTION_RESULT_KEY, error);
1660 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001661 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001662 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001663 break;
Hall Liud0f208c2020-10-14 16:54:44 -07001664 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001665
Meng Wang1a7c35a2016-05-05 20:56:15 -07001666 case CMD_SET_ALLOWED_CARRIERS:
1667 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001668 CarrierRestrictionRules argument =
1669 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001670 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001671 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001672 break;
1673
1674 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1675 ar = (AsyncResult) msg.obj;
1676 request = (MainThreadRequest) ar.userObj;
1677 if (ar.exception == null && ar.result != null) {
1678 request.result = ar.result;
1679 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001680 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1681 if (ar.exception instanceof CommandException) {
1682 loge("setAllowedCarriers: CommandException: " + ar.exception);
1683 CommandException.Error error =
1684 ((CommandException) (ar.exception)).getCommandError();
1685 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1686 request.result =
1687 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1688 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001689 } else {
1690 loge("setAllowedCarriers: Unknown exception");
1691 }
1692 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001693 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001694 break;
1695
1696 case CMD_GET_ALLOWED_CARRIERS:
1697 request = (MainThreadRequest) msg.obj;
1698 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001699 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001700 break;
1701
1702 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1703 ar = (AsyncResult) msg.obj;
1704 request = (MainThreadRequest) ar.userObj;
1705 if (ar.exception == null && ar.result != null) {
1706 request.result = ar.result;
1707 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001708 request.result = new IllegalStateException(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08001709 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001710 if (ar.result == null) {
1711 loge("getAllowedCarriers: Empty response");
1712 } else if (ar.exception instanceof CommandException) {
1713 loge("getAllowedCarriers: CommandException: " +
1714 ar.exception);
1715 } else {
1716 loge("getAllowedCarriers: Unknown exception");
1717 }
1718 }
arunvoddud7401012022-12-15 16:08:12 +00001719 if (request.argument != null) {
1720 // This is for the implementation of carrierRestrictionStatus.
1721 CallerCallbackInfo callbackInfo = (CallerCallbackInfo) request.argument;
1722 Consumer<Integer> callback = callbackInfo.getConsumer();
1723 int callerCarrierId = callbackInfo.getCarrierId();
1724 int lockStatus = TelephonyManager.CARRIER_RESTRICTION_STATUS_UNKNOWN;
1725 if (ar.exception == null && ar.result instanceof CarrierRestrictionRules) {
1726 CarrierRestrictionRules carrierRestrictionRules =
1727 (CarrierRestrictionRules) ar.result;
1728 int carrierId = -1;
1729 try {
1730 CarrierIdentifier carrierIdentifier =
1731 carrierRestrictionRules.getAllowedCarriers().get(0);
1732 carrierId = CarrierResolver.getCarrierIdFromIdentifier(mApp,
1733 carrierIdentifier);
1734 } catch (NullPointerException | IndexOutOfBoundsException ex) {
1735 Rlog.e(LOG_TAG, "CarrierIdentifier exception = " + ex);
1736 }
1737 lockStatus = carrierRestrictionRules.getCarrierRestrictionStatus();
1738 if (carrierId != -1 && callerCarrierId == carrierId && lockStatus
1739 == TelephonyManager.CARRIER_RESTRICTION_STATUS_RESTRICTED) {
Thomas Nguyen8ee49682023-02-01 11:46:09 -08001740 lockStatus = TelephonyManager
1741 .CARRIER_RESTRICTION_STATUS_RESTRICTED_TO_CALLER;
arunvoddud7401012022-12-15 16:08:12 +00001742 }
1743 } else {
1744 Rlog.e(LOG_TAG,
1745 "getCarrierRestrictionStatus: exception ex = " + ar.exception);
1746 }
1747 callback.accept(lockStatus);
1748 } else {
1749 // This is for the implementation of getAllowedCarriers.
1750 notifyRequester(request);
1751 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001752 break;
1753
Nathan Haroldb3014052017-01-25 15:57:32 -08001754 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1755 ar = (AsyncResult) msg.obj;
1756 request = (MainThreadRequest) ar.userObj;
1757 if (ar.exception == null && ar.result != null) {
1758 request.result = ar.result;
1759 } else {
1760 request.result = new IllegalArgumentException(
1761 "Failed to retrieve Forbidden Plmns");
1762 if (ar.result == null) {
1763 loge("getForbiddenPlmns: Empty response");
1764 } else {
1765 loge("getForbiddenPlmns: Unknown exception");
1766 }
1767 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001768 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001769 break;
1770
1771 case CMD_GET_FORBIDDEN_PLMNS:
1772 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001773 uiccPort = getUiccPortFromRequest(request);
1774 if (uiccPort == null) {
1775 loge("getForbiddenPlmns() UiccPort is null");
Nathan Haroldb3014052017-01-25 15:57:32 -08001776 request.result = new IllegalArgumentException(
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001777 "getForbiddenPlmns() UiccPort is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001778 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001779 break;
1780 }
1781 Integer appType = (Integer) request.argument;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00001782 UiccCardApplication uiccApp = uiccPort.getApplicationByType(appType);
Nathan Haroldb3014052017-01-25 15:57:32 -08001783 if (uiccApp == null) {
1784 loge("getForbiddenPlmns() no app with specified type -- "
1785 + appType);
1786 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001787 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001788 break;
1789 } else {
1790 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1791 + " specified type -- " + appType);
1792 }
1793 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1794 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08001795 onCompleted);
Nathan Haroldb3014052017-01-25 15:57:32 -08001796 break;
1797
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001798 case CMD_SWITCH_SLOTS:
1799 request = (MainThreadRequest) msg.obj;
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001800 List<UiccSlotMapping> slotMapping = (List<UiccSlotMapping>) request.argument;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001801 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00001802 UiccController.getInstance().switchSlots(slotMapping, onCompleted);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001803 break;
1804
1805 case EVENT_SWITCH_SLOTS_DONE:
1806 ar = (AsyncResult) msg.obj;
1807 request = (MainThreadRequest) ar.userObj;
1808 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001809 notifyRequester(request);
1810 break;
1811 case CMD_GET_NETWORK_SELECTION_MODE:
1812 request = (MainThreadRequest) msg.obj;
1813 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1814 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1815 break;
1816
1817 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1818 ar = (AsyncResult) msg.obj;
1819 request = (MainThreadRequest) ar.userObj;
1820 if (ar.exception != null) {
1821 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1822 } else {
1823 int mode = ((int[]) ar.result)[0];
1824 if (mode == 0) {
1825 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1826 } else {
1827 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1828 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001829 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001830 notifyRequester(request);
1831 break;
1832 case CMD_GET_CDMA_ROAMING_MODE:
1833 request = (MainThreadRequest) msg.obj;
1834 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1835 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1836 break;
1837 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1838 ar = (AsyncResult) msg.obj;
1839 request = (MainThreadRequest) ar.userObj;
1840 if (ar.exception != null) {
1841 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1842 } else {
1843 request.result = ((int[]) ar.result)[0];
1844 }
1845 notifyRequester(request);
1846 break;
1847 case CMD_SET_CDMA_ROAMING_MODE:
1848 request = (MainThreadRequest) msg.obj;
1849 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1850 int mode = (int) request.argument;
1851 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1852 break;
1853 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1854 ar = (AsyncResult) msg.obj;
1855 request = (MainThreadRequest) ar.userObj;
1856 request.result = ar.exception == null;
1857 notifyRequester(request);
1858 break;
Sarah Chinbaab1432020-10-28 13:46:24 -07001859 case CMD_GET_CDMA_SUBSCRIPTION_MODE:
1860 request = (MainThreadRequest) msg.obj;
1861 onCompleted = obtainMessage(EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1862 getPhoneFromRequest(request).queryCdmaSubscriptionMode(onCompleted);
1863 break;
1864 case EVENT_GET_CDMA_SUBSCRIPTION_MODE_DONE:
1865 ar = (AsyncResult) msg.obj;
1866 request = (MainThreadRequest) ar.userObj;
1867 if (ar.exception != null) {
1868 request.result = TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM;
1869 } else {
1870 request.result = ((int[]) ar.result)[0];
1871 }
1872 notifyRequester(request);
1873 break;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001874 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1875 request = (MainThreadRequest) msg.obj;
1876 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1877 int subscriptionMode = (int) request.argument;
Sarah Chinbaab1432020-10-28 13:46:24 -07001878 getPhoneFromRequest(request).setCdmaSubscriptionMode(
1879 subscriptionMode, onCompleted);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001880 break;
1881 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1882 ar = (AsyncResult) msg.obj;
1883 request = (MainThreadRequest) ar.userObj;
1884 request.result = ar.exception == null;
1885 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001886 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001887 case CMD_GET_ALL_CELL_INFO:
1888 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001889 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001890 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001891 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001892 case EVENT_GET_ALL_CELL_INFO_DONE:
1893 ar = (AsyncResult) msg.obj;
1894 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001895 // If a timeout occurs, the response will be null
1896 request.result = (ar.exception == null && ar.result != null)
1897 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001898 synchronized (request) {
1899 request.notifyAll();
1900 }
1901 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001902 case CMD_REQUEST_CELL_INFO_UPDATE:
1903 request = (MainThreadRequest) msg.obj;
1904 request.phone.requestCellInfoUpdate(request.workSource,
1905 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1906 break;
1907 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1908 ar = (AsyncResult) msg.obj;
1909 request = (MainThreadRequest) ar.userObj;
1910 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1911 try {
1912 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001913 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001914 cb.onError(
1915 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1916 ar.exception.getClass().getName(),
1917 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001918 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001919 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001920 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001921 } else {
1922 // use the result as returned
1923 cb.onCellInfo((List<CellInfo>) ar.result);
1924 }
1925 } catch (RemoteException re) {
1926 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1927 }
1928 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001929 case CMD_GET_CELL_LOCATION: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001930 request = (MainThreadRequest) msg.obj;
1931 WorkSource ws = (WorkSource) request.argument;
1932 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001933 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001934 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001935 }
1936 case EVENT_GET_CELL_LOCATION_DONE: {
Nathan Harold3ff88932018-08-14 10:19:49 -07001937 ar = (AsyncResult) msg.obj;
1938 request = (MainThreadRequest) ar.userObj;
1939 if (ar.exception == null) {
1940 request.result = ar.result;
1941 } else {
Sarah Chin679c08a2020-11-18 13:39:35 -08001942 Phone phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001943 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001944 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001945 }
1946
1947 synchronized (request) {
1948 request.notifyAll();
1949 }
1950 break;
Sarah Chin679c08a2020-11-18 13:39:35 -08001951 }
chen xu6dac5ab2018-10-26 17:39:23 -07001952 case CMD_MODEM_REBOOT:
1953 request = (MainThreadRequest) msg.obj;
1954 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001955 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001956 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001957 case EVENT_CMD_MODEM_REBOOT_DONE:
1958 handleNullReturnEvent(msg, "rebootModem");
1959 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001960 case CMD_REQUEST_ENABLE_MODEM:
1961 request = (MainThreadRequest) msg.obj;
1962 boolean enable = (boolean) request.argument;
1963 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001964 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001965 PhoneConfigurationManager.getInstance()
1966 .enablePhone(request.phone, enable, onCompleted);
1967 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001968 case EVENT_ENABLE_MODEM_DONE: {
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001969 ar = (AsyncResult) msg.obj;
1970 request = (MainThreadRequest) ar.userObj;
1971 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001972 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001973 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001974 if ((boolean) request.result) {
1975 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1976 updateModemStateMetrics();
1977 } else {
1978 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1979 + ar.exception);
1980 }
1981 notifyRequester(request);
1982 break;
Michele Berionne5e411512020-11-13 02:36:59 +00001983 }
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001984 case CMD_GET_MODEM_STATUS:
1985 request = (MainThreadRequest) msg.obj;
1986 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1987 PhoneConfigurationManager.getInstance()
1988 .getPhoneStatusFromModem(request.phone, onCompleted);
1989 break;
1990 case EVENT_GET_MODEM_STATUS_DONE:
1991 ar = (AsyncResult) msg.obj;
1992 request = (MainThreadRequest) ar.userObj;
1993 int id = request.phone.getPhoneId();
1994 if (ar.exception == null && ar.result != null) {
1995 request.result = ar.result;
1996 //update the cache as modem status has changed
1997 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1998 (boolean) request.result);
1999 } else {
2000 // Return true if modem status cannot be retrieved. For most cases,
2001 // modem status is on. And for older version modems, GET_MODEM_STATUS
2002 // and disable modem are not supported. Modem is always on.
2003 // TODO: this should be fixed in R to support a third
2004 // status UNKNOWN b/131631629
2005 request.result = true;
2006 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
2007 + ar.exception);
2008 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08002009 notifyRequester(request);
2010 break;
Hall Liu73f5d362020-01-20 13:42:00 -08002011 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
2012 request = (MainThreadRequest) msg.obj;
2013 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
2014 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
2015 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
2016 request.phone.setSystemSelectionChannels(args.first, onCompleted);
2017 break;
2018 }
2019 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
2020 ar = (AsyncResult) msg.obj;
2021 request = (MainThreadRequest) ar.userObj;
2022 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
2023 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
2024 args.second.accept(ar.exception == null);
2025 notifyRequester(request);
2026 break;
2027 }
Sarah Chin679c08a2020-11-18 13:39:35 -08002028 case CMD_GET_SYSTEM_SELECTION_CHANNELS: {
2029 request = (MainThreadRequest) msg.obj;
2030 onCompleted = obtainMessage(EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE, request);
2031 Phone phone = getPhoneFromRequest(request);
2032 if (phone != null) {
2033 phone.getSystemSelectionChannels(onCompleted);
2034 } else {
2035 loge("getSystemSelectionChannels: No phone object");
2036 request.result = new ArrayList<RadioAccessSpecifier>();
2037 notifyRequester(request);
2038 }
2039 break;
2040 }
2041 case EVENT_GET_SYSTEM_SELECTION_CHANNELS_DONE:
2042 ar = (AsyncResult) msg.obj;
2043 request = (MainThreadRequest) ar.userObj;
2044 if (ar.exception == null && ar.result != null) {
2045 request.result = ar.result;
2046 } else {
Sarah Chin428d1d62021-03-13 03:17:40 -08002047 request.result = new IllegalStateException(
2048 "Failed to retrieve system selecton channels");
Sarah Chin679c08a2020-11-18 13:39:35 -08002049 if (ar.result == null) {
2050 loge("getSystemSelectionChannels: Empty response");
2051 } else {
2052 loge("getSystemSelectionChannels: Unknown exception");
2053 }
2054 }
2055 notifyRequester(request);
2056 break;
yincheng zhao2737e882019-09-06 17:06:54 -07002057 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
2058 ar = (AsyncResult) msg.obj;
2059 request = (MainThreadRequest) ar.userObj;
2060 if (ar.exception == null && ar.result != null) {
2061 request.result = ar.result;
2062 } else {
2063 request.result = -1;
2064 loge("Failed to set Forbidden Plmns");
2065 if (ar.result == null) {
2066 loge("setForbidenPlmns: Empty response");
2067 } else if (ar.exception != null) {
2068 loge("setForbiddenPlmns: Exception: " + ar.exception);
2069 request.result = -1;
2070 } else {
2071 loge("setForbiddenPlmns: Unknown exception");
2072 }
2073 }
2074 notifyRequester(request);
2075 break;
2076 case CMD_SET_FORBIDDEN_PLMNS:
2077 request = (MainThreadRequest) msg.obj;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002078 uiccPort = getUiccPortFromRequest(request);
2079 if (uiccPort == null) {
2080 loge("setForbiddenPlmns: UiccPort is null");
yincheng zhao2737e882019-09-06 17:06:54 -07002081 request.result = -1;
2082 notifyRequester(request);
2083 break;
2084 }
2085 Pair<Integer, List<String>> setFplmnsArgs =
2086 (Pair<Integer, List<String>>) request.argument;
2087 appType = setFplmnsArgs.first;
2088 List<String> fplmns = setFplmnsArgs.second;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00002089 uiccApp = uiccPort.getApplicationByType(appType);
yincheng zhao2737e882019-09-06 17:06:54 -07002090 if (uiccApp == null) {
2091 loge("setForbiddenPlmns: no app with specified type -- " + appType);
2092 request.result = -1;
2093 loge("Failed to get UICC App");
2094 notifyRequester(request);
2095 } else {
2096 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
2097 ((SIMRecords) uiccApp.getIccRecords())
2098 .setForbiddenPlmns(onCompleted, fplmns);
2099 }
yinchengzhao4d163c02019-12-12 15:21:47 -08002100 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07002101 case CMD_ERASE_MODEM_CONFIG:
2102 request = (MainThreadRequest) msg.obj;
2103 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
2104 defaultPhone.eraseModemConfig(onCompleted);
2105 break;
2106 case EVENT_ERASE_MODEM_CONFIG_DONE:
2107 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07002108 break;
zoey chene02881a2019-12-30 16:11:23 +08002109
Kai Shif70f46f2021-03-03 13:59:46 -08002110 case CMD_ERASE_DATA_SHARED_PREFERENCES:
2111 request = (MainThreadRequest) msg.obj;
2112 request.result = defaultPhone.eraseDataInSharedPreferences();
2113 notifyRequester(request);
2114 break;
2115
zoey chene02881a2019-12-30 16:11:23 +08002116 case CMD_CHANGE_ICC_LOCK_PASSWORD:
2117 request = (MainThreadRequest) msg.obj;
2118 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
2119 Pair<String, String> changed = (Pair<String, String>) request.argument;
2120 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
2121 changed.first, changed.second, onCompleted);
2122 break;
2123 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
2124 ar = (AsyncResult) msg.obj;
2125 request = (MainThreadRequest) ar.userObj;
2126 if (ar.exception == null) {
2127 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00002128 // If the operation is successful, update the PIN storage
2129 Pair<String, String> passwords = (Pair<String, String>) request.argument;
2130 int phoneId = getPhoneFromRequest(request).getPhoneId();
Jon Spivack9c3bc762021-10-06 20:53:09 +00002131 UiccController.getInstance().getPinStorage()
2132 .storePin(passwords.second, phoneId);
zoey chene02881a2019-12-30 16:11:23 +08002133 } else {
2134 request.result = msg.arg1;
2135 }
2136 notifyRequester(request);
2137 break;
2138
Michele Berionne5e411512020-11-13 02:36:59 +00002139 case CMD_SET_ICC_LOCK_ENABLED: {
zoey chene02881a2019-12-30 16:11:23 +08002140 request = (MainThreadRequest) msg.obj;
2141 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
2142 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
2143 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
2144 enabled.first, enabled.second, onCompleted);
2145 break;
Michele Berionne5e411512020-11-13 02:36:59 +00002146 }
zoey chene02881a2019-12-30 16:11:23 +08002147 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
2148 ar = (AsyncResult) msg.obj;
2149 request = (MainThreadRequest) ar.userObj;
2150 if (ar.exception == null) {
2151 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
Michele Berionne5e411512020-11-13 02:36:59 +00002152 // If the operation is successful, update the PIN storage
2153 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
2154 int phoneId = getPhoneFromRequest(request).getPhoneId();
2155 if (enabled.first) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00002156 UiccController.getInstance().getPinStorage()
2157 .storePin(enabled.second, phoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00002158 } else {
2159 UiccController.getInstance().getPinStorage().clearPin(phoneId);
2160 }
zoey chene02881a2019-12-30 16:11:23 +08002161 } else {
2162 request.result = msg.arg1;
2163 }
Michele Berionne5e411512020-11-13 02:36:59 +00002164
2165
zoey chene02881a2019-12-30 16:11:23 +08002166 notifyRequester(request);
2167 break;
2168
Peter Wangdafb9ac2020-01-15 14:13:38 -08002169 case MSG_NOTIFY_USER_ACTIVITY:
2170 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08002171 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08002172 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
2173 getDefaultPhone().getContext().sendBroadcastAsUser(
2174 intent, UserHandle.ALL, permission.USER_ACTIVITY);
2175 break;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002176
2177 case CMD_SET_DATA_THROTTLING: {
2178 request = (MainThreadRequest) msg.obj;
2179 onCompleted = obtainMessage(EVENT_SET_DATA_THROTTLING_DONE, request);
2180 DataThrottlingRequest dataThrottlingRequest =
2181 (DataThrottlingRequest) request.argument;
2182 Phone phone = getPhoneFromRequest(request);
2183 if (phone != null) {
2184 phone.setDataThrottling(onCompleted,
2185 request.workSource, dataThrottlingRequest.getDataThrottlingAction(),
2186 dataThrottlingRequest.getCompletionDurationMillis());
2187 } else {
2188 loge("setDataThrottling: No phone object");
2189 request.result =
2190 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
2191 notifyRequester(request);
2192 }
2193
2194 break;
2195 }
2196 case EVENT_SET_DATA_THROTTLING_DONE:
2197 ar = (AsyncResult) msg.obj;
2198 request = (MainThreadRequest) ar.userObj;
2199
2200 if (ar.exception == null) {
2201 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
2202 } else if (ar.exception instanceof CommandException) {
2203 loge("setDataThrottling: CommandException: " + ar.exception);
2204 CommandException.Error error =
2205 ((CommandException) (ar.exception)).getCommandError();
2206
2207 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
2208 request.result = TelephonyManager
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002209 .THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002210 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2211 request.result = SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -08002212 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2213 request.result = MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -08002214 } else {
2215 request.result =
2216 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2217 }
2218 } else {
2219 request.result = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
2220 }
2221 Log.w(LOG_TAG, "DataThrottlingResult = " + request.result);
2222 notifyRequester(request);
2223 break;
Jordan Liu109698e2020-11-24 14:50:34 -08002224
2225 case CMD_SET_SIM_POWER: {
2226 request = (MainThreadRequest) msg.obj;
2227 onCompleted = obtainMessage(EVENT_SET_SIM_POWER_DONE, request);
2228 request = (MainThreadRequest) msg.obj;
2229 int stateToSet =
2230 ((Pair<Integer, IIntegerConsumer>)
2231 request.argument).first;
2232 request.phone.setSimPowerState(stateToSet, onCompleted, request.workSource);
2233 break;
2234 }
2235 case EVENT_SET_SIM_POWER_DONE: {
2236 ar = (AsyncResult) msg.obj;
2237 request = (MainThreadRequest) ar.userObj;
2238 IIntegerConsumer callback =
2239 ((Pair<Integer, IIntegerConsumer>) request.argument).second;
2240 if (ar.exception != null) {
2241 loge("setSimPower exception: " + ar.exception);
2242 int errorCode = TelephonyManager.CallForwardingInfoCallback
2243 .RESULT_ERROR_UNKNOWN;
2244 if (ar.exception instanceof CommandException) {
2245 CommandException.Error error =
2246 ((CommandException) (ar.exception)).getCommandError();
2247 if (error == CommandException.Error.SIM_ERR) {
2248 errorCode = TelephonyManager.SET_SIM_POWER_STATE_SIM_ERROR;
2249 } else if (error == CommandException.Error.INVALID_ARGUMENTS) {
2250 errorCode = TelephonyManager.SET_SIM_POWER_STATE_ALREADY_IN_STATE;
2251 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
2252 errorCode = TelephonyManager.SET_SIM_POWER_STATE_NOT_SUPPORTED;
2253 } else {
2254 errorCode = TelephonyManager.SET_SIM_POWER_STATE_MODEM_ERROR;
2255 }
2256 }
2257 try {
2258 callback.accept(errorCode);
2259 } catch (RemoteException e) {
2260 // Ignore if the remote process is no longer available to call back.
2261 Log.w(LOG_TAG, "setSimPower: callback not available.");
2262 }
2263 } else {
2264 try {
2265 callback.accept(TelephonyManager.SET_SIM_POWER_STATE_SUCCESS);
2266 } catch (RemoteException e) {
2267 // Ignore if the remote process is no longer available to call back.
2268 Log.w(LOG_TAG, "setSimPower: callback not available.");
2269 }
2270 }
2271 break;
2272 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002273 case CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2274 request = (MainThreadRequest) msg.obj;
2275
2276 final Phone phone = getPhoneFromRequest(request);
2277 if (phone == null || phone.getServiceStateTracker() == null) {
2278 request.result = new IllegalStateException("Phone or SST is null");
2279 notifyRequester(request);
2280 break;
2281 }
2282
2283 Pair<Integer, SignalStrengthUpdateRequest> pair =
2284 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2285 onCompleted = obtainMessage(EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2286 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002287 phone.getSignalStrengthController().setSignalStrengthUpdateRequest(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002288 request.subId, pair.first /*callingUid*/,
2289 pair.second /*request*/, onCompleted);
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002290 break;
2291 }
2292 case EVENT_SET_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2293 ar = (AsyncResult) msg.obj;
2294 request = (MainThreadRequest) ar.userObj;
2295 // request.result will be the exception of ar if present, true otherwise.
2296 // Be cautious not to leave result null which will wait() forever
2297 request.result = ar.exception != null ? ar.exception : true;
2298 notifyRequester(request);
2299 break;
2300 }
2301 case CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST: {
2302 request = (MainThreadRequest) msg.obj;
2303
2304 Phone phone = getPhoneFromRequest(request);
2305 if (phone == null || phone.getServiceStateTracker() == null) {
2306 request.result = new IllegalStateException("Phone or SST is null");
2307 notifyRequester(request);
2308 break;
2309 }
2310
2311 Pair<Integer, SignalStrengthUpdateRequest> pair =
2312 (Pair<Integer, SignalStrengthUpdateRequest>) request.argument;
2313 onCompleted = obtainMessage(EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE,
2314 request);
Rambo Wang6568f172021-02-03 16:56:47 -08002315 phone.getSignalStrengthController().clearSignalStrengthUpdateRequest(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002316 request.subId, pair.first /*callingUid*/,
2317 pair.second /*request*/, onCompleted);
Rambo Wanga5cc9b72021-01-07 10:51:54 -08002318 break;
2319 }
2320 case EVENT_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST_DONE: {
2321 ar = (AsyncResult) msg.obj;
2322 request = (MainThreadRequest) ar.userObj;
2323 request.result = ar.exception != null ? ar.exception : true;
2324 notifyRequester(request);
2325 break;
2326 }
Jordan Liu109698e2020-11-24 14:50:34 -08002327
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002328 case CMD_GET_SLICING_CONFIG: {
2329 request = (MainThreadRequest) msg.obj;
2330 onCompleted = obtainMessage(EVENT_GET_SLICING_CONFIG_DONE, request);
2331 request.phone.getSlicingConfig(onCompleted);
2332 break;
2333 }
2334 case EVENT_GET_SLICING_CONFIG_DONE: {
2335 ar = (AsyncResult) msg.obj;
2336 request = (MainThreadRequest) ar.userObj;
2337 ResultReceiver result = (ResultReceiver) request.argument;
2338
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002339 NetworkSlicingConfig slicingConfig = null;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002340 Bundle bundle = new Bundle();
2341 int resultCode = 0;
2342 if (ar.exception != null) {
2343 Log.e(LOG_TAG, "Exception retrieving slicing configuration="
2344 + ar.exception);
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002345 resultCode = TelephonyManager.NetworkSlicingException.ERROR_MODEM_ERROR;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002346 } else if (ar.result == null) {
2347 Log.w(LOG_TAG, "Timeout Waiting for slicing configuration!");
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002348 resultCode = TelephonyManager.NetworkSlicingException.ERROR_TIMEOUT;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002349 } else {
2350 // use the result as returned
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002351 resultCode = TelephonyManager.NetworkSlicingException.SUCCESS;
2352 slicingConfig = (NetworkSlicingConfig) ar.result;
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002353 }
2354
2355 if (slicingConfig == null) {
Hongbo Zeng0e18b162021-04-07 16:52:18 +08002356 slicingConfig = new NetworkSlicingConfig();
Hongbo Zeng156aa4a2021-02-08 21:50:28 +08002357 }
2358 bundle.putParcelable(TelephonyManager.KEY_SLICING_CONFIG_HANDLE, slicingConfig);
2359 result.send(resultCode, bundle);
2360 notifyRequester(request);
2361 break;
2362 }
2363
Sarah Chin71b3a852022-09-28 15:54:19 -07002364 case CMD_PURCHASE_PREMIUM_CAPABILITY: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002365 request = (MainThreadRequest) msg.obj;
2366 onCompleted = obtainMessage(EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE, request);
Sarah Chin71b3a852022-09-28 15:54:19 -07002367 PurchasePremiumCapabilityArgument arg =
2368 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin46355ba2022-11-01 23:51:16 -07002369 SlicePurchaseController.getInstance(request.phone).purchasePremiumCapability(
Sarah Chinb8218c22023-01-04 13:35:29 -08002370 arg.capability, onCompleted);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002371 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002372 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002373
Sarah Chin71b3a852022-09-28 15:54:19 -07002374 case EVENT_PURCHASE_PREMIUM_CAPABILITY_DONE: {
Sarah Chin2ec39f62022-08-31 17:03:26 -07002375 ar = (AsyncResult) msg.obj;
2376 request = (MainThreadRequest) ar.userObj;
Sarah Chin71b3a852022-09-28 15:54:19 -07002377 PurchasePremiumCapabilityArgument arg =
2378 (PurchasePremiumCapabilityArgument) request.argument;
Sarah Chin2ec39f62022-08-31 17:03:26 -07002379 try {
2380 int result = (int) ar.result;
Sarah Chin71b3a852022-09-28 15:54:19 -07002381 arg.callback.accept(result);
Sarah Chin2ec39f62022-08-31 17:03:26 -07002382 log("purchasePremiumCapability: capability="
Sarah Chin71b3a852022-09-28 15:54:19 -07002383 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002384 + ", result= "
2385 + TelephonyManager.convertPurchaseResultToString(result));
2386 } catch (RemoteException e) {
2387 String logStr = "Purchase premium capability "
Sarah Chin71b3a852022-09-28 15:54:19 -07002388 + TelephonyManager.convertPremiumCapabilityToString(arg.capability)
Sarah Chin2ec39f62022-08-31 17:03:26 -07002389 + " failed: " + e;
2390 if (DBG) log(logStr);
2391 AnomalyReporter.reportAnomaly(
2392 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
2393 }
2394 break;
Sarah Chin71b3a852022-09-28 15:54:19 -07002395 }
Sarah Chin2ec39f62022-08-31 17:03:26 -07002396
Michele Berionne5e411512020-11-13 02:36:59 +00002397 case CMD_PREPARE_UNATTENDED_REBOOT:
2398 request = (MainThreadRequest) msg.obj;
2399 request.result =
Rafael Higuera Silvad9630642021-09-20 15:32:01 +00002400 UiccController.getInstance().getPinStorage()
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002401 .prepareUnattendedReboot(request.workSource);
Michele Berionne5e411512020-11-13 02:36:59 +00002402 notifyRequester(request);
2403 break;
2404
Sarah Chineccfbd12023-01-20 19:00:35 -08002405 case CMD_START_SATELLITE_POSITION_UPDATES: {
2406 request = (MainThreadRequest) msg.obj;
2407 onCompleted =
2408 obtainMessage(EVENT_START_SATELLITE_POSITION_UPDATES_DONE, request);
2409 Phone phone = getPhoneFromRequest(request);
2410 if (phone != null) {
2411 phone.startSatellitePositionUpdates(onCompleted);
2412 } else {
2413 loge("startSatellitePositionUpdates: No phone object");
Sarah Chinf75afa72023-02-01 01:32:19 -08002414 request.result =
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002415 SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Sarah Chineccfbd12023-01-20 19:00:35 -08002416 notifyRequester(request);
2417 }
2418 break;
2419 }
2420
2421 case EVENT_START_SATELLITE_POSITION_UPDATES_DONE: {
2422 ar = (AsyncResult) msg.obj;
2423 request = (MainThreadRequest) ar.userObj;
2424 if (ar.exception == null) {
2425 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2426 } else {
2427 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2428 if (ar.exception instanceof CommandException) {
2429 CommandException.Error error =
2430 ((CommandException) (ar.exception)).getCommandError();
2431 request.result = RILUtils.convertToSatelliteError(error);
2432 loge("startSatellitePositionUpdates CommandException: " + ar.exception);
2433 } else {
2434 loge("startSatellitePositionUpdates unknown exception:" + ar.exception);
2435 }
2436 }
2437 notifyRequester(request);
2438 break;
2439 }
2440
2441 case CMD_STOP_SATELLITE_POSITION_UPDATES: {
2442 request = (MainThreadRequest) msg.obj;
2443 onCompleted =
2444 obtainMessage(EVENT_STOP_SATELLITE_POSITION_UPDATES_DONE, request);
2445 Phone phone = getPhoneFromRequest(request);
2446 if (phone != null) {
2447 phone.stopSatellitePositionUpdates(onCompleted);
2448 } else {
2449 loge("stopSatellitePositionUpdates: No phone object");
Sarah Chinf75afa72023-02-01 01:32:19 -08002450 request.result =
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002451 SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Sarah Chineccfbd12023-01-20 19:00:35 -08002452 notifyRequester(request);
2453 }
2454 break;
2455 }
2456
2457 case EVENT_STOP_SATELLITE_POSITION_UPDATES_DONE: {
2458 ar = (AsyncResult) msg.obj;
2459 request = (MainThreadRequest) ar.userObj;
2460 if (ar.exception == null) {
2461 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2462 } else {
2463 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2464 if (ar.exception instanceof CommandException) {
2465 CommandException.Error error =
2466 ((CommandException) (ar.exception)).getCommandError();
2467 request.result = RILUtils.convertToSatelliteError(error);
2468 loge("stopSatellitePositionUpdates CommandException: " + ar.exception);
2469 } else {
2470 loge("stopSatellitePositionUpdates unknown exception:" + ar.exception);
2471 }
2472 }
2473 notifyRequester(request);
2474 break;
2475 }
2476
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +00002477 case CMD_GET_MAX_CHAR_PER_SATELLITE_TEXT_MSG: {
2478 request = (MainThreadRequest) msg.obj;
2479 onCompleted = obtainMessage(EVENT_GET_MAX_CHAR_PER_SATELLITE_TEXT_MSG_DONE,
2480 request);
2481 Phone phone = getPhoneFromRequest(request);
2482 if (phone != null) {
2483 phone.getMaxCharactersPerSatelliteTextMessage(onCompleted);
2484 } else {
2485 loge("getMaxCharactersPerSatelliteTextMessage: No phone object");
2486 request.result = SatelliteManager
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002487 .SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +00002488 notifyRequester(request);
2489 }
2490 break;
2491 }
2492
2493 case EVENT_GET_MAX_CHAR_PER_SATELLITE_TEXT_MSG_DONE: {
2494 ar = (AsyncResult) msg.obj;
2495 request = (MainThreadRequest) ar.userObj;
2496 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
2497 if (ar.exception != null) {
2498 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2499 if (ar.exception instanceof CommandException) {
2500 CommandException.Error error =
2501 ((CommandException) (ar.exception)).getCommandError();
2502 request.result = RILUtils.convertToSatelliteError(error);
2503 loge("getMaxCharactersPerSatelliteTextMessage: "
2504 + "CommandException: " + ar.exception);
2505 } else {
2506 loge("getMaxCharactersPerSatelliteTextMessage: "
2507 + "unknown exception:" + ar.exception);
2508 }
2509 } else if (ar.result == null) {
2510 request.result = SatelliteManager
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002511 .SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +00002512 loge("getMaxCharactersPerSatelliteTextMessage: result is null");
2513 } else {
2514 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2515 int maxCharLimit = ((int[]) ar.result)[0];
Sarah Chin503828c2023-02-01 23:54:20 -08002516 if (DBG) log("getMaxCharactersPerSatelliteTextMessage: " + maxCharLimit);
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +00002517 callback.accept(maxCharLimit);
2518 }
2519 notifyRequester(request);
2520 break;
2521 }
2522
Thomas Nguyen8ee49682023-02-01 11:46:09 -08002523 case CMD_PROVISION_SATELLITE_SERVICE: {
2524 request = (MainThreadRequest) msg.obj;
2525 onCompleted =
2526 obtainMessage(EVENT_PROVISION_SATELLITE_SERVICE_DONE, request);
2527 Phone phone = getPhoneFromRequest(request);
2528 if (phone != null) {
2529 handleCmdProvisionSatelliteService(
2530 (ProvisionSatelliteServiceArgument) request.argument,
2531 phone, onCompleted);
2532 } else {
2533 loge("provisionSatelliteService: No phone object");
2534 request.result =
2535 SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2536 notifyRequester(request);
2537 }
2538 break;
2539 }
2540
2541 case EVENT_PROVISION_SATELLITE_SERVICE_DONE: {
2542 ar = (AsyncResult) msg.obj;
2543 request = (MainThreadRequest) ar.userObj;
2544 if (ar.exception == null) {
2545 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2546 } else {
2547 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2548 if (ar.exception instanceof CommandException) {
2549 CommandException.Error error =
2550 ((CommandException) (ar.exception)).getCommandError();
2551 request.result = RILUtils.convertToSatelliteError(error);
2552 loge("provisionSatelliteService CommandException: " + ar.exception);
2553 } else {
2554 loge("provisionSatelliteService unknown exception:" + ar.exception);
2555 }
2556 }
2557 handleEventProvisionSatelliteServiceDone(request);
2558 notifyRequester(request);
2559 break;
2560 }
2561
2562 case CMD_CANCEL_PROVISION_SATELLITE_SERVICE: {
2563 request = (MainThreadRequest) msg.obj;
2564 onCompleted =
2565 obtainMessage(EVENT_CANCEL_PROVISION_SATELLITE_SERVICE_DONE, request);
2566 handleCmdCancelProvisionSatelliteService((int) request.argument, onCompleted);
2567 break;
2568 }
2569
2570 case EVENT_CANCEL_PROVISION_SATELLITE_SERVICE_DONE: {
2571 ar = (AsyncResult) msg.obj;
2572 request = (MainThreadRequest) ar.userObj;
2573
2574 if (ar.exception == null) {
2575 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2576 log("cancelProvisionSatelliteService succeeded for subId="
2577 + (int) request.argument);
2578 } else {
2579 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2580 if (ar.exception instanceof CommandException) {
2581 CommandException.Error error =
2582 ((CommandException) (ar.exception)).getCommandError();
2583 request.result = RILUtils.convertToSatelliteError(error);
2584 loge("cancelProvisionSatelliteService CommandException: "
2585 + ar.exception + ", error code:" + request.result);
2586 } else {
2587 loge("cancelProvisionSatelliteService unknown exception:"
2588 + ar.exception);
2589 }
2590 }
2591 notifyRequester(request);
2592 break;
2593 }
2594
2595 case CMD_GET_PROVISIONED_SATELLITE_FEATURES: {
2596 request = (MainThreadRequest) msg.obj;
2597 onCompleted = obtainMessage(EVENT_GET_PROVISIONED_SATELLITE_FEATURES_DONE,
2598 request);
2599 Phone phone = getPhoneFromRequest(request);
2600 if (phone != null) {
2601 phone.getProvisionedSatelliteFeatures(onCompleted);
2602 } else {
2603 loge("getProvisionedSatelliteFeatures: No phone object");
2604 request.result = SatelliteManager
2605 .SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2606 notifyRequester(request);
2607 }
2608 break;
2609 }
2610
2611 case EVENT_GET_PROVISIONED_SATELLITE_FEATURES_DONE: {
2612 ar = (AsyncResult) msg.obj;
2613 request = (MainThreadRequest) ar.userObj;
2614 if (ar.exception != null) {
2615 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2616 if (ar.exception instanceof CommandException) {
2617 CommandException.Error error =
2618 ((CommandException) (ar.exception)).getCommandError();
2619 request.result = RILUtils.convertToSatelliteError(error);
2620 loge("getProvisionedSatelliteFeatures: "
2621 + "CommandException: " + ar.exception);
2622 } else {
2623 loge("getProvisionedSatelliteFeatures: "
2624 + "unknown exception:" + ar.exception);
2625 }
2626 } else if (ar.result == null) {
2627 request.result = SatelliteManager
2628 .SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2629 loge("getProvisionedSatelliteFeatures: result is null");
2630 } else {
2631 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2632 int[] features = ((int[]) ar.result);
2633 if (DBG) {
2634 log("getProvisionedSatelliteFeatures features:"
2635 + Arrays.toString(features));
2636 }
2637 IIntArrayConsumer callback = (IIntArrayConsumer) request.argument;
2638 if (callback != null) {
2639 try {
2640 callback.accept(features);
2641 } catch (RemoteException ex) {
2642 log("getProvisionedSatelliteFeatures: remote callback"
2643 + " not available");
2644 }
2645 } else {
2646 log("getProvisionedSatelliteFeatures: callback is null");
2647 }
2648 }
2649 notifyRequester(request);
2650 break;
2651 }
2652
Sarah Chin503828c2023-02-01 23:54:20 -08002653 case CMD_SET_SATELLITE_POWER: {
2654 request = (MainThreadRequest) msg.obj;
2655 onCompleted = obtainMessage(EVENT_SET_SATELLITE_POWER_DONE, request);
2656 Phone phone = getPhoneFromRequest(request);
2657 if (phone != null) {
2658 phone.setSatellitePower(onCompleted, (boolean) request.argument);
2659 } else {
2660 loge("setSatellitePower: No phone object");
2661 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2662 notifyRequester(request);
2663 }
2664 break;
2665 }
2666
2667 case EVENT_SET_SATELLITE_POWER_DONE: {
2668 ar = (AsyncResult) msg.obj;
2669 request = (MainThreadRequest) ar.userObj;
2670 if (ar.exception == null) {
2671 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2672 } else {
2673 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2674 if (ar.exception instanceof CommandException) {
2675 CommandException.Error error =
2676 ((CommandException) (ar.exception)).getCommandError();
2677 request.result = RILUtils.convertToSatelliteError(error);
2678 loge("setSatellitePower CommandException: " + ar.exception);
2679 } else {
2680 loge("setSatellitePower unknown exception:" + ar.exception);
2681 }
2682 }
2683 notifyRequester(request);
2684 break;
2685 }
2686
2687 case CMD_IS_SATELLITE_POWER_ON: {
2688 request = (MainThreadRequest) msg.obj;
2689 onCompleted = obtainMessage(EVENT_IS_SATELLITE_POWER_ON_DONE, request);
2690 Phone phone = getPhoneFromRequest(request);
2691 if (phone != null) {
2692 phone.isSatellitePowerOn(onCompleted);
2693 } else {
2694 loge("isSatellitePowerOn: No phone object");
2695 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2696 notifyRequester(request);
2697 }
2698 break;
2699 }
2700
2701 case EVENT_IS_SATELLITE_POWER_ON_DONE: {
2702 ar = (AsyncResult) msg.obj;
2703 request = (MainThreadRequest) ar.userObj;
2704 Consumer<Boolean> callback = (Consumer<Boolean>) request.argument;
2705 if (ar.exception != null) {
2706 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2707 if (ar.exception instanceof CommandException) {
2708 CommandException.Error error =
2709 ((CommandException) (ar.exception)).getCommandError();
2710 request.result = RILUtils.convertToSatelliteError(error);
2711 loge("isSatellitePowerOn CommandException: " + ar.exception);
2712 } else {
2713 loge("isSatellitePowerOn unknown exception:" + ar.exception);
2714 }
2715 } else if (ar.result == null) {
2716 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2717 loge("isSatellitePowerOn: result is null");
2718 } else {
2719 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2720 boolean powerOn = ((int[]) ar.result)[0] == 1;
2721 if (DBG) log("isSatellitePowerOn: " + powerOn);
2722 callback.accept(powerOn);
2723 }
2724 notifyRequester(request);
2725 break;
2726 }
2727
2728 case CMD_IS_SATELLITE_SUPPORTED: {
2729 request = (MainThreadRequest) msg.obj;
2730 onCompleted = obtainMessage(EVENT_IS_SATELLITE_SUPPORTED_DONE, request);
2731 Phone phone = getPhoneFromRequest(request);
2732 if (phone != null) {
2733 phone.isSatelliteSupported(onCompleted);
2734 } else {
2735 loge("isSatelliteSupported: No phone object");
2736 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2737 notifyRequester(request);
2738 }
2739 break;
2740 }
2741
2742 case EVENT_IS_SATELLITE_SUPPORTED_DONE: {
2743 ar = (AsyncResult) msg.obj;
2744 request = (MainThreadRequest) ar.userObj;
2745 Consumer<Boolean> callback = (Consumer<Boolean>) request.argument;
2746 if (ar.exception != null) {
2747 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2748 if (ar.exception instanceof CommandException) {
2749 CommandException.Error error =
2750 ((CommandException) (ar.exception)).getCommandError();
2751 request.result = RILUtils.convertToSatelliteError(error);
2752 loge("isSatelliteSupported CommandException: " + ar.exception);
2753 } else {
2754 loge("isSatelliteSupported unknown exception:" + ar.exception);
2755 }
2756 } else if (ar.result == null) {
2757 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2758 loge("isSatelliteSupported: result is null");
2759 } else {
2760 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2761 boolean powerOn = ((int[]) ar.result)[0] == 1;
2762 if (DBG) log("isSatelliteSupported: " + powerOn);
2763 callback.accept(powerOn);
2764 }
2765 notifyRequester(request);
2766 break;
2767 }
2768
2769 case CMD_GET_SATELLITE_CAPABILITIES: {
2770 request = (MainThreadRequest) msg.obj;
2771 onCompleted = obtainMessage(EVENT_GET_SATELLITE_CAPABILITIES_DONE, request);
2772 Phone phone = getPhoneFromRequest(request);
2773 if (phone != null) {
2774 phone.getSatelliteCapabilities(onCompleted);
2775 } else {
2776 loge("getSatelliteCapabilities: No phone object");
2777 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2778 notifyRequester(request);
2779 }
2780 break;
2781 }
2782
2783 case EVENT_GET_SATELLITE_CAPABILITIES_DONE: {
2784 ar = (AsyncResult) msg.obj;
2785 request = (MainThreadRequest) ar.userObj;
2786 Consumer<SatelliteCapabilities> callback =
2787 (Consumer<SatelliteCapabilities>) request.argument;
2788 if (ar.exception != null) {
2789 request.result = SatelliteManager.SATELLITE_SERVICE_ERROR;
2790 if (ar.exception instanceof CommandException) {
2791 CommandException.Error error =
2792 ((CommandException) (ar.exception)).getCommandError();
2793 request.result = RILUtils.convertToSatelliteError(error);
2794 loge("getSatelliteCapabilities CommandException: " + ar.exception);
2795 } else {
2796 loge("getSatelliteCapabilities unknown exception:" + ar.exception);
2797 }
2798 } else if (ar.result == null) {
2799 request.result = SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
2800 loge("getSatelliteCapabilities: result is null");
2801 } else {
2802 request.result = SatelliteManager.SATELLITE_SERVICE_SUCCESS;
2803 SatelliteCapabilities capabilities = (SatelliteCapabilities) ar.result;
2804 if (DBG) log("getSatelliteCapabilities: " + capabilities);
2805 callback.accept(capabilities);
2806 }
2807 notifyRequester(request);
2808 break;
2809 }
2810
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002811 default:
2812 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
2813 break;
2814 }
2815 }
Jake Hambye994d462014-02-03 13:10:13 -08002816
Pengquan Menga1bb6272018-09-06 09:59:22 -07002817 private void notifyRequester(MainThreadRequest request) {
2818 synchronized (request) {
2819 request.notifyAll();
2820 }
2821 }
2822
Jake Hambye994d462014-02-03 13:10:13 -08002823 private void handleNullReturnEvent(Message msg, String command) {
2824 AsyncResult ar = (AsyncResult) msg.obj;
2825 MainThreadRequest request = (MainThreadRequest) ar.userObj;
2826 if (ar.exception == null) {
2827 request.result = true;
2828 } else {
2829 request.result = false;
2830 if (ar.exception instanceof CommandException) {
2831 loge(command + ": CommandException: " + ar.exception);
2832 } else {
2833 loge(command + ": Unknown exception");
2834 }
2835 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07002836 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08002837 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002838 }
2839
2840 /**
2841 * Posts the specified command to be executed on the main thread,
2842 * waits for the request to complete, and returns the result.
2843 * @see #sendRequestAsync
2844 */
2845 private Object sendRequest(int command, Object argument) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002846 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null,
2847 null, -1 /*timeoutInMs*/);
vagdeviaf9a5b92018-08-15 16:01:53 -07002848 }
2849
2850 /**
2851 * Posts the specified command to be executed on the main thread,
2852 * waits for the request to complete, and returns the result.
2853 * @see #sendRequestAsync
2854 */
2855 private Object sendRequest(int command, Object argument, WorkSource workSource) {
2856 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Rambo Wang0f050d82021-02-12 11:43:36 -08002857 null, workSource, -1 /*timeoutInMs*/);
Wink Saville36469e72014-06-11 15:17:00 -07002858 }
2859
2860 /**
2861 * Posts the specified command to be executed on the main thread,
2862 * waits for the request to complete, and returns the result.
2863 * @see #sendRequestAsync
2864 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002865 private Object sendRequest(int command, Object argument, Integer subId) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002866 return sendRequest(command, argument, subId, null, null, -1 /*timeoutInMs*/);
2867 }
2868
2869 /**
2870 * Posts the specified command to be executed on the main thread,
2871 * waits for the request to complete for at most {@code timeoutInMs}, and returns the result
2872 * if not timeout or null otherwise.
2873 * @see #sendRequestAsync
2874 */
2875 private @Nullable Object sendRequest(int command, Object argument, Integer subId,
2876 long timeoutInMs) {
2877 return sendRequest(command, argument, subId, null, null, timeoutInMs);
vagdeviaf9a5b92018-08-15 16:01:53 -07002878 }
2879
2880 /**
2881 * Posts the specified command to be executed on the main thread,
2882 * waits for the request to complete, and returns the result.
2883 * @see #sendRequestAsync
2884 */
Nathan Harold92bed182018-10-12 18:16:49 -07002885 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002886 return sendRequest(command, argument, subId, null, workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002887 }
2888
2889 /**
2890 * Posts the specified command to be executed on the main thread,
2891 * waits for the request to complete, and returns the result.
2892 * @see #sendRequestAsync
2893 */
2894 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002895 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone,
2896 workSource, -1 /*timeoutInMs*/);
Nathan Harold92bed182018-10-12 18:16:49 -07002897 }
2898
2899 /**
Rambo Wang0f050d82021-02-12 11:43:36 -08002900 * Posts the specified command to be executed on the main thread. If {@code timeoutInMs} is
2901 * negative, waits for the request to complete, and returns the result. Otherwise, wait for
2902 * maximum of {@code timeoutInMs} milliseconds, interrupt and return null.
Nathan Harold92bed182018-10-12 18:16:49 -07002903 * @see #sendRequestAsync
2904 */
Rambo Wang0f050d82021-02-12 11:43:36 -08002905 private @Nullable Object sendRequest(int command, Object argument, Integer subId, Phone phone,
2906 WorkSource workSource, long timeoutInMs) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002907 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
2908 throw new RuntimeException("This method will deadlock if called from the main thread.");
2909 }
2910
Nathan Harold92bed182018-10-12 18:16:49 -07002911 MainThreadRequest request = null;
2912 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
2913 throw new IllegalArgumentException("subId and phone cannot both be specified!");
2914 } else if (phone != null) {
2915 request = new MainThreadRequest(argument, phone, workSource);
2916 } else {
2917 request = new MainThreadRequest(argument, subId, workSource);
2918 }
2919
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002920 Message msg = mMainThreadHandler.obtainMessage(command, request);
2921 msg.sendToTarget();
2922
Rambo Wang0f050d82021-02-12 11:43:36 -08002923
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002924 synchronized (request) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002925 if (timeoutInMs >= 0) {
2926 // Wait for at least timeoutInMs before returning null request result
2927 long now = SystemClock.elapsedRealtime();
2928 long deadline = now + timeoutInMs;
Grace Jia8a0a1e82021-05-23 22:59:52 -07002929 while (request.result == null && now < deadline) {
Rambo Wang0f050d82021-02-12 11:43:36 -08002930 try {
2931 request.wait(deadline - now);
2932 } catch (InterruptedException e) {
2933 // Do nothing, go back and check if request is completed or timeout
2934 } finally {
2935 now = SystemClock.elapsedRealtime();
2936 }
2937 }
2938 } else {
2939 // Wait for the request to complete
2940 while (request.result == null) {
2941 try {
2942 request.wait();
2943 } catch (InterruptedException e) {
2944 // Do nothing, go back and wait until the request is complete
2945 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002946 }
2947 }
2948 }
Rambo Wang0f050d82021-02-12 11:43:36 -08002949 if (request.result == null) {
2950 Log.wtf(LOG_TAG,
2951 "sendRequest: Blocking command timed out. Something has gone terribly wrong.");
2952 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002953 return request.result;
2954 }
2955
2956 /**
2957 * Asynchronous ("fire and forget") version of sendRequest():
2958 * Posts the specified command to be executed on the main thread, and
2959 * returns immediately.
2960 * @see #sendRequest
2961 */
2962 private void sendRequestAsync(int command) {
2963 mMainThreadHandler.sendEmptyMessage(command);
2964 }
2965
2966 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002967 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002968 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002969 */
2970 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002971 sendRequestAsync(command, argument, null, null);
2972 }
2973
2974 /**
2975 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
2976 * @see {@link #sendRequest(int,Object)}
2977 */
2978 private void sendRequestAsync(
2979 int command, Object argument, Phone phone, WorkSource workSource) {
2980 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002981 Message msg = mMainThreadHandler.obtainMessage(command, request);
2982 msg.sendToTarget();
2983 }
2984
2985 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002986 * Initialize the singleton PhoneInterfaceManager instance.
2987 * This is only done once, at startup, from PhoneApp.onCreate().
2988 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002989 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002990 synchronized (PhoneInterfaceManager.class) {
2991 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002992 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002993 } else {
2994 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
2995 }
2996 return sInstance;
2997 }
2998 }
2999
3000 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00003001 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003002 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003003 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebingerd1947d82021-05-17 20:54:49 +00003004 mImsResolver = ImsResolver.getInstance();
Stuart Scott981d8582015-04-21 14:09:50 -07003005 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003006 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
Grace Jia0ddb3612021-04-22 13:35:26 -07003007 mPm = app.getSystemService(PackageManager.class);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003008 mMainThreadHandler = new MainThreadHandler();
Jack Yue37dd262022-12-16 11:53:37 -08003009 if (!PhoneFactory.isSubscriptionManagerServiceEnabled()) {
3010 mSubscriptionController = SubscriptionController.getInstance();
3011 } else {
3012 mSubscriptionController = null;
3013 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003014 mTelephonySharedPreferences =
3015 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07003016 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07003017 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Daniel Bright94f43662021-03-01 14:43:40 -08003018 mRadioInterfaceCapabilities = RadioInterfaceCapabilityController.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08003019 mNotifyUserActivity = new AtomicBoolean(false);
Tyler Gunn64144d92022-03-17 14:16:41 -07003020 PropertyInvalidatedCache.invalidateCache(TelephonyManager.CACHE_KEY_PHONE_ACCOUNT_TO_SUBID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003021 publish();
arunvoddud7401012022-12-15 16:08:12 +00003022 CarrierAllowListInfo.loadInstance(mApp);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003023 }
3024
Gil Cukierman1c0eb932022-12-06 22:28:24 +00003025 @VisibleForTesting
3026 public SharedPreferences getSharedPreferences() {
3027 return mTelephonySharedPreferences;
3028 }
3029
Gil Cukierman92cc7db2023-01-06 19:25:53 +00003030 /**
3031 * Get the default phone for this device.
3032 */
3033 @VisibleForTesting
3034 public Phone getDefaultPhone() {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003035 Phone thePhone = getPhone(getDefaultSubscription());
3036 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
3037 }
3038
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003039 private void publish() {
3040 if (DBG) log("publish: " + this);
3041
Peter Wangc035ce42020-01-08 21:00:22 -08003042 TelephonyFrameworkInitializer
3043 .getTelephonyServiceManager()
3044 .getTelephonyServiceRegisterer()
3045 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003046 }
3047
Stuart Scott584921c2015-01-15 17:10:34 -08003048 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08003049 if (request.phone != null) {
3050 return request.phone;
3051 } else {
3052 return getPhoneFromSubId(request.subId);
3053 }
3054 }
3055
3056 private Phone getPhoneFromSubId(int subId) {
3057 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
3058 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08003059 }
3060
Aishwarya Mallampati5e581e12023-01-17 21:57:06 +00003061 /**
3062 * Get phone object associated with a subscription.
3063 * Return default phone if phone object associated with subscription is null
3064 * @param subId - subscriptionId
3065 * @return phone object associated with a subscription or default phone if null.
3066 */
3067 private Phone getPhoneFromSubIdOrDefault(int subId) {
3068 Phone phone = getPhoneFromSubId(subId);
3069 if (phone == null) {
3070 phone = getDefaultPhone();
3071 }
3072 return phone;
3073 }
3074
Rambo Wange53e07d2022-05-10 13:01:13 -07003075 @Nullable
3076 private UiccPort getUiccPortFromRequest(@NonNull MainThreadRequest request) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003077 Phone phone = getPhoneFromRequest(request);
3078 return phone == null ? null :
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00003079 UiccController.getInstance().getUiccPort(phone.getPhoneId());
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003080 }
3081
Wink Saville36469e72014-06-11 15:17:00 -07003082 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07003083 private Phone getPhone(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08003084 return PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07003085 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003086
Kai Shif70f46f2021-03-03 13:59:46 -08003087 private void sendEraseModemConfig(@NonNull Phone phone) {
3088 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
3089 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
3090 }
3091
3092 private void sendEraseDataInSharedPreferences(@NonNull Phone phone) {
3093 Boolean success = (Boolean) sendRequest(CMD_ERASE_DATA_SHARED_PREFERENCES, null);
3094 if (DBG) log("eraseDataInSharedPreferences:" + ' ' + (success ? "ok" : "fail"));
Naina Nallurid63128d2019-09-17 14:10:30 -07003095 }
3096
Peter Wang44b186e2020-01-13 23:33:09 -08003097 private boolean isImsAvailableOnDevice() {
3098 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
3099 if (pm == null) {
3100 // For some reason package manger is not available.. This will fail internally anyway,
3101 // so do not throw error and allow.
3102 return true;
3103 }
3104 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
3105 }
3106
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003107 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003108 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07003109 }
3110
Wink Savilleb564aae2014-10-23 10:18:09 -07003111 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003112 if (DBG) log("dial: " + number);
3113 // No permission check needed here: This is just a wrapper around the
3114 // ACTION_DIAL intent, which is available to any app since it puts up
3115 // the UI before it does anything.
3116
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003117 final long identity = Binder.clearCallingIdentity();
3118 try {
3119 String url = createTelUrl(number);
3120 if (url == null) {
3121 return;
3122 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003123
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003124 // PENDING: should we just silently fail if phone is offhook or ringing?
3125 PhoneConstants.State state = mCM.getState(subId);
3126 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
3127 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
3128 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3129 mApp.startActivity(intent);
3130 }
3131 } finally {
3132 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003133 }
3134 }
3135
3136 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003137 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07003138 }
3139
Wink Savilleb564aae2014-10-23 10:18:09 -07003140 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003141 if (DBG) log("call: " + number);
3142
3143 // This is just a wrapper around the ACTION_CALL intent, but we still
3144 // need to do a permission check since we're calling startActivity()
3145 // from the context of the phone app.
3146 enforceCallPermission();
3147
Jordan Liu1617b712019-07-10 15:06:26 -07003148 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003149 != AppOpsManager.MODE_ALLOWED) {
3150 return;
3151 }
3152
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003153 final long identity = Binder.clearCallingIdentity();
3154 try {
3155 String url = createTelUrl(number);
3156 if (url == null) {
3157 return;
3158 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003159
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003160 boolean isValid = false;
3161 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
3162 if (slist != null) {
3163 for (SubscriptionInfo subInfoRecord : slist) {
3164 if (subInfoRecord.getSubscriptionId() == subId) {
3165 isValid = true;
3166 break;
3167 }
Wink Saville3ab207e2014-11-20 13:07:20 -08003168 }
Wink Saville08874612014-08-31 19:19:58 -07003169 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003170 if (!isValid) {
3171 return;
3172 }
Wink Saville08874612014-08-31 19:19:58 -07003173
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003174 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
3175 intent.putExtra(SUBSCRIPTION_KEY, subId);
3176 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3177 mApp.startActivity(intent);
3178 } finally {
3179 Binder.restoreCallingIdentity(identity);
3180 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003181 }
3182
Wink Savilleb564aae2014-10-23 10:18:09 -07003183 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003184 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07003185 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
3186 }
3187
Wink Savilleb564aae2014-10-23 10:18:09 -07003188 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003189 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07003190 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
3191 }
3192
Wink Savilleb564aae2014-10-23 10:18:09 -07003193 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003194 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003195
3196 final long identity = Binder.clearCallingIdentity();
3197 try {
Michele Berionne5e411512020-11-13 02:36:59 +00003198 Phone phone = getPhone(subId);
3199 final UnlockSim checkSimPin = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003200 checkSimPin.start();
3201 return checkSimPin.unlockSim(null, pin);
3202 } finally {
3203 Binder.restoreCallingIdentity(identity);
3204 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003205 }
3206
Wink Savilleb564aae2014-10-23 10:18:09 -07003207 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003208 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003209
3210 final long identity = Binder.clearCallingIdentity();
3211 try {
Michele Berionne5e411512020-11-13 02:36:59 +00003212 Phone phone = getPhone(subId);
3213 final UnlockSim checkSimPuk = new UnlockSim(phone.getPhoneId(), phone.getIccCard());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003214 checkSimPuk.start();
3215 return checkSimPuk.unlockSim(puk, pin);
3216 } finally {
3217 Binder.restoreCallingIdentity(identity);
3218 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003219 }
3220
3221 /**
Wink Saville9de0f752013-10-22 19:04:03 -07003222 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003223 * a synchronous one.
3224 */
3225 private static class UnlockSim extends Thread {
3226
3227 private final IccCard mSimCard;
Michele Berionne5e411512020-11-13 02:36:59 +00003228 private final int mPhoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003229
3230 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07003231 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
3232 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003233
3234 // For replies from SimCard interface
3235 private Handler mHandler;
3236
3237 // For async handler to identify request type
3238 private static final int SUPPLY_PIN_COMPLETE = 100;
3239
Michele Berionne5e411512020-11-13 02:36:59 +00003240 UnlockSim(int phoneId, IccCard simCard) {
3241 mPhoneId = phoneId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003242 mSimCard = simCard;
3243 }
3244
3245 @Override
3246 public void run() {
3247 Looper.prepare();
3248 synchronized (UnlockSim.this) {
3249 mHandler = new Handler() {
3250 @Override
3251 public void handleMessage(Message msg) {
3252 AsyncResult ar = (AsyncResult) msg.obj;
3253 switch (msg.what) {
3254 case SUPPLY_PIN_COMPLETE:
3255 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
3256 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07003257 mRetryCount = msg.arg1;
3258 if (ar.exception != null) {
Thomas Nguyen8ee49682023-02-01 11:46:09 -08003259 CommandException.Error error = null;
3260 if (ar.exception instanceof CommandException) {
3261 error = ((CommandException) (ar.exception))
3262 .getCommandError();
3263 }
3264 if (error == CommandException.Error.PASSWORD_INCORRECT) {
Wink Saville9de0f752013-10-22 19:04:03 -07003265 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
Thomas Nguyen8ee49682023-02-01 11:46:09 -08003266 } else if (error == CommandException.Error.ABORTED) {
3267 /* When UiccCardApp dispose, handle message and return
3268 exception */
vivi.lib5e9ada2019-09-12 16:04:24 +08003269 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07003270 } else {
3271 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
3272 }
3273 } else {
3274 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
3275 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003276 mDone = true;
3277 UnlockSim.this.notifyAll();
3278 }
3279 break;
3280 }
3281 }
3282 };
3283 UnlockSim.this.notifyAll();
3284 }
3285 Looper.loop();
3286 }
3287
3288 /*
3289 * Use PIN or PUK to unlock SIM card
3290 *
3291 * If PUK is null, unlock SIM card with PIN
3292 *
3293 * If PUK is not null, unlock SIM card with PUK and set PIN code
3294 */
Wink Saville9de0f752013-10-22 19:04:03 -07003295 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003296
3297 while (mHandler == null) {
3298 try {
3299 wait();
3300 } catch (InterruptedException e) {
3301 Thread.currentThread().interrupt();
3302 }
3303 }
3304 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
3305
3306 if (puk == null) {
3307 mSimCard.supplyPin(pin, callback);
3308 } else {
3309 mSimCard.supplyPuk(puk, pin, callback);
3310 }
3311
3312 while (!mDone) {
3313 try {
3314 Log.d(LOG_TAG, "wait for done");
3315 wait();
3316 } catch (InterruptedException e) {
3317 // Restore the interrupted status
3318 Thread.currentThread().interrupt();
3319 }
3320 }
3321 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07003322 int[] resultArray = new int[2];
3323 resultArray[0] = mResult;
3324 resultArray[1] = mRetryCount;
Michele Berionne5e411512020-11-13 02:36:59 +00003325
3326 if (mResult == PhoneConstants.PIN_RESULT_SUCCESS && pin.length() > 0) {
Jon Spivack9c3bc762021-10-06 20:53:09 +00003327 UiccController.getInstance().getPinStorage().storePin(pin, mPhoneId);
Michele Berionne5e411512020-11-13 02:36:59 +00003328 }
3329
Wink Saville9de0f752013-10-22 19:04:03 -07003330 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003331 }
3332 }
3333
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003334 /**
3335 * This method has been removed due to privacy and stability concerns.
3336 */
3337 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003338 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003339 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
3340 return;
Wink Saville36469e72014-06-11 15:17:00 -07003341 }
3342
Nathan Harold1f889d82020-06-04 17:05:26 -07003343 @Override
3344 public void updateServiceLocationWithPackageName(String callingPackage) {
3345 mApp.getSystemService(AppOpsManager.class)
3346 .checkPackage(Binder.getCallingUid(), callingPackage);
3347
Nathan Haroldf096d982020-11-18 17:18:06 -08003348 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harold1f889d82020-06-04 17:05:26 -07003349 if (targetSdk > android.os.Build.VERSION_CODES.R) {
3350 // Callers targeting S have no business invoking this method.
3351 return;
3352 }
3353
3354 LocationAccessPolicy.LocationPermissionResult locationResult =
3355 LocationAccessPolicy.checkLocationPermission(mApp,
3356 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3357 .setCallingPackage(callingPackage)
3358 .setCallingFeatureId(null)
3359 .setCallingPid(Binder.getCallingPid())
3360 .setCallingUid(Binder.getCallingUid())
3361 .setMethod("updateServiceLocation")
3362 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
3363 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3364 .build());
3365 // Apps that lack location permission have no business calling this method;
3366 // however, because no permission was declared in the public API, denials must
3367 // all be "soft".
3368 switch (locationResult) {
3369 case DENIED_HARD: /* fall through */
3370 case DENIED_SOFT:
3371 return;
3372 }
3373
3374 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003375 final long identity = Binder.clearCallingIdentity();
3376 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07003377 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003378 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07003379 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003380 }
3381 } finally {
3382 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003383 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003384 }
3385
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003386 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003387 @Override
3388 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003389 return isRadioOnWithFeature(callingPackage, null);
3390 }
3391
3392
3393 @Override
3394 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
3395 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
3396 callingFeatureId);
3397 }
3398
3399 @Deprecated
3400 @Override
3401 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
3402 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07003403 }
3404
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003405 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003406 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
3407 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003408 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003409 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003410 return false;
3411 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003412
3413 final long identity = Binder.clearCallingIdentity();
3414 try {
3415 return isRadioOnForSubscriber(subId);
3416 } finally {
3417 Binder.restoreCallingIdentity(identity);
3418 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003419 }
3420
3421 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003422 final long identity = Binder.clearCallingIdentity();
3423 try {
3424 final Phone phone = getPhone(subId);
3425 if (phone != null) {
3426 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
3427 } else {
3428 return false;
3429 }
3430 } finally {
3431 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003432 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003433 }
3434
3435 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003436 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003437 }
Wink Saville36469e72014-06-11 15:17:00 -07003438
Wink Savilleb564aae2014-10-23 10:18:09 -07003439 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003440 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003441
3442 final long identity = Binder.clearCallingIdentity();
3443 try {
3444 final Phone phone = getPhone(subId);
3445 if (phone != null) {
3446 phone.setRadioPower(!isRadioOnForSubscriber(subId));
3447 }
3448 } finally {
3449 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003450 }
Wink Saville36469e72014-06-11 15:17:00 -07003451 }
3452
3453 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003454 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07003455 }
3456
Wink Savilleb564aae2014-10-23 10:18:09 -07003457 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07003458 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003459
3460 final long identity = Binder.clearCallingIdentity();
3461 try {
3462 final Phone phone = getPhone(subId);
3463 if (phone == null) {
3464 return false;
3465 }
3466 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
3467 toggleRadioOnOffForSubscriber(subId);
3468 }
3469 return true;
3470 } finally {
3471 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003472 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003473 }
Wink Saville36469e72014-06-11 15:17:00 -07003474
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003475 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08003476 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003477 /*
3478 * If any of the Radios are available, it will need to be
3479 * shutdown. So return true if any Radio is available.
3480 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003481 final long identity = Binder.clearCallingIdentity();
3482 try {
3483 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
3484 Phone phone = PhoneFactory.getPhone(i);
3485 if (phone != null && phone.isRadioAvailable()) return true;
3486 }
3487 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
3488 return false;
3489 } finally {
3490 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003491 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003492 }
3493
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003494 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003495 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003496 enforceModifyPermission();
3497
3498 final long identity = Binder.clearCallingIdentity();
3499 try {
3500 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
3501 logv("Shutting down Phone " + i);
3502 shutdownRadioUsingPhoneId(i);
3503 }
3504 } finally {
3505 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003506 }
3507 }
3508
3509 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07003510 Phone phone = PhoneFactory.getPhone(phoneId);
3511 if (phone != null && phone.isRadioAvailable()) {
3512 phone.shutdownRadio();
3513 }
3514 }
3515
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003516 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07003517 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003518
Ling Ma83dc5ea2023-01-12 15:06:04 -08003519 if (!turnOn) {
3520 log("setRadioPower off: callingPackage=" + getCurrentPackageName());
3521 }
3522
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003523 final long identity = Binder.clearCallingIdentity();
3524 try {
3525 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
3526 if (defaultPhone != null) {
3527 defaultPhone.setRadioPower(turnOn);
3528 return true;
3529 } else {
3530 loge("There's no default phone.");
3531 return false;
3532 }
3533 } finally {
3534 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07003535 }
Wink Saville36469e72014-06-11 15:17:00 -07003536 }
3537
Wink Savilleb564aae2014-10-23 10:18:09 -07003538 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003539 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003540
Ling Ma83dc5ea2023-01-12 15:06:04 -08003541 if (!turnOn) {
3542 log("setRadioPowerForSubscriber off: subId=" + subId
3543 + ",callingPackage=" + getCurrentPackageName());
3544 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003545 final long identity = Binder.clearCallingIdentity();
3546 try {
3547 final Phone phone = getPhone(subId);
3548 if (phone != null) {
3549 phone.setRadioPower(turnOn);
3550 return true;
3551 } else {
3552 return false;
3553 }
3554 } finally {
3555 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003556 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003557 }
3558
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00003559 /**
3560 * Vote on powering off the radio for a reason. The radio will be turned on only when there is
3561 * no reason to power it off. When any of the voters want to power it off, it will be turned
3562 * off. In case of emergency, the radio will be turned on even if there are some reasons for
3563 * powering it off, and these radio off votes will be cleared.
3564 * Multiple apps can vote for the same reason and the last vote will take effect. Each app is
3565 * responsible for its vote. A powering-off vote of a reason will be maintained until it is
3566 * cleared by calling {@link clearRadioPowerOffForReason} for that reason, or an emergency call
3567 * is made, or the device is rebooted. When an app comes backup from a crash, it needs to make
3568 * sure if its vote is as expected. An app can use the API {@link getRadioPowerOffReasons} to
3569 * check its vote.
3570 *
3571 * @param subId The subscription ID.
3572 * @param reason The reason for powering off radio.
3573 * @return true on success and false on failure.
3574 */
3575 public boolean requestRadioPowerOffForReason(int subId,
3576 @TelephonyManager.RadioPowerReason int reason) {
3577 enforceModifyPermission();
3578
Ling Ma83dc5ea2023-01-12 15:06:04 -08003579 log("requestRadioPowerOffForReason: subId=" + subId
3580 + ",reason=" + reason + ",callingPackage=" + getCurrentPackageName());
Thomas Nguyenfd0572f2022-07-15 22:28:49 +00003581 final long identity = Binder.clearCallingIdentity();
3582 try {
3583 final Phone phone = getPhone(subId);
3584 if (phone != null) {
3585 phone.setRadioPowerForReason(false, reason);
3586 return true;
3587 } else {
3588 return false;
3589 }
3590 } finally {
3591 Binder.restoreCallingIdentity(identity);
3592 }
3593 }
3594
3595 /**
3596 * Remove the vote on powering off the radio for a reason, as requested by
3597 * {@link requestRadioPowerOffForReason}.
3598 *
3599 * @param subId The subscription ID.
3600 * @param reason The reason for powering off radio.
3601 * @return true on success and false on failure.
3602 */
3603 public boolean clearRadioPowerOffForReason(int subId,
3604 @TelephonyManager.RadioPowerReason int reason) {
3605 enforceModifyPermission();
3606
3607 final long identity = Binder.clearCallingIdentity();
3608 try {
3609 final Phone phone = getPhone(subId);
3610 if (phone != null) {
3611 phone.setRadioPowerForReason(true, reason);
3612 return true;
3613 } else {
3614 return false;
3615 }
3616 } finally {
3617 Binder.restoreCallingIdentity(identity);
3618 }
3619 }
3620
3621 /**
3622 * Get reasons for powering off radio, as requested by {@link requestRadioPowerOffForReason}.
3623 *
3624 * @param subId The subscription ID.
3625 * @param callingPackage The package making the call.
3626 * @param callingFeatureId The feature in the package.
3627 * @return List of reasons for powering off radio.
3628 */
3629 public List getRadioPowerOffReasons(int subId, String callingPackage, String callingFeatureId) {
3630 enforceReadPrivilegedPermission("getRadioPowerOffReasons");
3631
3632 final long identity = Binder.clearCallingIdentity();
3633 List result = new ArrayList();
3634 try {
3635 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId,
3636 callingPackage, callingFeatureId, "getRadioPowerOffReasons")) {
3637 return result;
3638 }
3639
3640 final Phone phone = getPhone(subId);
3641 if (phone != null) {
3642 result.addAll(phone.getRadioPowerOffReasons());
3643 }
3644 } finally {
3645 Binder.restoreCallingIdentity(identity);
3646 }
3647 return result;
3648 }
3649
Wink Saville36469e72014-06-11 15:17:00 -07003650 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003651 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003652 public boolean enableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003653 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003654
3655 final long identity = Binder.clearCallingIdentity();
3656 try {
Jack Yu285100e2022-12-02 22:48:35 -08003657 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003658 final Phone phone = getPhone(subId);
3659 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003660 phone.getDataSettingsManager().setDataEnabled(
3661 TelephonyManager.DATA_ENABLED_REASON_USER, true, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003662 return true;
3663 } else {
3664 return false;
3665 }
3666 } finally {
3667 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003668 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003669 }
3670
Wink Saville36469e72014-06-11 15:17:00 -07003671 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07003672 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07003673 public boolean disableDataConnectivity(String callingPackage) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003674 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003675
3676 final long identity = Binder.clearCallingIdentity();
3677 try {
Jack Yu285100e2022-12-02 22:48:35 -08003678 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003679 final Phone phone = getPhone(subId);
3680 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003681 phone.getDataSettingsManager().setDataEnabled(
3682 TelephonyManager.DATA_ENABLED_REASON_USER, false, callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003683 return true;
3684 } else {
3685 return false;
3686 }
3687 } finally {
3688 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003689 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003690 }
3691
Sanket Padawe356d7632015-06-22 14:03:32 -07003692 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07003693 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003694 final long identity = Binder.clearCallingIdentity();
3695 try {
3696 final Phone phone = getPhone(subId);
3697 if (phone != null) {
Jack Yu59824e12022-03-23 01:42:44 -07003698 return phone.isDataAllowed();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003699 } else {
3700 return false;
3701 }
3702 } finally {
3703 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003704 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003705 }
3706
3707 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07003708 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07003709 }
3710
pkanwarae03a6b2016-11-06 20:37:09 -08003711 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003712 enforceCallPermission();
3713
3714 final long identity = Binder.clearCallingIdentity();
3715 try {
3716 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3717 return;
3718 }
3719 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
3720 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
3721 } finally {
3722 Binder.restoreCallingIdentity(identity);
3723 }
pkanwar32d516d2016-10-14 19:37:38 -07003724 };
3725
Wink Savilleb564aae2014-10-23 10:18:09 -07003726 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003727 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003728
3729 final long identity = Binder.clearCallingIdentity();
3730 try {
3731 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3732 return false;
3733 }
3734 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
3735 } finally {
3736 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003737 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003738 }
3739
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003740 /**
3741 * @deprecated This method is deprecated and is only being kept due to an UnsupportedAppUsage
3742 * tag on getCallState Binder call.
3743 */
3744 @Deprecated
3745 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003746 public int getCallState() {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003747 if (CompatChanges.isChangeEnabled(
3748 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3749 Binder.getCallingUid())) {
3750 // Do not allow this API to be called on API version 31+, it should only be
3751 // called on old apps using this Binder call directly.
3752 throw new SecurityException("This method can only be used for applications "
3753 + "targeting API version 30 or less.");
3754 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003755 final long identity = Binder.clearCallingIdentity();
3756 try {
Brad Ebinger4f6208e2021-03-23 21:04:45 +00003757 Phone phone = getPhone(getDefaultSubscription());
3758 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3759 PhoneConstantConversions.convertCallState(phone.getState());
3760 } finally {
3761 Binder.restoreCallingIdentity(identity);
3762 }
3763 }
3764
3765 @Override
3766 public int getCallStateForSubscription(int subId, String callingPackage, String featureId) {
3767 if (CompatChanges.isChangeEnabled(
3768 TelecomManager.ENABLE_GET_CALL_STATE_PERMISSION_PROTECTION,
3769 Binder.getCallingUid())) {
3770 // Check READ_PHONE_STATE for API version 31+
3771 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
3772 featureId, "getCallStateForSubscription")) {
3773 throw new SecurityException("getCallState requires READ_PHONE_STATE for apps "
3774 + "targeting API level 31+.");
3775 }
3776 }
3777 final long identity = Binder.clearCallingIdentity();
3778 try {
3779 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003780 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
3781 PhoneConstantConversions.convertCallState(phone.getState());
3782 } finally {
3783 Binder.restoreCallingIdentity(identity);
3784 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003785 }
3786
Sanket Padawe356d7632015-06-22 14:03:32 -07003787 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00003788 public int getDataState() {
Jack Yu285100e2022-12-02 22:48:35 -08003789 return getDataStateForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003790 }
3791
3792 @Override
3793 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003794 final long identity = Binder.clearCallingIdentity();
3795 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003796 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003797 if (phone != null) {
Jack Yu7968c6d2022-07-31 00:43:21 -07003798 return phone.getDataNetworkController().getInternetDataNetworkState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003799 } else {
3800 return PhoneConstantConversions.convertDataState(
3801 PhoneConstants.DataState.DISCONNECTED);
3802 }
3803 } finally {
3804 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003805 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003806 }
3807
Sanket Padawe356d7632015-06-22 14:03:32 -07003808 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003809 public @DataActivityType int getDataActivity() {
Jack Yu285100e2022-12-02 22:48:35 -08003810 return getDataActivityForSubId(SubscriptionManager.getDefaultDataSubscriptionId());
Nathan Haroldc4689b12019-06-14 16:58:30 -07003811 }
3812
3813 @Override
Jack Yu0eda6842022-04-18 00:34:46 -07003814 public @DataActivityType int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003815 final long identity = Binder.clearCallingIdentity();
3816 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07003817 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003818 if (phone != null) {
Jack Yu0eda6842022-04-18 00:34:46 -07003819 return phone.getDataActivityState();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003820 } else {
3821 return TelephonyManager.DATA_ACTIVITY_NONE;
3822 }
3823 } finally {
3824 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003825 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003826 }
3827
3828 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08003829 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003830 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003831 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003832
3833 LocationAccessPolicy.LocationPermissionResult locationResult =
3834 LocationAccessPolicy.checkLocationPermission(mApp,
3835 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3836 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003837 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003838 .setCallingPid(Binder.getCallingPid())
3839 .setCallingUid(Binder.getCallingUid())
3840 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08003841 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003842 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3843 .build());
3844 switch (locationResult) {
3845 case DENIED_HARD:
3846 throw new SecurityException("Not allowed to access cell location");
3847 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08003848 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
3849 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003850 }
3851
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003852 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003853 final long identity = Binder.clearCallingIdentity();
3854 try {
3855 if (DBG_LOC) log("getCellLocation: is active user");
Jack Yu285100e2022-12-02 22:48:35 -08003856 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Meng Wanga10e89e2019-12-09 13:13:01 -08003857 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003858 } finally {
3859 Binder.restoreCallingIdentity(identity);
3860 }
Svetoslav64fad262015-04-14 14:35:21 -07003861 }
3862
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003863 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08003864 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003865 // Reporting the correct network country is ambiguous when IWLAN could conflict with
3866 // registered cell info, so return a NULL country instead.
3867 final long identity = Binder.clearCallingIdentity();
3868 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07003869 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
3870 // Get default phone in this case.
3871 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
3872 }
Jack Yu285100e2022-12-02 22:48:35 -08003873 final int subId = SubscriptionManager.getSubscriptionId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003874 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07003875 if (phone == null) return "";
3876 ServiceStateTracker sst = phone.getServiceStateTracker();
3877 if (sst == null) return "";
3878 LocaleTracker lt = sst.getLocaleTracker();
3879 if (lt == null) return "";
Shuo Qian9418a922021-03-09 11:21:16 -08003880 return lt.getCurrentCountry();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003881 } finally {
3882 Binder.restoreCallingIdentity(identity);
3883 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003884 }
3885
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003886 /**
3887 * This method was removed due to potential issues caused by performing partial
3888 * updates of service state, and lack of a credible use case.
3889 *
3890 * This has the ability to break the telephony implementation by disabling notification of
3891 * changes in device connectivity. DO NOT USE THIS!
3892 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07003893 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003894 public void enableLocationUpdates() {
3895 mApp.enforceCallingOrSelfPermission(
3896 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003897 }
3898
Nathan Harold7c8d0f12020-05-28 20:40:31 -07003899 /**
3900 * This method was removed due to potential issues caused by performing partial
3901 * updates of service state, and lack of a credible use case.
3902 *
3903 * This has the ability to break the telephony implementation by disabling notification of
3904 * changes in device connectivity. DO NOT USE THIS!
3905 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003906 @Override
3907 public void disableLocationUpdates() {
3908 mApp.enforceCallingOrSelfPermission(
3909 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003910 }
3911
3912 @Override
3913 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003914 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
3915 String callingFeatureId) {
Nathan Haroldb55f63b2021-07-27 11:27:38 -07003916 try {
3917 mApp.getSystemService(AppOpsManager.class)
3918 .checkPackage(Binder.getCallingUid(), callingPackage);
3919 } catch (SecurityException e) {
3920 EventLog.writeEvent(0x534e4554, "190619791", Binder.getCallingUid());
3921 throw e;
3922 }
3923
Nathan Haroldf096d982020-11-18 17:18:06 -08003924 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07003925 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3926 throw new SecurityException(
3927 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
3928 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07003929
Jordan Liu1617b712019-07-10 15:06:26 -07003930 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003931 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3932 return null;
3933 }
Svetoslav64fad262015-04-14 14:35:21 -07003934
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003935 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003936
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003937 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07003938 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003939
Nathan Haroldf180aac2018-06-01 18:43:55 -07003940 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
3941 for (CellInfo ci : info) {
3942 if (ci instanceof CellInfoGsm) {
3943 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
3944 } else if (ci instanceof CellInfoWcdma) {
3945 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
3946 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003947 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07003948 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003949 }
3950
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003951 private List<CellInfo> getCachedCellInfo() {
3952 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3953 for (Phone phone : PhoneFactory.getPhones()) {
3954 List<CellInfo> info = phone.getAllCellInfo();
3955 if (info != null) cellInfos.addAll(info);
3956 }
3957 return cellInfos;
3958 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003959
3960 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003961 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003962 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08003963 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08003964
3965 LocationAccessPolicy.LocationPermissionResult locationResult =
3966 LocationAccessPolicy.checkLocationPermission(mApp,
3967 new LocationAccessPolicy.LocationPermissionQuery.Builder()
3968 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07003969 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08003970 .setCallingPid(Binder.getCallingPid())
3971 .setCallingUid(Binder.getCallingUid())
3972 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08003973 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08003974 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
3975 .build());
3976 switch (locationResult) {
3977 case DENIED_HARD:
3978 throw new SecurityException("Not allowed to access cell info");
3979 case DENIED_SOFT:
3980 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003981 }
3982
Nathan Haroldf096d982020-11-18 17:18:06 -08003983 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07003984 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
3985 return getCachedCellInfo();
3986 }
3987
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07003988 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00003989 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003990 final long identity = Binder.clearCallingIdentity();
3991 try {
3992 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
3993 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07003994 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07003995 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003996 if (info != null) cellInfos.addAll(info);
3997 }
3998 return cellInfos;
3999 } finally {
4000 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004001 }
4002 }
4003
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07004004 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07004005 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
4006 String callingFeatureId) {
4007 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
4008 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07004009 }
4010
4011 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07004012 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
4013 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07004014 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07004015 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07004016 }
4017
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07004018 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
4019 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004020 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07004021 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08004022
4023 LocationAccessPolicy.LocationPermissionResult locationResult =
4024 LocationAccessPolicy.checkLocationPermission(mApp,
4025 new LocationAccessPolicy.LocationPermissionQuery.Builder()
4026 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07004027 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08004028 .setCallingPid(Binder.getCallingPid())
4029 .setCallingUid(Binder.getCallingUid())
4030 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07004031 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
4032 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08004033 .build());
4034 switch (locationResult) {
4035 case DENIED_HARD:
Nathan Haroldf096d982020-11-18 17:18:06 -08004036 if (TelephonyPermissions
4037 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07004038 // Safetynet logging for b/154934934
4039 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
4040 }
Hall Liuf19c44f2018-11-27 14:38:17 -08004041 throw new SecurityException("Not allowed to access cell info");
4042 case DENIED_SOFT:
Nathan Haroldf096d982020-11-18 17:18:06 -08004043 if (TelephonyPermissions
4044 .getTargetSdk(mApp, callingPackage) < Build.VERSION_CODES.Q) {
Hall Liud60acc92020-05-21 17:09:35 -07004045 // Safetynet logging for b/154934934
4046 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
4047 }
Nathan Harold5320c422019-05-09 10:26:08 -07004048 try {
4049 cb.onCellInfo(new ArrayList<CellInfo>());
4050 } catch (RemoteException re) {
4051 // Drop without consequences
4052 }
Hall Liuf19c44f2018-11-27 14:38:17 -08004053 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07004054 }
4055
Nathan Harolda939a962019-05-09 10:13:47 -07004056
4057 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07004058 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
4059
4060 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
4061 }
4062
4063 @Override
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00004064 public void setCellInfoListRate(int rateInMillis, int subId) {
Jack Yua8d8cb82017-01-16 10:15:34 -08004065 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00004066 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004067
4068 final long identity = Binder.clearCallingIdentity();
4069 try {
Aishwarya Mallampati0603fb12022-08-24 21:16:56 +00004070 Phone phone = getPhone(subId);
4071 if (phone == null) {
4072 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
4073 } else {
4074 phone.setCellInfoListRate(rateInMillis, workSource);
4075 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004076 } finally {
4077 Binder.restoreCallingIdentity(identity);
4078 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004079 }
4080
Shishir Agrawala9f32182016-04-12 12:00:16 -07004081 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004082 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004083 Phone phone = PhoneFactory.getPhone(slotIndex);
4084 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004085 return null;
4086 }
Jeff Davidson913390f2018-02-23 17:11:49 -08004087 int subId = phone.getSubId();
Grace Jia0ddb3612021-04-22 13:35:26 -07004088 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getImeiForSlot");
Michael Groover70af6dc2018-10-01 16:23:15 -07004089 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004090 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004091 return null;
4092 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004093
4094 final long identity = Binder.clearCallingIdentity();
4095 try {
4096 return phone.getImei();
4097 } finally {
4098 Binder.restoreCallingIdentity(identity);
4099 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07004100 }
4101
4102 @Override
arunvoddud5c6ce02022-12-11 06:03:12 +00004103 public String getPrimaryImei(String callingPackage, String callingFeatureId) {
4104 enforceCallingPackage(callingPackage, Binder.getCallingUid(), "getPrimaryImei");
4105 if (!checkCallingOrSelfReadDeviceIdentifiersForAnySub(mApp, callingPackage,
4106 callingFeatureId, "getPrimaryImei")) {
4107 throw new SecurityException("Caller does not have permission");
4108 }
4109 final long identity = Binder.clearCallingIdentity();
4110 try {
4111 for (Phone phone : PhoneFactory.getPhones()) {
4112 if (phone.getImeiType() == Phone.IMEI_TYPE_PRIMARY) {
4113 return phone.getImei();
4114 }
4115 }
4116 throw new UnsupportedOperationException("Operation not supported");
4117 } finally {
4118 Binder.restoreCallingIdentity(identity);
4119 }
4120 }
4121
4122 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00004123 public String getTypeAllocationCodeForSlot(int slotIndex) {
4124 Phone phone = PhoneFactory.getPhone(slotIndex);
4125 String tac = null;
4126 if (phone != null) {
4127 String imei = phone.getImei();
Vala Zadehab005552021-09-21 15:54:29 -07004128 try {
4129 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
4130 } catch (IndexOutOfBoundsException e) {
4131 Log.e(LOG_TAG, "IMEI length shorter than upper index.");
4132 return null;
4133 }
David Kelly5e06a7f2018-03-12 14:10:59 +00004134 }
4135 return tac;
4136 }
4137
4138 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004139 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07004140 try {
4141 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4142 } catch (SecurityException se) {
4143 EventLog.writeEvent(0x534e4554, "186530496", Binder.getCallingUid());
4144 throw new SecurityException("Package " + callingPackage + " does not belong to "
4145 + Binder.getCallingUid());
4146 }
Jeff Davidson913390f2018-02-23 17:11:49 -08004147 Phone phone = PhoneFactory.getPhone(slotIndex);
4148 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07004149 return null;
4150 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004151
Jeff Davidson913390f2018-02-23 17:11:49 -08004152 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07004153 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004154 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004155 return null;
4156 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004157
4158 final long identity = Binder.clearCallingIdentity();
4159 try {
4160 return phone.getMeid();
4161 } finally {
4162 Binder.restoreCallingIdentity(identity);
4163 }
Jack Yu2af8d712017-03-15 17:14:14 -07004164 }
4165
4166 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00004167 public String getManufacturerCodeForSlot(int slotIndex) {
4168 Phone phone = PhoneFactory.getPhone(slotIndex);
4169 String manufacturerCode = null;
4170 if (phone != null) {
4171 String meid = phone.getMeid();
Vala Zadehab005552021-09-21 15:54:29 -07004172 try {
4173 manufacturerCode =
4174 meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
4175 } catch (IndexOutOfBoundsException e) {
4176 Log.e(LOG_TAG, "MEID length shorter than upper index.");
4177 return null;
4178 }
David Kelly5e06a7f2018-03-12 14:10:59 +00004179 }
4180 return manufacturerCode;
4181 }
4182
4183 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004184 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
4185 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004186 Phone phone = PhoneFactory.getPhone(slotIndex);
4187 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004188 return null;
4189 }
Jeff Davidson913390f2018-02-23 17:11:49 -08004190 int subId = phone.getSubId();
4191 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004192 mApp, subId, callingPackage, callingFeatureId,
4193 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004194 return null;
4195 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004196
4197 final long identity = Binder.clearCallingIdentity();
4198 try {
4199 return phone.getDeviceSvn();
4200 } finally {
4201 Binder.restoreCallingIdentity(identity);
4202 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07004203 }
4204
fionaxu43304da2017-11-27 22:51:16 -08004205 @Override
4206 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004207 final long identity = Binder.clearCallingIdentity();
4208 try {
4209 final Phone phone = getPhone(subId);
4210 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
4211 } finally {
4212 Binder.restoreCallingIdentity(identity);
4213 }
fionaxu43304da2017-11-27 22:51:16 -08004214 }
4215
4216 @Override
4217 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004218 final long identity = Binder.clearCallingIdentity();
4219 try {
4220 final Phone phone = getPhone(subId);
4221 return phone == null ? null : phone.getCarrierName();
4222 } finally {
4223 Binder.restoreCallingIdentity(identity);
4224 }
fionaxu43304da2017-11-27 22:51:16 -08004225 }
4226
calvinpanffe225e2018-11-01 19:43:06 +08004227 @Override
chen xu0026ca62019-03-06 15:28:50 -08004228 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08004229 final long identity = Binder.clearCallingIdentity();
4230 try {
4231 final Phone phone = getPhone(subId);
4232 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08004233 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08004234 } finally {
4235 Binder.restoreCallingIdentity(identity);
4236 }
4237 }
4238
4239 @Override
chen xu0026ca62019-03-06 15:28:50 -08004240 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08004241 final long identity = Binder.clearCallingIdentity();
4242 try {
4243 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08004244 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08004245 } finally {
4246 Binder.restoreCallingIdentity(identity);
4247 }
4248 }
4249
chen xu651eec72018-11-11 19:03:44 -08004250 @Override
chen xu864e11c2018-12-06 22:10:03 -08004251 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
4252 if (!isSubscriptionMccMnc) {
4253 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
4254 }
chen xu651eec72018-11-11 19:03:44 -08004255 final Phone phone = PhoneFactory.getPhone(slotIndex);
4256 if (phone == null) {
4257 return TelephonyManager.UNKNOWN_CARRIER_ID;
4258 }
4259 final long identity = Binder.clearCallingIdentity();
4260 try {
4261 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
4262 } finally {
4263 Binder.restoreCallingIdentity(identity);
4264 }
4265 }
4266
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004267 //
4268 // Internal helper methods.
4269 //
4270
Sanket Padaweee13a9b2016-03-08 17:30:28 -08004271 /**
Grace Jia0ddb3612021-04-22 13:35:26 -07004272 * Make sure the caller is the calling package itself
4273 *
4274 * @throws SecurityException if the caller is not the calling package
4275 */
4276 private void enforceCallingPackage(String callingPackage, int callingUid, String message) {
4277 int packageUid = -1;
Grace Jiadbefca02021-04-26 15:13:31 -07004278 PackageManager pm = mApp.getBaseContext().createContextAsUser(
4279 UserHandle.getUserHandleForUid(callingUid), 0).getPackageManager();
Grace Jia0ddb3612021-04-22 13:35:26 -07004280 try {
Grace Jiadbefca02021-04-26 15:13:31 -07004281 packageUid = pm.getPackageUid(callingPackage, 0);
Grace Jia0ddb3612021-04-22 13:35:26 -07004282 } catch (PackageManager.NameNotFoundException e) {
4283 // packageUid is -1
4284 }
4285 if (packageUid != callingUid) {
4286 throw new SecurityException(message + ": Package " + callingPackage
4287 + " does not belong to " + callingUid);
4288 }
4289 }
4290
4291 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004292 * Make sure the caller has the MODIFY_PHONE_STATE permission.
4293 *
4294 * @throws SecurityException if the caller does not have the required permission
4295 */
Gil Cukierman1c0eb932022-12-06 22:28:24 +00004296 @VisibleForTesting
4297 public void enforceModifyPermission() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004298 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
4299 }
4300
Gil Cukierman1c0eb932022-12-06 22:28:24 +00004301 /**
arunvoddud7401012022-12-15 16:08:12 +00004302 * Make sure the caller has the READ_PHONE_STATE permission.
Gil Cukierman1c0eb932022-12-06 22:28:24 +00004303 *
4304 * @throws SecurityException if the caller does not have the required permission
4305 */
4306 @VisibleForTesting
4307 public void enforceReadPermission() {
arunvoddud7401012022-12-15 16:08:12 +00004308 enforceReadPermission(null);
4309 }
4310
4311 /**
4312 * Make sure the caller has the READ_PHONE_STATE permissions.
4313 *
4314 * @throws SecurityException if the caller does not have the READ_PHONE_STATE permission.
4315 */
4316 @VisibleForTesting
4317 public void enforceReadPermission(String msg) {
4318 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE, msg);
Gil Cukierman1c0eb932022-12-06 22:28:24 +00004319 }
4320
Shuo Qian3b6ee772019-11-13 17:43:31 -08004321 private void enforceActiveEmergencySessionPermission() {
4322 mApp.enforceCallingOrSelfPermission(
4323 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
4324 }
4325
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004326 /**
4327 * Make sure the caller has the CALL_PHONE permission.
4328 *
4329 * @throws SecurityException if the caller does not have the required permission
4330 */
4331 private void enforceCallPermission() {
4332 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
4333 }
4334
paulhu5a773602019-08-23 19:17:33 +08004335 private void enforceSettingsPermission() {
4336 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07004337 }
4338
Michele Berionne5e411512020-11-13 02:36:59 +00004339 private void enforceRebootPermission() {
4340 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
4341 }
4342
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +00004343 /**
4344 * Make sure the caller has SATELLITE_COMMUNICATION permission.
4345 * @param message - log message to print.
4346 * @throws SecurityException if the caller does not have the required permission
4347 */
4348 private void enforceSatelliteCommunicationPermission(String message) {
4349 mApp.enforceCallingOrSelfPermission(permission.SATELLITE_COMMUNICATION, message);
4350 }
4351
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004352 private String createTelUrl(String number) {
4353 if (TextUtils.isEmpty(number)) {
4354 return null;
4355 }
4356
Jake Hambye994d462014-02-03 13:10:13 -08004357 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004358 }
4359
Ihab Awadf9e92732013-12-05 18:02:52 -08004360 private static void log(String msg) {
Ling Ma83dc5ea2023-01-12 15:06:04 -08004361 Log.d(LOG_TAG, msg);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004362 }
4363
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07004364 private static void logv(String msg) {
4365 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
4366 }
4367
Ihab Awadf9e92732013-12-05 18:02:52 -08004368 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004369 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
4370 }
4371
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004372 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004373 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07004374 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07004375 }
4376
Sanket Padawe356d7632015-06-22 14:03:32 -07004377 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004378 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004379 final long identity = Binder.clearCallingIdentity();
4380 try {
4381 final Phone phone = PhoneFactory.getPhone(slotIndex);
4382 if (phone == null) {
4383 return PhoneConstants.PHONE_TYPE_NONE;
4384 } else {
4385 return phone.getPhoneType();
4386 }
4387 } finally {
4388 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004389 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004390 }
4391
4392 /**
4393 * Returns the CDMA ERI icon index to display
4394 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004395 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004396 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
4397 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
4398 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004399 }
4400
Sanket Padawe356d7632015-06-22 14:03:32 -07004401 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004402 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
4403 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004404 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004405 mApp, subId, callingPackage, callingFeatureId,
4406 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004407 return -1;
4408 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004409
4410 final long identity = Binder.clearCallingIdentity();
4411 try {
4412 final Phone phone = getPhone(subId);
4413 if (phone != null) {
4414 return phone.getCdmaEriIconIndex();
4415 } else {
4416 return -1;
4417 }
4418 } finally {
4419 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004420 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004421 }
4422
4423 /**
4424 * Returns the CDMA ERI icon mode,
4425 * 0 - ON
4426 * 1 - FLASHING
4427 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004428 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004429 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
4430 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
4431 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004432 }
4433
Sanket Padawe356d7632015-06-22 14:03:32 -07004434 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004435 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
4436 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004437 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004438 mApp, subId, callingPackage, callingFeatureId,
4439 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004440 return -1;
4441 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004442
4443 final long identity = Binder.clearCallingIdentity();
4444 try {
4445 final Phone phone = getPhone(subId);
4446 if (phone != null) {
4447 return phone.getCdmaEriIconMode();
4448 } else {
4449 return -1;
4450 }
4451 } finally {
4452 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004453 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004454 }
4455
4456 /**
4457 * Returns the CDMA ERI text,
4458 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004459 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004460 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
4461 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
4462 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004463 }
4464
Sanket Padawe356d7632015-06-22 14:03:32 -07004465 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004466 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
4467 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004468 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004469 mApp, subId, callingPackage, callingFeatureId,
4470 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004471 return null;
4472 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004473
4474 final long identity = Binder.clearCallingIdentity();
4475 try {
4476 final Phone phone = getPhone(subId);
4477 if (phone != null) {
4478 return phone.getCdmaEriText();
4479 } else {
4480 return null;
4481 }
4482 } finally {
4483 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004484 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004485 }
4486
4487 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07004488 * Returns the CDMA MDN.
4489 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004490 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07004491 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004492 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4493 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004494
4495 final long identity = Binder.clearCallingIdentity();
4496 try {
4497 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004498 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004499 return phone.getLine1Number();
4500 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004501 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004502 return null;
4503 }
4504 } finally {
4505 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07004506 }
4507 }
4508
4509 /**
4510 * Returns the CDMA MIN.
4511 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004512 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07004513 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004514 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4515 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004516
4517 final long identity = Binder.clearCallingIdentity();
4518 try {
4519 final Phone phone = getPhone(subId);
4520 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
4521 return phone.getCdmaMin();
4522 } else {
4523 return null;
4524 }
4525 } finally {
4526 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07004527 }
4528 }
4529
Hall Liud892bec2018-11-30 14:51:45 -08004530 @Override
4531 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
4532 INumberVerificationCallback callback, String callingPackage) {
4533 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
4534 != PERMISSION_GRANTED) {
4535 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
4536 }
4537 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4538
4539 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
4540 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
Hall Liub9d8feb2021-01-13 10:28:04 -08004541 throw new SecurityException("Calling package must be configured in the device config: "
4542 + "calling package: " + callingPackage
4543 + ", configured package: " + authorizedPackage);
Hall Liud892bec2018-11-30 14:51:45 -08004544 }
4545
4546 if (range == null) {
4547 throw new NullPointerException("Range must be non-null");
4548 }
4549
4550 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08004551 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08004552
4553 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
4554 }
4555
Junda Liuca05d5d2014-08-14 22:36:34 -07004556 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004557 * Returns true if CDMA provisioning needs to run.
4558 */
4559 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004560 final long identity = Binder.clearCallingIdentity();
4561 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004562 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004563 } finally {
4564 Binder.restoreCallingIdentity(identity);
4565 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004566 }
4567
4568 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004569 * Sets the voice mail number of a given subId.
4570 */
4571 @Override
4572 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004573 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
4574 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004575
4576 final long identity = Binder.clearCallingIdentity();
4577 try {
4578 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
4579 new Pair<String, String>(alphaTag, number), new Integer(subId));
4580 return success;
4581 } finally {
4582 Binder.restoreCallingIdentity(identity);
4583 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004584 }
4585
Ta-wei Yen87c49842016-05-13 21:19:52 -07004586 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004587 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
4588 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004589 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
4590 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004591 if (!TextUtils.equals(callingPackage, systemDialer)) {
4592 throw new SecurityException("caller must be system dialer");
4593 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004594
4595 final long identity = Binder.clearCallingIdentity();
4596 try {
4597 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
4598 if (phoneAccountHandle == null) {
4599 return null;
4600 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004601 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004602 } finally {
4603 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004604 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07004605 }
4606
4607 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004608 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
4609 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08004610 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08004611 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004612 mApp, subId, callingPackage, callingFeatureId,
4613 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08004614 return null;
4615 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004616
Jeff Davidsona8e4e242018-03-15 17:16:18 -07004617 final long identity = Binder.clearCallingIdentity();
4618 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004619 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07004620 } finally {
4621 Binder.restoreCallingIdentity(identity);
4622 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08004623 }
4624
4625 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004626 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
4627 VisualVoicemailSmsFilterSettings settings) {
4628 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004629
4630 final long identity = Binder.clearCallingIdentity();
4631 try {
4632 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004633 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004634 } finally {
4635 Binder.restoreCallingIdentity(identity);
4636 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004637 }
4638
4639 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004640 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
4641 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004642
4643 final long identity = Binder.clearCallingIdentity();
4644 try {
4645 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004646 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004647 } finally {
4648 Binder.restoreCallingIdentity(identity);
4649 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004650 }
4651
4652 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07004653 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
4654 String callingPackage, int subId) {
4655 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004656
4657 final long identity = Binder.clearCallingIdentity();
4658 try {
4659 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004660 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004661 } finally {
4662 Binder.restoreCallingIdentity(identity);
4663 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07004664 }
4665
4666 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004667 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004668 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004669
4670 final long identity = Binder.clearCallingIdentity();
4671 try {
4672 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004673 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004674 } finally {
4675 Binder.restoreCallingIdentity(identity);
4676 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004677 }
4678
4679 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004680 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
4681 String callingAttributionTag, int subId, String number, int port, String text,
4682 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004683 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004684 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004685 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07004686 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07004687 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
4688 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07004689 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07004690
Shishir Agrawal76d5da92014-11-09 16:17:25 -08004691 /**
fionaxu0152e512016-11-14 13:36:14 -08004692 * Sets the voice activation state of a given subId.
4693 */
4694 @Override
4695 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004696 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4697 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004698
4699 final long identity = Binder.clearCallingIdentity();
4700 try {
4701 final Phone phone = getPhone(subId);
4702 if (phone != null) {
4703 phone.setVoiceActivationState(activationState);
4704 } else {
4705 loge("setVoiceActivationState fails with invalid subId: " + subId);
4706 }
4707 } finally {
4708 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004709 }
4710 }
4711
4712 /**
4713 * Sets the data activation state of a given subId.
4714 */
4715 @Override
4716 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004717 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4718 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004719
4720 final long identity = Binder.clearCallingIdentity();
4721 try {
4722 final Phone phone = getPhone(subId);
4723 if (phone != null) {
4724 phone.setDataActivationState(activationState);
4725 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09004726 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004727 }
4728 } finally {
4729 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004730 }
4731 }
4732
4733 /**
4734 * Returns the voice activation state of a given subId.
4735 */
4736 @Override
4737 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004738 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004739
fionaxu0152e512016-11-14 13:36:14 -08004740 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004741 final long identity = Binder.clearCallingIdentity();
4742 try {
4743 if (phone != null) {
4744 return phone.getVoiceActivationState();
4745 } else {
4746 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4747 }
4748 } finally {
4749 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004750 }
4751 }
4752
4753 /**
4754 * Returns the data activation state of a given subId.
4755 */
4756 @Override
4757 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004758 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004759
fionaxu0152e512016-11-14 13:36:14 -08004760 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004761 final long identity = Binder.clearCallingIdentity();
4762 try {
4763 if (phone != null) {
4764 return phone.getDataActivationState();
4765 } else {
4766 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
4767 }
4768 } finally {
4769 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08004770 }
4771 }
4772
4773 /**
Wink Saville36469e72014-06-11 15:17:00 -07004774 * Returns the unread count of voicemails for a subId
4775 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004776 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004777 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
4778 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004779 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004780 mApp, subId, callingPackage, callingFeatureId,
4781 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08004782 return 0;
4783 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004784 final long identity = Binder.clearCallingIdentity();
4785 try {
4786 final Phone phone = getPhone(subId);
4787 if (phone != null) {
4788 return phone.getVoiceMessageCount();
4789 } else {
4790 return 0;
4791 }
4792 } finally {
4793 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004794 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004795 }
4796
4797 /**
Thomas Nguyen8ee49682023-02-01 11:46:09 -08004798 * returns true, if the device is in a state where both voice and data
4799 * are supported simultaneously. This can change based on location or network condition.
pkanwar8a4dcfb2017-01-19 13:43:16 -08004800 */
4801 @Override
4802 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004803 final long identity = Binder.clearCallingIdentity();
4804 try {
4805 final Phone phone = getPhone(subId);
4806 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
4807 } finally {
4808 Binder.restoreCallingIdentity(identity);
4809 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08004810 }
4811
4812 /**
fionaxu235cc5e2017-03-06 22:25:57 -08004813 * Send the dialer code if called from the current default dialer or the caller has
4814 * carrier privilege.
4815 * @param inputCode The dialer code to send
4816 */
4817 @Override
4818 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004819 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08004820 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07004821 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
4822 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08004823 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08004824 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08004825 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08004826 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004827
4828 final long identity = Binder.clearCallingIdentity();
4829 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004830 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004831 } finally {
4832 Binder.restoreCallingIdentity(identity);
4833 }
fionaxu235cc5e2017-03-06 22:25:57 -08004834 }
4835
Pengquan Menga1bb6272018-09-06 09:59:22 -07004836 @Override
4837 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08004838 TelephonyPermissions
Thomas Nguyen8ee49682023-02-01 11:46:09 -08004839 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
4840 mApp, subId, "getNetworkSelectionMode");
shilufc958392020-01-20 11:36:01 -08004841 final long identity = Binder.clearCallingIdentity();
4842 try {
4843 if (!isActiveSubscription(subId)) {
4844 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
4845 }
4846 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
4847 } finally {
4848 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07004849 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07004850 }
4851
Brad Ebinger35c841c2018-10-01 10:40:55 -07004852 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07004853 public boolean isInEmergencySmsMode() {
4854 enforceReadPrivilegedPermission("isInEmergencySmsMode");
4855 final long identity = Binder.clearCallingIdentity();
4856 try {
4857 for (Phone phone : PhoneFactory.getPhones()) {
4858 if (phone.isInEmergencySmsMode()) {
4859 return true;
4860 }
4861 }
4862 } finally {
4863 Binder.restoreCallingIdentity(identity);
4864 }
4865 return false;
4866 }
4867
shilu366312e2019-12-17 09:28:10 -08004868 /**
4869 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4870 * @param subId The subscription to use to check the configuration.
4871 * @param c The callback that will be used to send the result.
4872 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07004873 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004874 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
4875 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07004876 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004877 mApp, subId, "registerImsRegistrationCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00004878
4879 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4880 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4881 "IMS not available on device.");
4882 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004883 final long token = Binder.clearCallingIdentity();
4884 try {
Brad Ebingera2628302022-02-18 03:44:55 +00004885 int slotId = getSlotIndexOrException(subId);
4886 verifyImsMmTelConfiguredOrThrow(slotId);
joonhunshin49f0aed2022-08-05 08:33:05 +00004887
4888 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4889 if (controller != null) {
4890 ImsManager imsManager = controller.getImsManager(subId);
4891 if (imsManager != null) {
4892 imsManager.addRegistrationCallbackForSubscription(c, subId);
4893 } else {
4894 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
4895 }
4896 } else {
4897 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
4898 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004899 } catch (ImsException e) {
4900 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07004901 } finally {
4902 Binder.restoreCallingIdentity(token);
4903 }
4904 }
4905
shilu366312e2019-12-17 09:28:10 -08004906 /**
4907 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
4908 * @param subId The subscription to use to check the configuration.
4909 * @param c The callback that will be used to send the result.
4910 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07004911 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08004912 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07004913 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004914 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08004915 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4916 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
4917 }
Meng Wangafbc5852019-09-19 17:37:13 -07004918 final long token = Binder.clearCallingIdentity();
joonhunshin49f0aed2022-08-05 08:33:05 +00004919
Meng Wangafbc5852019-09-19 17:37:13 -07004920 try {
joonhunshin49f0aed2022-08-05 08:33:05 +00004921 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
4922 if (controller != null) {
4923 ImsManager imsManager = controller.getImsManager(subId);
4924 if (imsManager != null) {
4925 imsManager.removeRegistrationCallbackForSubscription(c, subId);
4926 } else {
4927 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
4928 + "is inactive, ignoring unregister.");
4929 // If the ImsManager is not valid, just return, since the callback
4930 // will already have been removed internally.
4931 }
4932 }
Meng Wangafbc5852019-09-19 17:37:13 -07004933 } finally {
4934 Binder.restoreCallingIdentity(token);
4935 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07004936 }
4937
Brad Ebingera34a6c22019-10-22 17:36:18 -07004938 /**
4939 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
4940 */
4941 @Override
4942 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
4943 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
4944 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4945 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4946 "IMS not available on device.");
4947 }
4948 final long token = Binder.clearCallingIdentity();
4949 try {
4950 Phone phone = getPhone(subId);
4951 if (phone == null) {
4952 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4953 + subId + "'");
4954 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4955 }
4956 phone.getImsRegistrationState(regState -> {
4957 try {
4958 consumer.accept((regState == null)
4959 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
4960 } catch (RemoteException e) {
4961 // Ignore if the remote process is no longer available to call back.
4962 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
4963 }
4964 });
4965 } finally {
4966 Binder.restoreCallingIdentity(token);
4967 }
4968 }
4969
4970 /**
4971 * Get the transport type for the IMS service registration state.
4972 */
4973 @Override
4974 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Nathan Harold62c68512021-04-06 11:26:02 -07004975 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08004976 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07004977 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
4978 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
4979 "IMS not available on device.");
4980 }
4981 final long token = Binder.clearCallingIdentity();
4982 try {
4983 Phone phone = getPhone(subId);
4984 if (phone == null) {
4985 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
4986 + subId + "'");
4987 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
4988 }
4989 phone.getImsRegistrationTech(regTech -> {
4990 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
4991 int regTechConverted = (regTech == null)
4992 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
4993 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
4994 regTechConverted);
4995 try {
4996 consumer.accept(regTechConverted);
4997 } catch (RemoteException e) {
4998 // Ignore if the remote process is no longer available to call back.
4999 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
5000 }
5001 });
5002 } finally {
5003 Binder.restoreCallingIdentity(token);
5004 }
5005 }
5006
shilu366312e2019-12-17 09:28:10 -08005007 /**
5008 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5009 * @param subId The subscription to use to check the configuration.
5010 * @param c The callback that will be used to send the result.
5011 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005012 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08005013 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
5014 throws RemoteException {
Nathan Harold62c68512021-04-06 11:26:02 -07005015 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005016 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingera2628302022-02-18 03:44:55 +00005017 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5018 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5019 "IMS not available on device.");
5020 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07005021 final long token = Binder.clearCallingIdentity();
5022 try {
Brad Ebingera2628302022-02-18 03:44:55 +00005023 int slotId = getSlotIndexOrException(subId);
5024 verifyImsMmTelConfiguredOrThrow(slotId);
Hwangoo Park8646b0d2023-01-13 02:42:34 +00005025
5026 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
5027 if (controller != null) {
5028 ImsManager imsManager = controller.getImsManager(subId);
5029 if (imsManager != null) {
5030 imsManager.addCapabilitiesCallbackForSubscription(c, subId);
5031 } else {
5032 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
5033 }
5034 } else {
5035 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5036 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005037 } catch (ImsException e) {
5038 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005039 } finally {
5040 Binder.restoreCallingIdentity(token);
5041 }
5042 }
5043
shilu366312e2019-12-17 09:28:10 -08005044 /**
5045 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5046 * @param subId The subscription to use to check the configuration.
5047 * @param c The callback that will be used to send the result.
5048 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005049 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08005050 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Nathan Harold62c68512021-04-06 11:26:02 -07005051 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005052 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08005053 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5054 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5055 }
Meng Wangafbc5852019-09-19 17:37:13 -07005056
5057 final long token = Binder.clearCallingIdentity();
5058 try {
Hwangoo Park8646b0d2023-01-13 02:42:34 +00005059 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
5060 if (controller != null) {
5061 ImsManager imsManager = controller.getImsManager(subId);
5062 if (imsManager != null) {
5063 imsManager.removeCapabilitiesCallbackForSubscription(c, subId);
5064 } else {
5065 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
5066 + " is inactive, ignoring unregister.");
5067 // If the ImsManager is not valid, just return, since the callback
5068 // will already have been removed internally.
5069 }
5070 }
Meng Wangafbc5852019-09-19 17:37:13 -07005071 } finally {
5072 Binder.restoreCallingIdentity(token);
5073 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07005074 }
5075
5076 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08005077 public boolean isCapable(int subId, int capability, int regTech) {
5078 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005079 final long token = Binder.clearCallingIdentity();
5080 try {
Brad Ebingera2628302022-02-18 03:44:55 +00005081 int slotId = getSlotIndexOrException(subId);
5082 verifyImsMmTelConfiguredOrThrow(slotId);
5083 return ImsManager.getInstance(mApp, slotId).queryMmTelCapability(capability, regTech);
5084 } catch (com.android.ims.ImsException e) {
5085 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
5086 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005087 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08005088 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
5089 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07005090 } finally {
5091 Binder.restoreCallingIdentity(token);
5092 }
5093 }
5094
5095 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08005096 public boolean isAvailable(int subId, int capability, int regTech) {
5097 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005098 final long token = Binder.clearCallingIdentity();
5099 try {
5100 Phone phone = getPhone(subId);
5101 if (phone == null) return false;
5102 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07005103 } catch (com.android.ims.ImsException e) {
5104 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
5105 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07005106 } finally {
5107 Binder.restoreCallingIdentity(token);
5108 }
5109 }
5110
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005111 /**
5112 * Determines if the MmTel feature capability is supported by the carrier configuration for this
5113 * subscription.
5114 * @param subId The subscription to use to check the configuration.
5115 * @param callback The callback that will be used to send the result.
5116 * @param capability The MmTelFeature capability that will be used to send the result.
5117 * @param transportType The transport type of the MmTelFeature capability.
5118 */
5119 @Override
5120 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
5121 int transportType) {
5122 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
Brad Ebingera2628302022-02-18 03:44:55 +00005123 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5124 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5125 "IMS not available on device.");
5126 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005127 final long token = Binder.clearCallingIdentity();
5128 try {
Brad Ebingera2628302022-02-18 03:44:55 +00005129 int slotId = getSlotIndex(subId);
5130 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5131 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
5132 + subId + "'");
5133 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
5134 }
5135 verifyImsMmTelConfiguredOrThrow(slotId);
5136 ImsManager.getInstance(mApp, slotId).isSupported(capability,
5137 transportType, aBoolean -> {
5138 try {
5139 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
5140 } catch (RemoteException e) {
5141 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
5142 + "running. Ignore");
5143 }
5144 });
Brad Ebinger919631e2021-06-02 17:46:35 -07005145 } catch (ImsException e) {
5146 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005147 } finally {
5148 Binder.restoreCallingIdentity(token);
5149 }
5150 }
5151
shilu366312e2019-12-17 09:28:10 -08005152 /**
5153 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5154 * @param subId The subscription to use to check the configuration.
5155 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005156 @Override
5157 public boolean isAdvancedCallingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005158 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005159 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08005160
Brad Ebinger35c841c2018-10-01 10:40:55 -07005161 final long token = Binder.clearCallingIdentity();
5162 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005163 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005164 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005165 return ImsManager.getInstance(mApp, slotId).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005166 } catch (ImsException e) {
5167 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005168 } finally {
5169 Binder.restoreCallingIdentity(token);
5170 }
5171 }
5172
5173 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08005174 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005175 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08005176 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005177 final long identity = Binder.clearCallingIdentity();
5178 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005179 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005180 // This setting doesn't require an active ImsService connection, so do not verify. The
5181 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005182 ImsManager.getInstance(mApp, slotId).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005183 } catch (ImsException e) {
5184 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005185 } finally {
5186 Binder.restoreCallingIdentity(identity);
5187 }
5188 }
5189
shilu366312e2019-12-17 09:28:10 -08005190 /**
5191 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5192 * @param subId The subscription to use to check the configuration.
5193 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005194 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08005195 public boolean isVtSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005196 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005197 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005198 final long identity = Binder.clearCallingIdentity();
5199 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005200 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005201 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005202 return ImsManager.getInstance(mApp, slotId).isVtEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005203 } catch (ImsException e) {
5204 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005205 } finally {
5206 Binder.restoreCallingIdentity(identity);
5207 }
5208 }
5209
5210 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08005211 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005212 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08005213 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005214 final long identity = Binder.clearCallingIdentity();
5215 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005216 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005217 // This setting doesn't require an active ImsService connection, so do not verify. The
5218 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005219 ImsManager.getInstance(mApp, slotId).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005220 } catch (ImsException e) {
5221 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005222 } finally {
5223 Binder.restoreCallingIdentity(identity);
5224 }
5225 }
5226
shilu366312e2019-12-17 09:28:10 -08005227 /**
5228 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5229 * @param subId The subscription to use to check the configuration.
5230 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005231 @Override
5232 public boolean isVoWiFiSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005233 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005234 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005235 final long identity = Binder.clearCallingIdentity();
5236 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005237 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005238 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005239 return ImsManager.getInstance(mApp, slotId).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005240 } catch (ImsException e) {
5241 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005242 } finally {
5243 Binder.restoreCallingIdentity(identity);
5244 }
5245 }
5246
5247 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08005248 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005249 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08005250 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005251 final long identity = Binder.clearCallingIdentity();
5252 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005253 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005254 // This setting doesn't require an active ImsService connection, so do not verify. The
5255 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005256 ImsManager.getInstance(mApp, slotId).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005257 } catch (ImsException e) {
5258 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005259 } finally {
5260 Binder.restoreCallingIdentity(identity);
5261 }
5262 }
5263
shilu366312e2019-12-17 09:28:10 -08005264 /**
Sooraj Sasindrane655add2020-11-23 19:40:38 -08005265 * @return true if the user's setting for Voice over Cross SIM is enabled and false if it is not
5266 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5267 * @param subId The subscription to use to check the configuration.
5268 */
5269 @Override
5270 public boolean isCrossSimCallingEnabledByUser(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005271 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindrane655add2020-11-23 19:40:38 -08005272 mApp, subId, "isCrossSimCallingEnabledByUser");
5273 final long identity = Binder.clearCallingIdentity();
5274 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005275 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005276 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005277 return ImsManager.getInstance(mApp, slotId).isCrossSimCallingEnabledByUser();
Sooraj Sasindrane655add2020-11-23 19:40:38 -08005278 } catch (ImsException e) {
5279 throw new ServiceSpecificException(e.getCode());
5280 } finally {
5281 Binder.restoreCallingIdentity(identity);
5282 }
5283 }
5284
5285 /**
5286 * Sets the user's setting for whether or not Voice over Cross SIM is enabled.
5287 * Requires MODIFY_PHONE_STATE permission.
5288 * @param subId The subscription to use to check the configuration.
5289 * @param isEnabled true if the user's setting for Voice over Cross SIM is enabled,
5290 * false otherwise
5291 */
5292 @Override
5293 public void setCrossSimCallingEnabled(int subId, boolean isEnabled) {
5294 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5295 "setCrossSimCallingEnabled");
5296 final long identity = Binder.clearCallingIdentity();
5297 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005298 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005299 // This setting doesn't require an active ImsService connection, so do not verify. The
5300 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005301 ImsManager.getInstance(mApp, slotId).setCrossSimCallingEnabled(isEnabled);
Sooraj Sasindrane655add2020-11-23 19:40:38 -08005302 } catch (ImsException e) {
5303 throw new ServiceSpecificException(e.getCode());
5304 } finally {
5305 Binder.restoreCallingIdentity(identity);
5306 }
5307 }
5308
5309 /**
shilu366312e2019-12-17 09:28:10 -08005310 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5311 * @param subId The subscription to use to check the configuration.
5312 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005313 @Override
Nathan Harold62c68512021-04-06 11:26:02 -07005314
Brad Ebinger35c841c2018-10-01 10:40:55 -07005315 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005316 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005317 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005318 final long identity = Binder.clearCallingIdentity();
5319 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005320 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005321 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005322 return ImsManager.getInstance(mApp, slotId).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005323 } catch (ImsException e) {
5324 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005325 } finally {
5326 Binder.restoreCallingIdentity(identity);
5327 }
5328 }
5329
5330 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08005331 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005332 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08005333 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005334 final long identity = Binder.clearCallingIdentity();
5335 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005336 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005337 // This setting doesn't require an active ImsService connection, so do not verify. The
5338 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005339 ImsManager.getInstance(mApp, slotId).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005340 } catch (ImsException e) {
5341 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005342 } finally {
5343 Binder.restoreCallingIdentity(identity);
5344 }
5345 }
5346
5347 @Override
5348 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
5349 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5350 "setVoWiFiNonPersistent");
5351 final long identity = Binder.clearCallingIdentity();
5352 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005353 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005354 // This setting will be ignored if the ImsService isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005355 ImsManager.getInstance(mApp, slotId).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005356 } catch (ImsException e) {
5357 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005358 } finally {
5359 Binder.restoreCallingIdentity(identity);
5360 }
5361 }
5362
shilu366312e2019-12-17 09:28:10 -08005363 /**
5364 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5365 * @param subId The subscription to use to check the configuration.
5366 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005367 @Override
5368 public int getVoWiFiModeSetting(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005369 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005370 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005371 final long identity = Binder.clearCallingIdentity();
5372 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005373 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005374 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005375 return ImsManager.getInstance(mApp, slotId).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005376 } catch (ImsException e) {
5377 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005378 } finally {
5379 Binder.restoreCallingIdentity(identity);
5380 }
5381 }
5382
5383 @Override
5384 public void setVoWiFiModeSetting(int subId, int mode) {
5385 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5386 "setVoWiFiModeSetting");
5387 final long identity = Binder.clearCallingIdentity();
5388 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005389 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005390 // This setting doesn't require an active ImsService connection, so do not verify. The
5391 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005392 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005393 } catch (ImsException e) {
5394 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005395 } finally {
5396 Binder.restoreCallingIdentity(identity);
5397 }
5398 }
5399
5400 @Override
5401 public int getVoWiFiRoamingModeSetting(int subId) {
5402 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
5403 final long identity = Binder.clearCallingIdentity();
5404 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005405 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005406 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005407 return ImsManager.getInstance(mApp, slotId).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005408 } catch (ImsException e) {
5409 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005410 } finally {
5411 Binder.restoreCallingIdentity(identity);
5412 }
5413 }
5414
5415 @Override
5416 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
5417 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5418 "setVoWiFiRoamingModeSetting");
5419 final long identity = Binder.clearCallingIdentity();
5420 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005421 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005422 // This setting doesn't require an active ImsService connection, so do not verify. The
5423 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005424 ImsManager.getInstance(mApp, slotId).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005425 } catch (ImsException e) {
5426 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005427 } finally {
5428 Binder.restoreCallingIdentity(identity);
5429 }
5430 }
5431
5432 @Override
5433 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
5434 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5435 "setRttCapabilityEnabled");
5436 final long identity = Binder.clearCallingIdentity();
5437 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005438 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005439 // This setting doesn't require an active ImsService connection, so do not verify. The
5440 // new setting will be picked up when the ImsService comes up next if it isn't up.
Brad Ebinger919631e2021-06-02 17:46:35 -07005441 ImsManager.getInstance(mApp, slotId).setRttEnabled(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005442 } catch (ImsException e) {
5443 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005444 } finally {
5445 Binder.restoreCallingIdentity(identity);
5446 }
5447 }
5448
shilu366312e2019-12-17 09:28:10 -08005449 /**
5450 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
5451 * @param subId The subscription to use to check the configuration.
5452 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07005453 @Override
5454 public boolean isTtyOverVolteEnabled(int subId) {
Nathan Harold62c68512021-04-06 11:26:02 -07005455 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
Rambo Wang37f9c242020-02-10 14:45:28 -08005456 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07005457 final long identity = Binder.clearCallingIdentity();
5458 try {
Brad Ebinger919631e2021-06-02 17:46:35 -07005459 int slotId = getSlotIndexOrException(subId);
Brad Ebinger735c5ce2021-07-12 13:58:21 -07005460 // This setting doesn't require an active ImsService connection, so do not verify.
Brad Ebinger919631e2021-06-02 17:46:35 -07005461 return ImsManager.getInstance(mApp, slotId).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005462 } catch (ImsException e) {
5463 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07005464 } finally {
5465 Binder.restoreCallingIdentity(identity);
5466 }
5467 }
5468
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005469 @Override
5470 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
5471 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00005472
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005473 final long identity = Binder.clearCallingIdentity();
5474 try {
Brad Ebingera2628302022-02-18 03:44:55 +00005475 if (!isImsAvailableOnDevice()) {
5476 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5477 "IMS not available on device.");
5478 }
5479 int slotId = getSlotIndexOrException(subId);
5480 verifyImsMmTelConfiguredOrThrow(slotId);
Hwangoo Park8646b0d2023-01-13 02:42:34 +00005481
5482 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
5483 if (controller != null) {
5484 ImsManager imsManager = controller.getImsManager(subId);
5485 if (imsManager != null) {
5486 imsManager.addProvisioningCallbackForSubscription(callback, subId);
5487 } else {
5488 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE);
5489 }
5490 } else {
5491 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5492 }
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005493 } catch (ImsException e) {
5494 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005495 } finally {
5496 Binder.restoreCallingIdentity(identity);
5497 }
5498 }
5499
5500 @Override
5501 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
5502 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
joonhunshincffb7fc2021-11-28 07:32:01 +00005503
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005504 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08005505 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5506 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5507 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005508 try {
Hwangoo Park8646b0d2023-01-13 02:42:34 +00005509 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
5510 if (controller != null) {
5511 ImsManager imsManager = controller.getImsManager(subId);
5512 if (imsManager != null) {
5513 imsManager.removeProvisioningCallbackForSubscription(callback, subId);
5514 } else {
5515 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
5516 + " is inactive, ignoring unregister.");
5517 // If the ImsManager is not valid, just return, since the callback will already
5518 // have been removed internally.
5519 }
5520 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005521 } finally {
5522 Binder.restoreCallingIdentity(identity);
5523 }
5524 }
5525
joonhunshincffb7fc2021-11-28 07:32:01 +00005526 @Override
5527 public void registerFeatureProvisioningChangedCallback(int subId,
5528 IFeatureProvisioningCallback callback) {
5529 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5530 mApp, subId, "registerFeatureProvisioningChangedCallback");
5531
5532 final long identity = Binder.clearCallingIdentity();
5533 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5534 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5535 }
5536
joonhunshin91bc1952022-04-29 08:47:15 +00005537 try {
5538 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5539 if (controller == null) {
5540 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5541 "Device does not support IMS");
5542 }
5543 controller.addFeatureProvisioningChangedCallback(subId, callback);
5544 } finally {
5545 Binder.restoreCallingIdentity(identity);
5546 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005547 }
5548
5549 @Override
5550 public void unregisterFeatureProvisioningChangedCallback(int subId,
5551 IFeatureProvisioningCallback callback) {
5552 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5553 mApp, subId, "unregisterFeatureProvisioningChangedCallback");
5554
5555 final long identity = Binder.clearCallingIdentity();
5556 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5557 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
5558 }
5559
joonhunshin91bc1952022-04-29 08:47:15 +00005560 try {
5561 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5562 if (controller == null) {
5563 loge("unregisterFeatureProvisioningChangedCallback: Device does not support IMS");
5564 return;
5565 }
5566 controller.removeFeatureProvisioningChangedCallback(subId, callback);
5567 } finally {
5568 Binder.restoreCallingIdentity(identity);
5569 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005570 }
allenwtsu99c623b2020-01-03 18:24:23 +08005571
5572 private void checkModifyPhoneStatePermission(int subId, String message) {
5573 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5574 message);
5575 }
5576
allenwtsu99c623b2020-01-03 18:24:23 +08005577 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005578 public void setRcsProvisioningStatusForCapability(int subId, int capability, int tech,
allenwtsu99c623b2020-01-03 18:24:23 +08005579 boolean isProvisioned) {
5580 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
5581
5582 final long identity = Binder.clearCallingIdentity();
5583 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005584 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5585 if (controller == null) {
5586 loge("setRcsProvisioningStatusForCapability: Device does not support IMS");
5587 return;
5588 }
5589 controller.setRcsProvisioningStatusForCapability(
5590 subId, capability, tech, isProvisioned);
allenwtsu99c623b2020-01-03 18:24:23 +08005591 } finally {
5592 Binder.restoreCallingIdentity(identity);
5593 }
allenwtsu99c623b2020-01-03 18:24:23 +08005594 }
5595
5596
5597 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005598 public boolean getRcsProvisioningStatusForCapability(int subId, int capability, int tech) {
5599 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5600 mApp, subId, "getRcsProvisioningStatusForCapability");
5601
allenwtsu99c623b2020-01-03 18:24:23 +08005602 final long identity = Binder.clearCallingIdentity();
5603 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005604 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5605 if (controller == null) {
5606 loge("getRcsProvisioningStatusForCapability: Device does not support IMS");
5607
5608 // device does not support IMS, this method will return true always.
5609 return true;
5610 }
5611 return controller.getRcsProvisioningStatusForCapability(subId, capability, tech);
allenwtsu99c623b2020-01-03 18:24:23 +08005612 } finally {
5613 Binder.restoreCallingIdentity(identity);
5614 }
5615 }
5616
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005617 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005618 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
5619 boolean isProvisioned) {
allenwtsu99c623b2020-01-03 18:24:23 +08005620 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
joonhunshincffb7fc2021-11-28 07:32:01 +00005621
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005622 final long identity = Binder.clearCallingIdentity();
5623 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005624 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5625 if (controller == null) {
5626 loge("setImsProvisioningStatusForCapability: Device does not support IMS");
5627 return;
5628 }
5629 controller.setImsProvisioningStatusForCapability(
5630 subId, capability, tech, isProvisioned);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005631 } finally {
5632 Binder.restoreCallingIdentity(identity);
5633 }
5634 }
5635
5636 @Override
5637 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
joonhunshincffb7fc2021-11-28 07:32:01 +00005638 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5639 mApp, subId, "getProvisioningStatusForCapability");
5640
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005641 final long identity = Binder.clearCallingIdentity();
5642 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005643 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5644 if (controller == null) {
5645 loge("getImsProvisioningStatusForCapability: Device does not support IMS");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005646
joonhunshin91bc1952022-04-29 08:47:15 +00005647 // device does not support IMS, this method will return true always.
5648 return true;
5649 }
5650 return controller.getImsProvisioningStatusForCapability(subId, capability, tech);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005651 } finally {
5652 Binder.restoreCallingIdentity(identity);
5653 }
5654 }
5655
5656 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005657 public boolean isProvisioningRequiredForCapability(int subId, int capability, int tech) {
5658 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5659 mApp, subId, "isProvisioningRequiredForCapability");
5660
5661 final long identity = Binder.clearCallingIdentity();
5662 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005663 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5664 if (controller == null) {
5665 loge("isProvisioningRequiredForCapability: Device does not support IMS");
5666
5667 // device does not support IMS, this method will return false
5668 return false;
5669 }
5670 return controller.isImsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005671 } finally {
5672 Binder.restoreCallingIdentity(identity);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005673 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005674 }
5675
5676 @Override
joonhunshincffb7fc2021-11-28 07:32:01 +00005677 public boolean isRcsProvisioningRequiredForCapability(int subId, int capability, int tech) {
5678 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5679 mApp, subId, "isProvisioningRequiredForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005680
joonhunshincffb7fc2021-11-28 07:32:01 +00005681 final long identity = Binder.clearCallingIdentity();
5682 try {
joonhunshin91bc1952022-04-29 08:47:15 +00005683 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5684 if (controller == null) {
5685 loge("isRcsProvisioningRequiredForCapability: Device does not support IMS");
5686
5687 // device does not support IMS, this method will return false
5688 return false;
5689 }
5690 return controller.isRcsProvisioningRequiredForCapability(subId, capability, tech);
joonhunshincffb7fc2021-11-28 07:32:01 +00005691 } finally {
5692 Binder.restoreCallingIdentity(identity);
5693 }
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005694 }
5695
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005696 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005697 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005698 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5699 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5700 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005701 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5702 mApp, subId, "getImsProvisioningInt");
5703
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005704 final long identity = Binder.clearCallingIdentity();
5705 try {
5706 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005707 int slotId = getSlotIndex(subId);
5708 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5709 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
5710 + subId + "' for key:" + key);
5711 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5712 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005713
joonhunshin91bc1952022-04-29 08:47:15 +00005714 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5715 if (controller == null) {
5716 loge("getImsProvisioningInt: Device does not support IMS");
5717
5718 // device does not support IMS, this method will return CONFIG_RESULT_UNKNOWN.
5719 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
5720 }
5721 int retVal = controller.getProvisioningValue(subId, key);
joonhunshincffb7fc2021-11-28 07:32:01 +00005722 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5723 return retVal;
5724 }
5725
calvinpanb5a34062021-02-08 19:59:36 +08005726 return ImsManager.getInstance(mApp, slotId).getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005727 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005728 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
5729 + subId + "' for key:" + key);
5730 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005731 } finally {
5732 Binder.restoreCallingIdentity(identity);
5733 }
5734 }
5735
5736 @Override
5737 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005738 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5739 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5740 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005741 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5742 mApp, subId, "getImsProvisioningString");
5743
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005744 final long identity = Binder.clearCallingIdentity();
5745 try {
5746 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005747 int slotId = getSlotIndex(subId);
5748 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5749 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
5750 + subId + "' for key:" + key);
5751 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
5752 }
calvinpanb5a34062021-02-08 19:59:36 +08005753 return ImsManager.getInstance(mApp, slotId).getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005754 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005755 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
5756 + subId + "' for key:" + key);
5757 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005758 } finally {
5759 Binder.restoreCallingIdentity(identity);
5760 }
5761 }
5762
5763 @Override
5764 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005765 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5766 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5767 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005768 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5769 "setImsProvisioningInt");
joonhunshincffb7fc2021-11-28 07:32:01 +00005770
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005771 final long identity = Binder.clearCallingIdentity();
5772 try {
5773 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005774 int slotId = getSlotIndex(subId);
5775 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5776 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
5777 + subId + "' for key:" + key);
5778 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5779 }
joonhunshincffb7fc2021-11-28 07:32:01 +00005780
joonhunshin91bc1952022-04-29 08:47:15 +00005781 ImsProvisioningController controller = ImsProvisioningController.getInstance();
5782 if (controller == null) {
5783 loge("setImsProvisioningInt: Device does not support IMS");
5784
5785 // device does not support IMS, this method will return CONFIG_RESULT_FAILED.
5786 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5787 }
5788 int retVal = controller.setProvisioningValue(subId, key, value);
joonhunshincffb7fc2021-11-28 07:32:01 +00005789 if (retVal != ImsConfigImplBase.CONFIG_RESULT_UNKNOWN) {
5790 return retVal;
5791 }
5792
calvinpanb5a34062021-02-08 19:59:36 +08005793 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5794 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005795 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005796 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005797 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005798 } finally {
5799 Binder.restoreCallingIdentity(identity);
5800 }
5801 }
5802
5803 @Override
5804 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005805 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
5806 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
5807 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08005808 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
5809 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005810 final long identity = Binder.clearCallingIdentity();
5811 try {
5812 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005813 int slotId = getSlotIndex(subId);
5814 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5815 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
5816 + subId + "' for key:" + key);
5817 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
5818 }
calvinpanb5a34062021-02-08 19:59:36 +08005819 return ImsManager.getInstance(mApp, slotId).setConfig(key, value);
5820 } catch (com.android.ims.ImsException | RemoteException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005821 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
calvinpanb5a34062021-02-08 19:59:36 +08005822 + "' for key:" + key, e);
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005823 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07005824 } finally {
5825 Binder.restoreCallingIdentity(identity);
5826 }
5827 }
5828
Brad Ebinger919631e2021-06-02 17:46:35 -07005829 /**
5830 * Throw an ImsException if the IMS resolver does not have an ImsService configured for MMTEL
5831 * for the given slot ID or no ImsResolver instance has been created.
5832 * @param slotId The slot ID that the IMS service is created for.
5833 * @throws ImsException If there is no ImsService configured for this slot.
5834 */
5835 private void verifyImsMmTelConfiguredOrThrow(int slotId) throws ImsException {
5836 if (mImsResolver == null || !mImsResolver.isImsServiceConfiguredForFeature(slotId,
5837 ImsFeature.FEATURE_MMTEL)) {
5838 throw new ImsException("This subscription does not support MMTEL over IMS",
5839 ImsException.CODE_ERROR_UNSUPPORTED_OPERATION);
5840 }
5841 }
5842
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005843 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07005844 int slotId = SubscriptionManager.getSlotIndex(subId);
5845 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07005846 throw new ImsException("Invalid Subscription Id, subId=" + subId,
5847 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07005848 }
5849 return slotId;
5850 }
5851
Brad Ebinger1c8542e2019-01-14 13:43:14 -08005852 private int getSlotIndex(int subId) {
5853 int slotId = SubscriptionManager.getSlotIndex(subId);
5854 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
5855 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
5856 }
5857 return slotId;
5858 }
5859
Wink Saville36469e72014-06-11 15:17:00 -07005860 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07005861 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07005862 */
5863 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005864 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
5865 String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07005866 try {
5867 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
5868 } catch (SecurityException se) {
5869 EventLog.writeEvent(0x534e4554, "186776740", Binder.getCallingUid());
5870 throw new SecurityException("Package " + callingPackage + " does not belong to "
5871 + Binder.getCallingUid());
5872 }
Nathan Haroldf096d982020-11-18 17:18:06 -08005873 final int targetSdk = TelephonyPermissions.getTargetSdk(mApp, callingPackage);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005874 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005875 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07005876 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07005877 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08005878 mApp, subId, callingPackage, callingFeatureId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005879 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005880 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5881 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005882
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005883 final long identity = Binder.clearCallingIdentity();
5884 try {
5885 final Phone phone = getPhone(subId);
5886 if (phone != null) {
5887 return phone.getServiceState().getDataNetworkType();
5888 } else {
5889 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5890 }
5891 } finally {
5892 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005893 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005894 }
5895
5896 /**
5897 * Returns the data network type
5898 */
5899 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005900 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
Jack Yu285100e2022-12-02 22:48:35 -08005901 return getDataNetworkTypeForSubscriber(SubscriptionManager.getDefaultDataSubscriptionId(),
Zoey Chenfd61f7f2021-04-21 13:42:10 +08005902 callingPackage, callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07005903 }
5904
5905 /**
5906 * Returns the data network type for a subId
5907 */
5908 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005909 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
5910 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005911 String functionName = "getDataNetworkTypeForSubscriber";
5912 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5913 mApp, functionName)) {
5914 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5915 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5916 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5917 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005918 }
5919
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005920 final long identity = Binder.clearCallingIdentity();
5921 try {
5922 final Phone phone = getPhone(subId);
5923 if (phone != null) {
5924 return phone.getServiceState().getDataNetworkType();
5925 } else {
5926 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5927 }
5928 } finally {
5929 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005930 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005931 }
5932
5933 /**
Wink Saville36469e72014-06-11 15:17:00 -07005934 * Returns the Voice network type for a subId
5935 */
5936 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005937 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
5938 String callingFeatureId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07005939 String functionName = "getVoiceNetworkTypeForSubscriber";
5940 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
5941 mApp, functionName)) {
5942 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5943 mApp, subId, callingPackage, callingFeatureId, functionName)) {
5944 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5945 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07005946 }
5947
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005948 final long identity = Binder.clearCallingIdentity();
5949 try {
5950 final Phone phone = getPhone(subId);
5951 if (phone != null) {
5952 return phone.getServiceState().getVoiceNetworkType();
5953 } else {
5954 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
5955 }
5956 } finally {
5957 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07005958 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005959 }
5960
5961 /**
5962 * @return true if a ICC card is present
5963 */
5964 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07005965 // FIXME Make changes to pass defaultSimId of type int
Jack Yu285100e2022-12-02 22:48:35 -08005966 return hasIccCardUsingSlotIndex(SubscriptionManager.getSlotIndex(
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005967 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07005968 }
5969
5970 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005971 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07005972 */
Sanket Padawe356d7632015-06-22 14:03:32 -07005973 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07005974 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005975 final long identity = Binder.clearCallingIdentity();
5976 try {
5977 final Phone phone = PhoneFactory.getPhone(slotIndex);
5978 if (phone != null) {
5979 return phone.getIccCard().hasIccCard();
5980 } else {
5981 return false;
5982 }
5983 } finally {
5984 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08005985 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005986 }
5987
5988 /**
5989 * Return if the current radio is LTE on CDMA. This
5990 * is a tri-state return value as for a period of time
5991 * the mode may be unknown.
5992 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005993 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005994 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08005995 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005996 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07005997 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005998 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
5999 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
6000 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07006001 }
6002
Sanket Padawe356d7632015-06-22 14:03:32 -07006003 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006004 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
6005 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08006006 try {
6007 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
6008 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07006009 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
6010 }
6011
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006012 final long identity = Binder.clearCallingIdentity();
6013 try {
6014 final Phone phone = getPhone(subId);
6015 if (phone == null) {
6016 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
6017 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07006018 return TelephonyProperties.lte_on_cdma_device()
6019 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006020 }
6021 } finally {
6022 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07006023 }
Wink Saville36469e72014-06-11 15:17:00 -07006024 }
6025
Wink Saville36469e72014-06-11 15:17:00 -07006026 /**
6027 * {@hide}
6028 * Returns Default subId, 0 in the case of single standby.
6029 */
Wink Savilleb564aae2014-10-23 10:18:09 -07006030 private int getDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08006031 return SubscriptionManager.getDefaultSubscriptionId();
Wink Saville36469e72014-06-11 15:17:00 -07006032 }
6033
Shishir Agrawala9f32182016-04-12 12:00:16 -07006034 private int getSlotForDefaultSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08006035 return SubscriptionManager.getPhoneId(getDefaultSubscription());
Shishir Agrawala9f32182016-04-12 12:00:16 -07006036 }
6037
Wink Savilleb564aae2014-10-23 10:18:09 -07006038 private int getPreferredVoiceSubscription() {
Jack Yu285100e2022-12-02 22:48:35 -08006039 return SubscriptionManager.getDefaultVoiceSubscriptionId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07006040 }
Ihab Awadf2177b72013-11-25 13:33:23 -08006041
Pengquan Menge92a50d2018-09-21 15:54:48 -07006042 private boolean isActiveSubscription(int subId) {
Jack Yu285100e2022-12-02 22:48:35 -08006043 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
6044 return SubscriptionManagerService.getInstance().isActiveSubId(subId,
6045 mApp.getOpPackageName(), mApp.getFeatureId());
6046 }
Pengquan Menge92a50d2018-09-21 15:54:48 -07006047 return mSubscriptionController.isActiveSubId(subId);
6048 }
6049
Ihab Awadf2177b72013-11-25 13:33:23 -08006050 /**
6051 * @see android.telephony.TelephonyManager.WifiCallingChoices
6052 */
6053 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006054 final long identity = Binder.clearCallingIdentity();
6055 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006056 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006057 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
6058 getWhenToMakeWifiCallsDefaultPreference());
6059 } finally {
6060 Binder.restoreCallingIdentity(identity);
6061 }
Ihab Awadf2177b72013-11-25 13:33:23 -08006062 }
6063
6064 /**
6065 * @see android.telephony.TelephonyManager.WifiCallingChoices
6066 */
6067 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006068 final long identity = Binder.clearCallingIdentity();
6069 try {
6070 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006071 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006072 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
6073 } finally {
6074 Binder.restoreCallingIdentity(identity);
6075 }
Ihab Awadf9e92732013-12-05 18:02:52 -08006076 }
6077
Sailesh Nepald1e68152013-12-12 19:08:02 -08006078 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07006079 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08006080 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08006081 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08006082
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006083 private Phone getPhoneFromSlotPortIndexOrThrowException(int slotIndex, int portIndex) {
6084 int phoneId = UiccController.getInstance().getPhoneIdFromSlotPortIndex(slotIndex,
6085 portIndex);
Jordan Liu4c733742019-02-28 12:03:40 -08006086 if (phoneId == -1) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006087 throw new IllegalArgumentException("Given slot index: " + slotIndex + " port index: "
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006088 + portIndex + " does not correspond to an active phone");
Jordan Liu4c733742019-02-28 12:03:40 -08006089 }
6090 return PhoneFactory.getPhone(phoneId);
6091 }
6092
Shishir Agrawal566b7612013-10-28 14:41:00 -07006093 @Override
Derek Tan740e1672017-06-27 14:56:27 -07006094 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
Rambo Wanga1782702021-11-10 20:15:19 -08006095 @NonNull IccLogicalChannelRequest request) {
6096 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
6097 /*message=*/ "iccOpenLogicalChannel");
6098
6099 if (DBG) log("iccOpenLogicalChannel: request=" + request);
6100 // Verify that the callingPackage in the request belongs to the calling UID
6101 mAppOps.checkPackage(Binder.getCallingUid(), request.callingPackage);
6102
6103 return iccOpenLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08006104 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006105
Rambo Wanga1782702021-11-10 20:15:19 -08006106 private Phone getPhoneFromValidIccLogicalChannelRequest(
6107 @NonNull IccLogicalChannelRequest request, String message) {
6108 Phone phone;
6109 if (request.subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
6110 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6111 mApp, request.subId, message);
6112 phone = getPhoneFromSubId(request.subId);
6113 } else if (request.slotIndex != SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6114 enforceModifyPermission();
6115 phone = getPhoneFromSlotPortIndexOrThrowException(request.slotIndex, request.portIndex);
6116 } else {
6117 throw new IllegalArgumentException("Both subId and slotIndex in request are invalid.");
Jordan Liu4c733742019-02-28 12:03:40 -08006118 }
Rambo Wanga1782702021-11-10 20:15:19 -08006119 return phone;
Jordan Liu4c733742019-02-28 12:03:40 -08006120 }
6121
6122 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
Rambo Wanga1782702021-11-10 20:15:19 -08006123 IccLogicalChannelRequest channelRequest) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006124 final long identity = Binder.clearCallingIdentity();
6125 try {
Rambo Wanga1782702021-11-10 20:15:19 -08006126 if (TextUtils.equals(ISDR_AID, channelRequest.aid)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006127 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006128 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
6129 .getContext().getPackageManager());
Rambo Wanga1782702021-11-10 20:15:19 -08006130 if (bestComponent == null || !TextUtils.equals(channelRequest.callingPackage,
6131 bestComponent.packageName)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006132 loge("The calling package is not allowed to access ISD-R.");
6133 throw new SecurityException(
6134 "The calling package is not allowed to access ISD-R.");
6135 }
Derek Tan740e1672017-06-27 14:56:27 -07006136 }
Derek Tan740e1672017-06-27 14:56:27 -07006137
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006138 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Rambo Wanga1782702021-11-10 20:15:19 -08006139 CMD_OPEN_CHANNEL, channelRequest, phone, null /* workSource */);
6140 if (DBG) log("iccOpenLogicalChannelWithPermission: response=" + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006141 return response;
6142 } finally {
6143 Binder.restoreCallingIdentity(identity);
6144 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006145 }
6146
6147 @Override
Rambo Wanga1782702021-11-10 20:15:19 -08006148 public boolean iccCloseLogicalChannel(@NonNull IccLogicalChannelRequest request) {
6149 Phone phone = getPhoneFromValidIccLogicalChannelRequest(request,
6150 /*message=*/"iccCloseLogicalChannel");
6151
6152 if (DBG) log("iccCloseLogicalChannel: request=" + request);
6153
6154 return iccCloseLogicalChannelWithPermission(phone, request);
Jordan Liu4c733742019-02-28 12:03:40 -08006155 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006156
Rambo Wanga1782702021-11-10 20:15:19 -08006157 private boolean iccCloseLogicalChannelWithPermission(Phone phone,
6158 IccLogicalChannelRequest request) {
Chen Xua8f0dff2022-02-12 00:34:15 -08006159 // before this feature is enabled, this API should only return false if
6160 // the operation fails instead of throwing runtime exception for
6161 // backward-compatibility.
6162 final boolean shouldThrowExceptionOnFailure = CompatChanges.isChangeEnabled(
6163 ICC_CLOSE_CHANNEL_EXCEPTION_ON_FAILURE, Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006164 final long identity = Binder.clearCallingIdentity();
6165 try {
Rambo Wanga1782702021-11-10 20:15:19 -08006166 if (request.channel < 0) {
Chen Xu540470b2021-12-14 17:15:47 -08006167 throw new IllegalArgumentException("request.channel is less than 0");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006168 }
Chen Xue9d737e2022-01-01 23:41:31 -08006169 Object result = sendRequest(CMD_CLOSE_CHANNEL, request.channel, phone,
Jordan Liu4c733742019-02-28 12:03:40 -08006170 null /* workSource */);
Chen Xue9d737e2022-01-01 23:41:31 -08006171 Boolean success = false;
6172 if (result instanceof RuntimeException) {
6173 // if there is an exception returned, throw from the binder thread here.
Chen Xua8f0dff2022-02-12 00:34:15 -08006174 if (shouldThrowExceptionOnFailure) {
6175 throw (RuntimeException) result;
6176 } else {
6177 return false;
6178 }
Chen Xue9d737e2022-01-01 23:41:31 -08006179 } else if (result instanceof Boolean) {
6180 success = (Boolean) result;
6181 } else {
6182 loge("iccCloseLogicalChannelWithPermission: supported return type " + result);
6183 }
Rambo Wanga1782702021-11-10 20:15:19 -08006184 if (DBG) log("iccCloseLogicalChannelWithPermission: success=" + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006185 return success;
6186 } finally {
6187 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07006188 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006189 }
6190
6191 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006192 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07006193 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006194 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6195 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08006196 if (DBG) {
6197 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
6198 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
6199 + p3 + " data=" + data);
6200 }
6201 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
6202 command, p1, p2, p3, data);
6203 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006204
Jordan Liu4c733742019-02-28 12:03:40 -08006205 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006206 public String iccTransmitApduLogicalChannelByPort(int slotIndex, int portIndex, int channel,
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006207 int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08006208 enforceModifyPermission();
6209 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006210 log("iccTransmitApduLogicalChannelByPort: slotIndex=" + slotIndex + " portIndex="
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006211 + portIndex + " chnl=" + channel + " cla=" + cla + " cmd=" + command + " p1="
6212 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08006213 }
6214 return iccTransmitApduLogicalChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006215 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), channel, cla,
6216 command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08006217 }
6218
6219 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
6220 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006221 final long identity = Binder.clearCallingIdentity();
6222 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07006223 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006224 return "";
6225 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006226
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006227 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08006228 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
6229 null /* workSource */);
6230 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07006231
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006232 // Append the returned status code to the end of the response payload.
6233 String s = Integer.toHexString(
6234 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
6235 if (response.payload != null) {
6236 s = IccUtils.bytesToHexString(response.payload) + s;
6237 }
6238 return s;
6239 } finally {
6240 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07006241 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07006242 }
Jake Hambye994d462014-02-03 13:10:13 -08006243
Evan Charltonc66da362014-05-16 14:06:40 -07006244 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08006245 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
6246 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006247 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6248 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006249 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08006250 if (DBG) {
6251 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
6252 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
6253 }
6254 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
6255 cla, command, p1, p2, p3, data);
6256 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006257
Jordan Liu4c733742019-02-28 12:03:40 -08006258 @Override
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006259 public String iccTransmitApduBasicChannelByPort(int slotIndex, int portIndex,
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006260 String callingPackage, int cla, int command, int p1, int p2, int p3, String data) {
Jordan Liu4c733742019-02-28 12:03:40 -08006261 enforceModifyPermission();
6262 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
6263 if (DBG) {
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006264 log("iccTransmitApduBasicChannelByPort: slotIndex=" + slotIndex + " portIndex="
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006265 + portIndex + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2="
6266 + p2 + " p3=" + p3 + " data=" + data);
Jordan Liu4c733742019-02-28 12:03:40 -08006267 }
6268
6269 return iccTransmitApduBasicChannelWithPermission(
Muralidhar Reddybd38d952021-12-02 21:04:16 +00006270 getPhoneFromSlotPortIndexOrThrowException(slotIndex, portIndex), callingPackage,
6271 cla, command, p1, p2, p3, data);
Jordan Liu4c733742019-02-28 12:03:40 -08006272 }
6273
6274 // open APDU basic channel assuming the caller has sufficient permissions
6275 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
6276 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006277 final long identity = Binder.clearCallingIdentity();
6278 try {
6279 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
6280 && TextUtils.equals(ISDR_AID, data)) {
6281 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006282 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
6283 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006284 if (bestComponent == null
6285 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
6286 loge("The calling package is not allowed to select ISD-R.");
6287 throw new SecurityException(
6288 "The calling package is not allowed to select ISD-R.");
6289 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08006290 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08006291
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006292 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08006293 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
6294 null /* workSource */);
6295 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006296
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006297 // Append the returned status code to the end of the response payload.
6298 String s = Integer.toHexString(
6299 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
6300 if (response.payload != null) {
6301 s = IccUtils.bytesToHexString(response.payload) + s;
6302 }
6303 return s;
6304 } finally {
6305 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07006306 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006307 }
6308
6309 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006310 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006311 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006312 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6313 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006314
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006315 final long identity = Binder.clearCallingIdentity();
6316 try {
6317 if (DBG) {
6318 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
6319 + p1 + " " + p2 + " " + p3 + ":" + filePath);
6320 }
6321
6322 IccIoResult response =
6323 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
6324 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
6325 subId);
6326
6327 if (DBG) {
6328 log("Exchange SIM_IO [R]" + response);
6329 }
6330
6331 byte[] result = null;
6332 int length = 2;
6333 if (response.payload != null) {
6334 length = 2 + response.payload.length;
6335 result = new byte[length];
6336 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
6337 } else {
6338 result = new byte[length];
6339 }
6340
6341 result[length - 1] = (byte) response.sw2;
6342 result[length - 2] = (byte) response.sw1;
6343 return result;
6344 } finally {
6345 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006346 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006347 }
6348
Nathan Haroldb3014052017-01-25 15:57:32 -08006349 /**
6350 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
6351 * on a particular subscription
6352 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006353 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
6354 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07006355 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006356 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07006357 return null;
6358 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006359
6360 final long identity = Binder.clearCallingIdentity();
6361 try {
6362 if (appType != TelephonyManager.APPTYPE_USIM
6363 && appType != TelephonyManager.APPTYPE_SIM) {
6364 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
6365 return null;
6366 }
6367 Object response = sendRequest(
6368 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
6369 if (response instanceof String[]) {
6370 return (String[]) response;
6371 }
yincheng zhao2737e882019-09-06 17:06:54 -07006372 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006373 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08006374 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006375 } finally {
6376 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08006377 }
Nathan Haroldb3014052017-01-25 15:57:32 -08006378 }
6379
yincheng zhao2737e882019-09-06 17:06:54 -07006380 /**
6381 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
6382 * subscription.
6383 *
6384 * @param subId the id of the subscription.
6385 * @param appType the uicc app type, must be USIM or SIM.
6386 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
6387 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006388 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07006389 * @return number of fplmns that is successfully written to the SIM.
6390 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006391 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
6392 String callingFeatureId) {
Jayachandran C5b0d75a2021-10-21 22:15:27 -07006393 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6394 mApp, subId, "setForbiddenPlmns");
6395
yincheng zhao2737e882019-09-06 17:06:54 -07006396 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
6397 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
6398 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
6399 }
6400 if (fplmns == null) {
6401 throw new IllegalArgumentException("Fplmn List provided is null");
6402 }
6403 for (String fplmn : fplmns) {
6404 if (!CellIdentity.isValidPlmn(fplmn)) {
6405 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
6406 }
6407 }
6408 final long identity = Binder.clearCallingIdentity();
6409 try {
6410 Object response = sendRequest(
6411 CMD_SET_FORBIDDEN_PLMNS,
6412 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
6413 subId);
6414 return (int) response;
6415 } finally {
6416 Binder.restoreCallingIdentity(identity);
6417 }
6418 }
6419
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07006420 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006421 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006422 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6423 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07006424
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006425 final long identity = Binder.clearCallingIdentity();
6426 try {
6427 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
6428 if (response.payload == null) {
6429 return "";
6430 }
Evan Charltonc66da362014-05-16 14:06:40 -07006431
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006432 // Append the returned status code to the end of the response payload.
6433 String s = Integer.toHexString(
6434 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
6435 s = IccUtils.bytesToHexString(response.payload) + s;
6436 return s;
6437 } finally {
6438 Binder.restoreCallingIdentity(identity);
6439 }
Evan Charltonc66da362014-05-16 14:06:40 -07006440 }
6441
Jake Hambye994d462014-02-03 13:10:13 -08006442 /**
6443 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
6444 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
6445 *
6446 * @param itemID the ID of the item to read
6447 * @return the NV item as a String, or null on error.
6448 */
6449 @Override
6450 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006451 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08006452 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6453 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006454
6455 final long identity = Binder.clearCallingIdentity();
6456 try {
6457 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07006458 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006459 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
6460 return value;
6461 } finally {
6462 Binder.restoreCallingIdentity(identity);
6463 }
Jake Hambye994d462014-02-03 13:10:13 -08006464 }
6465
6466 /**
6467 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
6468 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
6469 *
6470 * @param itemID the ID of the item to read
6471 * @param itemValue the value to write, as a String
6472 * @return true on success; false on any failure
6473 */
6474 @Override
6475 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07006476 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08006477 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6478 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006479
6480 final long identity = Binder.clearCallingIdentity();
6481 try {
6482 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
6483 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07006484 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006485 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
6486 return success;
6487 } finally {
6488 Binder.restoreCallingIdentity(identity);
6489 }
Jake Hambye994d462014-02-03 13:10:13 -08006490 }
6491
6492 /**
6493 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
6494 * Used for device configuration by some CDMA operators.
6495 *
6496 * @param preferredRoamingList byte array containing the new PRL
6497 * @return true on success; false on any failure
6498 */
6499 @Override
6500 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006501 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6502 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006503
6504 final long identity = Binder.clearCallingIdentity();
6505 try {
6506 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
6507 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
6508 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
6509 return success;
6510 } finally {
6511 Binder.restoreCallingIdentity(identity);
6512 }
Jake Hambye994d462014-02-03 13:10:13 -08006513 }
6514
6515 /**
chen xu6dac5ab2018-10-26 17:39:23 -07006516 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08006517 * Used for device configuration by some CDMA operators.
6518 *
chen xu6dac5ab2018-10-26 17:39:23 -07006519 * @param slotIndex - device slot.
6520 *
Jake Hambye994d462014-02-03 13:10:13 -08006521 * @return true on success; false on any failure
6522 */
6523 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07006524 public boolean resetModemConfig(int slotIndex) {
6525 Phone phone = PhoneFactory.getPhone(slotIndex);
6526 if (phone != null) {
6527 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6528 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006529
chen xu6dac5ab2018-10-26 17:39:23 -07006530 final long identity = Binder.clearCallingIdentity();
6531 try {
6532 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
6533 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
6534 return success;
6535 } finally {
6536 Binder.restoreCallingIdentity(identity);
6537 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006538 }
chen xu6dac5ab2018-10-26 17:39:23 -07006539 return false;
6540 }
6541
6542 /**
6543 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
6544 *
6545 * @param slotIndex - device slot.
6546 *
6547 * @return true on success; false on any failure
6548 */
6549 @Override
6550 public boolean rebootModem(int slotIndex) {
6551 Phone phone = PhoneFactory.getPhone(slotIndex);
6552 if (phone != null) {
6553 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6554 mApp, phone.getSubId(), "rebootModem");
6555
6556 final long identity = Binder.clearCallingIdentity();
6557 try {
6558 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
6559 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
6560 return success;
6561 } finally {
6562 Binder.restoreCallingIdentity(identity);
6563 }
6564 }
6565 return false;
Jake Hambye994d462014-02-03 13:10:13 -08006566 }
Jake Hamby7c27be32014-03-03 13:25:59 -08006567
Brad Ebinger51f743a2017-01-23 13:50:20 -08006568 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08006569 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
6570 * {@link #disableIms(int)}.
6571 * @param slotIndex device slot.
6572 */
6573 public void resetIms(int slotIndex) {
6574 enforceModifyPermission();
6575
6576 final long identity = Binder.clearCallingIdentity();
6577 try {
6578 if (mImsResolver == null) {
6579 // may happen if the does not support IMS.
6580 return;
6581 }
Hyunhoa17ac7c2022-08-30 12:03:04 +00006582 mImsResolver.resetIms(slotIndex);
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08006583 } finally {
6584 Binder.restoreCallingIdentity(identity);
6585 }
6586 }
6587
6588 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006589 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
6590 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08006591 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006592 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08006593 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006594
6595 final long identity = Binder.clearCallingIdentity();
6596 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006597 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006598 // may happen if the device does not support IMS.
6599 return;
6600 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006601 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006602 } finally {
6603 Binder.restoreCallingIdentity(identity);
6604 }
Brad Ebinger34bef922017-11-09 10:27:08 -08006605 }
6606
6607 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006608 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
6609 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08006610 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006611 public void disableIms(int slotId) {
6612 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006613
6614 final long identity = Binder.clearCallingIdentity();
6615 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006616 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006617 // may happen if the device does not support IMS.
6618 return;
6619 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006620 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006621 } finally {
6622 Binder.restoreCallingIdentity(identity);
6623 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006624 }
6625
6626 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07006627 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
6628 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006629 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07006630 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07006631 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08006632 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006633
6634 final long identity = Binder.clearCallingIdentity();
6635 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006636 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07006637 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6638 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006639 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07006640 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006641 } finally {
6642 Binder.restoreCallingIdentity(identity);
6643 }
Brad Ebinger34bef922017-11-09 10:27:08 -08006644 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006645 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006646 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
6647 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07006648 @Override
6649 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006650 enforceModifyPermission();
6651
6652 final long identity = Binder.clearCallingIdentity();
6653 try {
6654 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07006655 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07006656 } finally {
6657 Binder.restoreCallingIdentity(identity);
6658 }
6659 }
6660
6661 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08006662 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006663 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08006664 */
6665 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
6666 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006667
6668 final long identity = Binder.clearCallingIdentity();
6669 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006670 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006671 // may happen if the device does not support IMS.
6672 return null;
6673 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006674 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006675 } finally {
6676 Binder.restoreCallingIdentity(identity);
6677 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08006678 }
6679
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006680 /**
6681 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006682 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006683 */
6684 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
6685 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006686
6687 final long identity = Binder.clearCallingIdentity();
6688 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006689 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006690 // may happen if the device does not support IMS.
6691 return null;
6692 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006693 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006694 } finally {
6695 Binder.restoreCallingIdentity(identity);
6696 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08006697 }
6698
Brad Ebinger884c07b2018-02-15 16:17:40 -08006699 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07006700 * Sets the ImsService Package Name that Telephony will bind to.
6701 *
Brad Ebinger24c29992019-12-05 13:03:21 -08006702 * @param slotIndex the slot ID that the ImsService should bind for.
6703 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07006704 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08006705 * @param featureTypes An integer array of feature types associated with a packageName.
6706 * @param packageName The name of the package that the current configuration will be replaced
6707 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006708 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006709 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006710 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
6711 int[] featureTypes, String packageName) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006712 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006713 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006714 SubscriptionManager.getSubscriptionId(slotIndex), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07006715
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006716 final long identity = Binder.clearCallingIdentity();
6717 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006718 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006719 // may happen if the device does not support IMS.
6720 return false;
6721 }
Brad Ebinger24c29992019-12-05 13:03:21 -08006722 Map<Integer, String> featureConfig = new HashMap<>();
6723 for (int featureType : featureTypes) {
6724 featureConfig.put(featureType, packageName);
6725 }
6726 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
6727 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006728 } finally {
6729 Binder.restoreCallingIdentity(identity);
6730 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006731 }
6732
6733 /**
Brad Ebinger999d3302020-11-25 14:31:39 -08006734 * Clears any carrier ImsService overrides for the slot index specified that were previously
6735 * set with {@link #setBoundImsServiceOverride(int, boolean, int[], String)}.
6736 *
6737 * This should only be used for testing.
6738 *
6739 * @param slotIndex the slot ID that the ImsService should bind for.
6740 * @return true if clearing the carrier ImsService override succeeded or false if it did not.
6741 */
6742 @Override
6743 public boolean clearCarrierImsServiceOverride(int slotIndex) {
Brad Ebinger999d3302020-11-25 14:31:39 -08006744 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
6745 "clearCarrierImsServiceOverride");
6746 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Jack Yu00ece8c2022-11-19 22:29:12 -08006747 SubscriptionManager.getSubscriptionId(slotIndex), "clearCarrierImsServiceOverride");
Brad Ebinger999d3302020-11-25 14:31:39 -08006748
6749 final long identity = Binder.clearCallingIdentity();
6750 try {
6751 if (mImsResolver == null) {
6752 // may happen if the device does not support IMS.
6753 return false;
6754 }
6755 return mImsResolver.clearCarrierImsServiceConfiguration(slotIndex);
6756 } finally {
6757 Binder.restoreCallingIdentity(identity);
6758 }
6759 }
6760
6761 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08006762 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006763 *
6764 * @param slotId The slot that the ImsService is associated with.
6765 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
6766 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08006767 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07006768 * @return the package name of the ImsService configuration.
6769 */
Brad Ebinger24c29992019-12-05 13:03:21 -08006770 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
6771 @ImsFeature.FeatureType int featureType) {
Brad Ebinger24c29992019-12-05 13:03:21 -08006772 TelephonyPermissions
Jack Yu00ece8c2022-11-19 22:29:12 -08006773 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(mApp,
6774 SubscriptionManager.getSubscriptionId(slotId), "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07006775
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006776 final long identity = Binder.clearCallingIdentity();
6777 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08006778 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08006779 // may happen if the device does not support IMS.
6780 return "";
6781 }
Brad Ebingera80c3312019-12-02 10:59:39 -08006782 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08006783 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
6784 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006785 } finally {
6786 Binder.restoreCallingIdentity(identity);
6787 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07006788 }
6789
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006790 /**
6791 * Get the MmTelFeature state associated with the requested subscription id.
6792 * @param subId The subscription that the MmTelFeature is associated with.
6793 * @param callback A callback with an integer containing the
6794 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
6795 */
6796 @Override
6797 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
6798 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
Brad Ebingera2628302022-02-18 03:44:55 +00006799 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
6800 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
6801 "IMS not available on device.");
6802 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006803 final long token = Binder.clearCallingIdentity();
6804 try {
Brad Ebingera2628302022-02-18 03:44:55 +00006805 int slotId = getSlotIndex(subId);
6806 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6807 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
6808 + subId + "'");
6809 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
6810 }
6811 verifyImsMmTelConfiguredOrThrow(slotId);
6812 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
6813 try {
6814 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
6815 } catch (RemoteException e) {
6816 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
6817 + "Ignore");
6818 }
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006819 });
Brad Ebinger919631e2021-06-02 17:46:35 -07006820 } catch (ImsException e) {
6821 throw new ServiceSpecificException(e.getCode());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006822 } finally {
6823 Binder.restoreCallingIdentity(token);
6824 }
6825 }
6826
Daniel Brightbb5840b2021-01-12 15:48:18 -08006827 /**
6828 * Sets the ims registration state on all valid {@link Phone}s.
6829 */
6830 public void setImsRegistrationState(final boolean registered) {
Wink Saville36469e72014-06-11 15:17:00 -07006831 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006832
6833 final long identity = Binder.clearCallingIdentity();
6834 try {
Daniel Brightbb5840b2021-01-12 15:48:18 -08006835 // NOTE: Before S, this method only set the default phone.
6836 for (final Phone phone : PhoneFactory.getPhones()) {
6837 if (SubscriptionManager.isValidSubscriptionId(phone.getSubId())) {
6838 phone.setImsRegistrationState(registered);
6839 }
6840 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006841 } finally {
6842 Binder.restoreCallingIdentity(identity);
6843 }
Wink Saville36469e72014-06-11 15:17:00 -07006844 }
6845
6846 /**
Stuart Scott54788802015-03-30 13:18:01 -07006847 * Set the network selection mode to automatic.
6848 *
6849 */
6850 @Override
6851 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006852 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6853 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006854
6855 final long identity = Binder.clearCallingIdentity();
6856 try {
shilufc958392020-01-20 11:36:01 -08006857 if (!isActiveSubscription(subId)) {
6858 return;
6859 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006860 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
Rambo Wang0f050d82021-02-12 11:43:36 -08006861 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId,
6862 SET_NETWORK_SELECTION_MODE_AUTOMATIC_TIMEOUT_MS);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006863 } finally {
6864 Binder.restoreCallingIdentity(identity);
6865 }
Stuart Scott54788802015-03-30 13:18:01 -07006866 }
6867
Jack Yud10cdd42020-09-28 20:28:01 -07006868 /**
Pengquan Mengea84e042018-09-20 14:57:26 -07006869 * Ask the radio to connect to the input network and change selection mode to manual.
6870 *
6871 * @param subId the id of the subscription.
6872 * @param operatorInfo the operator information, included the PLMN, long name and short name of
6873 * the operator to attach to.
6874 * @param persistSelection whether the selection will persist until reboot. If true, only allows
6875 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
6876 * normal network selection next time.
6877 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07006878 */
6879 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07006880 public boolean setNetworkSelectionModeManual(
6881 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006882 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6883 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07006884
Jack Yu285100e2022-12-02 22:48:35 -08006885 final long identity = Binder.clearCallingIdentity();
Pengquan Menge92a50d2018-09-21 15:54:48 -07006886 if (!isActiveSubscription(subId)) {
6887 return false;
6888 }
6889
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006890 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07006891 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006892 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07006893 if (DBG) {
6894 log("setNetworkSelectionModeManual: subId: " + subId
6895 + " operator: " + operatorInfo);
6896 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006897 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
6898 } finally {
6899 Binder.restoreCallingIdentity(identity);
6900 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006901 }
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006902 /**
shilu84f6e8b2019-12-19 13:58:01 -08006903 * Get the manual network selection
6904 *
6905 * @param subId the id of the subscription.
6906 *
6907 * @return the previously saved user selected PLMN
6908 */
6909 @Override
6910 public String getManualNetworkSelectionPlmn(int subId) {
6911 TelephonyPermissions
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006912 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
6913 mApp, subId, "getManualNetworkSelectionPlmn");
shilu84f6e8b2019-12-19 13:58:01 -08006914
6915 final long identity = Binder.clearCallingIdentity();
6916 try {
6917 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07006918 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006919 }
6920
6921 final Phone phone = getPhone(subId);
6922 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07006923 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08006924 }
6925 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
6926 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
Thomas Nguyen8ee49682023-02-01 11:46:09 -08006927 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
shilu84f6e8b2019-12-19 13:58:01 -08006928 } finally {
6929 Binder.restoreCallingIdentity(identity);
6930 }
6931 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006932
6933 /**
6934 * Scans for available networks.
6935 */
6936 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006937 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
6938 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006939 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
6940 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08006941 LocationAccessPolicy.LocationPermissionResult locationResult =
6942 LocationAccessPolicy.checkLocationPermission(mApp,
6943 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6944 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006945 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006946 .setCallingPid(Binder.getCallingPid())
6947 .setCallingUid(Binder.getCallingUid())
6948 .setMethod("getCellNetworkScanResults")
6949 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006950 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6951 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006952 .build());
6953 switch (locationResult) {
6954 case DENIED_HARD:
6955 throw new SecurityException("Not allowed to access scan results -- location");
6956 case DENIED_SOFT:
6957 return null;
6958 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006959
Pengquan Menga1bb6272018-09-06 09:59:22 -07006960 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006961 try {
6962 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07006963 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006964 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006965 } finally {
6966 Binder.restoreCallingIdentity(identity);
6967 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07006968 }
6969
6970 /**
Shuo Qian4a594052020-01-23 11:59:30 -08006971 * Get the call forwarding info, given the call forwarding reason.
6972 */
6973 @Override
Hall Liu27d24262020-09-18 19:04:59 -07006974 public void getCallForwarding(int subId, int callForwardingReason,
6975 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08006976 enforceReadPrivilegedPermission("getCallForwarding");
6977 long identity = Binder.clearCallingIdentity();
6978 try {
6979 if (DBG) {
6980 log("getCallForwarding: subId " + subId
6981 + " callForwardingReason" + callForwardingReason);
6982 }
Hall Liu27d24262020-09-18 19:04:59 -07006983
6984 Phone phone = getPhone(subId);
6985 if (phone == null) {
6986 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07006987 callback.onError(
6988 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07006989 } catch (RemoteException e) {
6990 // ignore
6991 }
6992 return;
6993 }
6994
6995 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
6996 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
6997 @Override
6998 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
6999 try {
7000 callback.onCallForwardingInfoAvailable(info);
7001 } catch (RemoteException e) {
7002 // ignore
7003 }
7004 }
7005
7006 @Override
7007 public void onError(int error) {
7008 try {
7009 callback.onError(error);
7010 } catch (RemoteException e) {
7011 // ignore
7012 }
7013 }
7014 });
7015 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08007016 } finally {
7017 Binder.restoreCallingIdentity(identity);
7018 }
7019 }
7020
7021 /**
7022 * Sets the voice call forwarding info including status (enable/disable), call forwarding
7023 * reason, the number to forward, and the timeout before the forwarding is attempted.
7024 */
7025 @Override
Hall Liu27d24262020-09-18 19:04:59 -07007026 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
7027 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08007028 enforceModifyPermission();
7029 long identity = Binder.clearCallingIdentity();
7030 try {
7031 if (DBG) {
7032 log("setCallForwarding: subId " + subId
7033 + " callForwardingInfo" + callForwardingInfo);
7034 }
Hall Liu27d24262020-09-18 19:04:59 -07007035
7036 Phone phone = getPhone(subId);
7037 if (phone == null) {
7038 try {
Hall Liu940c4ca2020-09-29 17:10:18 -07007039 callback.accept(
7040 TelephonyManager.CallForwardingInfoCallback.RESULT_ERROR_UNKNOWN);
Hall Liu27d24262020-09-18 19:04:59 -07007041 } catch (RemoteException e) {
7042 // ignore
7043 }
7044 return;
7045 }
7046
7047 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
7048 FunctionalUtils.ignoreRemoteException(callback::accept));
7049
7050 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08007051 } finally {
7052 Binder.restoreCallingIdentity(identity);
7053 }
7054 }
7055
7056 /**
Hall Liu27d24262020-09-18 19:04:59 -07007057 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08007058 */
7059 @Override
Hall Liu27d24262020-09-18 19:04:59 -07007060 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
SongFerngWang0e767992021-03-31 22:08:45 +08007061 enforceReadPrivilegedPermission("getCallWaitingStatus");
Shuo Qian4a594052020-01-23 11:59:30 -08007062 long identity = Binder.clearCallingIdentity();
7063 try {
Hall Liu27d24262020-09-18 19:04:59 -07007064 Phone phone = getPhone(subId);
7065 if (phone == null) {
7066 try {
7067 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
7068 } catch (RemoteException e) {
7069 // ignore
7070 }
7071 return;
7072 }
SongFerngWang0e767992021-03-31 22:08:45 +08007073 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
7074 PersistableBundle c = configManager.getConfigForSubId(subId);
7075 boolean requireUssd = c.getBoolean(
7076 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07007077
Shuo Qian4a594052020-01-23 11:59:30 -08007078 if (DBG) log("getCallWaitingStatus: subId " + subId);
SongFerngWang0e767992021-03-31 22:08:45 +08007079 if (requireUssd) {
7080 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
7081 getSubscriptionCarrierId(subId));
7082 String newUssdCommand = "";
7083 try {
7084 newUssdCommand = carrierXmlParser.getFeature(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08007085 CarrierXmlParser.FEATURE_CALL_WAITING)
SongFerngWang0e767992021-03-31 22:08:45 +08007086 .makeCommand(CarrierXmlParser.SsEntry.SSAction.QUERY, null);
7087 } catch (NullPointerException e) {
7088 loge("Failed to generate USSD number" + e);
7089 }
7090 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
7091 mMainThreadHandler, callback, carrierXmlParser,
7092 CarrierXmlParser.SsEntry.SSAction.QUERY);
7093 final String ussdCommand = newUssdCommand;
7094 Executors.newSingleThreadExecutor().execute(() -> {
7095 handleUssdRequest(subId, ussdCommand, wrappedCallback);
7096 });
7097 } else {
7098 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(
7099 callback::accept);
7100 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
7101 }
Shuo Qian4a594052020-01-23 11:59:30 -08007102 } finally {
7103 Binder.restoreCallingIdentity(identity);
7104 }
7105 }
7106
7107 /**
Hall Liu27d24262020-09-18 19:04:59 -07007108 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08007109 */
7110 @Override
Hall Liu27d24262020-09-18 19:04:59 -07007111 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08007112 enforceModifyPermission();
7113 long identity = Binder.clearCallingIdentity();
7114 try {
Hall Liu27d24262020-09-18 19:04:59 -07007115 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
7116
7117 Phone phone = getPhone(subId);
7118 if (phone == null) {
7119 try {
7120 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
7121 } catch (RemoteException e) {
7122 // ignore
7123 }
7124 return;
7125 }
7126
SongFerngWang0e767992021-03-31 22:08:45 +08007127 CarrierConfigManager configManager = new CarrierConfigManager(phone.getContext());
7128 PersistableBundle c = configManager.getConfigForSubId(subId);
7129 boolean requireUssd = c.getBoolean(
7130 CarrierConfigManager.KEY_USE_CALL_WAITING_USSD_BOOL, false);
Hall Liu27d24262020-09-18 19:04:59 -07007131
SongFerngWang0e767992021-03-31 22:08:45 +08007132 if (DBG) log("getCallWaitingStatus: subId " + subId);
7133 if (requireUssd) {
7134 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(phone.getContext(),
7135 getSubscriptionCarrierId(subId));
7136 CarrierXmlParser.SsEntry.SSAction ssAction =
7137 enable ? CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE
7138 : CarrierXmlParser.SsEntry.SSAction.UPDATE_DEACTIVATE;
7139 String newUssdCommand = "";
7140 try {
7141 newUssdCommand = carrierXmlParser.getFeature(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08007142 CarrierXmlParser.FEATURE_CALL_WAITING)
SongFerngWang0e767992021-03-31 22:08:45 +08007143 .makeCommand(ssAction, null);
7144 } catch (NullPointerException e) {
7145 loge("Failed to generate USSD number" + e);
7146 }
7147 ResultReceiver wrappedCallback = new CallWaitingUssdResultReceiver(
7148 mMainThreadHandler, callback, carrierXmlParser, ssAction);
7149 final String ussdCommand = newUssdCommand;
7150 Executors.newSingleThreadExecutor().execute(() -> {
7151 handleUssdRequest(subId, ussdCommand, wrappedCallback);
7152 });
7153 } else {
7154 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
7155 FunctionalUtils.ignoreRemoteException(callback::accept));
7156
7157 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
7158 }
Shuo Qian4a594052020-01-23 11:59:30 -08007159 } finally {
7160 Binder.restoreCallingIdentity(identity);
7161 }
7162 }
7163
7164 /**
yinxub1bed742017-04-17 11:45:04 -07007165 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07007166 *
yinxub1bed742017-04-17 11:45:04 -07007167 * @param subId id of the subscription
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007168 * @param renounceFineLocationAccess Set this to true if the caller would not like to receive
7169 * location related information which will be sent if the caller already possess
7170 * {@android.Manifest.permission.ACCESS_FINE_LOCATION} and do not renounce the permission
yinxub1bed742017-04-17 11:45:04 -07007171 * @param request contains the radio access networks with bands/channels to scan
7172 * @param messenger callback messenger for scan results or errors
7173 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07007174 * @return the id of the requested scan which can be used to stop the scan.
7175 */
7176 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007177 public int requestNetworkScan(int subId, boolean renounceFineLocationAccess,
7178 NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07007179 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007180 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7181 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08007182 LocationAccessPolicy.LocationPermissionResult locationResult =
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007183 LocationAccessPolicy.LocationPermissionResult.DENIED_HARD;
7184 if (!renounceFineLocationAccess) {
7185 locationResult = LocationAccessPolicy.checkLocationPermission(mApp,
Thomas Nguyen8ee49682023-02-01 11:46:09 -08007186 new LocationAccessPolicy.LocationPermissionQuery.Builder()
7187 .setCallingPackage(callingPackage)
7188 .setCallingFeatureId(callingFeatureId)
7189 .setCallingPid(Binder.getCallingPid())
7190 .setCallingUid(Binder.getCallingUid())
7191 .setMethod("requestNetworkScan")
7192 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
7193 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
7194 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
7195 .build());
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007196 }
Hall Liub2ac8ef2019-02-28 15:56:23 -08007197 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07007198 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
7199 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08007200 if (e != null) {
7201 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
7202 throw e;
7203 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07007204 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08007205 return TelephonyScanManager.INVALID_SCAN_ID;
7206 }
7207 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007208 }
Hall Liu912dfd32019-04-25 14:02:26 -07007209 int callingUid = Binder.getCallingUid();
7210 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07007211 final long identity = Binder.clearCallingIdentity();
7212 try {
7213 return mNetworkScanRequestTracker.startNetworkScan(
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08007214 renounceFineLocationAccess, request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07007215 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07007216 } finally {
7217 Binder.restoreCallingIdentity(identity);
7218 }
yinxu504e1392017-04-12 16:03:22 -07007219 }
7220
Hall Liub2ac8ef2019-02-28 15:56:23 -08007221 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07007222 NetworkScanRequest request, int subId, String callingPackage) {
Rambo Wang3dee30a2022-10-20 16:52:29 +00007223 boolean hasCarrierPriv;
7224 final long identity = Binder.clearCallingIdentity();
7225 try {
7226 hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
7227 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
7228 } finally {
7229 Binder.restoreCallingIdentity(identity);
7230 }
Hall Liu558027f2019-05-15 19:14:05 -07007231 boolean hasNetworkScanPermission =
7232 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
Thomas Nguyen8ee49682023-02-01 11:46:09 -08007233 == PERMISSION_GRANTED;
Hall Liu558027f2019-05-15 19:14:05 -07007234
7235 if (!hasCarrierPriv && !hasNetworkScanPermission) {
7236 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
7237 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08007238 }
7239
7240 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
7241 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08007242 if (ras.getChannels() != null && ras.getChannels().length > 0) {
7243 return new SecurityException("Specific channels must not be"
7244 + " scanned without location access.");
7245 }
7246 }
7247 }
7248
Hall Liub2ac8ef2019-02-28 15:56:23 -08007249 return null;
7250 }
7251
yinxu504e1392017-04-12 16:03:22 -07007252 /**
7253 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07007254 *
7255 * @param subId id of the subscription
7256 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07007257 */
7258 @Override
7259 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007260 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7261 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007262
Hall Liu912dfd32019-04-25 14:02:26 -07007263 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007264 final long identity = Binder.clearCallingIdentity();
7265 try {
Hall Liu912dfd32019-04-25 14:02:26 -07007266 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007267 } finally {
7268 Binder.restoreCallingIdentity(identity);
7269 }
yinxu504e1392017-04-12 16:03:22 -07007270 }
7271
7272 /**
SongFerngWang3ef3e072020-12-21 16:41:52 +08007273 * Get the allowed network types bitmask.
Junda Liu84d15a22014-07-02 11:21:04 -07007274 *
SongFerngWang3ef3e072020-12-21 16:41:52 +08007275 * @return the allowed network types bitmask, defined in RILConstants.java.
Junda Liu84d15a22014-07-02 11:21:04 -07007276 */
7277 @Override
SongFerngWang3ef3e072020-12-21 16:41:52 +08007278 public int getAllowedNetworkTypesBitmask(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08007279 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007280 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
SongFerngWang3ef3e072020-12-21 16:41:52 +08007281 mApp, subId, "getAllowedNetworkTypesBitmask");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007282
7283 final long identity = Binder.clearCallingIdentity();
7284 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08007285 if (DBG) log("getAllowedNetworkTypesBitmask");
7286 int[] result = (int[]) sendRequest(CMD_GET_ALLOWED_NETWORK_TYPES_BITMASK, null, subId);
7287 int networkTypesBitmask = (result != null ? result[0] : -1);
7288 if (DBG) log("getAllowedNetworkTypesBitmask: " + networkTypesBitmask);
7289 return networkTypesBitmask;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007290 } finally {
7291 Binder.restoreCallingIdentity(identity);
7292 }
Jake Hamby7c27be32014-03-03 13:25:59 -08007293 }
7294
7295 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07007296 * Get the allowed network types for certain reason.
7297 *
7298 * @param subId the id of the subscription.
7299 * @param reason the reason the allowed network type change is taking place
7300 * @return the allowed network types.
7301 */
7302 @Override
7303 public long getAllowedNetworkTypesForReason(int subId,
7304 @TelephonyManager.AllowedNetworkTypesReason int reason) {
Nathan Harold62c68512021-04-06 11:26:02 -07007305 TelephonyPermissions.enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007306 mApp, subId, "getAllowedNetworkTypesForReason");
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07007307 final long identity = Binder.clearCallingIdentity();
7308 try {
7309 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
7310 } finally {
7311 Binder.restoreCallingIdentity(identity);
7312 }
7313 }
7314
7315 /**
Sooraj Sasindran37444802020-08-11 10:40:43 -07007316 * Enable/Disable E-UTRA-NR Dual Connectivity
7317 * @param subId subscription id of the sim card
7318 * @param nrDualConnectivityState expected NR dual connectivity state
7319 * This can be passed following states
7320 * <ol>
7321 * <li>Enable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_ENABLE}
7322 * <li>Disable NR dual connectivity {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE}
7323 * <li>Disable NR dual connectivity and force secondary cell to be released
7324 * {@link TelephonyManager#NR_DUAL_CONNECTIVITY_DISABLE_IMMEDIATE}
7325 * </ol>
7326 * @return operation result.
7327 */
7328 @Override
7329 public int setNrDualConnectivityState(int subId,
7330 @TelephonyManager.NrDualConnectivityState int nrDualConnectivityState) {
7331 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7332 mApp, subId, "enableNRDualConnectivity");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07007333 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08007334 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
7335 return TelephonyManager.ENABLE_NR_DUAL_CONNECTIVITY_NOT_SUPPORTED;
7336 }
7337
Sooraj Sasindran37444802020-08-11 10:40:43 -07007338 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7339 final long identity = Binder.clearCallingIdentity();
7340 try {
7341 int result = (int) sendRequest(CMD_ENABLE_NR_DUAL_CONNECTIVITY,
7342 nrDualConnectivityState, subId,
7343 workSource);
7344 if (DBG) log("enableNRDualConnectivity result: " + result);
7345 return result;
7346 } finally {
7347 Binder.restoreCallingIdentity(identity);
7348 }
7349 }
7350
7351 /**
7352 * Is E-UTRA-NR Dual Connectivity enabled
7353 * @return true if dual connectivity is enabled else false
7354 */
7355 @Override
7356 public boolean isNrDualConnectivityEnabled(int subId) {
7357 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07007358 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran37444802020-08-11 10:40:43 -07007359 mApp, subId, "isNRDualConnectivityEnabled");
Sooraj Sasindran0e4e00a2021-03-16 18:02:32 -07007360 if (!isRadioInterfaceCapabilitySupported(
Sooraj Sasindrandfd595b2021-03-11 17:38:13 -08007361 TelephonyManager.CAPABILITY_NR_DUAL_CONNECTIVITY_CONFIGURATION_AVAILABLE)) {
7362 return false;
7363 }
Sooraj Sasindran37444802020-08-11 10:40:43 -07007364 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7365 final long identity = Binder.clearCallingIdentity();
7366 try {
7367 boolean isEnabled = (boolean) sendRequest(CMD_IS_NR_DUAL_CONNECTIVITY_ENABLED,
7368 null, subId, workSource);
7369 if (DBG) log("isNRDualConnectivityEnabled: " + isEnabled);
7370 return isEnabled;
7371 } finally {
7372 Binder.restoreCallingIdentity(identity);
7373 }
7374 }
7375
7376 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07007377 * Set the allowed network types of the device and
7378 * provide the reason triggering the allowed network change.
7379 *
7380 * @param subId the id of the subscription.
7381 * @param reason the reason the allowed network type change is taking place
7382 * @param allowedNetworkTypes the allowed network types.
7383 * @return true on success; false on any failure.
7384 */
7385 @Override
7386 public boolean setAllowedNetworkTypesForReason(int subId,
SongFerngWang3ef3e072020-12-21 16:41:52 +08007387 @TelephonyManager.AllowedNetworkTypesReason int reason,
7388 @TelephonyManager.NetworkTypeBitMask long allowedNetworkTypes) {
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07007389 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7390 mApp, subId, "setAllowedNetworkTypesForReason");
Gil Cukierman1d3d3752022-10-03 21:31:33 +00007391 // If the caller only has carrier privileges, then they should not be able to override
7392 // any network types which were set for security reasons.
7393 if (mApp.checkCallingOrSelfPermission(Manifest.permission.MODIFY_PHONE_STATE)
7394 != PERMISSION_GRANTED
Gil Cukierman2a8f48b2023-01-26 20:26:20 +00007395 && reason == TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_ENABLE_2G) {
Gil Cukierman1d3d3752022-10-03 21:31:33 +00007396 throw new SecurityException(
7397 "setAllowedNetworkTypesForReason cannot be called with carrier privileges for"
Gil Cukierman2a8f48b2023-01-26 20:26:20 +00007398 + " reason " + reason);
Gil Cukierman1d3d3752022-10-03 21:31:33 +00007399 }
SongFerngWang3ef3e072020-12-21 16:41:52 +08007400 if (!TelephonyManager.isValidAllowedNetworkTypesReason(reason)) {
Jack Yu5b494332023-01-23 18:18:04 +00007401 loge("setAllowedNetworkTypesForReason: Invalid allowed network type reason: " + reason);
SongFerngWang7ffc2732021-04-15 19:46:33 +08007402 return false;
7403 }
7404 if (!SubscriptionManager.isUsableSubscriptionId(subId)) {
7405 loge("setAllowedNetworkTypesForReason: Invalid subscriptionId:" + subId);
SongFerngWang3ef3e072020-12-21 16:41:52 +08007406 return false;
7407 }
7408
Jack Yu5b494332023-01-23 18:18:04 +00007409 log("setAllowedNetworkTypesForReason: subId=" + subId + ", reason=" + reason + " value: "
7410 + TelephonyManager.convertNetworkTypeBitmaskToString(allowedNetworkTypes));
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007411
Jack Yue37dd262022-12-16 11:53:37 -08007412 Phone phone = getPhone(subId);
7413 if (phone == null) {
7414 return false;
7415 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007416
Jack Yue37dd262022-12-16 11:53:37 -08007417 if (allowedNetworkTypes == phone.getAllowedNetworkTypes(reason)) {
Jack Yu5b494332023-01-23 18:18:04 +00007418 log("setAllowedNetworkTypesForReason: " + reason + "does not change value");
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007419 return true;
SongFerngWang3ef3e072020-12-21 16:41:52 +08007420 }
SongFerngWang8c6e82e2021-03-02 22:09:29 +08007421
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07007422 final long identity = Binder.clearCallingIdentity();
7423 try {
SongFerngWang3ef3e072020-12-21 16:41:52 +08007424 Boolean success = (Boolean) sendRequest(
7425 CMD_SET_ALLOWED_NETWORK_TYPES_FOR_REASON,
7426 new Pair<Integer, Long>(reason, allowedNetworkTypes), subId);
7427
7428 if (DBG) log("setAllowedNetworkTypesForReason: " + (success ? "ok" : "fail"));
7429 return success;
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07007430 } finally {
7431 Binder.restoreCallingIdentity(identity);
7432 }
7433 }
7434
7435 /**
Miaoa84611c2019-03-15 09:21:10 +08007436 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08007437 *
Miaoa84611c2019-03-15 09:21:10 +08007438 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07007439 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08007440 * @hide
7441 */
7442 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08007443 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007444 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007445 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08007446 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007447 try {
Miaoa84611c2019-03-15 09:21:10 +08007448 if (phone != null) {
7449 return phone.hasMatchedTetherApnSetting();
7450 } else {
7451 return false;
7452 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007453 } finally {
7454 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08007455 }
Junda Liu475951f2014-11-07 16:45:03 -08007456 }
7457
7458 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08007459 * Get the user enabled state of Mobile Data.
7460 *
7461 * TODO: remove and use isUserDataEnabled.
7462 * This can't be removed now because some vendor codes
7463 * calls through ITelephony directly while they should
7464 * use TelephonyManager.
7465 *
7466 * @return true on enabled
7467 */
7468 @Override
7469 public boolean getDataEnabled(int subId) {
7470 return isUserDataEnabled(subId);
7471 }
7472
7473 /**
7474 * Get whether mobile data is enabled per user setting.
7475 *
7476 * There are other factors deciding whether mobile data is actually enabled, but they are
7477 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07007478 *
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007479 * Accepts either READ_BASIC_PHONE_STATE, ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE
7480 * or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07007481 *
7482 * @return {@code true} if data is enabled else {@code false}
7483 */
7484 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08007485 public boolean isUserDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007486 String functionName = "isUserDataEnabled";
Robert Greenwalt646120a2014-05-23 11:54:03 -07007487 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007488 try {
7489 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
7490 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007491 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007492 mApp.enforceCallingOrSelfPermission(permission.ACCESS_NETWORK_STATE, functionName);
7493 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007494 } catch (SecurityException e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007495 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007496 mApp, subId, functionName);
7497
Robert Greenwalt646120a2014-05-23 11:54:03 -07007498 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007499
7500 final long identity = Binder.clearCallingIdentity();
7501 try {
Jack Yu285100e2022-12-02 22:48:35 -08007502 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007503 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
7504 Phone phone = PhoneFactory.getPhone(phoneId);
7505 if (phone != null) {
7506 boolean retVal = phone.isUserDataEnabled();
7507 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
7508 return retVal;
7509 } else {
7510 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
7511 return false;
7512 }
7513 } finally {
7514 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08007515 }
7516 }
7517
7518 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08007519 * Checks if the device is capable of mobile data by considering whether whether the
7520 * user has enabled mobile data, whether the carrier has enabled mobile data, and
7521 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08007522 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08007523 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08007524 */
7525 @Override
7526 public boolean isDataEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007527 String functionName = "isDataEnabled";
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007528 try {
7529 try {
7530 mApp.enforceCallingOrSelfPermission(
7531 android.Manifest.permission.ACCESS_NETWORK_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007532 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007533 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007534 try {
7535 mApp.enforceCallingOrSelfPermission(
7536 android.Manifest.permission.READ_PHONE_STATE,
7537 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007538 } catch (SecurityException e2) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007539 mApp.enforceCallingOrSelfPermission(
7540 permission.READ_BASIC_PHONE_STATE, functionName);
7541 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007542 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007543 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007544 enforceReadPrivilegedPermission(functionName);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007545 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007546
7547 final long identity = Binder.clearCallingIdentity();
7548 try {
Jack Yu285100e2022-12-02 22:48:35 -08007549 int phoneId = SubscriptionManager.getPhoneId(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007550 Phone phone = PhoneFactory.getPhone(phoneId);
7551 if (phone != null) {
Sarah Chine04784a2022-10-31 20:32:34 -07007552 boolean retVal = phone.getDataSettingsManager().isDataEnabled();
Jack Yu4ad64e52021-12-03 14:23:53 -08007553 if (DBG) log("isDataEnabled: " + retVal + ", subId=" + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007554 return retVal;
7555 } else {
7556 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
7557 return false;
7558 }
7559 } finally {
7560 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08007561 }
Robert Greenwalted86e582014-05-21 20:03:20 -07007562 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07007563
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007564 /**
7565 * Check if data is enabled for a specific reason
7566 * @param subId Subscription index
7567 * @param reason the reason the data enable change is taking place
7568 * @return {@code true} if the overall data is enabled; {@code false} if not.
7569 */
7570 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007571 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007572 @TelephonyManager.DataEnabledReason int reason) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007573 String functionName = "isDataEnabledForReason";
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007574 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007575 try {
7576 mApp.enforceCallingOrSelfPermission(
7577 android.Manifest.permission.ACCESS_NETWORK_STATE,
7578 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007579 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007580 mApp.enforceCallingOrSelfPermission(permission.READ_BASIC_PHONE_STATE,
7581 functionName);
7582 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007583 } catch (SecurityException e) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08007584 try {
7585 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007586 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07007587 } catch (SecurityException e2) {
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08007588 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07007589 mApp, subId, functionName);
Sooraj Sasindran0d909a02021-11-08 12:01:16 -08007590 }
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007591 }
7592
7593
7594 final long identity = Binder.clearCallingIdentity();
7595 try {
Jack Yu285100e2022-12-02 22:48:35 -08007596 int phoneId = SubscriptionManager.getPhoneId(subId);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007597 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007598 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007599 + " reason=" + reason);
7600 }
7601 Phone phone = PhoneFactory.getPhone(phoneId);
7602 if (phone != null) {
7603 boolean retVal;
Jack Yu7968c6d2022-07-31 00:43:21 -07007604 retVal = phone.getDataSettingsManager().isDataEnabledForReason(reason);
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007605 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007606 return retVal;
7607 } else {
7608 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007609 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007610 + subId + " retVal=false");
7611 }
7612 return false;
7613 }
7614 } finally {
7615 Binder.restoreCallingIdentity(identity);
7616 }
7617 }
7618
Shishir Agrawal60f9c952014-06-23 12:00:43 -07007619 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08007620 public int getCarrierPrivilegeStatus(int subId) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007621 // No permission needed; this only lets the caller inspect their own status.
7622 return getCarrierPrivilegeStatusForUidWithPermission(subId, Binder.getCallingUid());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07007623 }
Junda Liu29340342014-07-10 15:23:27 -07007624
7625 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08007626 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007627 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007628 return getCarrierPrivilegeStatusForUidWithPermission(subId, uid);
7629 }
7630
7631 private int getCarrierPrivilegeStatusForUidWithPermission(int subId, int uid) {
7632 Phone phone = getPhone(subId);
Jeff Davidson7e17e312018-02-13 18:17:36 -08007633 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09007634 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08007635 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7636 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007637 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7638 if (cpt == null) {
7639 loge("getCarrierPrivilegeStatusForUid: No CarrierPrivilegesTracker");
Jeff Davidson7e17e312018-02-13 18:17:36 -08007640 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7641 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007642 return cpt.getCarrierPrivilegeStatusForUid(uid);
Jeff Davidson7e17e312018-02-13 18:17:36 -08007643 }
7644
7645 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007646 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
Nazanin1adf4562021-03-29 15:35:30 -07007647 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackage");
chen xuf7e9fe82019-05-09 19:31:02 -07007648 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007649 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07007650 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007651 Phone phone = getPhone(subId);
7652 if (phone == null) {
7653 loge("checkCarrierPrivilegesForPackage: Invalid subId");
7654 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
7655 }
7656 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7657 if (cpt == null) {
7658 loge("checkCarrierPrivilegesForPackage: No CarrierPrivilegesTracker");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07007659 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
7660 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007661 return cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007662 }
7663
7664 @Override
7665 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007666 enforceReadPrivilegedPermission("checkCarrierPrivilegesForPackageAnyPhone");
Rambo Wange7209ce2022-02-23 13:41:02 -08007667 return checkCarrierPrivilegesForPackageAnyPhoneWithPermission(pkgName);
7668 }
7669
7670 private int checkCarrierPrivilegesForPackageAnyPhoneWithPermission(String pkgName) {
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007671 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08007672 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007673 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007674 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007675 for (int phoneId = 0; phoneId < TelephonyManager.getDefault().getPhoneCount(); phoneId++) {
7676 Phone phone = PhoneFactory.getPhone(phoneId);
7677 if (phone == null) {
7678 continue;
Zach Johnson50ecba32015-05-19 00:24:21 -07007679 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007680 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7681 if (cpt == null) {
7682 continue;
7683 }
7684 result = cpt.getCarrierPrivilegeStatusForPackage(pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07007685 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7686 break;
7687 }
7688 }
Zach Johnson50ecba32015-05-19 00:24:21 -07007689 return result;
Junda Liu29340342014-07-10 15:23:27 -07007690 }
Derek Tan89e89d42014-07-08 17:00:10 -07007691
7692 @Override
Junda Liue64de782015-04-16 17:19:16 -07007693 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007694 enforceReadPrivilegedPermission("getCarrierPackageNamesForIntentAndPhone");
Rambo Wang8a247eb2022-02-08 21:11:18 +00007695 Phone phone = PhoneFactory.getPhone(phoneId);
7696 if (phone == null) {
7697 return Collections.emptyList();
Junda Liue64de782015-04-16 17:19:16 -07007698 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007699 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7700 if (cpt == null) {
7701 return Collections.emptyList();
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007702 }
Rambo Wang8a247eb2022-02-08 21:11:18 +00007703 return cpt.getCarrierPackageNamesForIntent(intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007704 }
7705
Amith Yamasani6e118872016-02-19 12:53:51 -08007706 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07007707 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -07007708 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivileges");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007709 Phone phone = PhoneFactory.getPhone(phoneId);
7710 if (phone == null) {
7711 return Collections.emptyList();
Amith Yamasani6e118872016-02-19 12:53:51 -08007712 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007713 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7714 if (cpt == null) {
7715 return Collections.emptyList();
7716 }
7717 return new ArrayList<>(cpt.getPackagesWithCarrierPrivileges());
Amith Yamasani6e118872016-02-19 12:53:51 -08007718 }
7719
chen xuf7e9fe82019-05-09 19:31:02 -07007720 @Override
7721 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00007722 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007723 Set<String> privilegedPackages = new ArraySet<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00007724 final long identity = Binder.clearCallingIdentity();
Shuo Qian067a06d2019-12-03 23:40:18 +00007725 try {
7726 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
7727 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
7728 }
7729 } finally {
7730 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07007731 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08007732 return new ArrayList<>(privilegedPackages);
chen xuf7e9fe82019-05-09 19:31:02 -07007733 }
7734
Rambo Wang6812ffb2022-03-15 16:54:17 -07007735 @Override
7736 public @Nullable String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
7737 enforceReadPrivilegedPermission("getCarrierServicePackageNameForLogicalSlot");
7738
7739 final Phone phone = PhoneFactory.getPhone(logicalSlotIndex);
7740 if (phone == null) {
7741 return null;
7742 }
7743 final CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
7744 if (cpt == null) {
7745 return null;
7746 }
7747 return cpt.getCarrierServicePackageName();
7748 }
7749
Wink Savilleb564aae2014-10-23 10:18:09 -07007750 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07007751 final Phone phone = getPhone(subId);
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007752 UiccPort port = phone == null ? null : phone.getUiccPort();
7753 if (port == null) {
Derek Tan97ebb422014-09-05 16:55:38 -07007754 return null;
7755 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00007756 String iccId = port.getIccId();
Derek Tan97ebb422014-09-05 16:55:38 -07007757 if (TextUtils.isEmpty(iccId)) {
Derek Tan97ebb422014-09-05 16:55:38 -07007758 return null;
7759 }
7760 return iccId;
7761 }
7762
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07007763 @Override
Shuo Qiane4e11672020-12-15 22:15:33 -08007764 public void setCallComposerStatus(int subId, int status) {
7765 enforceModifyPermission();
7766
7767 final long identity = Binder.clearCallingIdentity();
7768 try {
7769 Phone phone = getPhone(subId);
7770 if (phone != null) {
7771 Phone defaultPhone = phone.getImsPhone();
7772 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7773 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7774 imsPhone.setCallComposerStatus(status);
Shuo Qian284ae752020-12-22 19:10:14 -08007775 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
7776 .updateImsServiceConfig();
Shuo Qiane4e11672020-12-15 22:15:33 -08007777 }
7778 }
Shuo Qian284ae752020-12-22 19:10:14 -08007779 } catch (ImsException e) {
7780 throw new ServiceSpecificException(e.getCode());
7781 } finally {
Shuo Qiane4e11672020-12-15 22:15:33 -08007782 Binder.restoreCallingIdentity(identity);
7783 }
7784 }
7785
7786 @Override
7787 public int getCallComposerStatus(int subId) {
7788 enforceReadPrivilegedPermission("getCallComposerStatus");
7789
7790 final long identity = Binder.clearCallingIdentity();
7791 try {
7792 Phone phone = getPhone(subId);
7793 if (phone != null) {
7794 Phone defaultPhone = phone.getImsPhone();
7795 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
7796 ImsPhone imsPhone = (ImsPhone) defaultPhone;
7797 return imsPhone.getCallComposerStatus();
7798 }
7799 }
7800 } finally {
7801 Binder.restoreCallingIdentity(identity);
7802 }
7803 return TelephonyManager.CALL_COMPOSER_STATUS_OFF;
7804 }
7805
7806 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08007807 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
7808 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08007809 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08007810 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07007811
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007812 final long identity = Binder.clearCallingIdentity();
7813 try {
7814 final String iccId = getIccId(subId);
7815 final Phone phone = getPhone(subId);
7816 if (phone == null) {
7817 return false;
7818 }
7819 final String subscriberId = phone.getSubscriberId();
7820
7821 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007822 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007823 + subscriberId + " to " + number);
7824 }
7825
7826 if (TextUtils.isEmpty(iccId)) {
7827 return false;
7828 }
7829
7830 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
7831
7832 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7833 if (alphaTag == null) {
7834 editor.remove(alphaTagPrefKey);
7835 } else {
7836 editor.putString(alphaTagPrefKey, alphaTag);
7837 }
7838
7839 // Record both the line number and IMSI for this ICCID, since we need to
7840 // track all merged IMSIs based on line number
7841 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7842 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7843 if (number == null) {
7844 editor.remove(numberPrefKey);
7845 editor.remove(subscriberPrefKey);
7846 } else {
7847 editor.putString(numberPrefKey, number);
7848 editor.putString(subscriberPrefKey, subscriberId);
7849 }
7850
7851 editor.commit();
7852 return true;
7853 } finally {
7854 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07007855 }
Derek Tan7226c842014-07-02 17:42:23 -07007856 }
7857
7858 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007859 public String getLine1NumberForDisplay(int subId, String callingPackage,
7860 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07007861 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007862 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007863 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08007864 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07007865 return null;
7866 }
Derek Tan97ebb422014-09-05 16:55:38 -07007867
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007868 final long identity = Binder.clearCallingIdentity();
7869 try {
7870 String iccId = getIccId(subId);
7871 if (iccId != null) {
7872 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7873 if (DBG_MERGE) {
7874 log("getLine1NumberForDisplay returning "
7875 + mTelephonySharedPreferences.getString(numberPrefKey, null));
7876 }
7877 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08007878 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007879 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
7880 return null;
7881 } finally {
7882 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007883 }
Derek Tan7226c842014-07-02 17:42:23 -07007884 }
7885
7886 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007887 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
7888 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007889 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007890 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07007891 return null;
7892 }
Derek Tan97ebb422014-09-05 16:55:38 -07007893
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007894 final long identity = Binder.clearCallingIdentity();
7895 try {
7896 String iccId = getIccId(subId);
7897 if (iccId != null) {
7898 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
7899 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
7900 }
7901 return null;
7902 } finally {
7903 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07007904 }
Derek Tan7226c842014-07-02 17:42:23 -07007905 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07007906
7907 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007908 public String[] getMergedSubscriberIds(int subId, String callingPackage,
7909 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08007910 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
7911 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08007912 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08007913 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007914 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007915 return null;
7916 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007917
Jordan Liub49b04b2019-05-06 14:45:15 -07007918 // Clear calling identity, when calling TelephonyManager, because callerUid must be
7919 // the process, where TelephonyManager was instantiated.
7920 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007921 final long identity = Binder.clearCallingIdentity();
7922 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007923 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007924 final TelephonyManager tele = TelephonyManager.from(context);
7925 final SubscriptionManager sub = SubscriptionManager.from(context);
7926
7927 // Figure out what subscribers are currently active
7928 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007929
Jordan Liub49b04b2019-05-06 14:45:15 -07007930 // Only consider subs which match the current subId
7931 // This logic can be simplified. See b/131189269 for progress.
7932 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007933 activeSubscriberIds.add(tele.getSubscriberId(subId));
7934 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007935
7936 // First pass, find a number override for an active subscriber
7937 String mergeNumber = null;
7938 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
7939 for (String key : prefs.keySet()) {
7940 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
7941 final String subscriberId = (String) prefs.get(key);
7942 if (activeSubscriberIds.contains(subscriberId)) {
7943 final String iccId = key.substring(
7944 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
7945 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
7946 mergeNumber = (String) prefs.get(numberKey);
7947 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007948 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007949 + " for active subscriber " + subscriberId);
7950 }
7951 if (!TextUtils.isEmpty(mergeNumber)) {
7952 break;
7953 }
7954 }
7955 }
7956 }
7957
7958 // Shortcut when no active merged subscribers
7959 if (TextUtils.isEmpty(mergeNumber)) {
7960 return null;
7961 }
7962
7963 // Second pass, find all subscribers under that line override
7964 final ArraySet<String> result = new ArraySet<>();
7965 for (String key : prefs.keySet()) {
7966 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
7967 final String number = (String) prefs.get(key);
7968 if (mergeNumber.equals(number)) {
7969 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
7970 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
7971 final String subscriberId = (String) prefs.get(subscriberKey);
7972 if (!TextUtils.isEmpty(subscriberId)) {
7973 result.add(subscriberId);
7974 }
7975 }
7976 }
7977 }
7978
7979 final String[] resultArray = result.toArray(new String[result.size()]);
7980 Arrays.sort(resultArray);
7981 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08007982 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007983 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
7984 }
7985 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07007986 } finally {
7987 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08007988 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08007989 }
7990
7991 @Override
zoey chen38003472019-12-13 17:16:31 +08007992 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
7993 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07007994
7995 final long identity = Binder.clearCallingIdentity();
7996 try {
7997 final TelephonyManager telephonyManager = mApp.getSystemService(
7998 TelephonyManager.class);
7999 String subscriberId = telephonyManager.getSubscriberId(subId);
8000 if (subscriberId == null) {
8001 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08008002 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07008003 + subId);
8004 }
8005 return null;
8006 }
8007
Jack Yu285100e2022-12-02 22:48:35 -08008008 ParcelUuid groupUuid;
8009 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8010 final SubscriptionInfo info = SubscriptionManagerService.getInstance()
8011 .getSubscriptionInfo(subId);
8012 groupUuid = info.getGroupUuid();
8013 } else {
8014 final SubscriptionInfo info = mSubscriptionController
8015 .getSubscriptionInfo(subId);
8016 groupUuid = info.getGroupUuid();
8017 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07008018 // If it doesn't belong to any group, return just subscriberId of itself.
8019 if (groupUuid == null) {
8020 return new String[]{subscriberId};
8021 }
8022
8023 // Get all subscriberIds from the group.
8024 final List<String> mergedSubscriberIds = new ArrayList<>();
Jack Yu285100e2022-12-02 22:48:35 -08008025 List<SubscriptionInfo> groupInfos;
8026 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8027 groupInfos = SubscriptionManagerService.getInstance()
8028 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
8029 mApp.getAttributionTag());
8030 } else {
8031 groupInfos = mSubscriptionController
8032 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
8033 mApp.getAttributionTag());
8034 }
Malcolm Chen6ca97372019-07-01 16:28:21 -07008035 for (SubscriptionInfo subInfo : groupInfos) {
8036 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
8037 if (subscriberId != null) {
8038 mergedSubscriberIds.add(subscriberId);
8039 }
8040 }
8041
8042 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
8043 } finally {
8044 Binder.restoreCallingIdentity(identity);
8045
8046 }
8047 }
8048
8049 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08008050 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08008051 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08008052 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008053
8054 final long identity = Binder.clearCallingIdentity();
8055 try {
8056 final Phone phone = getPhone(subId);
8057 return phone == null ? false : phone.setOperatorBrandOverride(brand);
8058 } finally {
8059 Binder.restoreCallingIdentity(identity);
8060 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07008061 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05008062
8063 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08008064 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08008065 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
8066 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08008067 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
8068 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008069
8070 final long identity = Binder.clearCallingIdentity();
8071 try {
8072 final Phone phone = getPhone(subId);
8073 if (phone == null) {
8074 return false;
8075 }
8076 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
8077 cdmaNonRoamingList);
8078 } finally {
8079 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08008080 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08008081 }
8082
8083 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00008084 @Deprecated
8085 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
8086 enforceModifyPermission();
8087
8088 int returnValue = 0;
8089 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07008090 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00008091 if(result.exception == null) {
8092 if (result.result != null) {
8093 byte[] responseData = (byte[])(result.result);
8094 if(responseData.length > oemResp.length) {
8095 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
8096 responseData.length + "bytes. Buffer Size is " +
8097 oemResp.length + "bytes.");
8098 }
8099 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
8100 returnValue = responseData.length;
8101 }
8102 } else {
8103 CommandException ex = (CommandException) result.exception;
8104 returnValue = ex.getCommandError().ordinal();
8105 if(returnValue > 0) returnValue *= -1;
8106 }
8107 } catch (RuntimeException e) {
8108 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
8109 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
8110 if(returnValue > 0) returnValue *= -1;
8111 }
8112
8113 return returnValue;
8114 }
8115
8116 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07008117 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08008118 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07008119 try {
8120 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07008121 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Shuo Qiandee53402020-05-29 14:08:15 -07008122 mApp, phone.getSubId(), "getRadioAccessFamily");
8123 } catch (SecurityException e) {
8124 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
8125 throw e;
8126 }
chen xub97461a2018-10-26 14:17:57 -07008127 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08008128 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07008129 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08008130 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008131 final long identity = Binder.clearCallingIdentity();
8132 try {
chen xub97461a2018-10-26 14:17:57 -07008133 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07008134 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
chen xub97461a2018-10-26 14:17:57 -07008135 mApp, phone.getSubId(), "getRadioAccessFamily");
8136 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008137 } finally {
8138 Binder.restoreCallingIdentity(identity);
8139 }
chen xub97461a2018-10-26 14:17:57 -07008140 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07008141 }
Andrew Leedf14ead2014-10-17 14:22:52 -07008142
8143 @Override
Hall Liu82694d52020-12-11 18:22:04 -08008144 public void uploadCallComposerPicture(int subscriptionId, String callingPackage,
Hall Liue31bac62020-12-23 19:16:10 -08008145 String contentType, ParcelFileDescriptor fd, ResultReceiver callback) {
Hall Liu82694d52020-12-11 18:22:04 -08008146 try {
8147 if (!Objects.equals(mApp.getPackageManager().getPackageUid(callingPackage, 0),
8148 Binder.getCallingUid())) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07008149 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08008150 }
8151 } catch (PackageManager.NameNotFoundException e) {
Tyler Gunnb87925a2021-06-10 14:54:27 -07008152 throw new SecurityException("Invalid package:" + callingPackage);
Hall Liu82694d52020-12-11 18:22:04 -08008153 }
8154 RoleManager rm = mApp.getSystemService(RoleManager.class);
8155 List<String> dialerRoleHolders = rm.getRoleHolders(RoleManager.ROLE_DIALER);
8156 if (!dialerRoleHolders.contains(callingPackage)) {
8157 throw new SecurityException("App must be the dialer role holder to"
8158 + " upload a call composer pic");
8159 }
8160
8161 Executors.newSingleThreadExecutor().execute(() -> {
8162 ByteArrayOutputStream output = new ByteArrayOutputStream(
8163 (int) TelephonyManager.getMaximumCallComposerPictureSize());
8164 InputStream input = new ParcelFileDescriptor.AutoCloseInputStream(fd);
8165 boolean readUntilEnd = false;
8166 int totalBytesRead = 0;
8167 byte[] buffer = new byte[16 * 1024];
8168 while (true) {
8169 int numRead;
8170 try {
8171 numRead = input.read(buffer);
8172 } catch (IOException e) {
8173 try {
8174 fd.checkError();
8175 callback.send(TelephonyManager.CallComposerException.ERROR_INPUT_CLOSED,
8176 null);
8177 } catch (IOException e1) {
8178 // This means that the other side closed explicitly with an error. If this
8179 // happens, log and ignore.
8180 loge("Remote end of call composer picture pipe closed: " + e1);
8181 }
8182 break;
8183 }
8184 if (numRead == -1) {
8185 readUntilEnd = true;
8186 break;
8187 }
8188 totalBytesRead += numRead;
8189 if (totalBytesRead > TelephonyManager.getMaximumCallComposerPictureSize()) {
8190 loge("Too many bytes read for call composer picture: " + totalBytesRead);
8191 try {
8192 input.close();
8193 } catch (IOException e) {
8194 // ignore
8195 }
8196 break;
8197 }
8198 output.write(buffer, 0, numRead);
8199 }
8200 // Generally, the remote end will close the file descriptors. The only case where we
8201 // close is above, where the picture size is too big.
8202
8203 try {
8204 fd.checkError();
8205 } catch (IOException e) {
8206 loge("Remote end for call composer closed with an error: " + e);
8207 return;
8208 }
8209
Hall Liuaa4211e2021-01-20 15:43:39 -08008210 if (!readUntilEnd) {
8211 loge("Did not finish reading entire image; aborting");
8212 return;
8213 }
Hall Liu82694d52020-12-11 18:22:04 -08008214
Hall Liuaa4211e2021-01-20 15:43:39 -08008215 ImageData imageData = new ImageData(output.toByteArray(), contentType, null);
8216 CallComposerPictureManager.getInstance(mApp, subscriptionId).handleUploadToServer(
8217 new CallComposerPictureTransfer.Factory() {},
8218 imageData,
8219 (result) -> {
8220 if (result.first != null) {
8221 ParcelUuid parcelUuid = new ParcelUuid(result.first);
8222 Bundle outputResult = new Bundle();
8223 outputResult.putParcelable(
8224 TelephonyManager.KEY_CALL_COMPOSER_PICTURE_HANDLE, parcelUuid);
8225 callback.send(TelephonyManager.CallComposerException.SUCCESS,
8226 outputResult);
8227 } else {
8228 callback.send(result.second, null);
8229 }
8230 }
8231 );
Hall Liu82694d52020-12-11 18:22:04 -08008232 });
8233 }
8234
8235 @Override
Andrew Leedf14ead2014-10-17 14:22:52 -07008236 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008237 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07008238 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008239
8240 final long identity = Binder.clearCallingIdentity();
8241 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008242 ImsManager.getInstance(defaultPhone.getContext(),
8243 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008244 } finally {
8245 Binder.restoreCallingIdentity(identity);
8246 }
Andrew Leedf14ead2014-10-17 14:22:52 -07008247 }
8248
8249 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008250 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008251 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008252 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
8253 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00008254 return false;
8255 }
Svet Ganovb320e182015-04-16 12:30:10 -07008256
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008257 final long identity = Binder.clearCallingIdentity();
8258 try {
8259 // Check the user preference and the system-level IMS setting. Even if the user has
8260 // enabled video calling, if IMS is disabled we aren't able to support video calling.
8261 // In the long run, we may instead need to check if there exists a connection service
8262 // which can support video calling.
8263 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008264 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008265 return imsManager.isVtEnabledByPlatform()
8266 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
8267 && imsManager.isVtEnabledByUser();
8268 } finally {
8269 Binder.restoreCallingIdentity(identity);
8270 }
Andrew Leedf14ead2014-10-17 14:22:52 -07008271 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06008272
Andrew Leea1239f22015-03-02 17:44:07 -08008273 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008274 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
8275 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008276 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008277 mApp, subId, callingPackage, callingFeatureId,
8278 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008279 return false;
8280 }
8281
8282 final long identity = Binder.clearCallingIdentity();
8283 try {
8284 CarrierConfigManager configManager =
8285 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008286 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008287 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
8288 } finally {
8289 Binder.restoreCallingIdentity(identity);
8290 }
Andrew Leea1239f22015-03-02 17:44:07 -08008291 }
8292
8293 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008294 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008295 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008296 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008297 return false;
8298 }
8299
8300 final long identity = Binder.clearCallingIdentity();
8301 try {
8302 CarrierConfigManager configManager =
8303 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008304 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008305 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
8306 } finally {
8307 Binder.restoreCallingIdentity(identity);
8308 }
Andrew Leea1239f22015-03-02 17:44:07 -08008309 }
8310
Andrew Lee9431b832015-03-09 18:46:45 -07008311 @Override
8312 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008313 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08008314 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07008315 }
8316
8317 @Override
8318 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008319 final long identity = Binder.clearCallingIdentity();
8320 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008321 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008322 } finally {
8323 Binder.restoreCallingIdentity(identity);
8324 }
Andrew Lee9431b832015-03-09 18:46:45 -07008325 }
8326
Hall Liuf6668912018-10-31 17:05:23 -07008327 /**
8328 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
8329 * support for the feature and device firmware support.
8330 *
8331 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
8332 */
8333 @Override
8334 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008335 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008336 final Phone phone = getPhone(subscriptionId);
8337 if (phone == null) {
8338 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
8339 return false;
8340 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008341 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008342 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008343 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
8344 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008345 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008346 return isCarrierSupported && isDeviceSupported;
8347 } finally {
8348 Binder.restoreCallingIdentity(identity);
8349 }
Hall Liu98187582018-01-22 19:15:32 -08008350 }
8351
Hall Liuf6668912018-10-31 17:05:23 -07008352 /**
Hall Liuf2daa022019-07-23 18:39:00 -07008353 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
8354 * RTT setting, will return true if the device and carrier both support RTT.
8355 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07008356 */
8357 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008358 final long identity = Binder.clearCallingIdentity();
8359 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00008360 boolean isRttSupported = isRttSupported(subscriptionId);
8361 boolean isUserRttSettingOn = Settings.Secure.getInt(
8362 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
8363 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
8364 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
8365 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008366 } finally {
8367 Binder.restoreCallingIdentity(identity);
8368 }
Hall Liu3ad5f012018-04-06 16:23:39 -07008369 }
8370
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008371 @Deprecated
8372 @Override
8373 public String getDeviceId(String callingPackage) {
8374 return getDeviceIdWithFeature(callingPackage, null);
8375 }
8376
Sanket Padawe7310cc72015-01-14 09:53:20 -08008377 /**
8378 * Returns the unique device ID of phone, for example, the IMEI for
8379 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
8380 *
8381 * <p>Requires Permission:
8382 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
8383 */
8384 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008385 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Shuo Qian13d89152021-05-10 23:58:11 -07008386 try {
8387 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
8388 } catch (SecurityException se) {
8389 EventLog.writeEvent(0x534e4554, "186530889", Binder.getCallingUid());
8390 throw new SecurityException("Package " + callingPackage + " does not belong to "
8391 + Binder.getCallingUid());
8392 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08008393 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08008394 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08008395 return null;
8396 }
Jeff Davidson913390f2018-02-23 17:11:49 -08008397 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07008398 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008399 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08008400 return null;
8401 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008402
8403 final long identity = Binder.clearCallingIdentity();
8404 try {
8405 return phone.getDeviceId();
8406 } finally {
8407 Binder.restoreCallingIdentity(identity);
8408 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08008409 }
8410
Ping Sunc67b7c22016-03-02 19:16:45 +08008411 /**
8412 * {@hide}
8413 * Returns the IMS Registration Status on a particular subid
8414 *
8415 * @param subId
8416 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008417 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08008418 Phone phone = getPhone(subId);
8419 if (phone != null) {
8420 return phone.isImsRegistered();
8421 } else {
8422 return false;
8423 }
8424 }
8425
Santos Cordon7a1885b2015-02-03 11:15:19 -08008426 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07008427 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008428 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07008429 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008430 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07008431 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8432 }
8433 final long identity = Binder.clearCallingIdentity();
8434 try {
8435 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
8436 } finally {
8437 Binder.restoreCallingIdentity(identity);
8438 }
8439 }
8440
8441 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07008442 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
Alireza Forouzan4ac4f982021-03-16 22:18:52 -07008443 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07008444 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08008445 mApp,
8446 subscriptionId,
8447 "getPhoneAccountHandleForSubscriptionId, " + "subscriptionId: "
8448 + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07008449 final long identity = Binder.clearCallingIdentity();
8450 try {
8451 Phone phone = getPhone(subscriptionId);
8452 if (phone == null) {
8453 return null;
8454 }
8455 return PhoneUtils.makePstnPhoneAccountHandle(phone);
8456 } finally {
8457 Binder.restoreCallingIdentity(identity);
8458 }
8459 }
8460
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008461 /**
8462 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07008463 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008464 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008465 final long identity = Binder.clearCallingIdentity();
8466 try {
8467 Phone phone = getPhone(subId);
8468 if (phone != null) {
8469 return phone.isWifiCallingEnabled();
8470 } else {
8471 return false;
8472 }
8473 } finally {
8474 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008475 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07008476 }
8477
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008478 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008479 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07008480 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008481 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008482 final long identity = Binder.clearCallingIdentity();
8483 try {
8484 Phone phone = getPhone(subId);
8485 if (phone != null) {
8486 return phone.isVideoEnabled();
8487 } else {
8488 return false;
8489 }
8490 } finally {
8491 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008492 }
8493 }
8494
8495 /**
8496 * @return the IMS registration technology for the MMTEL feature. Valid return values are
8497 * defined in {@link ImsRegistrationImplBase}.
8498 */
8499 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008500 final long identity = Binder.clearCallingIdentity();
8501 try {
8502 Phone phone = getPhone(subId);
8503 if (phone != null) {
8504 return phone.getImsRegistrationTech();
8505 } else {
8506 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
8507 }
8508 } finally {
8509 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08008510 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07008511 }
8512
Stuart Scott8eef64f2015-04-08 15:13:54 -07008513 @Override
Sarah Chinecc78c42022-03-31 21:16:48 -07008514 public void factoryReset(int subId, String callingPackage) {
paulhu5a773602019-08-23 19:17:33 +08008515 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07008516 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
8517 return;
8518 }
Kai Shif70f46f2021-03-03 13:59:46 -08008519 Phone defaultPhone = getDefaultPhone();
8520 if (defaultPhone != null) {
8521 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8522 mApp, getDefaultPhone().getSubId(), "factoryReset");
8523 }
Svet Ganovcc087f82015-05-12 20:35:54 -07008524 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008525
Svet Ganovcc087f82015-05-12 20:35:54 -07008526 try {
Stuart Scott981d8582015-04-21 14:09:50 -07008527 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
8528 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07008529 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sarah Chinecc78c42022-03-31 21:16:48 -07008530 getDefaultDataEnabled(), callingPackage);
Svet Ganovcc087f82015-05-12 20:35:54 -07008531 setNetworkSelectionModeAutomatic(subId);
SongFerngWang8c6e82e2021-03-02 22:09:29 +08008532 Phone phone = getPhone(subId);
SongFerngWangfd89b102021-05-27 22:44:54 +08008533 cleanUpAllowedNetworkTypes(phone, subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008534 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
Jordan Liu857e73a2021-03-05 16:24:04 -08008535 getPhone(subId).resetCarrierKeysForImsiEncryption();
Svet Ganovcc087f82015-05-12 20:35:54 -07008536 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07008537 // There has been issues when Sms raw table somehow stores orphan
8538 // fragments. They lead to garbled message when new fragments come
8539 // in and combined with those stale ones. In case this happens again,
8540 // user can reset all network settings which will clean up this table.
8541 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07008542 // Clean up IMS settings as well here.
8543 int slotId = getSlotIndex(subId);
8544 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
8545 ImsManager.getInstance(mApp, slotId).factoryReset();
8546 }
Naina Nallurid63128d2019-09-17 14:10:30 -07008547
Kai Shif70f46f2021-03-03 13:59:46 -08008548 if (defaultPhone == null) {
8549 return;
8550 }
Naina Nallurid63128d2019-09-17 14:10:30 -07008551 // Erase modem config if erase modem on network setting is enabled.
8552 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
8553 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
8554 if (configValue != null && Boolean.parseBoolean(configValue)) {
Kai Shif70f46f2021-03-03 13:59:46 -08008555 sendEraseModemConfig(defaultPhone);
Naina Nallurid63128d2019-09-17 14:10:30 -07008556 }
Kai Shif70f46f2021-03-03 13:59:46 -08008557
8558 sendEraseDataInSharedPreferences(defaultPhone);
Svet Ganovcc087f82015-05-12 20:35:54 -07008559 } finally {
8560 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07008561 }
8562 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008563
SongFerngWangfd89b102021-05-27 22:44:54 +08008564 @VisibleForTesting
8565 void cleanUpAllowedNetworkTypes(Phone phone, int subId) {
8566 if (phone == null || !SubscriptionManager.isUsableSubscriptionId(subId)) {
8567 return;
8568 }
8569 long defaultNetworkType = RadioAccessFamily.getRafFromNetworkType(
8570 RILConstants.PREFERRED_NETWORK_MODE);
8571 SubscriptionManager.setSubscriptionProperty(subId,
8572 SubscriptionManager.ALLOWED_NETWORK_TYPES,
8573 "user=" + defaultNetworkType);
8574 phone.loadAllowedNetworksFromSubscriptionDatabase();
8575 phone.setAllowedNetworkTypes(TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
8576 defaultNetworkType, null);
8577 }
8578
Amit Mahajan7dbbd822019-03-13 17:33:47 -07008579 private void cleanUpSmsRawTable(Context context) {
8580 ContentResolver resolver = context.getContentResolver();
8581 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
8582 resolver.delete(uri, null, null);
8583 }
8584
Narayan Kamath1c496c22015-04-16 14:40:19 +01008585 @Override
chen xu5d3637b2019-01-21 23:31:38 -08008586 public String getSimLocaleForSubscriber(int subId) {
8587 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
8588 final Phone phone = getPhone(subId);
8589 if (phone == null) {
8590 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08008591 return null;
chen xu5d3637b2019-01-21 23:31:38 -08008592 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008593 final long identity = Binder.clearCallingIdentity();
8594 try {
Jack Yu285100e2022-12-02 22:48:35 -08008595 SubscriptionInfo info;
8596 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8597 info = SubscriptionManagerService.getInstance().getActiveSubscriptionInfo(subId,
8598 phone.getContext().getOpPackageName(),
8599 phone.getContext().getAttributionTag());
8600 if (info == null) {
8601 log("getSimLocaleForSubscriber, inactive subId: " + subId);
8602 return null;
8603 }
8604 } else {
8605 info = mSubscriptionController.getActiveSubscriptionInfo(subId,
8606 phone.getContext().getOpPackageName(),
8607 phone.getContext().getAttributionTag());
8608 if (info == null) {
8609 log("getSimLocaleForSubscriber, inactive subId: " + subId);
8610 return null;
8611 }
chen xu6291c472019-02-04 12:55:53 -08008612 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008613 // Try and fetch the locale from the carrier properties or from the SIM language
8614 // preferences (EF-PL and EF-LI)...
8615 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008616 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08008617 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
8618 if (localeFromDefaultSim != null) {
8619 if (!localeFromDefaultSim.getCountry().isEmpty()) {
8620 if (DBG) log("Using locale from subId: " + subId + " locale: "
8621 + localeFromDefaultSim);
tom hsu60a8dc52022-10-27 00:10:04 +08008622 return matchLocaleFromSupportedLocaleList(phone, localeFromDefaultSim);
chen xu5d3637b2019-01-21 23:31:38 -08008623 } else {
8624 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008625 }
8626 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008627
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008628 // The SIM language preferences only store a language (e.g. fr = French), not an
8629 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
8630 // the SIM and carrier preferences does not include a country we add the country
8631 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08008632 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008633 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08008634 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
tom hsu60a8dc52022-10-27 00:10:04 +08008635 return matchLocaleFromSupportedLocaleList(phone, mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008636 }
8637
8638 if (DBG) log("No locale found - returning null");
8639 return null;
8640 } finally {
8641 Binder.restoreCallingIdentity(identity);
8642 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01008643 }
8644
tom hsu0b59d292022-09-29 23:49:21 +08008645 @VisibleForTesting
tom hsu60a8dc52022-10-27 00:10:04 +08008646 String matchLocaleFromSupportedLocaleList(Phone phone, @NonNull Locale inputLocale) {
tom hsu0b59d292022-09-29 23:49:21 +08008647 String[] supportedLocale = com.android.internal.app.LocalePicker.getSupportedLocales(
tom hsu60a8dc52022-10-27 00:10:04 +08008648 phone.getContext());
tom hsu0b59d292022-09-29 23:49:21 +08008649 for (String localeTag : supportedLocale) {
tom hsu60a8dc52022-10-27 00:10:04 +08008650 if (LocaleList.matchesLanguageAndScript(inputLocale, Locale.forLanguageTag(localeTag))
8651 && TextUtils.equals(inputLocale.getCountry(),
tom hsu0b59d292022-09-29 23:49:21 +08008652 Locale.forLanguageTag(localeTag).getCountry())) {
8653 return localeTag;
8654 }
8655 }
8656 return inputLocale.toLanguageTag();
8657 }
8658
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008659 /**
8660 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
8661 */
8662 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Jack Yu285100e2022-12-02 22:48:35 -08008663 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8664 return SubscriptionManagerService.getInstance().getActiveSubscriptionInfoList(
8665 mApp.getOpPackageName(), mApp.getAttributionTag());
8666 }
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008667 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08008668 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01008669 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008670
Gary Jian3aa9a762022-01-24 16:41:19 +08008671 private ActivityStatsTechSpecificInfo[] mLastModemActivitySpecificInfo = null;
8672 private ModemActivityInfo mLastModemActivityInfo = null;
Chenjie Yu1ba97252018-01-11 18:16:20 -08008673
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008674 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07008675 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
8676 * representing the state of the modem.
8677 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08008678 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
8679 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07008680 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008681 */
8682 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07008683 public void requestModemActivityInfo(ResultReceiver result) {
8684 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07008685 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008686
8687 final long identity = Binder.clearCallingIdentity();
8688 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08008689 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008690 } finally {
8691 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08008692 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07008693 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008694
Gary Jian76280a42022-12-07 16:18:33 +08008695 // Checks that ModemActivityInfo is valid. Sleep time and Idle time should be
Siddharth Rayb8114062018-06-17 15:02:38 -07008696 // less than total activity duration.
8697 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
8698 if (info == null) {
8699 return false;
8700 }
8701 int activityDurationMs =
Hall Liu49656c02020-10-09 19:00:11 -07008702 (int) (info.getTimestampMillis() - mLastModemActivityInfo.getTimestampMillis());
Gary Jian76280a42022-12-07 16:18:33 +08008703 activityDurationMs += MODEM_ACTIVITY_TIME_OFFSET_CORRECTION_MS;
8704
Hall Liu49656c02020-10-09 19:00:11 -07008705 int totalTxTimeMs = Arrays.stream(info.getTransmitTimeMillis()).sum();
8706
Siddharth Rayb8114062018-06-17 15:02:38 -07008707 return (info.isValid()
Thomas Nguyen8ee49682023-02-01 11:46:09 -08008708 && (info.getSleepTimeMillis() <= activityDurationMs)
8709 && (info.getIdleTimeMillis() <= activityDurationMs));
Siddharth Rayb8114062018-06-17 15:02:38 -07008710 }
8711
Gary Jian3aa9a762022-01-24 16:41:19 +08008712 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat, int freq) {
8713 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8714 int[] txTimeMs = info.getTransmitTimeMillis(rat, freq);
8715 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat, freq);
8716
8717 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8718 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8719 }
8720
8721 mLastModemActivityInfo.setTransmitTimeMillis(rat, freq, mergedTxTimeMs);
8722 mLastModemActivityInfo.setReceiveTimeMillis(
8723 rat,
8724 freq,
8725 info.getReceiveTimeMillis(rat, freq)
8726 + mLastModemActivityInfo.getReceiveTimeMillis(rat, freq));
8727 }
8728
8729 private void updateLastModemActivityInfo(ModemActivityInfo info, int rat) {
8730 int[] mergedTxTimeMs = new int [ModemActivityInfo.getNumTxPowerLevels()];
8731 int[] txTimeMs = info.getTransmitTimeMillis(rat);
8732 int[] lastModemTxTimeMs = mLastModemActivityInfo.getTransmitTimeMillis(rat);
8733
8734 for (int lvl = 0; lvl < mergedTxTimeMs.length; lvl++) {
8735 mergedTxTimeMs[lvl] = txTimeMs[lvl] + lastModemTxTimeMs[lvl];
8736 }
8737 mLastModemActivityInfo.setTransmitTimeMillis(rat, mergedTxTimeMs);
8738 mLastModemActivityInfo.setReceiveTimeMillis(
8739 rat,
8740 info.getReceiveTimeMillis(rat) + mLastModemActivityInfo.getReceiveTimeMillis(rat));
8741 }
8742
Thomas Nguyen8ee49682023-02-01 11:46:09 -08008743 /**
8744 * Merge this ModemActivityInfo with mLastModemActivitySpecificInfo
8745 * @param info recent ModemActivityInfo
8746 */
Gary Jian3aa9a762022-01-24 16:41:19 +08008747 private void mergeModemActivityInfo(ModemActivityInfo info) {
8748 List<ActivityStatsTechSpecificInfo> merged = new ArrayList<>();
Kai Shi917fdc62022-11-28 14:01:02 -08008749 ActivityStatsTechSpecificInfo deltaSpecificInfo;
Gary Jian3aa9a762022-01-24 16:41:19 +08008750 boolean matched;
8751 for (int i = 0; i < info.getSpecificInfoLength(); i++) {
8752 matched = false;
8753 int rat = info.getSpecificInfoRat(i);
8754 int freq = info.getSpecificInfoFrequencyRange(i);
8755 //Check each ActivityStatsTechSpecificInfo in this ModemActivityInfo for new rat returns
8756 //Add a new ActivityStatsTechSpecificInfo if is a new rat, and merge with the original
8757 //if it already exists
8758 for (int j = 0; j < mLastModemActivitySpecificInfo.length; j++) {
8759 if (rat == mLastModemActivityInfo.getSpecificInfoRat(j) && !matched) {
8760 //Merged based on frequency range (MMWAVE vs SUB6) for 5G
8761 if (rat == AccessNetworkConstants.AccessNetworkType.NGRAN) {
8762 if (freq == mLastModemActivityInfo.getSpecificInfoFrequencyRange(j)) {
8763 updateLastModemActivityInfo(info, rat, freq);
8764 matched = true;
8765 }
8766 } else {
8767 updateLastModemActivityInfo(info, rat);
8768 matched = true;
8769 }
8770 }
8771 }
8772
8773 if (!matched) {
Kai Shi917fdc62022-11-28 14:01:02 -08008774 deltaSpecificInfo =
Gary Jian3aa9a762022-01-24 16:41:19 +08008775 new ActivityStatsTechSpecificInfo(
8776 rat,
8777 freq,
8778 info.getTransmitTimeMillis(rat, freq),
8779 (int) info.getReceiveTimeMillis(rat, freq));
Kai Shi917fdc62022-11-28 14:01:02 -08008780 merged.addAll(Arrays.asList(deltaSpecificInfo));
Gary Jian3aa9a762022-01-24 16:41:19 +08008781 }
8782 }
8783 merged.addAll(Arrays.asList(mLastModemActivitySpecificInfo));
8784 mLastModemActivitySpecificInfo =
8785 new ActivityStatsTechSpecificInfo[merged.size()];
8786 merged.toArray(mLastModemActivitySpecificInfo);
8787
8788 mLastModemActivityInfo.setTimestamp(info.getTimestampMillis());
8789 mLastModemActivityInfo.setSleepTimeMillis(
8790 info.getSleepTimeMillis()
Thomas Nguyen8ee49682023-02-01 11:46:09 -08008791 + mLastModemActivityInfo.getSleepTimeMillis());
Gary Jian3aa9a762022-01-24 16:41:19 +08008792 mLastModemActivityInfo.setIdleTimeMillis(
8793 info.getIdleTimeMillis()
Thomas Nguyen8ee49682023-02-01 11:46:09 -08008794 + mLastModemActivityInfo.getIdleTimeMillis());
Kai Shi917fdc62022-11-28 14:01:02 -08008795
8796 mLastModemActivityInfo =
Thomas Nguyen8ee49682023-02-01 11:46:09 -08008797 new ModemActivityInfo(
8798 mLastModemActivityInfo.getTimestampMillis(),
8799 mLastModemActivityInfo.getSleepTimeMillis(),
8800 mLastModemActivityInfo.getIdleTimeMillis(),
8801 mLastModemActivitySpecificInfo);
Kai Shi917fdc62022-11-28 14:01:02 -08008802 }
8803
8804 private ActivityStatsTechSpecificInfo[] deepCopyModemActivitySpecificInfo(
8805 ActivityStatsTechSpecificInfo[] info) {
8806 int infoSize = info.length;
8807 ActivityStatsTechSpecificInfo[] ret = new ActivityStatsTechSpecificInfo[infoSize];
8808 for (int i = 0; i < infoSize; i++) {
8809 ret[i] = new ActivityStatsTechSpecificInfo(
8810 info[i].getRat(), info[i].getFrequencyRange(),
8811 info[i].getTransmitTimeMillis(),
8812 (int) info[i].getReceiveTimeMillis());
8813 }
8814 return ret;
Gary Jian3aa9a762022-01-24 16:41:19 +08008815 }
8816
Jack Yu85bd38a2015-11-09 11:34:32 -08008817 /**
Jack Yu85bd38a2015-11-09 11:34:32 -08008818 * Returns the service state information on specified subscription.
8819 */
8820 @Override
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008821 public ServiceState getServiceStateForSubscriber(int subId,
8822 boolean renounceFineLocationAccess, boolean renounceCoarseLocationAccess,
8823 String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008824 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008825 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08008826 return null;
8827 }
8828
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008829 boolean hasFinePermission = false;
8830 boolean hasCoarsePermission = false;
8831 if (!renounceFineLocationAccess) {
8832 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
8833 LocationAccessPolicy.checkLocationPermission(mApp,
8834 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8835 .setCallingPackage(callingPackage)
8836 .setCallingFeatureId(callingFeatureId)
8837 .setCallingPid(Binder.getCallingPid())
8838 .setCallingUid(Binder.getCallingUid())
8839 .setMethod("getServiceStateForSubscriber")
8840 .setLogAsInfo(true)
8841 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
8842 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8843 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8844 .build());
8845 hasFinePermission =
8846 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8847 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008848
Sooraj Sasindran2250dc02021-11-10 16:42:01 -08008849 if (!renounceCoarseLocationAccess) {
8850 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
8851 LocationAccessPolicy.checkLocationPermission(mApp,
8852 new LocationAccessPolicy.LocationPermissionQuery.Builder()
8853 .setCallingPackage(callingPackage)
8854 .setCallingFeatureId(callingFeatureId)
8855 .setCallingPid(Binder.getCallingPid())
8856 .setCallingUid(Binder.getCallingUid())
8857 .setMethod("getServiceStateForSubscriber")
8858 .setLogAsInfo(true)
8859 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
8860 .setMinSdkVersionForFine(Integer.MAX_VALUE)
8861 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
8862 .build());
8863 hasCoarsePermission =
8864 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
8865 }
Hall Liuf19c44f2018-11-27 14:38:17 -08008866
Jack Yu479f40e2020-10-27 21:29:25 -07008867 final Phone phone = getPhone(subId);
8868 if (phone == null) {
8869 return null;
8870 }
8871
Jordan Liu0f2bc442020-11-18 16:47:37 -08008872 final long identity = Binder.clearCallingIdentity();
8873
Jack Yu479f40e2020-10-27 21:29:25 -07008874 boolean isCallingPackageDataService = phone.getDataServicePackages()
8875 .contains(callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008876 try {
Jordan Liuc437b192020-08-17 10:59:12 -07008877 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
Jack Yu285100e2022-12-02 22:48:35 -08008878 if (PhoneFactory.isSubscriptionManagerServiceEnabled()) {
8879 SubscriptionInfoInternal subInfo = SubscriptionManagerService.getInstance()
8880 .getSubscriptionInfoInternal(subId);
8881 if (subInfo == null || !subInfo.isActive()) {
8882 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8883 + "subId=" + subId);
8884 return null;
8885 }
8886 } else {
8887 if (!mSubscriptionController.isActiveSubId(subId, callingPackage,
8888 callingFeatureId)) {
8889 Rlog.d(LOG_TAG, "getServiceStateForSubscriber returning null for inactive "
8890 + "subId=" + subId);
8891 return null;
8892 }
Jordan Liuc437b192020-08-17 10:59:12 -07008893 }
8894
Hall Liuf19c44f2018-11-27 14:38:17 -08008895 ServiceState ss = phone.getServiceState();
8896
8897 // Scrub out the location info in ServiceState depending on what level of access
8898 // the caller has.
Jack Yu479f40e2020-10-27 21:29:25 -07008899 if (hasFinePermission || isCallingPackageDataService) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08008900 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
8901 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008902 } finally {
8903 Binder.restoreCallingIdentity(identity);
8904 }
Jack Yu85bd38a2015-11-09 11:34:32 -08008905 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008906
8907 /**
8908 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
8909 *
8910 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8911 * voicemail ringtone.
8912 * @return The URI for the ringtone to play when receiving a voicemail from a specific
8913 * PhoneAccount.
8914 */
8915 @Override
8916 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008917 final long identity = Binder.clearCallingIdentity();
8918 try {
8919 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8920 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008921 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008922 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008923
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008924 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
8925 } finally {
8926 Binder.restoreCallingIdentity(identity);
8927 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008928 }
8929
8930 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008931 * Sets the per-account voicemail ringtone.
8932 *
8933 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8934 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8935 *
8936 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8937 * voicemail ringtone.
8938 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
8939 * PhoneAccount.
8940 */
8941 @Override
8942 public void setVoicemailRingtoneUri(String callingPackage,
8943 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008944 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008945 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07008946 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
8947 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08008948 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8949 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
8950 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008951 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008952
8953 final long identity = Binder.clearCallingIdentity();
8954 try {
8955 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
8956 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008957 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008958 }
8959 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
8960 } finally {
8961 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008962 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008963 }
8964
8965 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08008966 * Returns whether vibration is set for voicemail notification in Phone settings.
8967 *
8968 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
8969 * voicemail vibration setting.
8970 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
8971 */
8972 @Override
8973 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008974 final long identity = Binder.clearCallingIdentity();
8975 try {
8976 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
8977 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008978 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008979 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008980
Malcolm Chenaa4a8532018-02-28 15:00:40 -08008981 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
8982 } finally {
8983 Binder.restoreCallingIdentity(identity);
8984 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08008985 }
8986
Youhan Wange64578a2016-05-02 15:32:42 -07008987 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08008988 * Sets the per-account voicemail vibration.
8989 *
8990 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
8991 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
8992 *
8993 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
8994 * voicemail vibration setting.
8995 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
8996 * specific PhoneAccount.
8997 */
8998 @Override
8999 public void setVoicemailVibrationEnabled(String callingPackage,
9000 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009001 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08009002 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07009003 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
9004 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08009005 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9006 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
9007 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08009008 }
9009
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009010 final long identity = Binder.clearCallingIdentity();
9011 try {
9012 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
9013 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009014 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009015 }
9016 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
9017 } finally {
9018 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08009019 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08009020 }
9021
9022 /**
Youhan Wange64578a2016-05-02 15:32:42 -07009023 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
9024 *
9025 * @throws SecurityException if the caller does not have the required permission
9026 */
arunvoddud7401012022-12-15 16:08:12 +00009027 @VisibleForTesting
9028 public void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07009029 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07009030 message);
Youhan Wange64578a2016-05-02 15:32:42 -07009031 }
9032
9033 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08009034 * Make sure either called from same process as self (phone) or IPC caller has send SMS
9035 * permission.
9036 *
9037 * @throws SecurityException if the caller does not have the required permission
9038 */
9039 private void enforceSendSmsPermission() {
9040 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
9041 }
9042
9043 /**
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +00009044 * Make sure either called from same process as self (phone) or IPC caller has interact across
9045 * users permission.
9046 *
9047 * @throws SecurityException if the caller does not have the required permission
9048 */
9049 private void enforceInteractAcrossUsersPermission(String message) {
9050 mApp.enforceCallingOrSelfPermission(permission.INTERACT_ACROSS_USERS, message);
9051 }
9052
9053 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08009054 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08009055 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08009056 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08009057 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08009058 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009059 final long identity = Binder.clearCallingIdentity();
9060 try {
9061 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009062 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009063 if (componentName == null) {
9064 throw new SecurityException(
9065 "Caller not current active visual voicemail package[null]");
9066 }
9067 String vvmPackage = componentName.getPackageName();
9068 if (!callingPackage.equals(vvmPackage)) {
Hui Wang7f657552022-08-16 16:58:25 +00009069 throw new SecurityException("Caller not current active visual voicemail package");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009070 }
9071 } finally {
9072 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08009073 }
9074 }
9075
9076 /**
Youhan Wange64578a2016-05-02 15:32:42 -07009077 * Return the application ID for the app type.
9078 *
9079 * @param subId the subscription ID that this request applies to.
9080 * @param appType the uicc app type.
9081 * @return Application ID for specificied app type, or null if no uicc.
9082 */
9083 @Override
9084 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009085 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07009086 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009087
9088 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07009089 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009090 if (phone == null) {
9091 return null;
9092 }
9093 String aid = null;
9094 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009095 aid = UiccController.getInstance().getUiccPort(phone.getPhoneId())
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009096 .getApplicationByType(appType).getAid();
9097 } catch (Exception e) {
9098 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
9099 }
9100 return aid;
9101 } finally {
9102 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07009103 }
Youhan Wange64578a2016-05-02 15:32:42 -07009104 }
9105
Youhan Wang4001d252016-05-11 10:29:41 -07009106 /**
9107 * Return the Electronic Serial Number.
9108 *
9109 * @param subId the subscription ID that this request applies to.
9110 * @return ESN or null if error.
9111 */
9112 @Override
9113 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009114 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07009115 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009116
9117 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07009118 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009119 if (phone == null) {
9120 return null;
9121 }
9122 String esn = null;
9123 try {
9124 esn = phone.getEsn();
9125 } catch (Exception e) {
9126 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
9127 }
9128 return esn;
9129 } finally {
9130 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07009131 }
Youhan Wang4001d252016-05-11 10:29:41 -07009132 }
9133
Sanket Padawe99ef1e32016-05-18 16:12:33 -07009134 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07009135 * Return the Preferred Roaming List Version.
9136 *
9137 * @param subId the subscription ID that this request applies to.
9138 * @return PRLVersion or null if error.
9139 */
9140 @Override
9141 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009142 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07009143 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009144
9145 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07009146 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009147 if (phone == null) {
9148 return null;
9149 }
9150 String cdmaPrlVersion = null;
9151 try {
9152 cdmaPrlVersion = phone.getCdmaPrlVersion();
9153 } catch (Exception e) {
9154 Log.e(LOG_TAG, "Not getting PRLVersion", e);
9155 }
9156 return cdmaPrlVersion;
9157 } finally {
9158 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07009159 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07009160 }
9161
9162 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07009163 * Get snapshot of Telephony histograms
9164 * @return List of Telephony histograms
9165 * @hide
9166 */
9167 @Override
9168 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08009169 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9170 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009171
9172 final long identity = Binder.clearCallingIdentity();
9173 try {
9174 return RIL.getTelephonyRILTimingHistograms();
9175 } finally {
9176 Binder.restoreCallingIdentity(identity);
9177 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07009178 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07009179
9180 /**
9181 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08009182 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
9183 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07009184 * Require system privileges. In the future we may add this to carrier APIs.
9185 *
Michele Berionne482f8202018-11-27 18:57:59 -08009186 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07009187 */
9188 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08009189 @TelephonyManager.SetCarrierRestrictionResult
9190 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07009191 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07009192 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07009193
Michele Berionne482f8202018-11-27 18:57:59 -08009194 if (carrierRestrictionRules == null) {
9195 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08009196 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07009197
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009198 final long identity = Binder.clearCallingIdentity();
9199 try {
Michele Berionne482f8202018-11-27 18:57:59 -08009200 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07009201 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009202 } finally {
9203 Binder.restoreCallingIdentity(identity);
9204 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07009205 }
9206
9207 /**
9208 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08009209 * Get the allowed carrier list and the excluded carrier list, including the priority between
9210 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07009211 * Require system privileges. In the future we may add this to carrier APIs.
9212 *
Michele Berionne482f8202018-11-27 18:57:59 -08009213 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07009214 */
9215 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08009216 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009217 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07009218 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009219
9220 final long identity = Binder.clearCallingIdentity();
9221 try {
Michele Berionne482f8202018-11-27 18:57:59 -08009222 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
9223 if (response instanceof CarrierRestrictionRules) {
9224 return (CarrierRestrictionRules) response;
9225 }
9226 // Response is an Exception of some kind,
9227 // which is signalled to the user as a NULL retval
9228 return null;
9229 } catch (Exception e) {
9230 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
9231 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009232 } finally {
9233 Binder.restoreCallingIdentity(identity);
9234 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07009235 }
9236
fionaxu59545b42016-05-25 15:53:37 -07009237 /**
arunvoddud7401012022-12-15 16:08:12 +00009238 * Fetches the carrier restriction status of the device and sends the status to the caller
9239 * through the callback.
9240 *
9241 * @param callback The callback that will be used to send the result.
9242 * @throws SecurityException if the caller does not have the required permission/privileges or
9243 * the caller is not allowlisted.
9244 */
9245 @Override
9246 public void getCarrierRestrictionStatus(IIntegerConsumer callback, String packageName) {
9247 enforceReadPermission("getCarrierRestrictionStatus");
9248 int carrierId = validateCallerAndGetCarrierId(packageName);
9249 if (carrierId == CarrierAllowListInfo.INVALID_CARRIER_ID) {
9250 Rlog.e(LOG_TAG, "getCarrierRestrictionStatus: caller is not registered");
9251 throw new SecurityException("Not an authorized caller");
9252 }
9253 final long identity = Binder.clearCallingIdentity();
9254 try {
9255 Consumer<Integer> consumer = FunctionalUtils.ignoreRemoteException(callback::accept);
9256 CallerCallbackInfo callbackInfo = new CallerCallbackInfo(consumer, carrierId);
9257 sendRequestAsync(CMD_GET_ALLOWED_CARRIERS, callbackInfo);
9258 } finally {
9259 Binder.restoreCallingIdentity(identity);
9260 }
9261 }
9262
9263 @VisibleForTesting
9264 public int validateCallerAndGetCarrierId(String packageName) {
9265 CarrierAllowListInfo allowListInfo = CarrierAllowListInfo.loadInstance(mApp);
9266 return allowListInfo.validateCallerAndGetCarrierId(packageName);
9267 }
9268
9269 /**
fionaxu59545b42016-05-25 15:53:37 -07009270 * Action set from carrier signalling broadcast receivers to enable/disable radio
9271 * @param subId the subscription ID that this action applies to.
9272 * @param enabled control enable or disable radio.
9273 * {@hide}
9274 */
9275 @Override
9276 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
9277 enforceModifyPermission();
9278 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009279
9280 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07009281 if (phone == null) {
9282 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
9283 return;
9284 }
9285 try {
9286 phone.carrierActionSetRadioEnabled(enabled);
9287 } catch (Exception e) {
9288 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009289 } finally {
9290 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07009291 }
9292 }
9293
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07009294 /**
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07009295 * Enable or disable Voice over NR (VoNR)
9296 * @param subId the subscription ID that this action applies to.
9297 * @param enabled enable or disable VoNR.
9298 * @return operation result.
9299 */
9300 @Override
9301 public int setVoNrEnabled(int subId, boolean enabled) {
9302 enforceModifyPermission();
9303 final Phone phone = getPhone(subId);
9304
9305 final long identity = Binder.clearCallingIdentity();
9306 if (phone == null) {
9307 loge("setVoNrEnabled fails with no phone object for subId: " + subId);
9308 return TelephonyManager.ENABLE_VONR_RADIO_NOT_AVAILABLE;
9309 }
9310
9311 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9312 try {
9313 int result = (int) sendRequest(CMD_ENABLE_VONR, enabled, subId,
9314 workSource);
9315 if (DBG) log("setVoNrEnabled result: " + result);
Gary Jian8dd305f2021-10-14 16:31:35 +08009316
9317 if (result == TelephonyManager.ENABLE_VONR_SUCCESS) {
9318 if (DBG) {
9319 log("Set VoNR settings in siminfo db; subId=" + subId + ", value:" + enabled);
9320 }
9321 SubscriptionManager.setSubscriptionProperty(
9322 subId, SubscriptionManager.NR_ADVANCED_CALLING_ENABLED,
9323 (enabled ? "1" : "0"));
9324 }
9325
Sooraj Sasindrandaf060f2021-06-15 14:52:55 -07009326 return result;
9327 } finally {
9328 Binder.restoreCallingIdentity(identity);
9329 }
9330 }
9331
9332 /**
9333 * Is voice over NR enabled
9334 * @return true if VoNR is enabled else false
9335 */
9336 @Override
9337 public boolean isVoNrEnabled(int subId) {
9338 enforceReadPrivilegedPermission("isVoNrEnabled");
9339 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9340 final long identity = Binder.clearCallingIdentity();
9341 try {
9342 boolean isEnabled = (boolean) sendRequest(CMD_IS_VONR_ENABLED,
9343 null, subId, workSource);
9344 if (DBG) log("isVoNrEnabled: " + isEnabled);
9345 return isEnabled;
9346 } finally {
9347 Binder.restoreCallingIdentity(identity);
9348 }
9349 }
9350
9351 /**
fionaxu8da9cb12017-05-23 15:02:46 -07009352 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
9353 * network status based on which carrier apps could apply actions accordingly,
9354 * enable/disable default url handler for example.
9355 *
9356 * @param subId the subscription ID that this action applies to.
9357 * @param report control start/stop reporting the default network status.
9358 * {@hide}
9359 */
9360 @Override
9361 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
9362 enforceModifyPermission();
9363 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009364
9365 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07009366 if (phone == null) {
9367 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
9368 return;
9369 }
9370 try {
9371 phone.carrierActionReportDefaultNetworkStatus(report);
9372 } catch (Exception e) {
9373 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009374 } finally {
9375 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07009376 }
9377 }
9378
9379 /**
fionaxud9622282017-07-17 17:51:30 -07009380 * Action set from carrier signalling broadcast receivers to reset all carrier actions
9381 * @param subId the subscription ID that this action applies to.
9382 * {@hide}
9383 */
9384 @Override
9385 public void carrierActionResetAll(int subId) {
9386 enforceModifyPermission();
9387 final Phone phone = getPhone(subId);
9388 if (phone == null) {
9389 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
9390 return;
9391 }
9392 try {
9393 phone.carrierActionResetAll();
9394 } catch (Exception e) {
9395 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
9396 }
9397 }
9398
9399 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07009400 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
9401 * bug report is being generated.
9402 */
9403 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07009404 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009405 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
9406 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07009407 writer.println("Permission Denial: can't dump Phone from pid="
9408 + Binder.getCallingPid()
9409 + ", uid=" + Binder.getCallingUid()
9410 + "without permission "
9411 + android.Manifest.permission.DUMP);
9412 return;
9413 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009414 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07009415 }
Jack Yueb89b242016-06-22 13:27:47 -07009416
Brad Ebingerdac2f002018-04-03 15:17:52 -07009417 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08009418 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
9419 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
9420 @NonNull String[] args) {
9421 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
9422 this, in.getFileDescriptor(), out.getFileDescriptor(),
Thomas Nguyen8ee49682023-02-01 11:46:09 -08009423 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07009424 }
9425
Jack Yueb89b242016-06-22 13:27:47 -07009426 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009427 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00009428 * @param subId Subscription index
Sarah Chinecc78c42022-03-31 21:16:48 -07009429 * @param reason The reason the data enable change is taking place.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009430 * @param enabled True if enabling the data, otherwise disabling.
Sarah Chinecc78c42022-03-31 21:16:48 -07009431 * @param callingPackage The package that changed the data enabled state.
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009432 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07009433 */
9434 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07009435 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sarah Chinecc78c42022-03-31 21:16:48 -07009436 boolean enabled, String callingPackage) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009437 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
9438 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
9439 try {
9440 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07009441 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009442 } catch (SecurityException se) {
9443 enforceModifyPermission();
9444 }
9445 } else {
9446 enforceModifyPermission();
9447 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009448
9449 final long identity = Binder.clearCallingIdentity();
9450 try {
9451 Phone phone = getPhone(subId);
9452 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009453 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
9454 phone.carrierActionSetMeteredApnsEnabled(enabled);
9455 } else {
Jack Yu7968c6d2022-07-31 00:43:21 -07009456 phone.getDataSettingsManager().setDataEnabled(
9457 reason, enabled, callingPackage);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00009458 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009459 }
9460 } finally {
9461 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07009462 }
9463 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009464
9465 /**
9466 * Get Client request stats
9467 * @return List of Client Request Stats
9468 * @hide
9469 */
9470 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009471 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
9472 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08009473 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009474 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009475 return null;
9476 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009477 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009478
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009479 final long identity = Binder.clearCallingIdentity();
9480 try {
9481 if (phone != null) {
9482 return phone.getClientRequestStats();
9483 }
9484
9485 return null;
9486 } finally {
9487 Binder.restoreCallingIdentity(identity);
9488 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009489 }
9490
Narayan Kamathf04b5a12018-01-09 11:47:15 +00009491 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009492 String packageName = mApp.getPackageManager().getNameForUid(uid);
Hunter Knepshieldd03383b2022-03-29 22:47:54 +00009493 if (uid == Process.ROOT_UID && packageName == null) {
9494 // Downstream WorkSource attribution inside the RIL requires both a UID and package name
9495 // to be set for wakelock tracking, otherwise RIL requests fail with a runtime
9496 // exception. ROOT_UID seems not to have a valid package name returned by
9497 // PackageManager, so just fake it here to avoid issues when running telephony shell
9498 // commands that plumb through the RIL as root, like so:
9499 // $ adb root
9500 // $ adb shell cmd phone ...
9501 packageName = "root";
9502 }
Narayan Kamathf04b5a12018-01-09 11:47:15 +00009503 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07009504 }
Jack Yueb4124c2017-02-16 15:32:43 -08009505
9506 /**
Grace Chen70990072017-03-24 17:21:30 -07009507 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08009508 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07009509 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07009510 * @param state State of SIM (power down, power up, pass through)
9511 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
9512 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
9513 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08009514 *
9515 **/
9516 @Override
Grace Chen70990072017-03-24 17:21:30 -07009517 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08009518 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07009519 Phone phone = PhoneFactory.getPhone(slotIndex);
9520
vagdeviaf9a5b92018-08-15 16:01:53 -07009521 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9522
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009523 final long identity = Binder.clearCallingIdentity();
9524 try {
9525 if (phone != null) {
Jordan Liu109698e2020-11-24 14:50:34 -08009526 phone.setSimPowerState(state, null, workSource);
9527 }
9528 } finally {
9529 Binder.restoreCallingIdentity(identity);
9530 }
9531 }
9532
9533 /**
9534 * Set SIM card power state.
9535 *
9536 * @param slotIndex SIM slot id.
9537 * @param state State of SIM (power down, power up, pass through)
9538 * @param callback callback to trigger after success or failure
9539 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
9540 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
9541 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
9542 *
9543 **/
9544 @Override
9545 public void setSimPowerStateForSlotWithCallback(int slotIndex, int state,
9546 IIntegerConsumer callback) {
9547 enforceModifyPermission();
9548 Phone phone = PhoneFactory.getPhone(slotIndex);
9549
9550 WorkSource workSource = getWorkSource(Binder.getCallingUid());
9551
9552 final long identity = Binder.clearCallingIdentity();
9553 try {
9554 if (phone != null) {
9555 Pair<Integer, IIntegerConsumer> arguments = Pair.create(state, callback);
9556 sendRequestAsync(CMD_SET_SIM_POWER, arguments, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009557 }
9558 } finally {
9559 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08009560 }
9561 }
Shuo Qiandd210312017-04-12 22:11:33 +00009562
Tyler Gunn65d45c22017-06-05 11:22:26 -07009563 private boolean isUssdApiAllowed(int subId) {
9564 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08009565 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07009566 if (configManager == null) {
9567 return false;
9568 }
9569 PersistableBundle pb = configManager.getConfigForSubId(subId);
9570 if (pb == null) {
9571 return false;
9572 }
9573 return pb.getBoolean(
9574 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
9575 }
9576
Shuo Qiandd210312017-04-12 22:11:33 +00009577 /**
9578 * Check if phone is in emergency callback mode
9579 * @return true if phone is in emergency callback mode
9580 * @param subId sub id
9581 */
goneil9c5f4872017-12-05 14:07:56 -08009582 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00009583 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07009584 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00009585 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009586
9587 final long identity = Binder.clearCallingIdentity();
9588 try {
9589 if (phone != null) {
9590 return phone.isInEcm();
9591 } else {
9592 return false;
9593 }
9594 } finally {
9595 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00009596 }
9597 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08009598
9599 /**
9600 * Get the current signal strength information for the given subscription.
9601 * Because this information is not updated when the device is in a low power state
9602 * it should not be relied-upon to be current.
9603 * @param subId Subscription index
9604 * @return the most recent cached signal strength info from the modem
9605 */
9606 @Override
9607 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009608 final long identity = Binder.clearCallingIdentity();
9609 try {
9610 Phone p = getPhone(subId);
9611 if (p == null) {
9612 return null;
9613 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08009614
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009615 return p.getSignalStrength();
9616 } finally {
9617 Binder.restoreCallingIdentity(identity);
9618 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08009619 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009620
Pengquan Meng77b7f132018-08-22 14:49:57 -07009621 /**
Chen Xuf792fd62018-10-17 17:54:36 +00009622 * Get the current modem radio state for the given slot.
9623 * @param slotIndex slot index.
9624 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009625 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00009626 * @return the current radio power state from the modem
9627 */
9628 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009629 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00009630 Phone phone = PhoneFactory.getPhone(slotIndex);
9631 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07009632 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
9633 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00009634 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
9635 }
9636
9637 final long identity = Binder.clearCallingIdentity();
9638 try {
9639 return phone.getRadioPowerState();
9640 } finally {
9641 Binder.restoreCallingIdentity(identity);
9642 }
9643 }
9644 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
9645 }
9646
9647 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07009648 * Checks if data roaming is enabled on the subscription with id {@code subId}.
9649 *
9650 * <p>Requires one of the following permissions:
9651 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009652 * {@link android.Manifest.permission#READ_BASIC_PHONE_STATE},
Pengquan Meng77b7f132018-08-22 14:49:57 -07009653 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
9654 * privileges.
9655 *
9656 * @param subId subscription id
9657 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
9658 * {@code false}.
9659 */
9660 @Override
9661 public boolean isDataRoamingEnabled(int subId) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009662 String functionName = "isDataRoamingEnabled";
Shuo Qian093013d2020-08-13 15:42:55 -07009663 try {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009664 try {
9665 mApp.enforceCallingOrSelfPermission(
9666 android.Manifest.permission.ACCESS_NETWORK_STATE,
9667 functionName);
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07009668 } catch (SecurityException e) {
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009669 mApp.enforceCallingOrSelfPermission(
9670 permission.READ_BASIC_PHONE_STATE, functionName);
9671 }
Sooraj Sasindran12545cc2022-08-22 14:30:25 -07009672 } catch (SecurityException e) {
Nathan Harold62c68512021-04-06 11:26:02 -07009673 TelephonyPermissions.enforceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
Sooraj Sasindran5d051bf2021-11-05 13:14:15 -07009674 mApp, subId, functionName);
Shuo Qian093013d2020-08-13 15:42:55 -07009675 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07009676
Pengquan Menga1bb6272018-09-06 09:59:22 -07009677 boolean isEnabled = false;
9678 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07009679 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009680 Phone phone = getPhone(subId);
9681 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07009682 } finally {
9683 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009684 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07009685 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07009686 }
9687
9688
9689 /**
9690 * Enables/Disables the data roaming on the subscription with id {@code subId}.
9691 *
9692 * <p> Requires permission:
9693 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
9694 * privileges.
9695 *
9696 * @param subId subscription id
9697 * @param isEnabled {@code true} means enable, {@code false} means disable.
9698 */
9699 @Override
9700 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07009701 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
9702 mApp, subId, "setDataRoamingEnabled");
9703
Pengquan Menga1bb6272018-09-06 09:59:22 -07009704 final long identity = Binder.clearCallingIdentity();
9705 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07009706 Phone phone = getPhone(subId);
9707 if (phone != null) {
9708 phone.setDataRoamingEnabled(isEnabled);
9709 }
9710 } finally {
9711 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07009712 }
9713 }
9714
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009715 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009716 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08009717 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -07009718 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Thomas Nguyen8ee49682023-02-01 11:46:09 -08009719 mApp, subId, "isManualNetworkSelectionAllowed");
Pengquan Meng44e66f12019-04-01 10:48:20 -07009720
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009721 boolean isAllowed = true;
9722 final long identity = Binder.clearCallingIdentity();
9723 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009724 Phone phone = getPhone(subId);
9725 if (phone != null) {
9726 isAllowed = phone.isCspPlmnEnabled();
9727 }
9728 } finally {
9729 Binder.restoreCallingIdentity(identity);
9730 }
9731 return isAllowed;
9732 }
9733
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009734 private boolean haveCarrierPrivilegeAccess(UiccPort port, String callingPackage) {
9735 UiccProfile profile = port.getUiccProfile();
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009736 if (profile == null) {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009737 return false;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009738 }
Hunter Knepshieldcad7f0b2021-12-13 15:08:35 -08009739 Phone phone = PhoneFactory.getPhone(profile.getPhoneId());
9740 if (phone == null) {
9741 return false;
9742 }
9743 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
9744 return cpt != null && cpt.getCarrierPrivilegeStatusForPackage(callingPackage)
9745 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +00009746 }
9747
Pengquan Meng6884a2c2018-10-03 12:19:13 -07009748 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08009749 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009750 // Verify that the callingPackage belongs to the calling UID
Jordan Liu4cda4552020-03-23 11:55:07 -07009751 mApp.getSystemService(AppOpsManager.class)
9752 .checkPackage(Binder.getCallingUid(), callingPackage);
9753
Jordan Liu1e142fc2019-04-22 15:10:43 -07009754 boolean hasReadPermission = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009755 boolean isIccIdAccessRestricted = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08009756 try {
9757 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07009758 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08009759 } catch (SecurityException e) {
9760 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
9761 // has carrier privileges on an active UICC
Rambo Wange7209ce2022-02-23 13:41:02 -08009762 if (checkCarrierPrivilegesForPackageAnyPhoneWithPermission(callingPackage)
Thomas Nguyen8ee49682023-02-01 11:46:09 -08009763 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07009764 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08009765 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08009766 }
sandeepjsb6c87872021-09-27 15:34:44 +00009767 // checking compatibility, if calling app's target SDK is T and beyond.
9768 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9769 Binder.getCallingUid())) {
9770 isIccIdAccessRestricted = true;
9771 }
Jordan Liu5aa07002018-12-18 15:44:48 -08009772 final long identity = Binder.clearCallingIdentity();
9773 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08009774 UiccController uiccController = UiccController.getInstance();
9775 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07009776 if (hasReadPermission) {
9777 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08009778 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07009779
9780 // Remove private info if the caller doesn't have access
9781 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
9782 for (UiccCardInfo cardInfo : cardInfos) {
sandeepjsb6c87872021-09-27 15:34:44 +00009783 //setting the value after compatibility check
9784 cardInfo.setIccIdAccessRestricted(isIccIdAccessRestricted);
Jordan Liu1e142fc2019-04-22 15:10:43 -07009785 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
9786 // is available
sandeepjsb6c87872021-09-27 15:34:44 +00009787 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getPhysicalSlotIndex());
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009788 if (card == null) {
9789 // assume no access if the card is unavailable
sandeepjsb6c87872021-09-27 15:34:44 +00009790 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009791 continue;
9792 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009793 Collection<UiccPortInfo> portInfos = cardInfo.getPorts();
9794 if (portInfos.isEmpty()) {
sandeepjsb6c87872021-09-27 15:34:44 +00009795 filteredInfos.add(getUiccCardInfoUnPrivileged(cardInfo));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009796 continue;
Jordan Liu1e142fc2019-04-22 15:10:43 -07009797 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009798 List<UiccPortInfo> uiccPortInfos = new ArrayList<>();
9799 for (UiccPortInfo portInfo : portInfos) {
9800 UiccPort port = uiccController.getUiccPortForSlot(
9801 cardInfo.getPhysicalSlotIndex(), portInfo.getPortIndex());
9802 if (port == null) {
9803 // assume no access if port is null
9804 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9805 continue;
9806 }
9807 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9808 uiccPortInfos.add(portInfo);
9809 } else {
9810 uiccPortInfos.add(getUiccPortInfoUnPrivileged(portInfo));
9811 }
9812 }
9813 filteredInfos.add(new UiccCardInfo(
9814 cardInfo.isEuicc(),
9815 cardInfo.getCardId(),
9816 null,
9817 cardInfo.getPhysicalSlotIndex(),
9818 cardInfo.isRemovable(),
9819 cardInfo.isMultipleEnabledProfilesSupported(),
9820 uiccPortInfos));
Jordan Liu1e142fc2019-04-22 15:10:43 -07009821 }
9822 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08009823 } finally {
9824 Binder.restoreCallingIdentity(identity);
9825 }
9826 }
9827
sandeepjsb6c87872021-09-27 15:34:44 +00009828 /**
9829 * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
9830 * generally private and require carrier privileges to view.
9831 *
9832 * @hide
9833 */
9834 @NonNull
9835 public UiccCardInfo getUiccCardInfoUnPrivileged(UiccCardInfo cardInfo) {
9836 List<UiccPortInfo> portinfo = new ArrayList<>();
9837 for (UiccPortInfo portinfos : cardInfo.getPorts()) {
9838 portinfo.add(getUiccPortInfoUnPrivileged(portinfos));
9839 }
9840 return new UiccCardInfo(
9841 cardInfo.isEuicc(),
9842 cardInfo.getCardId(),
9843 null,
9844 cardInfo.getPhysicalSlotIndex(),
9845 cardInfo.isRemovable(),
9846 cardInfo.isMultipleEnabledProfilesSupported(),
9847 portinfo
9848 );
9849 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009850
sandeepjsb6c87872021-09-27 15:34:44 +00009851 /**
9852 * @hide
9853 * @return a copy of the UiccPortInfo with ICCID set to {@link UiccPortInfo#ICCID_REDACTED}.
9854 * These values are generally private and require carrier privileges to view.
9855 */
9856 @NonNull
9857 public UiccPortInfo getUiccPortInfoUnPrivileged(UiccPortInfo portInfo) {
9858 return new UiccPortInfo(
9859 UiccPortInfo.ICCID_REDACTED,
9860 portInfo.getPortIndex(),
9861 portInfo.getLogicalSlotIndex(),
9862 portInfo.isActive()
9863 );
9864 }
9865 @Override
9866 public UiccSlotInfo[] getUiccSlotsInfo(String callingPackage) {
sandeepjsa208e3b2021-11-17 04:05:58 +00009867 // Verify that the callingPackage belongs to the calling UID
sandeepjsb6c87872021-09-27 15:34:44 +00009868 mApp.getSystemService(AppOpsManager.class)
9869 .checkPackage(Binder.getCallingUid(), callingPackage);
9870
sandeepjsb6c87872021-09-27 15:34:44 +00009871 boolean isLogicalSlotAccessRestricted = false;
sandeepjsb6c87872021-09-27 15:34:44 +00009872
Aman Guptaf3c90b32022-03-17 04:54:16 +00009873 // This will make sure caller has the READ_PRIVILEGED_PHONE_STATE. Do not remove this as
9874 // we are reading iccId which is PII data.
9875 enforceReadPrivilegedPermission("getUiccSlotsInfo");
sandeepjsb6c87872021-09-27 15:34:44 +00009876
9877 // checking compatibility, if calling app's target SDK is T and beyond.
9878 if (CompatChanges.isChangeEnabled(GET_API_SIGNATURES_FROM_UICC_PORT_INFO,
9879 Binder.getCallingUid())) {
9880 isLogicalSlotAccessRestricted = true;
9881 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009882 final long identity = Binder.clearCallingIdentity();
9883 try {
9884 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
Muralidhar Reddyd196bbf2022-01-17 17:56:30 +00009885 if (slots == null || slots.length == 0) {
9886 Rlog.i(LOG_TAG, "slots is null or empty.");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009887 return null;
9888 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009889 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
9890 for (int i = 0; i < slots.length; i++) {
9891 UiccSlot slot = slots[i];
9892 if (slot == null) {
9893 continue;
9894 }
9895
Jordan Liu7be7e652019-05-06 18:55:02 +00009896 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009897 UiccCard card = slot.getUiccCard();
9898 if (card != null) {
9899 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00009900 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07009901 cardId = slot.getEid();
9902 if (TextUtils.isEmpty(cardId)) {
Aman Guptaf3c90b32022-03-17 04:54:16 +00009903 // If cardId is null, use iccId of default port as cardId.
9904 cardId = slot.getIccId(TelephonyManager.DEFAULT_PORT_INDEX);
Jordan Liu01bd00d2019-09-12 16:19:43 -07009905 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009906 }
9907
Jordan Liu857451f2019-05-09 16:35:35 -07009908 if (cardId != null) {
9909 // if cardId is an ICCID, strip off trailing Fs before exposing to user
9910 // if cardId is an EID, it's all digits so this is fine
9911 cardId = IccUtils.stripTrailingFs(cardId);
9912 }
9913
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009914 int cardState = 0;
9915 switch (slot.getCardState()) {
9916 case CARDSTATE_ABSENT:
9917 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
9918 break;
9919 case CARDSTATE_PRESENT:
9920 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
9921 break;
9922 case CARDSTATE_ERROR:
9923 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
9924 break;
9925 case CARDSTATE_RESTRICTED:
9926 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
9927 break;
9928 default:
9929 break;
9930
9931 }
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009932 List<UiccPortInfo> portInfos = new ArrayList<>();
9933 int[] portIndexes = slot.getPortList();
9934 for (int portIdx : portIndexes) {
9935 String iccId = IccUtils.stripTrailingFs(getIccId(slot, portIdx,
Aman Guptaf3c90b32022-03-17 04:54:16 +00009936 callingPackage, /* hasReadPermission= */ true));
Muralidhar Reddyfbcff0c2022-01-19 13:07:57 +00009937 portInfos.add(new UiccPortInfo(iccId, portIdx,
9938 slot.getPhoneIdFromPortIndex(portIdx), slot.isPortActive(portIdx)));
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009939 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009940 infos[i] = new UiccSlotInfo(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009941 slot.isEuicc(),
9942 cardId,
9943 cardState,
Jordan Liua2619582019-02-14 12:56:40 -08009944 slot.isExtendedApduSupported(),
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009945 slot.isRemovable(), portInfos);
sandeepjsb6c87872021-09-27 15:34:44 +00009946 //setting the value after compatibility check
9947 infos[i].setLogicalSlotAccessRestricted(isLogicalSlotAccessRestricted);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009948 }
9949 return infos;
9950 } finally {
9951 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07009952 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009953 }
9954
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009955 /* Returns null if doesn't have read permission or carrier privilege access. */
9956 private String getIccId(UiccSlot slot, int portIndex, String callingPackage,
9957 boolean hasReadPermission) {
9958 String iccId = slot.getIccId(portIndex);
9959 if (hasReadPermission) { // if has read permission
9960 return iccId;
9961 } else {
9962 if (slot.getUiccCard() != null && slot.getUiccCard().getUiccPort(portIndex) != null) {
9963 UiccPort port = slot.getUiccCard().getUiccPort(portIndex);
9964 // if no read permission, checking carrier privilege access
9965 if (haveCarrierPrivilegeAccess(port, callingPackage)) {
9966 return iccId;
9967 }
9968 }
9969 }
9970 // No read permission or carrier privilege access.
9971 return UiccPortInfo.ICCID_REDACTED;
9972 }
9973
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009974 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009975 @Deprecated
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009976 public boolean switchSlots(int[] physicalSlots) {
9977 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009978
9979 final long identity = Binder.clearCallingIdentity();
9980 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +00009981 List<UiccSlotMapping> slotMappings = new ArrayList<>();
9982 for (int i = 0; i < physicalSlots.length; i++) {
9983 // Deprecated API, hence MEP is not supported. Adding default portIndex 0.
9984 slotMappings.add(new UiccSlotMapping(TelephonyManager.DEFAULT_PORT_INDEX,
9985 physicalSlots[i], i));
9986 }
9987 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMappings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08009988 } finally {
9989 Binder.restoreCallingIdentity(identity);
9990 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00009991 }
Jack Yu4c988042018-02-27 15:30:01 -08009992
9993 @Override
sandeepjsb6c87872021-09-27 15:34:44 +00009994 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
9995 public boolean setSimSlotMapping(@NonNull List<UiccSlotMapping> slotMapping) {
9996 enforceModifyPermission();
9997
9998 final long identity = Binder.clearCallingIdentity();
9999 try {
Muralidhar Reddyeb809e32021-11-19 03:07:54 +000010000 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, slotMapping);
sandeepjsb6c87872021-09-27 15:34:44 +000010001 } finally {
10002 Binder.restoreCallingIdentity(identity);
10003 }
10004 }
10005
10006 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -080010007 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -080010008 final long identity = Binder.clearCallingIdentity();
10009 try {
10010 return UiccController.getInstance().getCardIdForDefaultEuicc();
10011 } finally {
10012 Binder.restoreCallingIdentity(identity);
10013 }
10014 }
10015
Pengquan Meng85728fb2018-03-12 16:31:21 -070010016 /**
goneil47ffb6e2018-04-06 15:40:58 -070010017 * A test API to reload the UICC profile.
10018 *
10019 * <p>Requires that the calling app has permission
10020 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
10021 * @hide
10022 */
10023 @Override
10024 public void refreshUiccProfile(int subId) {
10025 enforceModifyPermission();
10026
10027 final long identity = Binder.clearCallingIdentity();
10028 try {
10029 Phone phone = getPhone(subId);
10030 if (phone == null) {
10031 return;
10032 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000010033 UiccPort uiccPort = phone.getUiccPort();
10034 if (uiccPort == null) {
goneil47ffb6e2018-04-06 15:40:58 -070010035 return;
10036 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000010037 UiccProfile uiccProfile = uiccPort.getUiccProfile();
goneil47ffb6e2018-04-06 15:40:58 -070010038 if (uiccProfile == null) {
10039 return;
10040 }
10041 uiccProfile.refresh();
10042 } finally {
10043 Binder.restoreCallingIdentity(identity);
10044 }
10045 }
10046
10047 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -070010048 * Returns false if the mobile data is disabled by default, otherwise return true.
10049 */
10050 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +090010051 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -070010052 }
10053
10054 /**
10055 * Returns true if the data roaming is enabled by default, i.e the system property
10056 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
10057 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
10058 */
10059 private boolean getDefaultDataRoamingEnabled(int subId) {
10060 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -080010061 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -070010062 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -070010063 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
10064 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
10065 return isDataRoamingEnabled;
10066 }
10067
10068 /**
10069 * Returns the default network type for the given {@code subId}, if the default network type is
10070 * not set, return {@link Phone#PREFERRED_NT_MODE}.
10071 */
10072 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +090010073 List<Integer> list = TelephonyProperties.default_network();
Jack Yu285100e2022-12-02 22:48:35 -080010074 int phoneId = SubscriptionManager.getPhoneId(subId);
Inseob Kim14bb3d02018-12-13 17:11:34 +090010075 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
10076 return list.get(phoneId);
10077 }
10078 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -070010079 }
fionaxua13278b2018-03-21 00:08:13 -070010080
10081 @Override
10082 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -070010083 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -070010084 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -080010085
10086 final long identity = Binder.clearCallingIdentity();
10087 try {
10088 final Phone phone = getPhone(subId);
10089 if (phone == null) {
10090 loge("setCarrierTestOverride fails with invalid subId: " + subId);
10091 return;
10092 }
Rambo Wang9c9ffdd2022-01-13 21:51:44 -080010093 CarrierPrivilegesTracker cpt = phone.getCarrierPrivilegesTracker();
10094 if (cpt != null) {
10095 cpt.setTestOverrideCarrierPrivilegeRules(carrierPrivilegeRules);
10096 }
10097 // TODO(b/211796398): remove the legacy logic below once CPT migration is done.
chen xueaba88a2019-03-15 13:15:10 -070010098 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
10099 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -070010100 if (carrierPrivilegeRules == null) {
10101 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
10102 } else {
10103 mCarrierPrivilegeTestOverrideSubIds.add(subId);
10104 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -080010105 } finally {
10106 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -070010107 }
fionaxua13278b2018-03-21 00:08:13 -070010108 }
10109
10110 @Override
10111 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -070010112 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -080010113
10114 final long identity = Binder.clearCallingIdentity();
10115 try {
10116 final Phone phone = getPhone(subId);
10117 if (phone == null) {
10118 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
10119 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
10120 }
10121 return phone.getCarrierIdListVersion();
10122 } finally {
10123 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -070010124 }
fionaxua13278b2018-03-21 00:08:13 -070010125 }
Malcolm Chen2c63d402018-08-14 16:00:53 -070010126
10127 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010128 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
10129 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -070010130 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010131 mApp, subId, callingPackage, callingFeatureId,
10132 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -070010133 return -1;
10134 }
10135
10136 final long identity = Binder.clearCallingIdentity();
10137 try {
10138 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
10139 } finally {
10140 Binder.restoreCallingIdentity(identity);
10141 }
10142 }
Pengquan Menga1bb6272018-09-06 09:59:22 -070010143
10144 @Override
10145 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +080010146 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010147 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Thomas Nguyen8ee49682023-02-01 11:46:09 -080010148 mApp, subId, "getCdmaRoamingMode");
Pengquan Menga1bb6272018-09-06 09:59:22 -070010149
10150 final long identity = Binder.clearCallingIdentity();
10151 try {
10152 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
10153 } finally {
10154 Binder.restoreCallingIdentity(identity);
10155 }
10156 }
10157
10158 @Override
10159 public boolean setCdmaRoamingMode(int subId, int mode) {
10160 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10161 mApp, subId, "setCdmaRoamingMode");
10162
10163 final long identity = Binder.clearCallingIdentity();
10164 try {
10165 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
10166 } finally {
10167 Binder.restoreCallingIdentity(identity);
10168 }
10169 }
10170
10171 @Override
Sarah Chinbaab1432020-10-28 13:46:24 -070010172 public int getCdmaSubscriptionMode(int subId) {
10173 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010174 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chinbaab1432020-10-28 13:46:24 -070010175 mApp, subId, "getCdmaSubscriptionMode");
10176
10177 final long identity = Binder.clearCallingIdentity();
10178 try {
10179 return (int) sendRequest(CMD_GET_CDMA_SUBSCRIPTION_MODE, null /* argument */, subId);
10180 } finally {
10181 Binder.restoreCallingIdentity(identity);
10182 }
10183 }
10184
10185 @Override
Pengquan Menga1bb6272018-09-06 09:59:22 -070010186 public boolean setCdmaSubscriptionMode(int subId, int mode) {
10187 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10188 mApp, subId, "setCdmaSubscriptionMode");
10189
10190 final long identity = Binder.clearCallingIdentity();
10191 try {
10192 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
10193 } finally {
10194 Binder.restoreCallingIdentity(identity);
10195 }
10196 }
Makoto Onukida3bf792018-09-18 16:06:29 -070010197
sqianc5eccab2018-10-19 18:46:41 -070010198 @Override
sqian8c685422019-02-22 15:55:18 -080010199 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010200 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -080010201 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010202 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
10203 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -080010204 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10205 }
10206 final long identity = Binder.clearCallingIdentity();
10207 try {
sqian854d44b2018-12-12 16:48:18 -080010208 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
10209 for (Phone phone: PhoneFactory.getPhones()) {
10210 if (phone.getEmergencyNumberTracker() != null
10211 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
10212 emergencyNumberListInternal.put(
10213 phone.getSubId(),
10214 phone.getEmergencyNumberTracker().getEmergencyNumberList());
10215 }
sqian11b7a0e2018-12-05 18:48:28 -080010216 }
sqian854d44b2018-12-12 16:48:18 -080010217 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -080010218 } finally {
10219 Binder.restoreCallingIdentity(identity);
10220 }
sqianc5eccab2018-10-19 18:46:41 -070010221 }
10222
10223 @Override
sqian8c685422019-02-22 15:55:18 -080010224 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -080010225 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -080010226 if (!exactMatch) {
10227 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010228 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -080010229 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -080010230 }
10231 final long identity = Binder.clearCallingIdentity();
10232 try {
sqian854d44b2018-12-12 16:48:18 -080010233 for (Phone phone: PhoneFactory.getPhones()) {
Chinmay Dhodapkard521bb12022-08-16 15:49:54 -070010234 //Note: we ignore passed in param exactMatch. We can remove it once
10235 // TelephonyManager#isPotentialEmergencyNumber is removed completely
sqian854d44b2018-12-12 16:48:18 -080010236 if (phone.getEmergencyNumberTracker() != null
Taesu Leee050c002020-10-13 17:19:35 +090010237 && phone.getEmergencyNumberTracker()
Thomas Nguyen8ee49682023-02-01 11:46:09 -080010238 .isEmergencyNumber(number)) {
Taesu Leee050c002020-10-13 17:19:35 +090010239 return true;
sqian11b7a0e2018-12-05 18:48:28 -080010240 }
sqian11b7a0e2018-12-05 18:48:28 -080010241 }
10242 return false;
10243 } finally {
10244 Binder.restoreCallingIdentity(identity);
10245 }
10246 }
10247
sqianf4ca7ed2019-01-15 18:32:07 -080010248 /**
Shuo Qianccbaf742021-02-22 18:32:21 -080010249 * Start emergency callback mode for GsmCdmaPhone for testing.
10250 */
10251 @Override
10252 public void startEmergencyCallbackMode() {
10253 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10254 "startEmergencyCallbackMode");
10255 enforceModifyPermission();
10256 final long identity = Binder.clearCallingIdentity();
10257 try {
10258 for (Phone phone : PhoneFactory.getPhones()) {
10259 Rlog.d(LOG_TAG, "startEmergencyCallbackMode phone type: " + phone.getPhoneType());
10260 if (phone != null && ((phone.getPhoneType() == PHONE_TYPE_GSM)
10261 || (phone.getPhoneType() == PHONE_TYPE_CDMA))) {
10262 GsmCdmaPhone gsmCdmaPhone = (GsmCdmaPhone) phone;
10263 gsmCdmaPhone.obtainMessage(
10264 GsmCdmaPhone.EVENT_EMERGENCY_CALLBACK_MODE_ENTER).sendToTarget();
10265 Rlog.d(LOG_TAG, "startEmergencyCallbackMode: triggered");
10266 }
10267 }
10268 } finally {
10269 Binder.restoreCallingIdentity(identity);
10270 }
10271 }
10272
10273 /**
sqianf4ca7ed2019-01-15 18:32:07 -080010274 * Update emergency number list for test mode.
10275 */
10276 @Override
10277 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
10278 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10279 "updateEmergencyNumberListTestMode");
10280
10281 final long identity = Binder.clearCallingIdentity();
10282 try {
10283 for (Phone phone: PhoneFactory.getPhones()) {
10284 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
10285 if (tracker != null) {
10286 tracker.executeEmergencyNumberTestModeCommand(action, num);
10287 }
10288 }
10289 } finally {
10290 Binder.restoreCallingIdentity(identity);
10291 }
10292 }
10293
10294 /**
10295 * Get the full emergency number list for test mode.
10296 */
10297 @Override
10298 public List<String> getEmergencyNumberListTestMode() {
10299 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
10300 "getEmergencyNumberListTestMode");
10301
10302 final long identity = Binder.clearCallingIdentity();
10303 try {
10304 Set<String> emergencyNumbers = new HashSet<>();
10305 for (Phone phone: PhoneFactory.getPhones()) {
10306 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
10307 if (tracker != null) {
10308 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
10309 emergencyNumbers.add(num.getNumber());
10310 }
10311 }
10312 }
10313 return new ArrayList<>(emergencyNumbers);
10314 } finally {
10315 Binder.restoreCallingIdentity(identity);
10316 }
10317 }
10318
chen xud6b45bd2018-10-30 22:27:10 -070010319 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -080010320 public int getEmergencyNumberDbVersion(int subId) {
10321 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
10322
10323 final long identity = Binder.clearCallingIdentity();
10324 try {
10325 final Phone phone = getPhone(subId);
10326 if (phone == null) {
10327 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
10328 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
10329 }
10330 return phone.getEmergencyNumberDbVersion();
10331 } finally {
10332 Binder.restoreCallingIdentity(identity);
10333 }
10334 }
10335
10336 @Override
10337 public void notifyOtaEmergencyNumberDbInstalled() {
10338 enforceModifyPermission();
10339
10340 final long identity = Binder.clearCallingIdentity();
10341 try {
10342 for (Phone phone: PhoneFactory.getPhones()) {
10343 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
10344 if (tracker != null) {
10345 tracker.updateOtaEmergencyNumberDatabase();
10346 }
10347 }
10348 } finally {
10349 Binder.restoreCallingIdentity(identity);
10350 }
10351 }
10352
10353 @Override
Shuo Qianc373f112020-03-05 17:55:34 -080010354 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -080010355 enforceActiveEmergencySessionPermission();
10356
10357 final long identity = Binder.clearCallingIdentity();
10358 try {
10359 for (Phone phone: PhoneFactory.getPhones()) {
10360 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
10361 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -080010362 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
10363 }
10364 }
10365 } finally {
10366 Binder.restoreCallingIdentity(identity);
10367 }
10368 }
10369
10370 @Override
10371 public void resetOtaEmergencyNumberDbFilePath() {
10372 enforceActiveEmergencySessionPermission();
10373
10374 final long identity = Binder.clearCallingIdentity();
10375 try {
10376 for (Phone phone: PhoneFactory.getPhones()) {
10377 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
10378 if (tracker != null) {
10379 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -080010380 }
10381 }
10382 } finally {
10383 Binder.restoreCallingIdentity(identity);
10384 }
10385 }
10386
10387 @Override
chen xud6b45bd2018-10-30 22:27:10 -070010388 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
10389 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
10390 Phone phone = getPhone(subId);
10391 if (phone == null) {
10392 return null;
10393 }
10394 final long identity = Binder.clearCallingIdentity();
10395 try {
10396 UiccProfile profile = UiccController.getInstance()
10397 .getUiccProfileForPhone(phone.getPhoneId());
10398 if (profile != null) {
10399 return profile.getCertsFromCarrierPrivilegeAccessRules();
10400 }
10401 } finally {
10402 Binder.restoreCallingIdentity(identity);
10403 }
10404 return null;
10405 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -080010406
10407 /**
10408 * Enable or disable a modem stack.
10409 */
10410 @Override
10411 public boolean enableModemForSlot(int slotIndex, boolean enable) {
10412 enforceModifyPermission();
10413
10414 final long identity = Binder.clearCallingIdentity();
10415 try {
10416 Phone phone = PhoneFactory.getPhone(slotIndex);
10417 if (phone == null) {
10418 return false;
10419 } else {
10420 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
10421 }
10422 } finally {
10423 Binder.restoreCallingIdentity(identity);
10424 }
10425 }
Michelecea4cf22018-12-21 15:00:11 -080010426
Malcolm Chen4bcd9822019-03-27 18:34:05 -070010427 /**
10428 * Whether a modem stack is enabled or not.
10429 */
10430 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010431 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
10432 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -070010433 Phone phone = PhoneFactory.getPhone(slotIndex);
10434 if (phone == null) return false;
10435
10436 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010437 mApp, phone.getSubId(), callingPackage, callingFeatureId,
10438 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -070010439 throw new SecurityException("Requires READ_PHONE_STATE permission.");
10440 }
10441
10442 final long identity = Binder.clearCallingIdentity();
10443 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -070010444 try {
10445 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
10446 } catch (NoSuchElementException ex) {
10447 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
10448 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -070010449 } finally {
10450 Binder.restoreCallingIdentity(identity);
10451 }
10452 }
10453
Michelecea4cf22018-12-21 15:00:11 -080010454 @Override
Michele0ea7d782019-03-19 14:58:42 -070010455 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -080010456 enforceModifyPermission();
10457
10458 final long identity = Binder.clearCallingIdentity();
10459 try {
10460 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -070010461 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -080010462 .commit();
10463 } finally {
10464 Binder.restoreCallingIdentity(identity);
10465 }
10466 }
10467
10468 @Override
Michele0ea7d782019-03-19 14:58:42 -070010469 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010470 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -080010471 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010472 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
10473 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -070010474 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -080010475 }
Michelecea4cf22018-12-21 15:00:11 -080010476
10477 final long identity = Binder.clearCallingIdentity();
10478 try {
Michele0ea7d782019-03-19 14:58:42 -070010479 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -080010480 } finally {
10481 Binder.restoreCallingIdentity(identity);
10482 }
10483 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010484
Michele0ea7d782019-03-19 14:58:42 -070010485 @TelephonyManager.IsMultiSimSupportedResult
10486 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -080010487 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
10488 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
10489 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -070010490 loge("isMultiSimSupportedInternal: requires at least 2 cards");
10491 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -080010492 }
10493 // Check if the hardware supports multisim functionality. If usage of multisim is not
10494 // supported by the modem, indicate that it is restricted.
10495 PhoneCapability staticCapability =
10496 mPhoneConfigurationManager.getStaticPhoneCapability();
10497 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -070010498 loge("isMultiSimSupportedInternal: no static configuration available");
10499 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -080010500 }
SongFerngWang8236caa2021-01-17 21:51:44 +080010501 if (staticCapability.getLogicalModemList().size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -070010502 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
10503 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -080010504 }
10505 // Check if support of multiple SIMs is restricted by carrier
10506 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -070010507 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -080010508 }
10509
Michele0ea7d782019-03-19 14:58:42 -070010510 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -080010511 }
10512
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010513 /**
10514 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -080010515 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
10516 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
10517 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010518 * @param numOfSims number of active sims we want to switch to
10519 */
10520 @Override
10521 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -080010522 if (numOfSims == 1) {
10523 enforceModifyPermission();
10524 } else {
10525 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10526 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
10527 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010528 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -080010529
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010530 try {
Michele30b57b22019-03-01 12:01:14 -080010531 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -070010532 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -080010533 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
10534 return;
10535 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010536 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
10537 } finally {
10538 Binder.restoreCallingIdentity(identity);
10539 }
10540 }
10541
Hyungjun Parkbb07fde2019-01-10 15:28:51 +090010542 @Override
10543 public boolean isApplicationOnUicc(int subId, int appType) {
10544 enforceReadPrivilegedPermission("isApplicationOnUicc");
10545 Phone phone = getPhone(subId);
10546 if (phone == null) {
10547 return false;
10548 }
10549 final long identity = Binder.clearCallingIdentity();
10550 try {
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000010551 UiccPort uiccPort = phone.getUiccPort();
10552 if (uiccPort == null) {
Hyungjun Parkbb07fde2019-01-10 15:28:51 +090010553 return false;
10554 }
Muralidhar Reddy472c2ae2021-09-29 19:38:40 +000010555 UiccProfile uiccProfile = uiccPort.getUiccProfile();
Hyungjun Parkbb07fde2019-01-10 15:28:51 +090010556 if (uiccProfile == null) {
10557 return false;
10558 }
10559 if (TelephonyManager.APPTYPE_SIM <= appType
10560 && appType <= TelephonyManager.APPTYPE_ISIM) {
10561 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
10562 }
10563 return false;
10564 } finally {
10565 Binder.restoreCallingIdentity(identity);
10566 }
10567 }
10568
Nazanin Bakhshi628473f2019-01-29 17:37:52 -080010569 /**
chen xub4baa772019-04-03 10:23:41 -070010570 * Get whether making changes to modem configurations will trigger reboot.
10571 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -080010572 */
10573 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010574 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
10575 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -070010576 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -070010577 mApp, subId, callingPackage, callingFeatureId,
10578 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -070010579 return false;
10580 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -080010581 final long identity = Binder.clearCallingIdentity();
10582 try {
10583 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
10584 } finally {
10585 Binder.restoreCallingIdentity(identity);
10586 }
10587 }
10588
Nathan Harold29f5f052019-02-15 13:41:57 -080010589 private void updateModemStateMetrics() {
10590 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
10591 // TODO: check the state for each modem if the api is ready.
10592 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
10593 }
10594
Pengquan Meng3889a572019-01-23 11:16:29 -080010595 @Override
sandeepjsa208e3b2021-11-17 04:05:58 +000010596 public List<UiccSlotMapping> getSlotsMapping(String callingPackage) {
Pengquan Meng3889a572019-01-23 11:16:29 -080010597 enforceReadPrivilegedPermission("getSlotsMapping");
sandeepjsa208e3b2021-11-17 04:05:58 +000010598 // Verify that the callingPackage belongs to the calling UID
10599 mApp.getSystemService(AppOpsManager.class)
10600 .checkPackage(Binder.getCallingUid(), callingPackage);
Pengquan Meng3889a572019-01-23 11:16:29 -080010601 final long identity = Binder.clearCallingIdentity();
sandeepjsa208e3b2021-11-17 04:05:58 +000010602 List<UiccSlotMapping> slotMap = new ArrayList<>();
Pengquan Meng3889a572019-01-23 11:16:29 -080010603 try {
sandeepjsa208e3b2021-11-17 04:05:58 +000010604 UiccSlotInfo[] slotInfos = getUiccSlotsInfo(mApp.getOpPackageName());
10605 if (slotInfos != null) {
10606 for (int i = 0; i < slotInfos.length; i++) {
10607 for (UiccPortInfo portInfo : slotInfos[i].getPorts()) {
10608 if (SubscriptionManager.isValidPhoneId(portInfo.getLogicalSlotIndex())) {
10609 slotMap.add(new UiccSlotMapping(portInfo.getPortIndex(), i,
10610 portInfo.getLogicalSlotIndex()));
10611 }
10612 }
Pengquan Meng3889a572019-01-23 11:16:29 -080010613 }
10614 }
sandeepjsa208e3b2021-11-17 04:05:58 +000010615 return slotMap;
Pengquan Meng3889a572019-01-23 11:16:29 -080010616 } finally {
10617 Binder.restoreCallingIdentity(identity);
10618 }
10619 }
Nathan Harold48d6fd52019-02-06 19:01:40 -080010620
10621 /**
10622 * Get the IRadio HAL Version
jimsunf9ec1622022-09-13 21:18:43 +080010623 * @deprecated use getHalVersion instead
Nathan Harold48d6fd52019-02-06 19:01:40 -080010624 */
jimsunf9ec1622022-09-13 21:18:43 +080010625 @Deprecated
Nathan Harold48d6fd52019-02-06 19:01:40 -080010626 @Override
10627 public int getRadioHalVersion() {
jimsunf9ec1622022-09-13 21:18:43 +080010628 return getHalVersion(HAL_SERVICE_RADIO);
10629 }
10630
10631 /**
10632 * Get the HAL Version of a specific service
10633 */
10634 @Override
10635 public int getHalVersion(int service) {
Nathan Harold48d6fd52019-02-06 19:01:40 -080010636 Phone phone = getDefaultPhone();
10637 if (phone == null) return -1;
jimsunf9ec1622022-09-13 21:18:43 +080010638 HalVersion hv = phone.getHalVersion(service);
Nathan Harold48d6fd52019-02-06 19:01:40 -080010639 if (hv.equals(HalVersion.UNKNOWN)) return -1;
10640 return hv.major * 100 + hv.minor;
10641 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010642
10643 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010644 * Get the current calling package name.
10645 * @return the current calling package name
10646 */
10647 @Override
10648 public String getCurrentPackageName() {
10649 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
10650 }
10651
10652 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -070010653 * Return whether data is enabled for certain APN type. This will tell if framework will accept
10654 * corresponding network requests on a subId.
10655 *
10656 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010657 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -070010658 * 2) APN is un-metered for this subscription, or
10659 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
Hall Liu746e03c2020-09-25 11:13:49 -070010660 * {@link TelephonyManager#MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED} is enabled.
Malcolm Chene5ad5792019-04-18 13:51:02 -070010661 *
10662 * @return whether data is allowed for a apn type.
10663 *
10664 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010665 */
10666 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -070010667 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -070010668 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
10669 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010670
10671 // Now that all security checks passes, perform the operation as ourselves.
10672 final long identity = Binder.clearCallingIdentity();
10673 try {
10674 Phone phone = getPhone(subId);
10675 if (phone == null) return false;
10676
Jack Yu27422a52022-03-21 10:38:05 -070010677 boolean isMetered;
Jack Yu99e87332021-12-17 23:14:15 -080010678 boolean isDataEnabled;
Jack Yu7968c6d2022-07-31 00:43:21 -070010679 isMetered = phone.getDataNetworkController().getDataConfigManager()
10680 .isMeteredCapability(DataUtils.apnTypeToNetworkCapability(apnType),
10681 phone.getServiceState().getDataRoaming());
10682 isDataEnabled = phone.getDataSettingsManager().isDataEnabled(apnType);
Jack Yu99e87332021-12-17 23:14:15 -080010683 return !isMetered || isDataEnabled;
Malcolm Chene5ad5792019-04-18 13:51:02 -070010684 } finally {
10685 Binder.restoreCallingIdentity(identity);
10686 }
10687 }
10688
10689 @Override
Jack Yu41407ee2019-05-13 16:54:09 -070010690 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -070010691 enforceReadPrivilegedPermission("isApnMetered");
10692
10693 // Now that all security checks passes, perform the operation as ourselves.
10694 final long identity = Binder.clearCallingIdentity();
10695 try {
10696 Phone phone = getPhone(subId);
10697 if (phone == null) return true; // By default return true.
Jack Yu7968c6d2022-07-31 00:43:21 -070010698 return phone.getDataNetworkController().getDataConfigManager().isMeteredCapability(
10699 DataUtils.apnTypeToNetworkCapability(apnType),
10700 phone.getServiceState().getDataRoaming());
Malcolm Chendc8c10e2019-04-10 18:25:07 -070010701 } finally {
10702 Binder.restoreCallingIdentity(identity);
10703 }
10704 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010705
10706 @Override
Hall Liu73f5d362020-01-20 13:42:00 -080010707 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
10708 int subscriptionId, IBooleanConsumer resultCallback) {
10709 enforceModifyPermission();
10710 long token = Binder.clearCallingIdentity();
10711 try {
10712 Phone phone = getPhone(subscriptionId);
10713 if (phone == null) {
10714 try {
10715 if (resultCallback != null) {
10716 resultCallback.accept(false);
10717 }
10718 } catch (RemoteException e) {
10719 // ignore
10720 }
10721 return;
10722 }
10723 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
10724 Pair.create(specifiers, (x) -> {
10725 try {
10726 if (resultCallback != null) {
10727 resultCallback.accept(x);
10728 }
10729 } catch (RemoteException e) {
10730 // ignore
10731 }
10732 });
10733 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
10734 } finally {
10735 Binder.restoreCallingIdentity(token);
10736 }
10737 }
10738
10739 @Override
Sarah Chin679c08a2020-11-18 13:39:35 -080010740 public List<RadioAccessSpecifier> getSystemSelectionChannels(int subId) {
10741 TelephonyPermissions
Nathan Harold62c68512021-04-06 11:26:02 -070010742 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Sarah Chin679c08a2020-11-18 13:39:35 -080010743 mApp, subId, "getSystemSelectionChannels");
10744 WorkSource workSource = getWorkSource(Binder.getCallingUid());
10745 final long identity = Binder.clearCallingIdentity();
10746 try {
Sarah Chin428d1d62021-03-13 03:17:40 -080010747 Object result = sendRequest(CMD_GET_SYSTEM_SELECTION_CHANNELS, null, subId, workSource);
10748 if (result instanceof IllegalStateException) {
10749 throw (IllegalStateException) result;
10750 }
10751 List<RadioAccessSpecifier> specifiers = (List<RadioAccessSpecifier>) result;
Sarah Chin679c08a2020-11-18 13:39:35 -080010752 if (DBG) log("getSystemSelectionChannels: " + specifiers);
10753 return specifiers;
10754 } finally {
10755 Binder.restoreCallingIdentity(identity);
10756 }
10757 }
10758
10759 @Override
Jack Yu8b766fc2022-03-21 09:42:33 -070010760 public boolean isMvnoMatched(int slotIndex, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +080010761 enforceReadPrivilegedPermission("isMvnoMatched");
Jack Yu8b766fc2022-03-21 09:42:33 -070010762 return UiccController.getInstance().mvnoMatches(slotIndex, mvnoType, mvnoMatchData);
changbetty7157e9e2019-12-06 18:16:37 +080010763 }
10764
10765 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010766 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
10767 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -080010768 if (callingPackage == null) {
10769 callingPackage = getCurrentPackageName();
10770 }
Brad Ebingera63db5f2019-04-23 16:31:13 -070010771 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
10772 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -070010773 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
10774 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -070010775 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
10776 }
10777 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
10778 Intent intent = new Intent();
10779 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
10780 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10781 // Bring up choose default SMS subscription dialog right now
10782 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
10783 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
10784 mApp.startActivity(intent);
10785 }
chen xud5ca2d52019-05-28 15:20:57 -070010786
10787 @Override
Ayush Sharma787854b2022-12-12 14:55:02 +000010788 public void showSwitchToManagedProfileDialog() {
10789 enforceModifyPermission();
10790
10791 Intent intent = new Intent();
10792 intent.setClass(mApp, ErrorDialogActivity.class);
10793 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
10794 mApp.startActivity(intent);
10795 }
10796
10797 @Override
chen xud5ca2d52019-05-28 15:20:57 -070010798 public String getMmsUAProfUrl(int subId) {
10799 //TODO investigate if this API should require proper permission check in R b/133791609
10800 final long identity = Binder.clearCallingIdentity();
10801 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010802 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
10803 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
10804 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
10805 return carrierUAProfUrl;
10806 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010807 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10808 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -070010809 } finally {
10810 Binder.restoreCallingIdentity(identity);
10811 }
10812 }
10813
10814 @Override
10815 public String getMmsUserAgent(int subId) {
10816 //TODO investigate if this API should require proper permission check in R b/133791609
10817 final long identity = Binder.clearCallingIdentity();
10818 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +080010819 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
10820 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
10821 if (!TextUtils.isEmpty(carrierUserAgent)) {
10822 return carrierUserAgent;
10823 }
Daniel Brightebb4eb72020-02-18 15:16:33 -080010824 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
10825 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -070010826 } finally {
10827 Binder.restoreCallingIdentity(identity);
10828 }
10829 }
Jack Yub07d4972019-05-28 16:12:25 -070010830
10831 @Override
Hall Liua62f5da2020-09-25 10:42:19 -070010832 public boolean isMobileDataPolicyEnabled(int subscriptionId, int policy) {
10833 enforceReadPrivilegedPermission("isMobileDataPolicyEnabled");
Jack Yub07d4972019-05-28 16:12:25 -070010834
Jack Yub07d4972019-05-28 16:12:25 -070010835 final long identity = Binder.clearCallingIdentity();
10836 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010837 Phone phone = getPhone(subscriptionId);
Jack Yub07d4972019-05-28 16:12:25 -070010838 if (phone == null) return false;
10839
Ling Maf188d502022-09-16 15:22:36 -070010840 return phone.getDataSettingsManager().isMobileDataPolicyEnabled(policy);
Jack Yub07d4972019-05-28 16:12:25 -070010841 } finally {
10842 Binder.restoreCallingIdentity(identity);
10843 }
10844 }
10845
10846 @Override
Hall Liuc66bb112021-02-02 12:09:32 -080010847 public void setMobileDataPolicyEnabled(int subscriptionId, int policy,
Hall Liua62f5da2020-09-25 10:42:19 -070010848 boolean enabled) {
changbettyd5c246e2019-12-24 15:40:37 +080010849 enforceModifyPermission();
10850
changbettyd5c246e2019-12-24 15:40:37 +080010851 final long identity = Binder.clearCallingIdentity();
10852 try {
Hall Liua62f5da2020-09-25 10:42:19 -070010853 Phone phone = getPhone(subscriptionId);
10854 if (phone == null) return;
changbettyd5c246e2019-12-24 15:40:37 +080010855
Ling Maf188d502022-09-16 15:22:36 -070010856 phone.getDataSettingsManager().setMobileDataPolicy(policy, enabled);
changbettyd5c246e2019-12-24 15:40:37 +080010857 } finally {
10858 Binder.restoreCallingIdentity(identity);
10859 }
10860 }
10861
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010862 /**
Hall Liu746e03c2020-09-25 11:13:49 -070010863 * Updates whether conference event package handling is enabled.
Tyler Gunn7bcdc742019-10-04 15:56:59 -070010864 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
10865 * otherwise.
10866 */
10867 @Override
10868 public void setCepEnabled(boolean isCepEnabled) {
10869 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
10870
10871 final long identity = Binder.clearCallingIdentity();
10872 try {
10873 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
10874 for (Phone phone : PhoneFactory.getPhones()) {
10875 Phone defaultPhone = phone.getImsPhone();
10876 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
10877 ImsPhone imsPhone = (ImsPhone) defaultPhone;
10878 ImsPhoneCallTracker imsPhoneCallTracker =
10879 (ImsPhoneCallTracker) imsPhone.getCallTracker();
10880 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
10881 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
10882 + imsPhone.getMsisdn());
10883 }
10884 }
10885 } finally {
10886 Binder.restoreCallingIdentity(identity);
10887 }
10888 }
allenwtsu46dcc572020-01-08 18:24:03 +080010889
10890 /**
10891 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
10892 *
10893 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
10894 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
10895 * before being read.
10896 */
10897 @Override
10898 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
10899 isCompressed) {
10900 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
10901 mApp, subId, "notifyRcsAutoConfigurationReceived");
Hui Wang761a6682020-10-31 05:12:53 +000010902 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
10903 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
10904 }
10905 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000010906 // ProvisioningManager can not handle ServiceSpecificException.
10907 // Throw the IllegalStateException and annotate ProvisioningManager.
10908 throw new IllegalStateException("IMS not available on device.");
Hui Wang761a6682020-10-31 05:12:53 +000010909 }
10910
10911 final long identity = Binder.clearCallingIdentity();
allenwtsu46dcc572020-01-08 18:24:03 +080010912 try {
Hui Wang761a6682020-10-31 05:12:53 +000010913 RcsProvisioningMonitor.getInstance().updateConfig(subId, config, isCompressed);
10914 } finally {
10915 Binder.restoreCallingIdentity(identity);
allenwtsu46dcc572020-01-08 18:24:03 +080010916 }
10917 }
zoey chene02881a2019-12-30 16:11:23 +080010918
10919 @Override
10920 public boolean isIccLockEnabled(int subId) {
10921 enforceReadPrivilegedPermission("isIccLockEnabled");
10922
10923 // Now that all security checks passes, perform the operation as ourselves.
10924 final long identity = Binder.clearCallingIdentity();
10925 try {
10926 Phone phone = getPhone(subId);
10927 if (phone != null && phone.getIccCard() != null) {
10928 return phone.getIccCard().getIccLockEnabled();
10929 } else {
10930 return false;
10931 }
10932 } finally {
10933 Binder.restoreCallingIdentity(identity);
10934 }
10935 }
10936
10937 /**
10938 * Set the ICC pin lock enabled or disabled.
10939 *
10940 * @return an integer representing the status of IccLock enabled or disabled in the following
10941 * three cases:
10942 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
10943 * successfully.
10944 * - Positive number and zero for remaining password attempts.
10945 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10946 *
10947 */
10948 @Override
10949 public int setIccLockEnabled(int subId, boolean enabled, String password) {
10950 enforceModifyPermission();
10951
10952 Phone phone = getPhone(subId);
10953 if (phone == null) {
10954 return 0;
10955 }
10956 // Now that all security checks passes, perform the operation as ourselves.
10957 final long identity = Binder.clearCallingIdentity();
10958 try {
10959 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
10960 new Pair<Boolean, String>(enabled, password), phone, null);
10961 return attemptsRemaining;
10962
10963 } catch (Exception e) {
10964 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
10965 } finally {
10966 Binder.restoreCallingIdentity(identity);
10967 }
10968 return 0;
10969 }
10970
10971 /**
10972 * Change the ICC password used in ICC pin lock.
10973 *
10974 * @return an integer representing the status of IccLock changed in the following three cases:
10975 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
10976 * - Positive number and zero for remaining password attempts.
10977 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
10978 *
10979 */
10980 @Override
10981 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
10982 enforceModifyPermission();
10983
10984 Phone phone = getPhone(subId);
10985 if (phone == null) {
10986 return 0;
10987 }
10988 // Now that all security checks passes, perform the operation as ourselves.
10989 final long identity = Binder.clearCallingIdentity();
10990 try {
10991 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
10992 new Pair<String, String>(oldPassword, newPassword), phone, null);
10993 return attemptsRemaining;
10994
10995 } catch (Exception e) {
10996 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
10997 } finally {
10998 Binder.restoreCallingIdentity(identity);
10999 }
11000 return 0;
11001 }
Peter Wangdafb9ac2020-01-15 14:13:38 -080011002
11003 /**
11004 * Request for receiving user activity notification
11005 */
11006 @Override
11007 public void requestUserActivityNotification() {
11008 if (!mNotifyUserActivity.get()
11009 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
11010 mNotifyUserActivity.set(true);
11011 }
11012 }
11013
11014 /**
11015 * Called when userActivity is signalled in the power manager.
11016 * This is safe to call from any thread, with any window manager locks held or not.
11017 */
11018 @Override
11019 public void userActivity() {
11020 // ***************************************
11021 // * Inherited from PhoneWindowManager *
11022 // ***************************************
11023 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
11024 // WITH ITS LOCKS HELD.
11025 //
11026 // This code must be VERY careful about the locks
11027 // it acquires.
11028 // In fact, the current code acquires way too many,
11029 // and probably has lurking deadlocks.
11030
11031 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
11032 throw new SecurityException("Only the OS may call notifyUserActivity()");
11033 }
11034
11035 if (mNotifyUserActivity.getAndSet(false)) {
11036 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
11037 USER_ACTIVITY_NOTIFICATION_DELAY);
11038 }
11039 }
Malcolm Chen4639c562020-04-13 11:59:40 -070011040
11041 @Override
11042 public boolean canConnectTo5GInDsdsMode() {
11043 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
11044 }
Jack Yud10cdd42020-09-28 20:28:01 -070011045
11046 @Override
11047 public @NonNull List<String> getEquivalentHomePlmns(int subId, String callingPackage,
11048 String callingFeatureId) {
11049 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
11050 mApp, subId, callingPackage, callingFeatureId, "getEquivalentHomePlmns")) {
11051 throw new SecurityException("Requires READ_PHONE_STATE permission.");
11052 }
11053
11054 Phone phone = getPhone(subId);
11055 if (phone == null) {
11056 throw new RuntimeException("phone is not available");
11057 }
11058 // Now that all security checks passes, perform the operation as ourselves.
11059 final long identity = Binder.clearCallingIdentity();
11060 try {
11061 return phone.getEquivalentHomePlmns();
11062 } finally {
11063 Binder.restoreCallingIdentity(identity);
11064 }
11065 }
Daniel Bright59e67312020-11-13 11:49:37 -080011066
11067 @Override
11068 public boolean isRadioInterfaceCapabilitySupported(
Daniel Bright95a4c1f2021-02-11 09:57:16 -080011069 final @NonNull @TelephonyManager.RadioInterfaceCapability String capability) {
11070 Set<String> radioInterfaceCapabilities =
Daniel Bright94f43662021-03-01 14:43:40 -080011071 mRadioInterfaceCapabilities.getCapabilities();
Daniel Bright59e67312020-11-13 11:49:37 -080011072 if (radioInterfaceCapabilities == null) {
11073 throw new RuntimeException("radio interface capabilities are not available");
Daniel Bright59e67312020-11-13 11:49:37 -080011074 }
Daniel Bright95a4c1f2021-02-11 09:57:16 -080011075 return radioInterfaceCapabilities.contains(capability);
Daniel Bright59e67312020-11-13 11:49:37 -080011076 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011077
Hui Wang641e81c2020-10-12 12:14:23 -070011078 @Override
11079 public void bootstrapAuthenticationRequest(int subId, int appType, Uri nafUrl,
11080 UaSecurityProtocolIdentifier securityProtocol,
Brad Ebinger34c09a52021-02-17 23:23:21 +000011081 boolean forceBootStrapping, IBootstrapAuthenticationCallback callback) {
11082 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11083 Binder.getCallingUid(), "bootstrapAuthenticationRequest",
11084 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
11085 Manifest.permission.MODIFY_PHONE_STATE);
Hui Wang641e81c2020-10-12 12:14:23 -070011086 if (DBG) {
11087 log("bootstrapAuthenticationRequest, subId:" + subId + ", appType:"
11088 + appType + ", NAF:" + nafUrl + ", sp:" + securityProtocol
11089 + ", forceBootStrapping:" + forceBootStrapping + ", callback:" + callback);
11090 }
11091
11092 if (!SubscriptionManager.isValidSubscriptionId(subId)
11093 || appType < TelephonyManager.APPTYPE_UNKNOWN
11094 || appType > TelephonyManager.APPTYPE_ISIM
11095 || nafUrl == null || securityProtocol == null || callback == null) {
11096 Log.d(LOG_TAG, "bootstrapAuthenticationRequest failed due to invalid parameters");
11097 if (callback != null) {
11098 try {
11099 callback.onAuthenticationFailure(
11100 0, TelephonyManager.GBA_FAILURE_REASON_FEATURE_NOT_SUPPORTED);
11101 } catch (RemoteException exception) {
11102 log("Fail to notify onAuthenticationFailure due to " + exception);
11103 }
11104 return;
11105 }
11106 }
11107
11108 final long token = Binder.clearCallingIdentity();
11109 try {
11110 getGbaManager(subId).bootstrapAuthenticationRequest(
11111 new GbaAuthRequest(subId, appType, nafUrl, securityProtocol.toByteArray(),
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011112 forceBootStrapping, callback));
Hui Wang641e81c2020-10-12 12:14:23 -070011113 } finally {
11114 Binder.restoreCallingIdentity(token);
11115 }
11116 }
11117
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011118 /**
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011119 * Attempts to set the radio power state for all phones for thermal reason.
11120 * This does not guarantee that the
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011121 * requested radio power state will actually be set. See {@link
11122 * PhoneInternalInterface#setRadioPowerForReason} for more details.
11123 *
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011124 * @param enable {@code true} if trying to turn radio on.
11125 * @return {@code true} if phone setRadioPowerForReason was called. Otherwise, returns {@code
11126 * false}.
11127 */
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011128 private boolean setRadioPowerForThermal(boolean enable) {
11129 boolean isPhoneAvailable = false;
11130 for (int i = 0; i < TelephonyManager.getDefault().getActiveModemCount(); i++) {
11131 Phone phone = PhoneFactory.getPhone(i);
11132 if (phone != null) {
Thomas Nguyenfd0572f2022-07-15 22:28:49 +000011133 phone.setRadioPowerForReason(enable, TelephonyManager.RADIO_POWER_REASON_THERMAL);
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011134 isPhoneAvailable = true;
11135 }
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011136 }
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011137
11138 // return true if successfully informed the phone object about the thermal radio power
11139 // request.
11140 return isPhoneAvailable;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011141 }
11142
11143 private int handleDataThrottlingRequest(int subId,
Sarah Chinecc78c42022-03-31 21:16:48 -070011144 DataThrottlingRequest dataThrottlingRequest, String callingPackage) {
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080011145 boolean isDataThrottlingSupported = isRadioInterfaceCapabilitySupported(
11146 TelephonyManager.CAPABILITY_THERMAL_MITIGATION_DATA_THROTTLING);
11147 if (!isDataThrottlingSupported && dataThrottlingRequest.getDataThrottlingAction()
11148 != DataThrottlingRequest.DATA_THROTTLING_ACTION_NO_DATA_THROTTLING) {
11149 throw new IllegalArgumentException("modem does not support data throttling");
11150 }
11151
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011152 // Ensure that radio is on. If not able to power on due to phone being unavailable, return
11153 // THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011154 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011155 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
11156 }
11157
Sarah Chinecc78c42022-03-31 21:16:48 -070011158 setDataEnabledForReason(
11159 subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL, true, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011160
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080011161 if (isDataThrottlingSupported) {
11162 int thermalMitigationResult =
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011163 (int) sendRequest(CMD_SET_DATA_THROTTLING, dataThrottlingRequest, subId);
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080011164 if (thermalMitigationResult == SET_DATA_THROTTLING_MODEM_THREW_INVALID_PARAMS) {
11165 throw new IllegalArgumentException("modem returned INVALID_ARGUMENTS");
11166 } else if (thermalMitigationResult
11167 == MODEM_DOES_NOT_SUPPORT_DATA_THROTTLING_ERROR_CODE) {
Jack Nudelman760d0962021-05-20 13:57:30 -070011168 log("Modem likely does not support data throttling on secondary carrier. Data " +
11169 "throttling action = " + dataThrottlingRequest.getDataThrottlingAction());
11170 return TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080011171 }
11172 return thermalMitigationResult;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011173 }
Jack Nudelman5d6a98b2021-03-04 14:26:25 -080011174
11175 return TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011176 }
11177
Jack Nudelman644b91a2021-03-12 14:09:48 -080011178 private static List<String> getThermalMitigationAllowlist(Context context) {
11179 if (sThermalMitigationAllowlistedPackages.isEmpty()) {
11180 for (String pckg : context.getResources()
11181 .getStringArray(R.array.thermal_mitigation_allowlisted_packages)) {
11182 sThermalMitigationAllowlistedPackages.add(pckg);
11183 }
11184 }
11185
11186 return sThermalMitigationAllowlistedPackages;
11187 }
11188
Jack Nudelmane69bbc82021-05-13 10:00:15 -070011189 private boolean isAnyPhoneInEmergencyState() {
11190 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
11191 if (tm.isInEmergencyCall()) {
11192 Log.e(LOG_TAG , "Phone state is not valid. One of the phones is in an emergency call");
11193 return true;
11194 }
11195 for (Phone phone : PhoneFactory.getPhones()) {
11196 if (phone.isInEmergencySmsMode() || phone.isInEcm()) {
11197 Log.e(LOG_TAG, "Phone state is not valid. isInEmergencySmsMode = "
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011198 + phone.isInEmergencySmsMode() + " isInEmergencyCallbackMode = "
11199 + phone.isInEcm());
Jack Nudelmane69bbc82021-05-13 10:00:15 -070011200 return true;
11201 }
11202 }
11203
11204 return false;
11205 }
11206
Jack Nudelman644b91a2021-03-12 14:09:48 -080011207 /**
11208 * Used by shell commands to add an authorized package name for thermal mitigation.
11209 * @param packageName name of package to be allowlisted
11210 * @param context
11211 */
11212 static void addPackageToThermalMitigationAllowlist(String packageName, Context context) {
11213 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
11214 sThermalMitigationAllowlistedPackages.add(packageName);
11215 }
11216
11217 /**
11218 * Used by shell commands to remove an authorized package name for thermal mitigation.
11219 * @param packageName name of package to remove from allowlist
11220 * @param context
11221 */
11222 static void removePackageFromThermalMitigationAllowlist(String packageName, Context context) {
11223 sThermalMitigationAllowlistedPackages = getThermalMitigationAllowlist(context);
11224 sThermalMitigationAllowlistedPackages.remove(packageName);
11225 }
11226
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011227 /**
11228 * Thermal mitigation request to control functionalities at modem.
11229 *
11230 * @param subId the id of the subscription.
11231 * @param thermalMitigationRequest holds all necessary information to be passed down to modem.
Jack Nudelman644b91a2021-03-12 14:09:48 -080011232 * @param callingPackage the package name of the calling package.
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011233 *
11234 * @return thermalMitigationResult enum as defined in android.telephony.Annotation.
11235 */
11236 @Override
11237 @ThermalMitigationResult
11238 public int sendThermalMitigationRequest(
11239 int subId,
Jack Nudelman644b91a2021-03-12 14:09:48 -080011240 ThermalMitigationRequest thermalMitigationRequest,
11241 String callingPackage) throws IllegalArgumentException {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011242 enforceModifyPermission();
11243
Jack Nudelman644b91a2021-03-12 14:09:48 -080011244 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
11245 if (!getThermalMitigationAllowlist(getDefaultPhone().getContext())
11246 .contains(callingPackage)) {
11247 throw new SecurityException("Calling package must be configured in the device config. "
11248 + "calling package: " + callingPackage);
11249 }
11250
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011251 WorkSource workSource = getWorkSource(Binder.getCallingUid());
11252 final long identity = Binder.clearCallingIdentity();
11253
11254 int thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_UNKNOWN_ERROR;
11255 try {
11256 int thermalMitigationAction = thermalMitigationRequest.getThermalMitigationAction();
11257 switch (thermalMitigationAction) {
11258 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_DATA_THROTTLING:
11259 thermalMitigationResult =
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011260 handleDataThrottlingRequest(subId,
11261 thermalMitigationRequest.getDataThrottlingRequest(),
11262 callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011263 break;
11264 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY:
11265 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
11266 throw new IllegalArgumentException("dataThrottlingRequest must be null for "
11267 + "ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_VOICE_ONLY");
11268 }
11269
11270 // Ensure that radio is on. If not able to power on due to phone being
11271 // unavailable, return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011272 if (!setRadioPowerForThermal(true)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011273 thermalMitigationResult =
11274 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
11275 break;
11276 }
11277
11278 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_THERMAL,
Sarah Chinecc78c42022-03-31 21:16:48 -070011279 false, callingPackage);
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011280 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
11281 break;
11282 case ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF:
11283 if (thermalMitigationRequest.getDataThrottlingRequest() != null) {
11284 throw new IllegalArgumentException("dataThrottlingRequest must be null for"
11285 + " ThermalMitigationRequest.THERMAL_MITIGATION_ACTION_RADIO_OFF");
11286 }
11287
11288 TelecomAccountRegistry registry = TelecomAccountRegistry.getInstance(null);
11289 if (registry != null) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011290 Phone phone = getPhone(subId);
11291 if (phone == null) {
11292 thermalMitigationResult =
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011293 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011294 break;
11295 }
11296
Jack Nudelmane69bbc82021-05-13 10:00:15 -070011297 TelephonyConnectionService service =
11298 registry.getTelephonyConnectionService();
Jack Nudelmanb30ac302021-06-17 15:39:58 -070011299 if (service != null && service.isEmergencyCallPending()) {
Jack Nudelmane69bbc82021-05-13 10:00:15 -070011300 Log.e(LOG_TAG, "An emergency call is pending");
11301 thermalMitigationResult =
11302 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
11303 break;
11304 } else if (isAnyPhoneInEmergencyState()) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011305 thermalMitigationResult =
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011306 TelephonyManager.THERMAL_MITIGATION_RESULT_INVALID_STATE;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011307 break;
11308 }
11309 } else {
11310 thermalMitigationResult =
11311 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
11312 break;
11313 }
11314
11315 // Turn radio off. If not able to power off due to phone being unavailable,
11316 // return THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE.
Sooraj Sasindran72cff492021-07-29 09:42:42 -070011317 if (!setRadioPowerForThermal(false)) {
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011318 thermalMitigationResult =
11319 TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_NOT_AVAILABLE;
11320 break;
11321 }
11322 thermalMitigationResult =
Thomas Nguyen8ee49682023-02-01 11:46:09 -080011323 TelephonyManager.THERMAL_MITIGATION_RESULT_SUCCESS;
Jack Nudelmanb0b87642020-11-12 15:04:39 -080011324 break;
11325 default:
11326 throw new IllegalArgumentException("the requested thermalMitigationAction does "
11327 + "not exist. Requested action: " + thermalMitigationAction);
11328 }
11329 } catch (IllegalArgumentException e) {
11330 throw e;
11331 } catch (Exception e) {
11332 Log.e(LOG_TAG, "thermalMitigationRequest. Exception e =" + e);
11333 thermalMitigationResult = TelephonyManager.THERMAL_MITIGATION_RESULT_MODEM_ERROR;
11334 } finally {
11335 Binder.restoreCallingIdentity(identity);
11336 }
11337
11338 if (DBG) {
11339 log("thermalMitigationRequest returning with thermalMitigationResult: "
11340 + thermalMitigationResult);
11341 }
11342
11343 return thermalMitigationResult;
11344 }
Hui Wang641e81c2020-10-12 12:14:23 -070011345
11346 /**
11347 * Set the GbaService Package Name that Telephony will bind to.
11348 *
11349 * @param subId The sim that the GbaService is associated with.
11350 * @param packageName The name of the package to be replaced with.
11351 * @return true if setting the GbaService to bind to succeeded, false if it did not.
11352 */
11353 @Override
11354 public boolean setBoundGbaServiceOverride(int subId, String packageName) {
11355 enforceModifyPermission();
11356
11357 final long identity = Binder.clearCallingIdentity();
11358 try {
11359 return getGbaManager(subId).overrideServicePackage(packageName);
11360 } finally {
11361 Binder.restoreCallingIdentity(identity);
11362 }
11363 }
11364
11365 /**
11366 * Return the package name of the currently bound GbaService.
11367 *
11368 * @param subId The sim that the GbaService is associated with.
11369 * @return the package name of the GbaService configuration, null if GBA is not supported.
11370 */
11371 @Override
11372 public String getBoundGbaService(int subId) {
11373 enforceReadPrivilegedPermission("getBoundGbaServicePackage");
11374
11375 final long identity = Binder.clearCallingIdentity();
11376 try {
11377 return getGbaManager(subId).getServicePackage();
11378 } finally {
11379 Binder.restoreCallingIdentity(identity);
11380 }
11381 }
11382
11383 /**
11384 * Set the release time for telephony to unbind GbaService.
11385 *
11386 * @param subId The sim that the GbaService is associated with.
11387 * @param interval The release time to unbind GbaService by millisecond.
11388 * @return true if setting the GbaService to bind to succeeded, false if it did not.
11389 */
11390 @Override
11391 public boolean setGbaReleaseTimeOverride(int subId, int interval) {
11392 enforceModifyPermission();
11393
11394 final long identity = Binder.clearCallingIdentity();
11395 try {
11396 return getGbaManager(subId).overrideReleaseTime(interval);
11397 } finally {
11398 Binder.restoreCallingIdentity(identity);
11399 }
11400 }
11401
11402 /**
11403 * Return the release time for telephony to unbind GbaService.
11404 *
11405 * @param subId The sim that the GbaService is associated with.
11406 * @return The release time to unbind GbaService by millisecond.
11407 */
11408 @Override
11409 public int getGbaReleaseTime(int subId) {
11410 enforceReadPrivilegedPermission("getGbaReleaseTime");
11411
11412 final long identity = Binder.clearCallingIdentity();
11413 try {
11414 return getGbaManager(subId).getReleaseTime();
11415 } finally {
11416 Binder.restoreCallingIdentity(identity);
11417 }
11418 }
11419
11420 private GbaManager getGbaManager(int subId) {
11421 GbaManager instance = GbaManager.getInstance(subId);
11422 if (instance == null) {
11423 String packageName = mApp.getResources().getString(R.string.config_gba_package);
11424 int releaseTime = mApp.getResources().getInteger(R.integer.config_gba_release_time);
11425 instance = GbaManager.make(mApp, subId, packageName, releaseTime);
11426 }
11427 return instance;
11428 }
Hui Wang761a6682020-10-31 05:12:53 +000011429
11430 /**
11431 * indicate whether the device and the carrier can support
11432 * RCS VoLTE single registration.
11433 */
11434 @Override
11435 public boolean isRcsVolteSingleRegistrationCapable(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011436 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
11437 Binder.getCallingUid(), "isRcsVolteSingleRegistrationCapable",
11438 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
11439 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000011440
11441 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11442 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11443 }
11444
11445 final long identity = Binder.clearCallingIdentity();
11446 try {
11447 RcsProvisioningMonitor rpm = RcsProvisioningMonitor.getInstance();
11448 if (rpm != null) {
Hui Wang67af90e2021-06-04 16:57:15 -070011449 Boolean isCapable = rpm.isRcsVolteSingleRegistrationEnabled(subId);
11450 if (isCapable != null) {
11451 return isCapable;
11452 }
Hui Wang761a6682020-10-31 05:12:53 +000011453 }
Hui Wang67af90e2021-06-04 16:57:15 -070011454 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
11455 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000011456 } finally {
11457 Binder.restoreCallingIdentity(identity);
11458 }
11459 }
11460
11461 /**
11462 * Register RCS provisioning callback.
11463 */
11464 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080011465 public void registerRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000011466 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011467 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080011468 Binder.getCallingUid(), "registerRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000011469 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
11470 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000011471
11472 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11473 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11474 }
11475 if (!isImsAvailableOnDevice()) {
11476 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11477 "IMS not available on device.");
11478 }
11479
11480 final long identity = Binder.clearCallingIdentity();
11481 try {
Hui Wang68cd3722021-01-11 20:04:53 -080011482 if (!RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080011483 .registerRcsProvisioningCallback(subId, callback)) {
Brad Ebinger919631e2021-06-02 17:46:35 -070011484 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
11485 "Active subscription not found.");
Hui Wang68cd3722021-01-11 20:04:53 -080011486 }
Hui Wang761a6682020-10-31 05:12:53 +000011487 } finally {
11488 Binder.restoreCallingIdentity(identity);
11489 }
11490 }
11491
11492 /**
11493 * Unregister RCS provisioning callback.
11494 */
11495 @Override
Hui Wang3cac7e52021-01-27 14:45:25 -080011496 public void unregisterRcsProvisioningCallback(int subId,
Hui Wang761a6682020-10-31 05:12:53 +000011497 IRcsConfigCallback callback) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011498 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
Hui Wang3cac7e52021-01-27 14:45:25 -080011499 Binder.getCallingUid(), "unregisterRcsProvisioningCallback",
Brad Ebinger34c09a52021-02-17 23:23:21 +000011500 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION,
11501 permission.READ_PRIVILEGED_PHONE_STATE);
Hui Wang761a6682020-10-31 05:12:53 +000011502
11503 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11504 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11505 }
11506 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000011507 // operation failed silently
11508 Rlog.w(LOG_TAG, "IMS not available on device.");
11509 return;
Hui Wang761a6682020-10-31 05:12:53 +000011510 }
11511
11512 final long identity = Binder.clearCallingIdentity();
11513 try {
Hui Wang68cd3722021-01-11 20:04:53 -080011514 RcsProvisioningMonitor.getInstance()
Hui Wang3cac7e52021-01-27 14:45:25 -080011515 .unregisterRcsProvisioningCallback(subId, callback);
Hui Wang761a6682020-10-31 05:12:53 +000011516 } finally {
11517 Binder.restoreCallingIdentity(identity);
11518 }
11519 }
11520
11521 /**
11522 * trigger RCS reconfiguration.
11523 */
11524 public void triggerRcsReconfiguration(int subId) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011525 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
11526 "triggerRcsReconfiguration",
11527 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000011528
11529 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11530 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11531 }
11532 if (!isImsAvailableOnDevice()) {
joonhunshin46b49a32022-12-21 05:33:23 +000011533 // ProvisioningManager can not handle ServiceSpecificException.
11534 // Throw the IllegalStateException and annotate ProvisioningManager.
11535 throw new IllegalStateException("IMS not available on device.");
Hui Wang761a6682020-10-31 05:12:53 +000011536 }
11537
11538 final long identity = Binder.clearCallingIdentity();
11539 try {
11540 RcsProvisioningMonitor.getInstance().requestReconfig(subId);
11541 } finally {
11542 Binder.restoreCallingIdentity(identity);
11543 }
11544 }
11545
11546 /**
11547 * Provide the client configuration parameters of the RCS application.
11548 */
11549 public void setRcsClientConfiguration(int subId, RcsClientConfiguration rcc) {
Brad Ebinger34c09a52021-02-17 23:23:21 +000011550 TelephonyPermissions.enforceAnyPermissionGranted(mApp, Binder.getCallingUid(),
11551 "setRcsClientConfiguration",
11552 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
Hui Wang761a6682020-10-31 05:12:53 +000011553
11554 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
11555 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
11556 }
11557 if (!isImsAvailableOnDevice()) {
11558 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
11559 "IMS not available on device.");
11560 }
11561
11562 final long identity = Binder.clearCallingIdentity();
11563
11564 try {
11565 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
11566 if (configBinder == null) {
11567 Rlog.e(LOG_TAG, "null result for setRcsClientConfiguration");
Brad Ebinger919631e2021-06-02 17:46:35 -070011568 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION,
11569 "could not find the requested subscription");
Hui Wang761a6682020-10-31 05:12:53 +000011570 } else {
11571 configBinder.setRcsClientConfiguration(rcc);
11572 }
joonhunshin3e154242021-09-17 06:33:39 +000011573
11574 RcsStats.getInstance().onRcsClientProvisioningStats(subId,
11575 RCS_CLIENT_PROVISIONING_STATS__EVENT__CLIENT_PARAMS_SENT);
Hui Wang761a6682020-10-31 05:12:53 +000011576 } catch (RemoteException e) {
11577 Rlog.e(LOG_TAG, "fail to setRcsClientConfiguration " + e.getMessage());
Brad Ebinger919631e2021-06-02 17:46:35 -070011578 throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE,
11579 "service is temporarily unavailable.");
Hui Wang761a6682020-10-31 05:12:53 +000011580 } finally {
11581 Binder.restoreCallingIdentity(identity);
11582 }
11583 }
11584
11585 /**
Hui Wangbaaee6a2021-02-19 20:45:36 -080011586 * Enables or disables the test mode for RCS VoLTE single registration.
11587 */
11588 @Override
11589 public void setRcsSingleRegistrationTestModeEnabled(boolean enabled) {
11590 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11591 "setRcsSingleRegistrationTestModeEnabled");
11592
11593 RcsProvisioningMonitor.getInstance().setTestModeEnabled(enabled);
11594 }
11595
11596 /**
11597 * Gets the test mode for RCS VoLTE single registration.
11598 */
11599 @Override
11600 public boolean getRcsSingleRegistrationTestModeEnabled() {
11601 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11602 "getRcsSingleRegistrationTestModeEnabled");
11603
11604 return RcsProvisioningMonitor.getInstance().getTestModeEnabled();
11605 }
11606
11607 /**
Hui Wang761a6682020-10-31 05:12:53 +000011608 * Overrides the config of RCS VoLTE single registration enabled for the device.
11609 */
11610 @Override
11611 public void setDeviceSingleRegistrationEnabledOverride(String enabledStr) {
11612 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11613 "setDeviceSingleRegistrationEnabledOverride");
11614 enforceModifyPermission();
11615
11616 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11617 : Boolean.parseBoolean(enabledStr);
11618 RcsProvisioningMonitor.getInstance().overrideDeviceSingleRegistrationEnabled(enabled);
Brad Ebinger49a72b42021-01-29 00:55:24 +000011619 mApp.imsRcsController.setDeviceSingleRegistrationSupportOverride(enabled);
Hui Wang761a6682020-10-31 05:12:53 +000011620 }
11621
11622 /**
Tyler Gunn92479152021-01-20 16:30:10 -080011623 * Sends a device to device communication message. Only usable via shell.
11624 * @param message message to send.
11625 * @param value message value.
11626 */
11627 @Override
11628 public void sendDeviceToDeviceMessage(int message, int value) {
11629 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
Tyler Gunnbabbda02021-02-10 11:05:02 -080011630 "sendDeviceToDeviceMessage");
Tyler Gunn92479152021-01-20 16:30:10 -080011631 enforceModifyPermission();
11632
11633 final long identity = Binder.clearCallingIdentity();
11634 try {
11635 TelephonyConnectionService service =
11636 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
11637 if (service == null) {
11638 Rlog.e(LOG_TAG, "sendDeviceToDeviceMessage: not in a call.");
11639 return;
11640 }
11641 service.sendTestDeviceToDeviceMessage(message, value);
11642 } finally {
11643 Binder.restoreCallingIdentity(identity);
11644 }
11645 }
11646
Tyler Gunnbabbda02021-02-10 11:05:02 -080011647 /**
11648 * Sets the specified device to device transport active.
11649 * @param transport The transport to set active.
11650 */
11651 @Override
11652 public void setActiveDeviceToDeviceTransport(@NonNull String transport) {
11653 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11654 "setActiveDeviceToDeviceTransport");
11655 enforceModifyPermission();
11656
11657 final long identity = Binder.clearCallingIdentity();
11658 try {
11659 TelephonyConnectionService service =
11660 TelecomAccountRegistry.getInstance(null).getTelephonyConnectionService();
11661 if (service == null) {
11662 Rlog.e(LOG_TAG, "setActiveDeviceToDeviceTransport: not in a call.");
11663 return;
11664 }
11665 service.setActiveDeviceToDeviceTransport(transport);
11666 } finally {
11667 Binder.restoreCallingIdentity(identity);
11668 }
11669 }
Tyler Gunn92479152021-01-20 16:30:10 -080011670
Tyler Gunnd4339262021-05-03 14:46:49 -070011671 @Override
11672 public void setDeviceToDeviceForceEnabled(boolean isForceEnabled) {
11673 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11674 "setDeviceToDeviceForceEnabled");
11675
11676 final long identity = Binder.clearCallingIdentity();
11677 try {
11678 Arrays.stream(PhoneFactory.getPhones()).forEach(
11679 p -> {
11680 Phone thePhone = p.getImsPhone();
11681 if (thePhone != null && thePhone instanceof ImsPhone) {
11682 ImsPhone imsPhone = (ImsPhone) thePhone;
11683 CallTracker tracker = imsPhone.getCallTracker();
11684 if (tracker != null && tracker instanceof ImsPhoneCallTracker) {
11685 ImsPhoneCallTracker imsPhoneCallTracker =
11686 (ImsPhoneCallTracker) tracker;
11687 imsPhoneCallTracker.setDeviceToDeviceForceEnabled(isForceEnabled);
11688 }
11689 }
11690 }
11691 );
11692 } finally {
11693 Binder.restoreCallingIdentity(identity);
11694 }
11695 }
11696
Tyler Gunn92479152021-01-20 16:30:10 -080011697 /**
Hui Wang761a6682020-10-31 05:12:53 +000011698 * Gets the config of RCS VoLTE single registration enabled for the device.
11699 */
11700 @Override
11701 public boolean getDeviceSingleRegistrationEnabled() {
11702 enforceReadPrivilegedPermission("getDeviceSingleRegistrationEnabled");
11703 return RcsProvisioningMonitor.getInstance().getDeviceSingleRegistrationEnabled();
11704 }
11705
11706 /**
11707 * Overrides the config of RCS VoLTE single registration enabled for the carrier/subscription.
11708 */
11709 @Override
11710 public boolean setCarrierSingleRegistrationEnabledOverride(int subId, String enabledStr) {
11711 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11712 "setCarrierSingleRegistrationEnabledOverride");
11713 enforceModifyPermission();
11714
11715 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11716 : Boolean.parseBoolean(enabledStr);
11717 return RcsProvisioningMonitor.getInstance().overrideCarrierSingleRegistrationEnabled(
11718 subId, enabled);
11719 }
11720
11721 /**
11722 * Gets the config of RCS VoLTE single registration enabled for the carrier/subscription.
11723 */
11724 @Override
11725 public boolean getCarrierSingleRegistrationEnabled(int subId) {
11726 enforceReadPrivilegedPermission("getCarrierSingleRegistrationEnabled");
11727 return RcsProvisioningMonitor.getInstance().getCarrierSingleRegistrationEnabled(subId);
11728 }
Chiachang Wangd6d34772020-12-22 11:38:27 +080011729
11730 /**
Hui Wangb647abe2021-02-26 09:33:38 -080011731 * Overrides the ims feature validation result
11732 */
11733 @Override
11734 public boolean setImsFeatureValidationOverride(int subId, String enabledStr) {
11735 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11736 "setImsFeatureValidationOverride");
11737
11738 Boolean enabled = "NULL".equalsIgnoreCase(enabledStr) ? null
11739 : Boolean.parseBoolean(enabledStr);
11740 return RcsProvisioningMonitor.getInstance().overrideImsFeatureValidation(
11741 subId, enabled);
11742 }
11743
11744 /**
11745 * Gets the ims feature validation override value
11746 */
11747 @Override
11748 public boolean getImsFeatureValidationOverride(int subId) {
11749 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11750 "getImsFeatureValidationOverride");
11751 return RcsProvisioningMonitor.getInstance().getImsFeatureValidationOverride(subId);
11752 }
11753
11754 /**
Chiachang Wangd6d34772020-12-22 11:38:27 +080011755 * Get the mobile provisioning url that is used to launch a browser to allow users to manage
11756 * their mobile plan.
11757 */
11758 @Override
11759 public String getMobileProvisioningUrl() {
11760 enforceReadPrivilegedPermission("getMobileProvisioningUrl");
11761 final long identity = Binder.clearCallingIdentity();
11762 try {
11763 return getDefaultPhone().getMobileProvisioningUrl();
11764 } finally {
11765 Binder.restoreCallingIdentity(identity);
11766 }
11767 }
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011768
James.cf Linbcdf8b32021-01-14 16:44:13 +080011769 /**
calvinpane4a8a1d2021-01-25 13:51:18 +080011770 * Get the EAB contact from the EAB database.
11771 */
11772 @Override
11773 public String getContactFromEab(String contact) {
11774 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getContactFromEab");
11775 enforceModifyPermission();
11776 final long identity = Binder.clearCallingIdentity();
11777 try {
11778 return EabUtil.getContactFromEab(getDefaultPhone().getContext(), contact);
11779 } finally {
11780 Binder.restoreCallingIdentity(identity);
11781 }
11782 }
11783
11784 /**
Calvin Pana1434322021-07-01 19:27:01 +080011785 * Get the EAB capability from the EAB database.
11786 */
11787 @Override
11788 public String getCapabilityFromEab(String contact) {
11789 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getCapabilityFromEab");
11790 enforceModifyPermission();
11791 final long identity = Binder.clearCallingIdentity();
11792 try {
11793 return EabUtil.getCapabilityFromEab(getDefaultPhone().getContext(), contact);
11794 } finally {
11795 Binder.restoreCallingIdentity(identity);
11796 }
11797 }
11798
11799 /**
James.cf Linbcdf8b32021-01-14 16:44:13 +080011800 * Remove the EAB contacts from the EAB database.
11801 */
11802 @Override
11803 public int removeContactFromEab(int subId, String contacts) {
11804 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "removeCapabilitiesFromEab");
11805 enforceModifyPermission();
11806 final long identity = Binder.clearCallingIdentity();
11807 try {
11808 return EabUtil.removeContactFromEab(subId, contacts, getDefaultPhone().getContext());
11809 } finally {
11810 Binder.restoreCallingIdentity(identity);
11811 }
11812 }
11813
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011814 @Override
James.cf Lin4b784aa2021-01-31 03:25:15 +080011815 public boolean getDeviceUceEnabled() {
11816 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getDeviceUceEnabled");
11817 final long identity = Binder.clearCallingIdentity();
11818 try {
11819 return mApp.getDeviceUceEnabled();
11820 } finally {
11821 Binder.restoreCallingIdentity(identity);
11822 }
11823 }
11824
11825 @Override
11826 public void setDeviceUceEnabled(boolean isEnabled) {
11827 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setDeviceUceEnabled");
11828 final long identity = Binder.clearCallingIdentity();
11829 try {
11830 mApp.setDeviceUceEnabled(isEnabled);
11831 } finally {
11832 Binder.restoreCallingIdentity(identity);
11833 }
11834 }
11835
Brad Ebinger14d467f2021-02-12 06:18:28 +000011836 /**
11837 * Add new feature tags to the Set used to calculate the capabilities in PUBLISH.
11838 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11839 */
11840 // Used for SHELL command only right now.
11841 @Override
11842 public RcsContactUceCapability addUceRegistrationOverrideShell(int subId,
11843 List<String> featureTags) {
11844 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11845 "addUceRegistrationOverrideShell");
11846 final long identity = Binder.clearCallingIdentity();
11847 try {
11848 return mApp.imsRcsController.addUceRegistrationOverrideShell(subId,
11849 new ArraySet<>(featureTags));
11850 } catch (ImsException e) {
11851 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11852 } finally {
11853 Binder.restoreCallingIdentity(identity);
11854 }
11855 }
11856
11857 /**
11858 * Remove existing feature tags to the Set used to calculate the capabilities in PUBLISH.
11859 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11860 */
11861 // Used for SHELL command only right now.
11862 @Override
11863 public RcsContactUceCapability removeUceRegistrationOverrideShell(int subId,
11864 List<String> featureTags) {
11865 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11866 "removeUceRegistrationOverrideShell");
11867 final long identity = Binder.clearCallingIdentity();
11868 try {
11869 return mApp.imsRcsController.removeUceRegistrationOverrideShell(subId,
11870 new ArraySet<>(featureTags));
11871 } catch (ImsException e) {
11872 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11873 } finally {
11874 Binder.restoreCallingIdentity(identity);
11875 }
11876 }
11877
11878 /**
11879 * Clear all overrides in the Set used to calculate the capabilities in PUBLISH.
11880 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11881 */
11882 // Used for SHELL command only right now.
11883 @Override
11884 public RcsContactUceCapability clearUceRegistrationOverrideShell(int subId) {
11885 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11886 "clearUceRegistrationOverrideShell");
11887 final long identity = Binder.clearCallingIdentity();
11888 try {
11889 return mApp.imsRcsController.clearUceRegistrationOverrideShell(subId);
11890 } catch (ImsException e) {
11891 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11892 } finally {
11893 Binder.restoreCallingIdentity(identity);
11894 }
11895 }
11896
11897 /**
11898 * @return current RcsContactUceCapability instance that will be used for PUBLISH.
11899 */
11900 // Used for SHELL command only right now.
11901 @Override
11902 public RcsContactUceCapability getLatestRcsContactUceCapabilityShell(int subId) {
11903 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
11904 "getLatestRcsContactUceCapabilityShell");
11905 final long identity = Binder.clearCallingIdentity();
11906 try {
11907 return mApp.imsRcsController.getLatestRcsContactUceCapabilityShell(subId);
11908 } catch (ImsException e) {
11909 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11910 } finally {
11911 Binder.restoreCallingIdentity(identity);
11912 }
11913 }
11914
11915 /**
11916 * Returns the last PIDF XML sent to the network during the last PUBLISH or "none" if the
11917 * device does not have an active PUBLISH.
11918 */
11919 // Used for SHELL command only right now.
11920 @Override
11921 public String getLastUcePidfXmlShell(int subId) {
11922 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceGetLastPidfXml");
11923 final long identity = Binder.clearCallingIdentity();
11924 try {
11925 return mApp.imsRcsController.getLastUcePidfXmlShell(subId);
11926 } catch (ImsException e) {
11927 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11928 } finally {
11929 Binder.restoreCallingIdentity(identity);
11930 }
11931 }
11932
James.cf Line8713a42021-04-29 16:04:26 +080011933 /**
11934 * Remove UCE requests cannot be sent to the network status.
11935 */
11936 // Used for SHELL command only right now.
11937 @Override
11938 public boolean removeUceRequestDisallowedStatus(int subId) {
11939 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "uceRemoveDisallowedStatus");
11940 final long identity = Binder.clearCallingIdentity();
11941 try {
11942 return mApp.imsRcsController.removeUceRequestDisallowedStatus(subId);
11943 } catch (ImsException e) {
11944 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11945 } finally {
11946 Binder.restoreCallingIdentity(identity);
11947 }
11948 }
11949
James.cf Lin18bb9002021-05-25 01:37:38 +080011950 /**
11951 * Remove UCE requests cannot be sent to the network status.
11952 */
11953 // Used for SHELL command only.
11954 @Override
11955 public boolean setCapabilitiesRequestTimeout(int subId, long timeoutAfterMs) {
11956 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCapRequestTimeout");
11957 final long identity = Binder.clearCallingIdentity();
11958 try {
11959 return mApp.imsRcsController.setCapabilitiesRequestTimeout(subId, timeoutAfterMs);
11960 } catch (ImsException e) {
11961 throw new ServiceSpecificException(e.getCode(), e.getMessage());
11962 } finally {
11963 Binder.restoreCallingIdentity(identity);
11964 }
11965 }
Brad Ebinger14d467f2021-02-12 06:18:28 +000011966
James.cf Lin4b784aa2021-01-31 03:25:15 +080011967 @Override
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011968 public void setSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11969 String callingPackage) {
11970 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11971 mApp, subId, "setSignalStrengthUpdateRequest");
11972
11973 final int callingUid = Binder.getCallingUid();
11974 // Verify that tha callingPackage belongs to the calling UID
11975 mApp.getSystemService(AppOpsManager.class)
11976 .checkPackage(callingUid, callingPackage);
11977
Rambo Wang3607f502021-02-01 21:51:40 -080011978 validateSignalStrengthUpdateRequest(mApp, request, callingUid);
Rambo Wanga5cc9b72021-01-07 10:51:54 -080011979
11980 final long identity = Binder.clearCallingIdentity();
11981 try {
11982 Object result = sendRequest(CMD_SET_SIGNAL_STRENGTH_UPDATE_REQUEST,
11983 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
11984
11985 if (result instanceof IllegalStateException) {
11986 throw (IllegalStateException) result;
11987 }
11988 } finally {
11989 Binder.restoreCallingIdentity(identity);
11990 }
11991 }
11992
11993 @Override
11994 public void clearSignalStrengthUpdateRequest(int subId, SignalStrengthUpdateRequest request,
11995 String callingPackage) {
11996 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
11997 mApp, subId, "clearSignalStrengthUpdateRequest");
11998
11999 final int callingUid = Binder.getCallingUid();
12000 // Verify that tha callingPackage belongs to the calling UID
12001 mApp.getSystemService(AppOpsManager.class)
12002 .checkPackage(callingUid, callingPackage);
12003
12004 final long identity = Binder.clearCallingIdentity();
12005 try {
12006 Object result = sendRequest(CMD_CLEAR_SIGNAL_STRENGTH_UPDATE_REQUEST,
12007 new Pair<Integer, SignalStrengthUpdateRequest>(callingUid, request), subId);
12008
12009 if (result instanceof IllegalStateException) {
12010 throw (IllegalStateException) result;
12011 }
12012 } finally {
12013 Binder.restoreCallingIdentity(identity);
12014 }
12015 }
12016
Rambo Wang3607f502021-02-01 21:51:40 -080012017 private static void validateSignalStrengthUpdateRequest(Context context,
12018 SignalStrengthUpdateRequest request, int callingUid) {
Rambo Wanga5cc9b72021-01-07 10:51:54 -080012019 if (callingUid == Process.PHONE_UID || callingUid == Process.SYSTEM_UID) {
12020 // phone/system process do not have further restriction on request
12021 return;
12022 }
12023
12024 // Applications has restrictions on how to use the request:
Rambo Wang3607f502021-02-01 21:51:40 -080012025 // Non-system callers need permission to set mIsSystemThresholdReportingRequestedWhileIdle
Rambo Wanga5cc9b72021-01-07 10:51:54 -080012026 if (request.isSystemThresholdReportingRequestedWhileIdle()) {
Rambo Wang3607f502021-02-01 21:51:40 -080012027 context.enforceCallingOrSelfPermission(
12028 android.Manifest.permission.LISTEN_ALWAYS_REPORTED_SIGNAL_STRENGTH,
12029 "validateSignalStrengthUpdateRequest");
Rambo Wanga5cc9b72021-01-07 10:51:54 -080012030 }
12031
12032 for (SignalThresholdInfo info : request.getSignalThresholdInfos()) {
Nagendra Prasad Nagarle Basavarajufee544c2022-12-07 16:34:52 +000012033 // Only system caller can set mHysteresisMs/mIsEnabled.
Rambo Wanga5cc9b72021-01-07 10:51:54 -080012034 if (info.getHysteresisMs() != SignalThresholdInfo.HYSTERESIS_MS_DISABLED
Rambo Wanga5cc9b72021-01-07 10:51:54 -080012035 || info.isEnabled()) {
12036 throw new IllegalArgumentException(
12037 "Only system can set hide fields in SignalThresholdInfo");
12038 }
12039
12040 // Thresholds length for each RAN need in range. This has been validated in
12041 // SignalThresholdInfo#Builder#setThreshold. Here we prevent apps calling hide method
12042 // setThresholdUnlimited (e.g. through reflection) with too short or too long thresholds
12043 final int[] thresholds = info.getThresholds();
12044 Objects.requireNonNull(thresholds);
12045 if (thresholds.length < SignalThresholdInfo.getMinimumNumberOfThresholdsAllowed()
12046 || thresholds.length
12047 > SignalThresholdInfo.getMaximumNumberOfThresholdsAllowed()) {
12048 throw new IllegalArgumentException(
12049 "thresholds length is out of range: " + thresholds.length);
12050 }
12051 }
12052 }
SongFerngWang8236caa2021-01-17 21:51:44 +080012053
12054 /**
12055 * Gets the current phone capability.
12056 *
12057 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
12058 * @return the PhoneCapability which describes the data connection capability of modem.
12059 * It's used to evaluate possible phone config change, for example from single
12060 * SIM device to multi-SIM device.
12061 */
12062 @Override
12063 public PhoneCapability getPhoneCapability() {
12064 enforceReadPrivilegedPermission("getPhoneCapability");
12065 final long identity = Binder.clearCallingIdentity();
12066 try {
12067 return mPhoneConfigurationManager.getCurrentPhoneCapability();
12068 } finally {
12069 Binder.restoreCallingIdentity(identity);
12070 }
12071 }
Michele Berionne5e411512020-11-13 02:36:59 +000012072
12073 /**
12074 * Prepare TelephonyManager for an unattended reboot. The reboot is
12075 * required to be done shortly after the API is invoked.
12076 */
12077 @Override
12078 @TelephonyManager.PrepareUnattendedRebootResult
12079 public int prepareForUnattendedReboot() {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000012080 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Michele Berionne5e411512020-11-13 02:36:59 +000012081 enforceRebootPermission();
12082
12083 final long identity = Binder.clearCallingIdentity();
12084 try {
Rafael Higuera Silvad9630642021-09-20 15:32:01 +000012085 return (int) sendRequest(CMD_PREPARE_UNATTENDED_REBOOT, null, workSource);
Michele Berionne5e411512020-11-13 02:36:59 +000012086 } finally {
12087 Binder.restoreCallingIdentity(identity);
12088 }
12089 }
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080012090
12091 /**
12092 * Request to get the current slicing configuration including URSP rules and
12093 * NSSAIs (configured, allowed and rejected).
12094 *
12095 * Requires carrier privileges or READ_PRIVILEGED_PHONE_STATE permission.
12096 */
12097 @Override
12098 public void getSlicingConfig(ResultReceiver callback) {
Hongbo Zeng1b2063d2022-02-21 01:33:03 +000012099 TelephonyPermissions
12100 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
12101 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, "getSlicingConfig");
Hongbo Zeng156aa4a2021-02-08 21:50:28 +080012102
12103 final long identity = Binder.clearCallingIdentity();
12104 try {
12105 Phone phone = getDefaultPhone();
12106 sendRequestAsync(CMD_GET_SLICING_CONFIG, callback, phone, null);
12107 } finally {
12108 Binder.restoreCallingIdentity(identity);
12109 }
12110 }
Hunsuk Choi3b742d62021-10-25 19:48:34 +000012111
12112 /**
Sarah Chin2ec39f62022-08-31 17:03:26 -070012113 * Check whether the given premium capability is available for purchase from the carrier.
12114 *
12115 * @param capability The premium capability to check.
12116 * @param subId The subId to check the premium capability for.
12117 *
12118 * @return Whether the given premium capability is available to purchase.
12119 */
12120 @Override
12121 public boolean isPremiumCapabilityAvailableForPurchase(int capability, int subId) {
12122 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
12123 mApp, "isPremiumCapabilityAvailableForPurchase")) {
12124 log("Premium capability "
12125 + TelephonyManager.convertPremiumCapabilityToString(capability)
12126 + " is not available for purchase due to missing permissions.");
12127 throw new SecurityException("isPremiumCapabilityAvailableForPurchase requires "
12128 + "permission READ_BASIC_PHONE_STATE.");
12129 }
12130
12131 Phone phone = getPhone(subId);
Thomas Nguyen7216ed62022-11-29 16:45:31 -080012132 if (phone == null) {
12133 loge("isPremiumCapabilityAvailableForPurchase: phone is null, subId=" + subId);
12134 return false;
12135 }
Sarah Chin2ec39f62022-08-31 17:03:26 -070012136 final long identity = Binder.clearCallingIdentity();
12137 try {
Sarah Chin46355ba2022-11-01 23:51:16 -070012138 return SlicePurchaseController.getInstance(phone)
Sarah Chin2ec39f62022-08-31 17:03:26 -070012139 .isPremiumCapabilityAvailableForPurchase(capability);
12140 } finally {
12141 Binder.restoreCallingIdentity(identity);
12142 }
12143 }
12144
12145 /**
12146 * Purchase the given premium capability from the carrier.
12147 *
12148 * @param capability The premium capability to purchase.
12149 * @param callback The result of the purchase request.
12150 * @param subId The subId to purchase the premium capability for.
12151 */
12152 @Override
12153 public void purchasePremiumCapability(int capability, IIntegerConsumer callback, int subId) {
12154 log("purchasePremiumCapability: capability="
12155 + TelephonyManager.convertPremiumCapabilityToString(capability) + ", caller="
12156 + getCurrentPackageName());
12157
12158 if (!TelephonyPermissions.checkCallingOrSelfReadNonDangerousPhoneStateNoThrow(
12159 mApp, "purchasePremiumCapability")) {
12160 log("purchasePremiumCapability "
12161 + TelephonyManager.convertPremiumCapabilityToString(capability)
12162 + " failed due to missing permissions.");
12163 throw new SecurityException("purchasePremiumCapability requires permission "
12164 + "READ_BASIC_PHONE_STATE.");
Sarah Chin532d6bb2022-12-28 22:50:43 -080012165 } else if (!TelephonyPermissions.checkInternetPermissionNoThrow(
12166 mApp, "purchasePremiumCapability")) {
12167 log("purchasePremiumCapability "
12168 + TelephonyManager.convertPremiumCapabilityToString(capability)
12169 + " failed due to missing permissions.");
12170 throw new SecurityException("purchasePremiumCapability requires permission INTERNET.");
Sarah Chin2ec39f62022-08-31 17:03:26 -070012171 }
12172
12173 Phone phone = getPhone(subId);
Sarah Chin19694112022-12-06 15:41:37 -080012174 if (phone == null) {
12175 try {
12176 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED;
12177 callback.accept(result);
12178 loge("purchasePremiumCapability: phone is null, subId=" + subId);
12179 } catch (RemoteException e) {
12180 String logStr = "Purchase premium capability "
12181 + TelephonyManager.convertPremiumCapabilityToString(capability)
12182 + " failed due to RemoteException handling null phone: " + e;
12183 if (DBG) log(logStr);
12184 AnomalyReporter.reportAnomaly(
12185 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
12186 }
12187 return;
12188 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080012189
12190 String callingProcess;
Sarah Chin71b3a852022-09-28 15:54:19 -070012191 try {
Sarah Chin532d6bb2022-12-28 22:50:43 -080012192 callingProcess = mApp.getPackageManager().getApplicationInfo(
12193 getCurrentPackageName(), 0).processName;
Sarah Chin71b3a852022-09-28 15:54:19 -070012194 } catch (PackageManager.NameNotFoundException e) {
Sarah Chin532d6bb2022-12-28 22:50:43 -080012195 callingProcess = getCurrentPackageName();
Sarah Chin71b3a852022-09-28 15:54:19 -070012196 }
Sarah Chin532d6bb2022-12-28 22:50:43 -080012197
12198 boolean isVisible = false;
12199 ActivityManager am = mApp.getSystemService(ActivityManager.class);
12200 if (am != null) {
12201 List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
12202 if (processes != null) {
12203 for (ActivityManager.RunningAppProcessInfo process : processes) {
12204 log("purchasePremiumCapability: process " + process.processName
12205 + "has importance " + process.importance);
12206 if (process.processName.equals(callingProcess) && process.importance
12207 <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
12208 isVisible = true;
12209 break;
12210 }
12211 }
12212 }
12213 }
12214
12215 if (!isVisible) {
12216 try {
12217 int result = TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_FOREGROUND;
12218 callback.accept(result);
12219 loge("purchasePremiumCapability: " + callingProcess + " is not in the foreground.");
12220 } catch (RemoteException e) {
12221 String logStr = "Purchase premium capability "
12222 + TelephonyManager.convertPremiumCapabilityToString(capability)
12223 + " failed due to RemoteException handling background application: " + e;
12224 if (DBG) log(logStr);
12225 AnomalyReporter.reportAnomaly(
12226 UUID.fromString(PURCHASE_PREMIUM_CAPABILITY_ERROR_UUID), logStr);
12227 }
12228 return;
12229 }
12230
Sarah Chin71b3a852022-09-28 15:54:19 -070012231 sendRequestAsync(CMD_PURCHASE_PREMIUM_CAPABILITY,
Sarah Chinb8218c22023-01-04 13:35:29 -080012232 new PurchasePremiumCapabilityArgument(capability, callback), phone, null);
Sarah Chin2ec39f62022-08-31 17:03:26 -070012233 }
12234
12235 /**
Hunsuk Choi3b742d62021-10-25 19:48:34 +000012236 * Register an IMS connection state callback
12237 */
12238 @Override
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000012239 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
12240 String callingPackage) {
Hunsuk Choi3b742d62021-10-25 19:48:34 +000012241 if (feature == ImsFeature.FEATURE_MMTEL) {
12242 // ImsMmTelManager
12243 // The following also checks READ_PRIVILEGED_PHONE_STATE.
12244 TelephonyPermissions
12245 .enforceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
12246 mApp, subId, "registerImsStateCallback");
12247 } else if (feature == ImsFeature.FEATURE_RCS) {
12248 // ImsRcsManager or SipDelegateManager
12249 TelephonyPermissions.enforceAnyPermissionGrantedOrCarrierPrivileges(mApp, subId,
12250 Binder.getCallingUid(), "registerImsStateCallback",
12251 Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
12252 Manifest.permission.READ_PRECISE_PHONE_STATE,
12253 Manifest.permission.ACCESS_RCS_USER_CAPABILITY_EXCHANGE,
12254 Manifest.permission.PERFORM_IMS_SINGLE_REGISTRATION);
12255 }
12256
12257 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
12258 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
12259 "IMS not available on device.");
12260 }
12261
12262 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
12263 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
12264 }
12265
12266 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
12267 if (controller == null) {
12268 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
12269 "IMS not available on device.");
12270 }
12271
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000012272 if (callingPackage == null) {
12273 callingPackage = getCurrentPackageName();
12274 }
12275
Hunsuk Choi3b742d62021-10-25 19:48:34 +000012276 final long token = Binder.clearCallingIdentity();
12277 try {
12278 int slotId = getSlotIndexOrException(subId);
Hunsuk Choi89bd22c2021-11-01 13:04:54 +000012279 controller.registerImsStateCallback(subId, feature, cb, callingPackage);
Hunsuk Choi3b742d62021-10-25 19:48:34 +000012280 } catch (ImsException e) {
12281 throw new ServiceSpecificException(e.getCode());
12282 } finally {
12283 Binder.restoreCallingIdentity(token);
12284 }
12285 }
12286
12287 /**
12288 * Unregister an IMS connection state callback
12289 */
12290 @Override
12291 public void unregisterImsStateCallback(IImsStateCallback cb) {
12292 final long token = Binder.clearCallingIdentity();
12293 ImsStateCallbackController controller = ImsStateCallbackController.getInstance();
12294 if (controller == null) {
12295 return;
12296 }
12297 try {
12298 controller.unregisterImsStateCallback(cb);
12299 } finally {
12300 Binder.restoreCallingIdentity(token);
12301 }
12302 }
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070012303
12304 /**
12305 * @return {@CellIdentity} last known cell identity {@CellIdentity}.
12306 *
12307 * Require {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and
12308 * com.android.phone.permission.ACCESS_LAST_KNOWN_CELL_ID, otherwise throws
12309 * SecurityException.
12310 * If there is current registered network this value will be same as the registered cell
12311 * identity. If the device goes out of service the previous cell identity is cached and
12312 * will be returned. If the cache age of the Cell identity is more than 24 hours
12313 * it will be cleared and null will be returned.
12314 *
12315 */
12316 @Override
12317 public @Nullable CellIdentity getLastKnownCellIdentity(int subId, String callingPackage,
12318 String callingFeatureId) {
12319 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
12320 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
12321 LocationAccessPolicy.checkLocationPermission(mApp,
12322 new LocationAccessPolicy.LocationPermissionQuery.Builder()
12323 .setCallingPackage(callingPackage)
12324 .setCallingFeatureId(callingFeatureId)
12325 .setCallingPid(Binder.getCallingPid())
12326 .setCallingUid(Binder.getCallingUid())
12327 .setMethod("getLastKnownCellIdentity")
12328 .setLogAsInfo(true)
12329 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
12330 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
12331 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
12332 .build());
12333
12334 boolean hasFinePermission =
12335 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
12336 if (!hasFinePermission
12337 || !TelephonyPermissions.checkLastKnownCellIdAccessPermission(mApp)) {
12338 throw new SecurityException("getLastKnownCellIdentity need ACCESS_FINE_LOCATION "
Rambo Wang918993a2022-04-27 09:08:36 -070012339 + "and ACCESS_LAST_KNOWN_CELL_ID permission.");
Sooraj Sasindranfae41b32021-10-26 02:10:05 -070012340 }
12341
12342 final long identity = Binder.clearCallingIdentity();
12343 try {
12344 Phone phone = getPhone(subId);
12345 if (phone == null) return null;
12346 ServiceStateTracker sst = phone.getServiceStateTracker();
12347 if (sst == null) return null;
12348 return sst.getLastKnownCellIdentity();
12349 } finally {
12350 Binder.restoreCallingIdentity(identity);
12351 }
12352 }
Jack Yu4c0a5502021-12-03 23:58:26 -080012353
jimsun3b9ccac2021-10-26 15:01:23 +080012354 /**
12355 * Sets the modem service class Name that Telephony will bind to.
12356 *
12357 * @param serviceName The class name of the modem service.
12358 * @return true if the operation is succeed, otherwise false.
12359 */
12360 public boolean setModemService(String serviceName) {
12361 Log.d(LOG_TAG, "setModemService - " + serviceName);
12362 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setModemService");
12363 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012364 SubscriptionManager.INVALID_SUBSCRIPTION_ID,
12365 "setModemService");
jimsun3b9ccac2021-10-26 15:01:23 +080012366 return mPhoneConfigurationManager.setModemService(serviceName);
12367 }
12368
12369 /**
12370 * Return the class name of the currently bounded modem service.
12371 *
12372 * @return the class name of the modem service.
12373 */
12374 public String getModemService() {
12375 String result;
12376 Log.d(LOG_TAG, "getModemService");
12377 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "getModemService");
12378 TelephonyPermissions
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012379 .enforceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
jimsun3b9ccac2021-10-26 15:01:23 +080012380 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
12381 "getModemService");
12382 result = mPhoneConfigurationManager.getModemService();
12383 Log.d(LOG_TAG, "result = " + result);
12384 return result;
12385 }
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080012386
12387 @Override
12388 public void setVoiceServiceStateOverride(int subId, boolean hasService, String callingPackage) {
12389 // Only telecom (and shell, for CTS purposes) is allowed to call this method.
12390 mApp.enforceCallingOrSelfPermission(
12391 permission.BIND_TELECOM_CONNECTION_SERVICE, "setVoiceServiceStateOverride");
12392 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
12393
12394 final long identity = Binder.clearCallingIdentity();
12395 try {
12396 Phone phone = getPhone(subId);
12397 if (phone == null) return;
Grant Menke63ade122023-01-20 14:31:54 -080012398 Log.i(LOG_TAG, "setVoiceServiceStateOverride: subId=" + subId + ", phone=" + phone
12399 + ", hasService=" + hasService + ", callingPackage=" + callingPackage);
Hunter Knepshield2b076fa2022-01-19 02:26:22 -080012400 phone.setVoiceServiceStateOverride(hasService);
12401 } finally {
12402 Binder.restoreCallingIdentity(identity);
12403 }
12404 }
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000012405
12406 /**
12407 * set removable eSIM as default eUICC.
12408 *
12409 * @hide
12410 */
12411 @Override
12412 public void setRemovableEsimAsDefaultEuicc(boolean isDefault, String callingPackage) {
12413 enforceModifyPermission();
12414 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
12415
12416 final long identity = Binder.clearCallingIdentity();
12417 try {
12418 UiccController.getInstance().setRemovableEsimAsDefaultEuicc(isDefault);
12419 } finally {
12420 Binder.restoreCallingIdentity(identity);
12421 }
12422 }
12423
12424 /**
12425 * Returns whether the removable eSIM is default eUICC or not.
12426 *
12427 * @hide
12428 */
12429 @Override
12430 public boolean isRemovableEsimDefaultEuicc(String callingPackage) {
12431 enforceReadPrivilegedPermission("isRemovableEsimDefaultEuicc");
12432 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
12433
12434 final long identity = Binder.clearCallingIdentity();
12435 try {
12436 return UiccController.getInstance().isRemovableEsimDefaultEuicc();
12437 } finally {
12438 Binder.restoreCallingIdentity(identity);
12439 }
12440 }
12441
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000012442 /**
12443 * Get the component name of the default app to direct respond-via-message intent for the
12444 * user associated with this subscription, update the cache if there is no respond-via-message
12445 * application currently configured for this user.
12446 * @return component name of the app and class to direct Respond Via Message intent to, or
12447 * {@code null} if the functionality is not supported.
12448 * @hide
12449 */
12450 @Override
12451 public @Nullable ComponentName getDefaultRespondViaMessageApplication(int subId,
12452 boolean updateIfNeeded) {
12453 enforceInteractAcrossUsersPermission("getDefaultRespondViaMessageApplication");
Muralidhar Reddy4e5a8012022-05-11 14:49:00 +000012454
Aishwarya Mallampati5e581e12023-01-17 21:57:06 +000012455 Context context = getPhoneFromSubIdOrDefault(subId).getContext();
12456
Aishwarya Mallampatifbc70d32022-11-10 20:33:02 +000012457 UserHandle userHandle = null;
12458 final long identity = Binder.clearCallingIdentity();
12459 try {
12460 userHandle = TelephonyUtils.getSubscriptionUserHandle(context, subId);
12461 } finally {
12462 Binder.restoreCallingIdentity(identity);
12463 }
12464 return SmsApplication.getDefaultRespondViaMessageApplicationAsUser(context,
12465 updateIfNeeded, userHandle);
12466 }
Jack Yuf5badd92022-12-08 00:50:53 -080012467
12468 /**
Gil Cukierman1c0eb932022-12-06 22:28:24 +000012469 * Set whether the device is able to connect with null ciphering or integrity
12470 * algorithms. This is a global setting and will apply to all active subscriptions
12471 * and all new subscriptions after this.
12472 *
12473 * @param enabled when true, null cipher and integrity algorithms are allowed.
12474 * @hide
12475 */
12476 @Override
12477 @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
12478 public void setNullCipherAndIntegrityEnabled(boolean enabled) {
12479 enforceModifyPermission();
12480 checkForNullCipherAndIntegritySupport();
12481
12482 // Persist the state of our preference. Each GsmCdmaPhone instance is responsible
12483 // for listening to these preference changes and applying them immediately.
12484 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
12485 editor.putBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, enabled);
12486 editor.apply();
12487
12488 for (Phone phone: PhoneFactory.getPhones()) {
12489 phone.handleNullCipherEnabledChange();
12490 }
12491 }
12492
12493
12494 /**
12495 * Get whether the device is able to connect with null ciphering or integrity
12496 * algorithms. Note that this retrieves the phone-global preference and not
12497 * the state of the radio.
12498 *
12499 * @throws SecurityException if {@link permission#MODIFY_PHONE_STATE} is not satisfied
12500 * @throws UnsupportedOperationException if the device does not support the minimum HAL
12501 * version for this feature.
12502 * @hide
12503 */
12504 @Override
12505 @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
12506 public boolean isNullCipherAndIntegrityPreferenceEnabled() {
12507 enforceReadPermission();
12508 checkForNullCipherAndIntegritySupport();
12509 return getDefaultPhone().getNullCipherAndIntegrityEnabledPreference();
12510 }
12511
12512 private void checkForNullCipherAndIntegritySupport() {
12513 if (getHalVersion(HAL_SERVICE_NETWORK) < MIN_NULL_CIPHER_AND_INTEGRITY_VERSION) {
12514 throw new UnsupportedOperationException(
12515 "Null cipher and integrity operations require HAL 2.1 or above");
12516 }
Gil Cukierman92cc7db2023-01-06 19:25:53 +000012517 if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
12518 throw new UnsupportedOperationException(
12519 "Null cipher and integrity operations unsupported by modem");
12520 }
Gil Cukierman1c0eb932022-12-06 22:28:24 +000012521 }
12522
12523 /**
Jack Yuf5badd92022-12-08 00:50:53 -080012524 * Get the SIM state for the slot index.
12525 * For Remote-SIMs, this method returns {@link IccCardConstants.State#UNKNOWN}
12526 *
12527 * @return SIM state as the ordinal of {@link IccCardConstants.State}
12528 */
12529 @Override
12530 @SimState
12531 public int getSimStateForSlotIndex(int slotIndex) {
12532 IccCardConstants.State simState;
12533 if (slotIndex < 0) {
12534 simState = IccCardConstants.State.UNKNOWN;
12535 } else {
12536 Phone phone = null;
12537 try {
12538 phone = PhoneFactory.getPhone(slotIndex);
12539 } catch (IllegalStateException e) {
12540 // ignore
12541 }
12542 if (phone == null) {
12543 simState = IccCardConstants.State.UNKNOWN;
12544 } else {
12545 IccCard icc = phone.getIccCard();
12546 if (icc == null) {
12547 simState = IccCardConstants.State.UNKNOWN;
12548 } else {
12549 simState = icc.getState();
12550 }
12551 }
12552 }
12553 return simState.ordinal();
12554 }
Hui Wang9b5793a2022-12-05 14:38:06 -060012555
12556 /**
12557 * Get current cell broadcast ranges.
12558 */
12559 @Override
12560 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
12561 public List<CellBroadcastIdRange> getCellBroadcastIdRanges(int subId) {
12562 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
12563 "getCellBroadcastIdRanges");
12564 final long identity = Binder.clearCallingIdentity();
12565 try {
12566 return getPhone(subId).getCellBroadcastIdRanges();
12567 } finally {
12568 Binder.restoreCallingIdentity(identity);
12569 }
12570 }
12571
12572 /**
12573 * Set reception of cell broadcast messages with the list of the given ranges
12574 *
12575 * @param ranges the list of {@link CellBroadcastIdRange} to be enabled
12576 */
12577 @Override
12578 @RequiresPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS)
12579 public void setCellBroadcastIdRanges(int subId, @NonNull List<CellBroadcastIdRange> ranges,
12580 @Nullable IIntegerConsumer callback) {
12581 mApp.enforceCallingPermission(android.Manifest.permission.MODIFY_CELL_BROADCASTS,
12582 "setCellBroadcastIdRanges");
12583 final long identity = Binder.clearCallingIdentity();
12584 try {
12585 Phone phone = getPhoneFromSubId(subId);
12586 if (DBG) {
12587 log("setCellBroadcastIdRanges for subId :" + subId + ", phone:" + phone);
12588 }
12589 phone.setCellBroadcastIdRanges(ranges, result -> {
12590 if (callback != null) {
12591 try {
12592 callback.accept(result);
12593 } catch (RemoteException e) {
12594 Log.w(LOG_TAG, "setCellBroadcastIdRanges: callback not available.");
12595 }
12596 }
12597 });
12598 } finally {
12599 Binder.restoreCallingIdentity(identity);
12600 }
12601 }
Hunsuk Choi42cc62a2022-10-16 06:03:40 +000012602
12603 /**
12604 * Returns whether the device supports the domain selection service.
12605 *
12606 * @return {@code true} if the device supports the domain selection service.
12607 */
12608 @Override
12609 public boolean isDomainSelectionSupported() {
12610 mApp.enforceCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
12611 "isDomainSelectionSupported");
12612
12613 final long identity = Binder.clearCallingIdentity();
12614 try {
12615 return DomainSelectionResolver.getInstance().isDomainSelectionSupported();
12616 } finally {
12617 Binder.restoreCallingIdentity(identity);
12618 }
12619 }
arunvoddud5c6ce02022-12-11 06:03:12 +000012620
12621 /**
Sarah Chin503828c2023-02-01 23:54:20 -080012622 * Power on or off the satellite modem.
12623 *
12624 * @param subId The subId to set satellite power for.
12625 * @param powerOn {@code true} to power on the satellite modem and {@code false} to power off.
12626 * @return The result of the operation.
12627 *
12628 * @throws SecurityException if the caller doesn't have the required permission.
12629 */
12630 @Override
12631 @SatelliteManager.SatelliteServiceResult public int setSatellitePower(int subId,
12632 boolean powerOn) {
12633 enforceSatelliteCommunicationPermission("setSatellitePower");
12634
12635 final int validSubId = getValidSatelliteSubId(subId);
12636 if (!isSatelliteEnabled(validSubId)) {
12637 return SatelliteManager.SATELLITE_SERVICE_DISABLED;
12638 }
12639
12640 Phone phone = getPhoneOrDefault(validSubId, "setSatellitePower");
12641 if (phone == null) {
12642 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
12643 }
12644
12645 int result = (int) sendRequest(CMD_SET_SATELLITE_POWER, powerOn, subId);
12646 if (DBG) log("setSatellitePower result: " + result);
12647 return result;
12648 }
12649
12650 /**
12651 * Check whether the satellite modem is powered on.
12652 *
12653 * @param subId The subId to check satellite power for.
12654 * @param callback The callback that will be used to send the result if the operation is
12655 * successful. Returns {@code true} if the satellite modem is powered on and
12656 * {@code false} otherwise.
12657 * @return The result of the operation.
12658 *
12659 * @throws SecurityException if the caller doesn't have the required permission.
12660 */
12661 @Override
12662 @SatelliteManager.SatelliteServiceResult public int isSatellitePowerOn(int subId,
12663 @NonNull IBooleanConsumer callback) {
12664 enforceSatelliteCommunicationPermission("isSatellitePowerOn");
12665
12666 final int validSubId = getValidSatelliteSubId(subId);
12667 if (!isSatelliteEnabled(validSubId)) {
12668 return SatelliteManager.SATELLITE_SERVICE_DISABLED;
12669 }
12670
12671 Phone phone = getPhoneOrDefault(validSubId, "isSatellitePowerOn");
12672 if (phone == null) {
12673 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
12674 }
12675
12676 Consumer<Boolean> argument = FunctionalUtils.ignoreRemoteException(callback::accept);
12677 int result = (int) sendRequest(CMD_IS_SATELLITE_POWER_ON, argument, subId);
12678 if (DBG) log("isSatellitePowerOn result: " + result);
12679 return result;
12680 }
12681
12682 /**
12683 * Check whether the satellite service is supported on the device.
12684 *
12685 * @param subId The subId to check satellite service support for.
12686 * @param callback The callback that will be used to send the result if the operation is
12687 * successful. Returns {@code true} if the satellite service is supported on
12688 * the device and {@code false} otherwise.
12689 * @return The result of the operation.
12690 *
12691 * @throws SecurityException if the caller doesn't have the required permission.
12692 */
12693 @Override
12694 @SatelliteManager.SatelliteServiceResult public int isSatelliteSupported(int subId,
12695 @NonNull IBooleanConsumer callback) {
12696 enforceSatelliteCommunicationPermission("isSatelliteSupported");
12697
12698 final int validSubId = getValidSatelliteSubId(subId);
12699 Phone phone = getPhoneOrDefault(validSubId, "isSatelliteSupported");
12700 if (phone == null) {
12701 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
12702 }
12703
12704 Consumer<Boolean> argument = FunctionalUtils.ignoreRemoteException(callback::accept);
12705 int result = (int) sendRequest(CMD_IS_SATELLITE_SUPPORTED, argument, subId);
12706 if (DBG) log("isSatelliteSupported result: " + result);
12707 return result;
12708 }
12709
12710 /**
12711 * Get the {@link SatelliteCapabilities} with all capabilities of the satellite service.
12712 *
12713 * @param subId The subId to get the satellite capabilities for.
12714 * @param callback The callback that will be used to send the {@link SatelliteCapabilities}
12715 * if the operation is successful.
12716 * @return The result of the operation.
12717 *
12718 * @throws SecurityException if the caller doesn't have required permission.
12719 */
12720 @Override
12721 @SatelliteManager.SatelliteServiceResult public int getSatelliteCapabilities(int subId,
12722 @NonNull ISatelliteCapabilitiesConsumer callback) {
12723 enforceSatelliteCommunicationPermission("getSatelliteCapabilities");
12724
12725 final int validSubId = getValidSatelliteSubId(subId);
12726 Phone phone = getPhoneOrDefault(validSubId, "getSatelliteCapabilities");
12727 if (phone == null) {
12728 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
12729 }
12730
12731 Consumer<SatelliteCapabilities> argument =
12732 FunctionalUtils.ignoreRemoteException(callback::accept);
12733 int result = (int) sendRequest(CMD_GET_SATELLITE_CAPABILITIES, argument, subId);
12734 if (DBG) log("getSatelliteCapabilities result: " + result);
12735 return result;
12736 }
12737
12738 /**
Sarah Chineccfbd12023-01-20 19:00:35 -080012739 * Start receiving satellite position updates.
12740 * This can be called by the pointing UI when the user starts pointing to the satellite.
12741 * Modem should continue to report the pointing input as the device or satellite moves.
12742 *
12743 * @param subId The subId to start satellite position updates for.
Sarah Chineccfbd12023-01-20 19:00:35 -080012744 * @param callback The callback to notify of changes in satellite position.
12745 * @return The result of the operation.
Sarah Chin503828c2023-02-01 23:54:20 -080012746 *
12747 * @throws SecurityException if the caller doesn't have the required permission.
Sarah Chineccfbd12023-01-20 19:00:35 -080012748 */
12749 @Override
12750 @SatelliteManager.SatelliteServiceResult public int startSatellitePositionUpdates(int subId,
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012751 @NonNull ISatelliteStateListener callback) {
Sarah Chinf75afa72023-02-01 01:32:19 -080012752 enforceSatelliteCommunicationPermission("startSatellitePositionUpdates");
12753
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012754 final int validSubId = getValidSatelliteSubId(subId);
12755 if (!isSatelliteEnabled(validSubId)) {
Sarah Chinf75afa72023-02-01 01:32:19 -080012756 return SatelliteManager.SATELLITE_SERVICE_DISABLED;
12757 }
12758
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012759 Phone phone = getPhoneOrDefault(validSubId, "startSatellitePositionUpdates");
Sarah Chineccfbd12023-01-20 19:00:35 -080012760 if (phone == null) {
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012761 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Sarah Chineccfbd12023-01-20 19:00:35 -080012762 }
12763
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012764 if (mSatellitePositionUpdateHandlers.containsKey(callback.asBinder())) {
12765 log("startSatellitePositionUpdates: callback already registered: "
12766 + callback.asBinder());
12767 return SatelliteManager.SATELLITE_SERVICE_CALLBACK_ALREADY_REGISTERED;
Sarah Chineccfbd12023-01-20 19:00:35 -080012768 }
12769
12770 SatellitePositionUpdateHandler handler =
12771 new SatellitePositionUpdateHandler(callback, Looper.getMainLooper());
12772 phone.registerForSatellitePointingInfoChanged(handler,
12773 SatellitePositionUpdateHandler.EVENT_POSITION_UPDATE, null);
12774 phone.registerForSatelliteMessagesTransferComplete(handler,
12775 SatellitePositionUpdateHandler.EVENT_MESSAGE_TRANSFER_STATE_UPDATE, null);
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012776 mSatellitePositionUpdateHandlers.put(callback.asBinder(), handler);
Sarah Chineccfbd12023-01-20 19:00:35 -080012777
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012778 int result = (int) sendRequest(CMD_START_SATELLITE_POSITION_UPDATES, null, validSubId);
Sarah Chineccfbd12023-01-20 19:00:35 -080012779 if (DBG) log("startSatellitePositionUpdates result: " + result);
12780 return result;
12781 }
12782
12783 /**
12784 * Stop receiving satellite position updates.
12785 * This can be called by the pointing UI when the user stops pointing to the satellite.
12786 *
12787 * @param subId The subId to stop satellite position updates for.
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012788 * @param callback The callback that was passed in {@link
12789 * #startSatellitePositionUpdates(int, ISatelliteStateListener)}
Sarah Chineccfbd12023-01-20 19:00:35 -080012790 * @return The result of the operation.
Sarah Chin503828c2023-02-01 23:54:20 -080012791 *
12792 * @throws SecurityException if the caller doesn't have the required permission.
Sarah Chineccfbd12023-01-20 19:00:35 -080012793 */
12794 @Override
12795 @SatelliteManager.SatelliteServiceResult public int stopSatellitePositionUpdates(int subId,
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012796 @NonNull ISatelliteStateListener callback) {
Sarah Chinf75afa72023-02-01 01:32:19 -080012797 enforceSatelliteCommunicationPermission("stopSatellitePositionUpdates");
12798
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012799 final int validSubId = getValidSatelliteSubId(subId);
12800 if (!isSatelliteEnabled(validSubId)) {
Sarah Chinf75afa72023-02-01 01:32:19 -080012801 return SatelliteManager.SATELLITE_SERVICE_DISABLED;
12802 }
12803
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012804 Phone phone = getPhoneOrDefault(validSubId, "stopSatellitePositionUpdates");
Sarah Chineccfbd12023-01-20 19:00:35 -080012805 if (phone == null) {
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012806 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Sarah Chineccfbd12023-01-20 19:00:35 -080012807 }
12808
12809 SatellitePositionUpdateHandler handler =
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012810 mSatellitePositionUpdateHandlers.remove(callback.asBinder());
Sarah Chineccfbd12023-01-20 19:00:35 -080012811 if (handler == null) {
12812 loge("stopSatellitePositionUpdates: No SatellitePositionArgument");
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012813 return SatelliteManager.SATELLITE_SERVICE_CALLBACK_NOT_REGISTERED;
Sarah Chineccfbd12023-01-20 19:00:35 -080012814 } else {
12815 phone.unregisterForSatellitePointingInfoChanged(handler);
12816 phone.unregisterForSatelliteMessagesTransferComplete(handler);
12817 }
12818
12819 if (!mSatellitePositionUpdateHandlers.isEmpty()) {
12820 log("stopSatellitePositionUpdates: other listeners still exist.");
12821 return SatelliteManager.SATELLITE_SERVICE_SUCCESS;
12822 }
12823
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012824 int result = (int) sendRequest(CMD_STOP_SATELLITE_POSITION_UPDATES, null, validSubId);
Sarah Chineccfbd12023-01-20 19:00:35 -080012825 if (DBG) log("stopSatellitePositionUpdates result: " + result);
12826 return result;
12827 }
12828
12829 /**
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +000012830 * Get maximum number of characters per text message on satellite.
12831 * @param subId - The subId of the subscription.
12832 * @param callback - The callback that will be used to send maximum characters limit
12833 * if operation is successful.
12834 * @return The result of the operation.
12835 *
12836 * @throws SecurityException if the caller doesn't have the required permission.
12837 */
12838 @Override
12839 public int getMaxCharactersPerSatelliteTextMessage(int subId, IIntegerConsumer callback) {
12840 enforceSatelliteCommunicationPermission("getMaxCharactersPerSatelliteTextMessage");
12841
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012842 final int validSubId = getValidSatelliteSubId(subId);
12843 if (!isSatelliteEnabled(validSubId)) {
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +000012844 return SatelliteManager.SATELLITE_SERVICE_DISABLED;
12845 }
12846
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012847 Phone phone = getPhoneOrDefault(validSubId, "getMaxCharactersPerSatelliteTextMessage");
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +000012848 if (phone == null) {
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012849 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +000012850 }
12851
12852 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(callback::accept);
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012853 int result = (int) sendRequest(
12854 CMD_GET_MAX_CHAR_PER_SATELLITE_TEXT_MSG, argument, validSubId);
Aishwarya Mallampati60fe1132023-01-24 19:07:21 +000012855 if (DBG) log("getMaxCharPerTextMessageOnSatellite result: " + result);
12856 return result;
12857 }
12858
12859 /**
Thomas Nguyen8ee49682023-02-01 11:46:09 -080012860 * Register the subscription with a satellite provider.
12861 * This is needed to register the subscription if the provider allows dynamic registration.
12862 *
12863 * @param subId The subId of the subscription to be provisioned.
12864 * @param features List of features to be provisioned.
12865 * @param callback The callback to get the error code of the request.
12866 * @return The signal transport used by the caller to cancel the provision request.
12867 * @throws SecurityException if the caller doesn't have the required permission.
12868 */
12869 @Override
12870 public ICancellationSignal provisionSatelliteService(int subId, int[] features,
12871 IIntegerConsumer callback) {
12872 enforceSatelliteCommunicationPermission("provisionSatelliteService");
12873
12874 final int validSubId = getValidSatelliteSubId(subId);
12875 String callbackRemoteExErrorMessage = "provisionSatelliteService: callback not available.";
12876 Phone phone = getPhoneOrDefault(validSubId, "provisionSatelliteService");
12877 if (phone == null) {
12878 sendResponse(callback, SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE,
12879 callbackRemoteExErrorMessage);
12880 return null;
12881 }
12882
12883 if (mSatelliteProvisionCallbacks.containsKey(validSubId)) {
12884 sendResponse(callback, SatelliteManager.SATELLITE_SERVICE_PROVISION_IN_PROGRESS,
12885 callbackRemoteExErrorMessage);
12886 return null;
12887 }
12888
12889 if (isSatelliteEnabled(validSubId)) {
12890 sendResponse(callback, SatelliteManager.SATELLITE_SERVICE_ALREADY_PROVISIONED,
12891 callbackRemoteExErrorMessage);
12892 return null;
12893 }
12894
12895 sendRequestAsync(CMD_PROVISION_SATELLITE_SERVICE,
12896 new ProvisionSatelliteServiceArgument(features, callback, validSubId), phone, null);
12897
12898 ICancellationSignal cancelTransport = CancellationSignal.createTransport();
12899 CancellationSignal.fromTransport(cancelTransport)
12900 .setOnCancelListener(() -> {
12901 sendRequestAsync(CMD_CANCEL_PROVISION_SATELLITE_SERVICE, validSubId);
12902 });
12903 return cancelTransport;
12904 }
12905
12906 /**
12907 * Register for the satellite provision state change.
12908 *
12909 * @param subId The subId of the subscription associated with the satellite service.
12910 * @param callback The callback to handle the satellite provision state changed event.
12911 * @throws SecurityException if the caller doesn't have the required permission.
12912 */
12913 @Override
12914 @SatelliteManager.SatelliteServiceResult
12915 public int registerForSatelliteProvisionStateChanged(
12916 int subId, ISatelliteStateListener callback) {
12917 //enforceSatelliteCommunicationPermission("registerForSatelliteProvisionStateChanged");
12918
12919 final int validSubId = getValidSatelliteSubId(subId);
12920 Phone phone = getPhoneOrDefault(
12921 validSubId, "registerForSatelliteProvisionStateChanged");
12922 if (phone == null) {
12923 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
12924 }
12925
12926 SatelliteProvisionStateChangedHandler satelliteProvisionStateChangedHandler =
12927 mSatelliteProvisionStateChangedHandlers.get(validSubId);
12928 if (satelliteProvisionStateChangedHandler == null) {
12929 satelliteProvisionStateChangedHandler =
12930 new SatelliteProvisionStateChangedHandler(
12931 Looper.getMainLooper(), validSubId);
12932 phone.registerForSatelliteProvisionStateChanged(
12933 satelliteProvisionStateChangedHandler,
12934 SatelliteProvisionStateChangedHandler.EVENT_PROVISION_STATE_CHANGED, null);
12935 }
12936
12937 if (callback != null) {
12938 satelliteProvisionStateChangedHandler.addListener(callback);
12939 }
12940 mSatelliteProvisionStateChangedHandlers.put(
12941 validSubId, satelliteProvisionStateChangedHandler);
12942 return SatelliteManager.SATELLITE_SERVICE_SUCCESS;
12943 }
12944
12945 /**
12946 * Unregister for the satellite provision state change.
12947 *
12948 * @param subId The subId of the subscription associated with the satellite service.
12949 * @param callback The callback that was passed to
12950 * {@link #registerForSatelliteProvisionStateChanged(int, ISatelliteStateListener)}
12951 * @throws SecurityException if the caller doesn't have the required permission.
12952 */
12953 @Override
12954 @SatelliteManager.SatelliteServiceResult
12955 public int unregisterForSatelliteProvisionStateChanged(
12956 int subId, ISatelliteStateListener callback) {
12957 enforceSatelliteCommunicationPermission("unregisterForSatelliteProvisionStateChanged");
12958
12959 final int validSubId = getValidSatelliteSubId(subId);
12960 SatelliteProvisionStateChangedHandler satelliteProvisionStateChangedHandler =
12961 mSatelliteProvisionStateChangedHandlers.get(validSubId);
12962 if (satelliteProvisionStateChangedHandler != null) {
12963 if (satelliteProvisionStateChangedHandler.removeListener(callback)) {
12964 return SatelliteManager.SATELLITE_SERVICE_SUCCESS;
12965 }
12966 }
12967 return SatelliteManager.SATELLITE_SERVICE_CALLBACK_NOT_REGISTERED;
12968 }
12969
12970 /**
12971 * Get the list of provisioned satellite features.
12972 *
12973 * @param subId The subId of the subscription associated with the satellite service.
12974 * @param callback The callback to get the list of provisioned satellite features.
12975 * @return The error code of the request.
12976 * @throws SecurityException if the caller doesn't have the required permission.
12977 */
12978 @Override
12979 @SatelliteManager.SatelliteServiceResult
12980 public int getProvisionedSatelliteFeatures(int subId, IIntArrayConsumer callback) {
12981 enforceSatelliteCommunicationPermission("getProvisionedSatelliteFeatures");
12982
12983 final int validSubId = getValidSatelliteSubId(subId);
12984 if (!isSatelliteEnabled(validSubId)) {
12985 return SatelliteManager.SATELLITE_SERVICE_DISABLED;
12986 }
12987
12988 final Phone phone = getPhoneOrDefault(validSubId, "getProvisionedSatelliteFeatures");
12989 if (phone == null) {
12990 return SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE;
12991 }
12992
12993 int result = (int) sendRequest(
12994 CMD_GET_PROVISIONED_SATELLITE_FEATURES, callback, validSubId);
12995 if (DBG) log("getProvisionedSatelliteFeatures result: " + result);
12996 return result;
12997 }
12998
12999 private void handleCmdProvisionSatelliteService(@NonNull ProvisionSatelliteServiceArgument arg,
13000 @NonNull Phone phone, Message onCompleted) {
13001 String callbackRemoteExErrorMessage =
13002 "handleCmdProvisionSatelliteService: callback not available.";
13003 if (arg == null) {
13004 loge("handleCmdProvisionSatelliteService: arg is null");
13005 return;
13006 }
13007 if (phone == null) {
13008 loge("handleCmdProvisionSatelliteService: phone is null");
13009 sendResponse(arg.callback, SatelliteManager.SATELLITE_SERVICE_INVALID_TELEPHONY_STATE,
13010 callbackRemoteExErrorMessage);
13011 return;
13012 }
13013
13014 if (!mSatelliteProvisionCallbacks.containsKey(arg.subId)) {
13015 mSatelliteProvisionCallbacks.put(arg.subId, arg.callback);
13016 phone.provisionSatelliteService(onCompleted, phone.getImei(), phone.getMsisdn(),
13017 getSatelliteImsi(arg.subId), arg.features);
13018 } else {
13019 sendResponse(arg.callback, SatelliteManager.SATELLITE_SERVICE_PROVISION_IN_PROGRESS,
13020 callbackRemoteExErrorMessage);
13021 }
13022 }
13023
13024 private void handleEventProvisionSatelliteServiceDone(MainThreadRequest request) {
13025 final ProvisionSatelliteServiceArgument arg =
13026 (ProvisionSatelliteServiceArgument) request.argument;
13027 if (arg == null) {
13028 loge("handleEventProvisionSatelliteServiceDone: arg is null");
13029 return;
13030 }
13031
13032 IIntegerConsumer callback = mSatelliteProvisionCallbacks.remove(arg.subId);
13033 if (callback == null) {
13034 loge("handleEventProvisionSatelliteServiceDone: callback is null for subId="
13035 + arg.subId);
13036 return;
13037 }
13038
13039 int result = (int) request.result;
13040 if (DBG) {
13041 log("handleEventProvisionSatelliteServiceDone: result="
13042 + result + ", subId=" + arg.subId);
13043 }
13044 sendResponse(callback, result,
13045 "handleEventProvisionSatelliteServiceDone: callback not available.");
13046
13047 if (result == SatelliteManager.SATELLITE_SERVICE_SUCCESS) {
13048 setSatelliteEnabled(arg.subId, true);
13049 }
13050
13051 /**
13052 * We need to update satellite provision status in SubscriptionController
13053 * or SatelliteController.
13054 * TODO (b/267826133) we need to do this for all subscriptions on the device.
13055 */
13056 registerForSatelliteProvisionStateChanged(arg.subId, null);
13057 }
13058
13059 private void handleCmdCancelProvisionSatelliteService(int subId, Message onCompleted) {
13060 final Phone phone = getPhoneOrDefault(
13061 subId, "handleCmdCancelProvisionSatelliteService");
13062 if (phone == null) {
13063 return;
13064 }
13065 phone.cancelProvisionSatelliteService(onCompleted, getSatelliteImsi(subId));
13066 }
13067
13068 private void sendResponse(@NonNull IIntegerConsumer callback, int result, String message) {
13069 Objects.requireNonNull(callback);
13070
13071 try {
13072 callback.accept(result);
13073 } catch (RemoteException e) {
13074 Log.w(LOG_TAG, message);
13075 }
13076 }
13077
13078 private String getSatelliteImsi(int subId) {
13079 if (subId == SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
13080 return "";
13081 }
13082
13083 if (mSubscriptionController == null) {
13084 loge("getSatelliteImsi: mSubscriptionController is null");
13085 return "";
13086 }
13087 return mSubscriptionController.getImsiPrivileged(subId);
13088 }
13089
13090 private Phone getPhoneOrDefault(int subId, String caller) {
13091 Phone phone = getPhone(subId);
13092 if (phone == null) {
13093 loge(caller + " called with invalid subId: " + subId
13094 + ". Retrying with default phone.");
13095 phone = getDefaultPhone();
13096 if (phone == null) {
13097 loge(caller + " failed with no phone object.");
13098 }
13099 }
13100 return phone;
13101 }
13102
13103 /**
13104 * Check if satellite is enabled for a subscription.
13105 *
13106 * Note: this is the version without permission check for telephony internal use only. The
13107 * caller need to take care of the permission check.
13108 */
13109 private boolean isSatelliteEnabled(int subId) {
13110 if (subId != SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
13111 if (mSubscriptionController == null) {
13112 loge("setSatelliteEnabled mSubscriptionController is null");
13113 return false;
13114 }
13115
13116 String strResult = mSubscriptionController.getSubscriptionProperty(
13117 subId, SubscriptionManager.SATELLITE_ENABLED);
13118 if (strResult != null) {
13119 int intResult = Integer.parseInt(strResult);
13120 return (intResult == 1) ? true : false;
13121 }
13122 } else {
13123 //TODO (b/267826133): check via SatelliteController
13124 }
13125 return false;
13126 }
13127
13128 /**
13129 * Set satellite enabled for a subscription.
13130 *
13131 * The permission {@link android.Manifest.permission#MODIFY_PHONE_STATE} will be enforced by
13132 * {@link SubscriptionController} when setting satellite enabled for an active subscription.
13133 * Otherwise, {@link android.Manifest.permission#SATELLITE_COMMUNICATION} will be enforced.
13134 */
13135 private void setSatelliteEnabled(int subId, boolean isEnabled) {
13136 if (subId != SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) {
13137 if (mSubscriptionController == null) {
13138 loge("setSatelliteEnabled mSubscriptionController is null");
13139 return;
13140 }
13141 mSubscriptionController.setSubscriptionProperty(
13142 subId, SubscriptionManager.SATELLITE_ENABLED, isEnabled ? "1" : "0");
13143 } else {
13144 //TODO (b/267826133): enable via SatelliteController
13145 }
13146 }
13147
13148 private int getValidSatelliteSubId(int subId) {
13149 if (mSubscriptionController == null) {
13150 loge("getValidSatelliteSubId mSubscriptionController is null. "
13151 + "Use DEFAULT_SUBSCRIPTION_ID for subId=" + subId);
13152 return SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
13153 }
13154 final long identity = Binder.clearCallingIdentity();
13155 try {
13156 Context context = getDefaultPhone().getContext();
13157 if (mSubscriptionController.isActiveSubId(
13158 subId, context.getOpPackageName(), context.getAttributionTag())) {
13159 return subId;
13160 }
13161 } finally {
13162 Binder.restoreCallingIdentity(identity);
13163 }
13164 if (DBG) log("getValidSatelliteSubId: use DEFAULT_SUBSCRIPTION_ID for subId=" + subId);
13165 return SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
13166 }
13167
13168 /**
arunvoddud5c6ce02022-12-11 06:03:12 +000013169 * Check whether the caller (or self, if not processing an IPC) can read device identifiers.
13170 *
13171 * <p>This method behaves in one of the following ways:
13172 * <ul>
13173 * <li>return true : if the calling package has the appop permission {@link
13174 * Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER} in the manifest </>
13175 * <li>return true : if any one subscription has the READ_PRIVILEGED_PHONE_STATE
13176 * permission, the calling package passes a DevicePolicyManager Device Owner / Profile
13177 * Owner device identifier access check, or the calling package has carrier privileges</>
13178 * <li>throw SecurityException: if the caller does not meet any of the requirements.
13179 * </ul>
13180 */
13181 private static boolean checkCallingOrSelfReadDeviceIdentifiersForAnySub(Context context,
13182 String callingPackage, @Nullable String callingFeatureId, String message) {
13183 for (Phone phone : PhoneFactory.getPhones()) {
13184 if (TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(context,
13185 phone.getSubId(), callingPackage, callingFeatureId, message)) {
13186 return true;
13187 }
13188 }
13189 return false;
13190 }
arunvoddud7401012022-12-15 16:08:12 +000013191
13192 /**
13193 * Class binds the consumer[callback] and carrierId.
13194 */
13195 private static class CallerCallbackInfo {
13196 private final Consumer<Integer> mConsumer;
13197 private final int mCarrierId;
13198
13199 public CallerCallbackInfo(Consumer<Integer> consumer, int carrierId) {
13200 mConsumer = consumer;
13201 mCarrierId = carrierId;
13202 }
13203
13204 public Consumer<Integer> getConsumer() {
13205 return mConsumer;
13206 }
13207
13208 public int getCarrierId() {
13209 return mCarrierId;
13210 }
13211 }
arunvoddud5c6ce02022-12-11 06:03:12 +000013212}